Last active
May 14, 2018 22:36
-
-
Save GRAYgoose124/aa0377ad12152721a2e9132c93b7b64d to your computer and use it in GitHub Desktop.
Plugins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import logging | |
| import os | |
| from importlib.util import spec_from_file_location, module_from_spec | |
| logger = logging.getLogger(__name__) | |
| class Plugin: | |
| """The command dictionaries are indexed by the command name and point to a function | |
| which returns a response to send or None if no response is necessary. | |
| """ | |
| def __init__(self, manager): | |
| """This function needs to be copied by a plugin with the command dictionaries | |
| filled out with 'cmd_name': function_pointer. | |
| """ | |
| self.manager = manager | |
| self.admin_commands = {} | |
| self.commands = {'command': self.command} | |
| def msg_hook(self, message): | |
| """This function is automatically called on a message hook. | |
| The response is then returned. | |
| """ | |
| response = None | |
| return response | |
| def command(self, message, parameters): | |
| '''<parameters> : | |
| <documentation> | |
| ''' | |
| response = None | |
| return response | |
| class PluginManager: | |
| """ | |
| """ | |
| def __init__(self, plugins_path): | |
| self.plugins_path = plugins_path | |
| self.plugins = {} | |
| self.admin_commands = {'🔌': self.cmd_plugin} | |
| self.commands = {} | |
| self.pluginlist = [] | |
| pluginlist_path = os.path.join(self.plugins_path, '.plugins') | |
| if os.path.exists(pluginlist_path): | |
| with open(pluginlist_path, "r") as f: | |
| for plugin in f.readlines(): | |
| self.pluginlist.append(plugin.strip('\n')) | |
| self.load_plugins() | |
| def load_plugin(self, name): | |
| if name in self.plugins: | |
| self.unload_plugin(name) | |
| try: | |
| script = os.path.join(self.plugins_path, "{}.py".format(name)) | |
| spec = spec_from_file_location(name, script) | |
| module = module_from_spec(spec) | |
| spec.loader.exec_module(module) | |
| plugin = getattr(module, name) | |
| plugin = plugin(self) | |
| self.commands.update(plugin.commands) | |
| self.admin_commands.update(plugin.admin_commands) | |
| self.plugins[name] = plugin | |
| return self.plugins[name] | |
| except (ValueError, TypeError, AttributeError) as e: | |
| logger.warning(f'PEx| {type(e)}') | |
| logger.debug("P+ | {} loaded.".format(name)) | |
| def unload_plugin(self, name): | |
| if name not in self.plugins: | |
| return | |
| for key in self.plugins[name].commands: | |
| if key in self.commands: | |
| self.commands.pop(key) | |
| for key in self.plugins[name].admin_commands: | |
| if key in self.admin_commands: | |
| self.admin_commands.pop(key) | |
| self.plugins.pop(name) | |
| logger.debug("P- | {} unloaded.".format(name)) | |
| def list_plugins(self): | |
| for name in os.listdir(self.plugins_path): | |
| if name[-3:] == ".py" and \ | |
| name != "__init__.py" and \ | |
| name[:2] != ".#": | |
| yield name[:-3] | |
| def load_plugins(self): | |
| for plugin_name in self.list_plugins(): | |
| if self.pluginlist is None: | |
| self.load_plugin(plugin_name) | |
| elif plugin_name in self.pluginlist: | |
| self.load_plugin(plugin_name) | |
| def unload_plugins(self): | |
| for plugin_name in self.plugins.keys(): | |
| self.unload_plugin(plugin_name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment