summaryrefslogtreecommitdiff
path: root/src/plugin.py
blob: d332ca018462214278dbe4b4e7c31a47346301af (plain)
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
52
53
54
import os
from configparser import ConfigParser

class PluginConfig(ConfigParser):
    def __init__(self, filename):
        ConfigParser.__init__(self)
        self.__config_file__ = filename
        self.read()

    def read(self):
        """Read the config file"""
        ConfigParser.read(self, self.__config_file__)

    def write(self):
        """Write the config to the disk"""
        try:
            fp = open(self.__config_file__, 'w')
            ConfigParser.write(self, fp)
            fp.close()
            return True
        except IOError:
            return False

class BasePlugin(object):
    """
    Class that all plugins derive from.  Any methods beginning with command_
    are interpreted as a command and beginning with on_ are interpreted as
    event handlers
    """

    def __init__(self, plugin_manager, core, plugins_conf_dir):
        self.core = core
        self.plugin_manager = plugin_manager
        conf = os.path.join(plugins_conf_dir, self.__module__+'.cfg')
        self.config = PluginConfig(conf)
        self.init()

    def init(self):
        pass

    def cleanup(self):
        pass

    def unload(self):
        self.cleanup()

    def add_command(self, name, handler, help, completion=None):
        return self.plugin_manager.add_command(self.__module__, name, handler, help, completion)

    def add_event_handler(self, event_name, handler):
        return self.plugin_manager.add_event_handler(self.__module__, event_name, handler)

    def del_event_handler(self, event_name, handler):
        return self.plugin_manager.del_event_handler(self.__module__, event_name, handler)