2011-09-24 20:26:31 +00:00
|
|
|
import imp
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from config import config
|
|
|
|
from gettext import gettext as _
|
|
|
|
|
|
|
|
plugins_dir = config.get('plugins_dir', '')
|
|
|
|
plugins_dir = plugins_dir or\
|
|
|
|
os.path.join(os.environ.get('XDG_DATA_HOME') or\
|
|
|
|
os.path.join(os.environ.get('HOME'), '.local', 'share'),
|
|
|
|
'poezio', 'plugins')
|
|
|
|
try:
|
|
|
|
os.makedirs(plugins_dir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
sys.path.append(plugins_dir)
|
|
|
|
|
2011-09-23 15:43:01 +00:00
|
|
|
class PluginManager(object):
|
|
|
|
def __init__(self, core):
|
|
|
|
self.core = core
|
2011-09-24 20:26:31 +00:00
|
|
|
self.modules = {} # module name -> module object
|
|
|
|
self.plugins = {} # module name -> plugin object
|
|
|
|
self.commands = {} # module name -> dict of commands loaded for the module
|
|
|
|
self.event_handlers = {} # module name -> list of event_name/handler pairs loaded for the module
|
2011-09-23 15:43:01 +00:00
|
|
|
|
|
|
|
def load(self, name):
|
|
|
|
if name in self.plugins:
|
|
|
|
self.plugins[name].unload()
|
|
|
|
|
|
|
|
try:
|
2011-09-24 20:26:31 +00:00
|
|
|
if name in self.modules:
|
|
|
|
imp.acquire_lock()
|
|
|
|
module = imp.reload(self.modules[name])
|
|
|
|
imp.release_lock()
|
|
|
|
else:
|
|
|
|
file, filename, info = imp.find_module(name, [plugins_dir])
|
|
|
|
imp.acquire_lock()
|
|
|
|
module = imp.load_module(name, file, filename, info)
|
|
|
|
imp.release_lock()
|
2011-09-23 15:43:01 +00:00
|
|
|
except Exception as e:
|
2011-09-24 20:26:31 +00:00
|
|
|
import traceback
|
|
|
|
self.core.information(_("Could not load plugin: ") + traceback.format_exc())
|
|
|
|
return
|
|
|
|
finally:
|
|
|
|
if imp.lock_held():
|
|
|
|
imp.release_lock()
|
|
|
|
|
|
|
|
self.modules[name] = module
|
|
|
|
self.commands[name] = {}
|
|
|
|
self.event_handlers[name] = []
|
|
|
|
self.plugins[name] = module.Plugin(self, self.core)
|
2011-09-23 15:43:01 +00:00
|
|
|
|
|
|
|
def unload(self, name):
|
|
|
|
if name in self.plugins:
|
|
|
|
try:
|
2011-09-24 20:26:31 +00:00
|
|
|
for command in self.commands[name].keys():
|
|
|
|
del self.core.commands[command]
|
|
|
|
for event_name, handler in self.event_handlers[name]:
|
|
|
|
self.core.xmpp.del_event_handler(event_name, handler)
|
|
|
|
|
2011-09-23 15:43:01 +00:00
|
|
|
self.plugins[name].unload()
|
|
|
|
del self.plugins[name]
|
2011-09-24 20:26:31 +00:00
|
|
|
del self.commands[name]
|
|
|
|
del self.event_handlers[name]
|
2011-09-23 15:43:01 +00:00
|
|
|
except Exception as e:
|
2011-09-24 20:26:31 +00:00
|
|
|
import traceback
|
|
|
|
self.core.information(_("Could not unload plugin (may not be safe to try again): ") + traceback.format_exc())
|
|
|
|
|
|
|
|
def add_command(self, module_name, name, handler, help, completion=None):
|
|
|
|
if name in self.core.commands:
|
|
|
|
raise Exception(_("Command '%s' already exists") % (name,))
|
|
|
|
|
|
|
|
commands = self.commands[module_name]
|
|
|
|
commands[name] = (handler, help, completion)
|
|
|
|
self.core.commands[name] = (handler, help, completion)
|
|
|
|
|
|
|
|
def add_event_handler(self, module_name, event_name, handler):
|
|
|
|
eh = self.event_handlers[module_name]
|
|
|
|
eh.append((event_name, handler))
|
|
|
|
self.core.xmpp.add_event_handler(event_name, handler)
|
|
|
|
|
|
|
|
def del_event_handler(self, module_name, event_name, handler):
|
|
|
|
self.core.xmpp.del_event_handler(event_name, handler)
|
|
|
|
eh = self.event_handlers[module_name]
|
|
|
|
eh = list(filter(lambda e : e != (event_name, handler), eh))
|