Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support disabling modules/commands per channel #1235

Merged
merged 1 commit into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sopel/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import unicode_literals, absolute_import, print_function, division

from ast import literal_eval
import collections
import os
import re
Expand Down Expand Up @@ -466,6 +467,28 @@ def call(self, func, sopel, trigger):
)
return

# if channel has its own config section, check for excluded modules/modules methods
if trigger.sender in self.config:
channel_config = self.config[trigger.sender]

# disable listed modules completely on provided channel
if 'disable_modules' in channel_config:
disabled_modules = channel_config.disable_modules.split(',')

# if "*" is used, we are disabling all modules on provided channel
if '*' in disabled_modules:
return
if func.__module__ in disabled_modules:
return

# disable chosen methods from modules
if 'disable_commands' in channel_config:
disabled_commands = literal_eval(channel_config.disable_commands)

if func.__module__ in disabled_commands:
if func.__name__ in disabled_commands[func.__module__]:
return

try:
exit_code = func(sopel, trigger)
except Exception:
Expand Down
9 changes: 9 additions & 0 deletions sopel/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ def __init__(self, name, items, parent):
def __getattr__(self, name):
return None

def __contains__(self, name):
return name in vars(self)

def __setattr__(self, name, value):
object.__setattr__(self, name, value)
if type(value) is list:
Expand All @@ -164,6 +167,12 @@ def __getattr__(self, name):
raise AttributeError("%r object has no attribute %r"
% (type(self).__name__, name))

def __getitem__(self, name):
return self.__getattr__(name)

def __contains__(self, name):
return name in self.parser.sections()

def option(self, question, default=False):
"""Ask "y/n" and return the corresponding boolean answer.

Expand Down