2011-09-24 20:26:31 +00:00
|
|
|
import imp
|
|
|
|
import os
|
|
|
|
import sys
|
2011-11-10 13:39:19 +00:00
|
|
|
import tabs
|
2011-09-24 20:26:31 +00:00
|
|
|
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')
|
2011-09-25 00:39:00 +00:00
|
|
|
|
2011-09-25 01:01:32 +00:00
|
|
|
config_home = os.environ.get("XDG_CONFIG_HOME")
|
|
|
|
if not config_home:
|
|
|
|
config_home = os.path.join(os.environ.get('HOME'), '.config')
|
|
|
|
plugins_conf_dir = os.path.join(config_home, 'poezio', 'plugins')
|
2011-09-25 00:39:00 +00:00
|
|
|
|
2011-09-24 20:26:31 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(plugins_dir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2011-09-25 00:39:00 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(plugins_conf_dir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2011-09-24 20:26:31 +00:00
|
|
|
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-11-10 13:39:19 +00:00
|
|
|
self.tab_commands = {} #module name -> dict of tab types; tab type -> commands loaded by the module
|
2011-09-23 15:43:01 +00:00
|
|
|
|
2011-11-10 16:06:28 +00:00
|
|
|
def load(self, name, notify=True):
|
2011-09-23 15:43:01 +00:00
|
|
|
if name in self.plugins:
|
2011-09-27 17:14:18 +00:00
|
|
|
self.unload(name)
|
2011-09-23 15:43:01 +00:00
|
|
|
|
|
|
|
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
|
2011-09-24 21:10:55 +00:00
|
|
|
self.core.information(_("Could not load plugin: ") + traceback.format_exc(), 'Error')
|
2011-09-24 20:26:31 +00:00
|
|
|
return
|
|
|
|
finally:
|
|
|
|
if imp.lock_held():
|
|
|
|
imp.release_lock()
|
|
|
|
|
|
|
|
self.modules[name] = module
|
|
|
|
self.commands[name] = {}
|
2011-11-10 13:39:19 +00:00
|
|
|
self.tab_commands[name] = {}
|
2011-09-24 20:26:31 +00:00
|
|
|
self.event_handlers[name] = []
|
2011-09-25 00:39:00 +00:00
|
|
|
self.plugins[name] = module.Plugin(self, self.core, plugins_conf_dir)
|
2011-11-10 16:06:28 +00:00
|
|
|
if notify:
|
|
|
|
self.core.information('Plugin %s loaded' % name, 'Info')
|
2011-09-23 15:43:01 +00:00
|
|
|
|
2011-11-10 16:06:28 +00:00
|
|
|
def unload(self, name, notify=True):
|
2011-09-23 15:43:01 +00:00
|
|
|
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]
|
2011-11-10 13:39:19 +00:00
|
|
|
for tab in list(self.tab_commands[name].keys()):
|
|
|
|
for command in self.tab_commands[name][tab]:
|
|
|
|
self.del_tab_command(name, getattr(tabs, tab), command[0])
|
|
|
|
del self.tab_commands[name][tab]
|
2011-09-24 20:26:31 +00:00
|
|
|
for event_name, handler in self.event_handlers[name]:
|
2011-11-09 13:02:17 +00:00
|
|
|
self.del_event_handler(name, event_name, handler)
|
2011-09-24 20:26:31 +00:00
|
|
|
|
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]
|
2011-11-10 13:39:19 +00:00
|
|
|
del self.tab_commands[name]
|
2011-09-24 20:26:31 +00:00
|
|
|
del self.event_handlers[name]
|
2011-11-10 16:06:28 +00:00
|
|
|
if notify:
|
|
|
|
self.core.information('Plugin %s unloaded' % name, 'Info')
|
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())
|
|
|
|
|
2011-10-02 11:21:51 +00:00
|
|
|
def del_command(self, module_name, name):
|
|
|
|
if name in self.commands[module_name]:
|
|
|
|
del self.commands[module_name][name]
|
|
|
|
if name in self.core.commands:
|
|
|
|
del self.core.commands[name]
|
|
|
|
|
2011-11-10 13:39:19 +00:00
|
|
|
def add_tab_command(self, module_name, tab_type, name, handler, help, completion=None):
|
|
|
|
commands = self.tab_commands[module_name]
|
|
|
|
t = tab_type.__name__
|
2011-11-12 23:25:30 +00:00
|
|
|
if name in tab_type.plugin_commands:
|
|
|
|
return
|
2011-11-10 13:39:19 +00:00
|
|
|
if not t in commands:
|
|
|
|
commands[t] = []
|
|
|
|
commands[t].append((name, handler, help, completion))
|
2011-11-12 23:25:30 +00:00
|
|
|
tab_type.plugin_commands[name] = (handler, help, completion)
|
2011-11-10 13:39:19 +00:00
|
|
|
for tab in self.core.tabs:
|
|
|
|
if isinstance(tab, tab_type):
|
2011-11-12 23:25:30 +00:00
|
|
|
tab.update_commands()
|
2011-11-10 13:39:19 +00:00
|
|
|
|
|
|
|
def del_tab_command(self, module_name, tab_type, name):
|
|
|
|
commands = self.tab_commands[module_name]
|
|
|
|
t = tab_type.__name__
|
|
|
|
if not t in commands:
|
|
|
|
return
|
|
|
|
for command in commands[t]:
|
|
|
|
if command[0] == name:
|
|
|
|
commands[t].remove(command)
|
|
|
|
del tab_type.plugin_commands[name]
|
|
|
|
for tab in self.core.tabs:
|
|
|
|
if isinstance(tab, tab_type) and name in tab.commands:
|
|
|
|
del tab.commands[name]
|
|
|
|
|
2011-09-24 20:26:31 +00:00
|
|
|
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)
|
|
|
|
|
2011-11-09 13:02:17 +00:00
|
|
|
def add_event_handler(self, module_name, event_name, handler, position=0):
|
2011-09-24 20:26:31 +00:00
|
|
|
eh = self.event_handlers[module_name]
|
|
|
|
eh.append((event_name, handler))
|
2011-11-09 13:02:17 +00:00
|
|
|
if event_name in self.core.events.events:
|
|
|
|
self.core.events.add_event_handler(event_name, handler, position)
|
|
|
|
else:
|
|
|
|
self.core.xmpp.add_event_handler(event_name, handler)
|
2011-09-24 20:26:31 +00:00
|
|
|
|
|
|
|
def del_event_handler(self, module_name, event_name, handler):
|
2011-11-09 13:02:17 +00:00
|
|
|
if event_name in self.core.events.events:
|
|
|
|
self.core.events.del_event_handler(None, handler)
|
|
|
|
else:
|
|
|
|
self.core.xmpp.del_event_handler(event_name, handler)
|
2011-09-24 20:26:31 +00:00
|
|
|
eh = self.event_handlers[module_name]
|
|
|
|
eh = list(filter(lambda e : e != (event_name, handler), eh))
|
2011-09-24 21:10:55 +00:00
|
|
|
|
|
|
|
def completion_load(self, the_input):
|
|
|
|
"""
|
|
|
|
completion function that completes the name of the plugins, from
|
|
|
|
all .py files in plugins_dir
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
names = os.listdir(plugins_dir)
|
|
|
|
except OSError as e:
|
|
|
|
self.core.information(_('Completion failed: %s' % e), 'Error')
|
|
|
|
return
|
|
|
|
plugins_files = [name[:-3] for name in names if name.endswith('.py')]
|
|
|
|
return the_input.auto_completion(plugins_files, '')
|
|
|
|
|
|
|
|
def completion_unload(self, the_input):
|
|
|
|
"""
|
|
|
|
completion function that completes the name of the plugins that are loaded
|
|
|
|
"""
|
|
|
|
return the_input.auto_completion(list(self.plugins.keys()), '')
|