From 9a13903a2f7d3a65fdf15a65fb59cccd622e2066 Mon Sep 17 00:00:00 2001 From: Livio Recchia Date: Mon, 10 Feb 2020 23:06:34 +0100 Subject: Initial commit --- plugins/__init__.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 plugins/__init__.py (limited to 'plugins/__init__.py') diff --git a/plugins/__init__.py b/plugins/__init__.py new file mode 100644 index 0000000..0e40be9 --- /dev/null +++ b/plugins/__init__.py @@ -0,0 +1,51 @@ +import logging +import sys + +sys.path.insert(0, "plugins") +debuglog = logging.getLogger("ManaChat.Debug") + +plugins_loaded = [] + + +class PluginError(Exception): + pass + + +def load_plugin(config, plugin_name): + if plugin_name in plugins_loaded: + return + + plugin = __import__(plugin_name) + + for p in plugin.PLUGIN['blocks']: + if p in plugins_loaded: + raise PluginError("{} blocks {}".format(p, plugin_name)) + + for p in plugin.PLUGIN['requires']: + if p not in plugins_loaded: + load_plugin(config, p) + + this = sys.modules[__name__] + setattr(this, plugin_name, plugin) + + # filling the gaps in config + if not config.has_section(plugin_name): + config.add_section(plugin_name) + + default_config = plugin.PLUGIN.setdefault('default_config', {}) + for option, value in default_config.iteritems(): + if not config.has_option(plugin_name, option): + config.set(plugin_name, option, str(value)) + + plugin.init(config) + plugins_loaded.append(plugin_name) + debuglog.info('Plugin %s loaded', plugin_name) + + +def load_plugins(config): + for pn in config.options('Plugins'): + if config.getboolean('Plugins', pn): + try: + load_plugin(config, pn) + except ImportError as e: + debuglog.error('Error loading plugin %s: %s', pn, e) -- cgit v1.2.3-70-g09d2