summaryrefslogtreecommitdiff
path: root/plugins/__init__.py
blob: 0e40be9a7c791a603d39d928e028eb341a3613d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)