2009-12-17 01:07:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
2009-12-17 12:54:42 +00:00
|
|
|
"""plugin.py - Base plugin system
|
2009-12-19 15:07:22 +00:00
|
|
|
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)
|
2009-12-17 12:54:42 +00:00
|
|
|
|
|
|
|
>>> registry = PluginRegistry()
|
2009-12-17 13:51:55 +00:00
|
|
|
>>> registry.instances
|
|
|
|
{}
|
2009-12-17 12:54:42 +00:00
|
|
|
>>> registry.load_plugins()
|
2009-12-17 13:51:55 +00:00
|
|
|
>>> plugins = registry.get_plugins_by_capability('test')
|
|
|
|
>>> len(plugins)
|
|
|
|
1
|
|
|
|
>>> plugins[0] #doctest: +ELLIPSIS
|
|
|
|
<testplugin.TestPlugin object at 0x...>
|
2009-12-17 12:54:42 +00:00
|
|
|
>>> registry.get_plugins_by_capability('this_should_not_ever_exist')
|
|
|
|
[]
|
2009-12-17 13:51:55 +00:00
|
|
|
>>> plugins[0].do_test()
|
|
|
|
'TestPluginWin'
|
2009-12-17 12:54:42 +00:00
|
|
|
|
|
|
|
"""
|
2009-12-17 01:07:01 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import borg
|
2009-12-17 12:54:42 +00:00
|
|
|
from util import dbg, err
|
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"""
|
|
|
|
instances = None
|
|
|
|
path = None
|
2009-12-17 23:16:42 +00:00
|
|
|
done = None
|
2009-12-17 01:07:01 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
|
|
|
borg.Borg.__init__(self)
|
|
|
|
self.prepare_attributes()
|
|
|
|
|
|
|
|
def prepare_attributes(self):
|
|
|
|
"""Prepare our attributes"""
|
|
|
|
if not self.instances:
|
|
|
|
self.instances = {}
|
|
|
|
if not self.path:
|
|
|
|
(head, tail) = os.path.split(borg.__file__)
|
2009-12-18 09:24:23 +00:00
|
|
|
# FIXME: self.path should really be a list so we can have something
|
|
|
|
# in the users home directory
|
2009-12-17 01:07:01 +00:00
|
|
|
self.path = os.path.join(head, 'plugins')
|
|
|
|
dbg('PluginRegistry::prepare_attributes: Plugin path: %s' %
|
|
|
|
self.path)
|
2009-12-17 23:16:42 +00:00
|
|
|
if not self.done:
|
|
|
|
self.done = False
|
2009-12-17 01:07:01 +00:00
|
|
|
|
|
|
|
def load_plugins(self):
|
|
|
|
"""Load all plugins present in the plugins/ directory in our module"""
|
2009-12-17 23:16:42 +00:00
|
|
|
if self.done:
|
|
|
|
dbg('PluginRegistry::load_plugins: Already loaded')
|
|
|
|
return
|
|
|
|
|
2009-12-17 01:07:01 +00:00
|
|
|
sys.path.insert(0, self.path)
|
|
|
|
files = os.listdir(self.path)
|
|
|
|
for plugin in files:
|
2009-12-17 13:51:55 +00:00
|
|
|
pluginpath = os.path.join(self.path, plugin)
|
|
|
|
if os.path.isfile(pluginpath) and plugin[-3:] == '.py':
|
2009-12-17 01:07:01 +00:00
|
|
|
dbg('PluginRegistry::load_plugins: Importing plugin %s' %
|
|
|
|
plugin)
|
2009-12-17 12:54:42 +00:00
|
|
|
try:
|
2009-12-17 13:51:55 +00:00
|
|
|
module = __import__(plugin[:-3], None, None, [''])
|
|
|
|
for item in getattr(module, 'available'):
|
|
|
|
if item not in self.instances:
|
|
|
|
func = getattr(module, item)
|
|
|
|
self.instances[item] = func()
|
2009-12-17 12:54:42 +00:00
|
|
|
except Exception as e:
|
|
|
|
err('PluginRegistry::load_plugins: Importing plugin %s \
|
|
|
|
failed: %s' % (plugin, e))
|
2009-12-17 01:07:01 +00:00
|
|
|
|
2009-12-17 23:16:42 +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 = []
|
2009-12-17 12:54:42 +00:00
|
|
|
dbg('PluginRegistry::get_plugins_by_capability: searching %d plugins \
|
2009-12-17 13:51:55 +00:00
|
|
|
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
|
|
|
|
|
2009-12-17 13:51:55 +00:00
|
|
|
def get_all_plugins(self):
|
|
|
|
"""Return all plugins"""
|
|
|
|
return(self.instances)
|
|
|
|
|
2009-12-17 12:54:42 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import doctest
|
|
|
|
sys.path.insert(0, 'plugins')
|
|
|
|
(failed, attempted) = doctest.testmod()
|
|
|
|
print "%d/%d tests failed" % (failed, attempted)
|
2009-12-17 13:51:55 +00:00
|
|
|
|