|
| 1 | +import imp |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from config import config |
| 5 | +from gettext import gettext as _ |
| 6 | + |
| 7 | +plugins_dir = config.get('plugins_dir', '') |
| 8 | +plugins_dir = plugins_dir or\ |
| 9 | + os.path.join(os.environ.get('XDG_DATA_HOME') or\ |
| 10 | + os.path.join(os.environ.get('HOME'), '.local', 'share'), |
| 11 | + 'poezio', 'plugins') |
| 12 | +try: |
| 13 | + os.makedirs(plugins_dir) |
| 14 | +except OSError: |
| 15 | + pass |
| 16 | + |
| 17 | +sys.path.append(plugins_dir) |
| 18 | + |
1 | 19 | class PluginManager(object):
|
2 | 20 | def __init__(self, core):
|
3 | 21 | self.core = core
|
4 |
| - self.plugins = {} |
| 22 | + self.modules = {} # module name -> module object |
| 23 | + self.plugins = {} # module name -> plugin object |
| 24 | + self.commands = {} # module name -> dict of commands loaded for the module |
| 25 | + self.event_handlers = {} # module name -> list of event_name/handler pairs loaded for the module |
5 | 26 |
|
6 | 27 | def load(self, name):
|
7 | 28 | if name in self.plugins:
|
8 | 29 | self.plugins[name].unload()
|
9 | 30 |
|
10 | 31 | try:
|
11 |
| - code = compile(open(name).read(), name, 'exec') |
12 |
| - from plugin import BasePlugin |
13 |
| - globals = { 'BasePlugin' : BasePlugin } |
14 |
| - exec(code, globals) |
15 |
| - self.plugins[name] = globals['Plugin'](self.core) |
| 32 | + if name in self.modules: |
| 33 | + imp.acquire_lock() |
| 34 | + module = imp.reload(self.modules[name]) |
| 35 | + imp.release_lock() |
| 36 | + else: |
| 37 | + file, filename, info = imp.find_module(name, [plugins_dir]) |
| 38 | + imp.acquire_lock() |
| 39 | + module = imp.load_module(name, file, filename, info) |
| 40 | + imp.release_lock() |
16 | 41 | except Exception as e:
|
17 |
| - self.core.information("Could not load plugin: %s" % (e,)) |
| 42 | + import traceback |
| 43 | + self.core.information(_("Could not load plugin: ") + traceback.format_exc()) |
| 44 | + return |
| 45 | + finally: |
| 46 | + if imp.lock_held(): |
| 47 | + imp.release_lock() |
| 48 | + |
| 49 | + self.modules[name] = module |
| 50 | + self.commands[name] = {} |
| 51 | + self.event_handlers[name] = [] |
| 52 | + self.plugins[name] = module.Plugin(self, self.core) |
18 | 53 |
|
19 | 54 | def unload(self, name):
|
20 | 55 | if name in self.plugins:
|
21 | 56 | try:
|
| 57 | + for command in self.commands[name].keys(): |
| 58 | + del self.core.commands[command] |
| 59 | + for event_name, handler in self.event_handlers[name]: |
| 60 | + self.core.xmpp.del_event_handler(event_name, handler) |
| 61 | + |
22 | 62 | self.plugins[name].unload()
|
23 | 63 | del self.plugins[name]
|
| 64 | + del self.commands[name] |
| 65 | + del self.event_handlers[name] |
24 | 66 | except Exception as e:
|
25 |
| - self.core.information("Could not unload plugin (may not be safe to try again): %s" % (e,)) |
| 67 | + import traceback |
| 68 | + self.core.information(_("Could not unload plugin (may not be safe to try again): ") + traceback.format_exc()) |
| 69 | + |
| 70 | + def add_command(self, module_name, name, handler, help, completion=None): |
| 71 | + if name in self.core.commands: |
| 72 | + raise Exception(_("Command '%s' already exists") % (name,)) |
| 73 | + |
| 74 | + commands = self.commands[module_name] |
| 75 | + commands[name] = (handler, help, completion) |
| 76 | + self.core.commands[name] = (handler, help, completion) |
| 77 | + |
| 78 | + def add_event_handler(self, module_name, event_name, handler): |
| 79 | + eh = self.event_handlers[module_name] |
| 80 | + eh.append((event_name, handler)) |
| 81 | + self.core.xmpp.add_event_handler(event_name, handler) |
| 82 | + |
| 83 | + def del_event_handler(self, module_name, event_name, handler): |
| 84 | + self.core.xmpp.del_event_handler(event_name, handler) |
| 85 | + eh = self.event_handlers[module_name] |
| 86 | + eh = list(filter(lambda e : e != (event_name, handler), eh)) |
0 commit comments