]>
Commit | Line | Data |
---|---|---|
685e346e A |
1 | import traceback |
2 | import logging | |
3 | import task | |
0200bbb8 | 4 | import importlib |
e4587a03 A |
5 | import core |
6 | import sys | |
685e346e A |
7 | |
8 | class AcidPlugin(object): | |
9 | def __init__(self): | |
10 | super(AcidPlugin, self).__init__() | |
11 | ||
e4587a03 A |
12 | self.prefix = core.config.get('control').get('prefix') |
13 | self.logchan = core.config.get('control').get('channel') | |
685e346e | 14 | self.log = logging.getLogger(__name__) |
e4587a03 A |
15 | self.config = core.config |
16 | self.dbp = core.dbx.cursor() # do these have to be closed? | |
685e346e A |
17 | |
18 | def start(self): | |
19 | pass | |
20 | ||
21 | def stop(self): | |
22 | pass | |
23 | ||
24 | def getCommands(self): | |
25 | pass | |
26 | ||
27 | modules = {} | |
28 | plugin_to_modname = lambda x: '%s.%s' % (x, x) | |
29 | ||
2d09c59a | 30 | def loadPlugin(plugin): |
685e346e A |
31 | global modules |
32 | ||
33 | modname = plugin_to_modname(plugin) | |
34 | ||
35 | mod = importlib.import_module(modname) | |
33b894d2 A |
36 | try: |
37 | obj = getattr(mod, plugin)() | |
685e346e | 38 | |
33b894d2 A |
39 | obj.start() |
40 | except: | |
41 | # initialization failed, remove from sys.modules so it can be reloaded | |
42 | deps = [module for module in sys.modules if module.startswith(plugin + '.')] | |
43 | for dep in deps: | |
44 | del sys.modules[dep] | |
45 | ||
46 | raise | |
685e346e | 47 | |
0200bbb8 | 48 | modules[plugin] = obj |
685e346e A |
49 | |
50 | def unloadPlugin(plugin): | |
51 | global modules | |
52 | ||
0200bbb8 | 53 | obj = modules[plugin] |
685e346e A |
54 | obj.stop() |
55 | ||
8a2c0df3 | 56 | deps = [module for module in sys.modules if module.startswith(plugin + '.')] |
685e346e A |
57 | for dep in deps: |
58 | del sys.modules[dep] | |
685e346e | 59 | |
0200bbb8 | 60 | del modules[plugin] |
685e346e A |
61 | |
62 | def getPlugins(): | |
63 | return modules.keys() | |
64 | ||
65 | def call(name, *args): | |
66 | task.Run() | |
67 | ||
68 | for m in modules.itervalues(): | |
69 | try: | |
70 | func = getattr(m, name) | |
71 | except: | |
72 | continue | |
73 | ||
74 | try: | |
75 | func(*args) | |
76 | except: | |
77 | traceback.print_exc() | |
78 | raise |