terminator/terminatorlib/plugin.py

166 lines
5.5 KiB
Python
Raw Normal View History

2009-12-17 01:07:01 +00:00
#!/usr/bin/python
# Terminator by Chris Jones <cmsj@tenshu.net>
# GPL v2 only
"""plugin.py - Base plugin system
Inspired by Armin Ronacher's post at
http://lucumr.pocoo.org/2006/7/3/python-plugin-system
Used with permission (the code in that post is to be
considered BSD licenced, per the authors wishes)
>>> registry = PluginRegistry()
>>> registry.instances
{}
>>> registry.load_plugins(True)
>>> plugins = registry.get_plugins_by_capability('test')
>>> len(plugins)
1
>>> plugins[0] #doctest: +ELLIPSIS
<testplugin.TestPlugin object at 0x...>
>>> registry.get_plugins_by_capability('this_should_not_ever_exist')
[]
>>> plugins[0].do_test()
'TestPluginWin'
"""
2009-12-17 01:07:01 +00:00
import sys
import os
import borg
from config import Config
from util import dbg, err, get_config_dir
2009-12-17 01:07:01 +00:00
class Plugin(object):
"""Definition of our base plugin class"""
capabilities = None
def __init__(self):
"""Class initialiser."""
pass
class PluginRegistry(borg.Borg):
"""Definition of a class to store plugin instances"""
available_plugins = None
2009-12-17 01:07:01 +00:00
instances = None
path = None
done = None
2009-12-17 01:07:01 +00:00
def __init__(self):
"""Class initialiser"""
2009-12-22 00:25:25 +00:00
borg.Borg.__init__(self, self.__class__.__name__)
2009-12-17 01:07:01 +00:00
self.prepare_attributes()
def prepare_attributes(self):
"""Prepare our attributes"""
if not self.instances:
self.instances = {}
if not self.path:
self.path = []
2010-01-22 19:08:12 +00:00
(head, _tail) = os.path.split(borg.__file__)
self.path.append(os.path.join(head, 'plugins'))
self.path.append(os.path.join(get_config_dir(), 'plugins'))
2009-12-17 01:07:01 +00:00
dbg('PluginRegistry::prepare_attributes: Plugin path: %s' %
self.path)
if not self.done:
self.done = False
if not self.available_plugins:
self.available_plugins = {}
2009-12-17 01:07:01 +00:00
def load_plugins(self, testing=False):
2009-12-17 01:07:01 +00:00
"""Load all plugins present in the plugins/ directory in our module"""
if self.done:
dbg('PluginRegistry::load_plugins: Already loaded')
return
config = Config()
for plugindir in self.path:
sys.path.insert(0, plugindir)
try:
files = os.listdir(plugindir)
except OSError:
sys.path.remove(plugindir)
continue
for plugin in files:
pluginpath = os.path.join(plugindir, plugin)
if os.path.isfile(pluginpath) and plugin[-3:] == '.py':
dbg('PluginRegistry::load_plugins: Importing plugin %s' %
plugin)
try:
module = __import__(plugin[:-3], None, None, [''])
2010-06-10 15:56:17 +00:00
for item in getattr(module, 'AVAILABLE'):
if item not in self.available_plugins.keys():
func = getattr(module, item)
self.available_plugins[item] = func
if not testing and item not in config['enabled_plugins']:
dbg('plugin %s not enabled, skipping' % item)
continue
if item not in self.instances:
self.instances[item] = func()
2010-01-22 19:08:12 +00:00
except Exception, ex:
err('PluginRegistry::load_plugins: Importing plugin %s \
2010-01-22 19:08:12 +00:00
failed: %s' % (plugin, ex))
2009-12-17 01:07:01 +00:00
self.done = True
2009-12-17 01:07:01 +00:00
def get_plugins_by_capability(self, capability):
"""Return a list of plugins with a particular capability"""
result = []
dbg('PluginRegistry::get_plugins_by_capability: searching %d plugins \
for %s' % (len(self.instances), capability))
for plugin in self.instances:
if capability in self.instances[plugin].capabilities:
2009-12-17 01:07:01 +00:00
result.append(self.instances[plugin])
return result
def get_all_plugins(self):
"""Return all plugins"""
return(self.instances)
def get_available_plugins(self):
"""Return a list of all available plugins whether they are enabled or
disabled"""
return(self.available_plugins.keys())
def is_enabled(self, plugin):
"""Return a boolean value indicating whether a plugin is enabled or
not"""
return(self.instances.has_key(plugin))
def enable(self, plugin):
"""Enable a plugin"""
dbg("Enabling %s" % plugin)
if plugin not in self.instances:
self.instances[plugin] = self.available_plugins[plugin]()
def disable(self, plugin):
"""Disable a plugin"""
dbg("Disabling %s" % plugin)
del(self.instances[plugin])
# This is where we should define a base class for each type of plugin we
# support
# URLHandler - This adds a regex match to the Terminal widget and provides a
# callback to turn that into a URL.
class URLHandler(Plugin):
"""Base class for URL handlers"""
capabilities = ['url_handler']
handler_name = None
match = None
def callback(self, url):
"""Callback to transform the enclosed URL"""
raise NotImplementedError
# MenuItem - This is able to execute code during the construction of the
# context menu of a Terminal.
class MenuItem(Plugin):
"""Base class for menu items"""
capabilities = ['terminal_menu']
def callback(self, menuitems, menu, terminal):
"""Callback to transform the enclosed URL"""
raise NotImplementedError