summaryrefslogtreecommitdiff
path: root/plugins/__init__.py
diff options
context:
space:
mode:
authorLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
committerLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
commit9a13903a2f7d3a65fdf15a65fb59cccd622e2066 (patch)
tree9403b7dff39eb5e5d7fa0f79efb69b496add4c4b /plugins/__init__.py
parent11cc316b74d5f3f283413a33e7693b314741aa4a (diff)
downloadmanachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.gz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.bz2
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.xz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.zip
Initial commit
Diffstat (limited to 'plugins/__init__.py')
-rw-r--r--plugins/__init__.py51
1 files changed, 51 insertions, 0 deletions
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)