Skip to content

Commit

Permalink
bot: fix per-channel configuration by plugin names
Browse files Browse the repository at this point in the history
Changes to how plugins are loaded resulted in plugin names no longer
being sufficient to disable plugins or individual plugin functions on a
per-channel basis. Rather than change how the feature works, apply some
magic to translate plugin names into module names so the documented way
continues to work as originally intended.
  • Loading branch information
dgw committed Apr 7, 2020
1 parent fd610c8 commit 26c4204
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
38 changes: 37 additions & 1 deletion sopel/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,21 @@ def get_plugin_meta(self, name):

return self._plugins[name].get_meta_description()

def get_plugin_module_name(self, name):
"""Get the internal Python module name from which a registered plugin came.
:param str name: name of the plugin
:return: the plugin's Python module name
(see :meth:`~.plugins.handlers.AbstractPluginHandler.get_module_path`)
:rtype: str
:raise plugins.exceptions.PluginNotRegistered: when there is no
``name`` plugin registered
"""
if not self.has_plugin(name):
raise plugins.exceptions.PluginNotRegistered(name)

return self._plugins[name].get_module_path()

def unregister(self, obj):
"""Unregister a callable.
Expand Down Expand Up @@ -575,12 +590,33 @@ def call(self, func, sopel, trigger):
# if "*" is used, we are disabling all plugins on provided channel
if '*' in disabled_plugins:
return
if func.__module__ in disabled_plugins:

# if we aren't disabling "*", convert plugin names to module names
disabled_modules = [
(
self.get_plugin_module_name(plugin)
if self.has_plugin(plugin)
else plugin
)
for plugin
in disabled_plugins
]

if func.__module__ in disabled_modules:
return

# disable chosen methods from plugins
if 'disable_commands' in channel_config:
disabled_commands = literal_eval(channel_config.disable_commands)
disabled_commands = {
(
self.get_plugin_module_name(plugin)
if self.has_plugin(plugin)
else plugin
): functions
for plugin, functions
in disabled_commands.items()
}

if func.__module__ in disabled_commands:
if func.__name__ in disabled_commands[func.__module__]:
Expand Down
17 changes: 17 additions & 0 deletions sopel/plugins/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ def get_meta_description(self):
"""
raise NotImplementedError

def get_module_path(self):
"""Retrieve the Python module path from which this plugin was loaded
:return: Python module name
:rtype: str
This method should return the same module name as the ``__module__``
attribute of its callables.
"""
raise NotImplementedError

def is_loaded(self):
"""Tell if the plugin is loaded or not
Expand Down Expand Up @@ -241,6 +252,9 @@ def get_meta_description(self):
'source': self.module_name,
}

def get_module_path(self):
return self.module_name

def load(self):
self._module = importlib.import_module(self.module_name)

Expand Down Expand Up @@ -436,3 +450,6 @@ def get_meta_description(self):
'source': str(self.entry_point),
})
return data

def get_module_path(self):
return self.entry_point.resolve().__name__

0 comments on commit 26c4204

Please sign in to comment.