From 0485c4d6cb2d3bc22ecbac9730ddb1d1384e959b Mon Sep 17 00:00:00 2001 From: Martin Thompson Date: Sun, 2 Nov 2008 20:36:36 +0000 Subject: [PATCH] A first bash at some plugin code. Very sparse, but the manager can find plugins and report back on them --This line, and those below, will be ignored-- A plugins/plugin_manager.py A plugins/test A plugins/test/test_plugin_manager.py A plugins/testplugin2 A plugins/testplugin2/__init__.py A plugins/testplugin2/testplugin2.py A plugins/plugin.py A plugins/__init__.py A plugins/testplugin1.py bzr-revno: 73 --- openlp/plugins/__init__.py | 2 + openlp/plugins/plugin.py | 33 +++++++++++++++++ openlp/plugins/plugin_manager.py | 43 ++++++++++++++++++++++ openlp/plugins/test/test_plugin_manager.py | 39 ++++++++++++++++++++ openlp/plugins/testplugin1.py | 12 ++++++ openlp/plugins/testplugin2/__init__.py | 0 openlp/plugins/testplugin2/testplugin2.py | 7 ++++ 7 files changed, 136 insertions(+) create mode 100644 openlp/plugins/__init__.py create mode 100644 openlp/plugins/plugin.py create mode 100644 openlp/plugins/plugin_manager.py create mode 100644 openlp/plugins/test/test_plugin_manager.py create mode 100644 openlp/plugins/testplugin1.py create mode 100644 openlp/plugins/testplugin2/__init__.py create mode 100644 openlp/plugins/testplugin2/testplugin2.py diff --git a/openlp/plugins/__init__.py b/openlp/plugins/__init__.py new file mode 100644 index 000000000..41a166708 --- /dev/null +++ b/openlp/plugins/__init__.py @@ -0,0 +1,2 @@ +from plugin import Plugin +from plugin_manager import PluginManager diff --git a/openlp/plugins/plugin.py b/openlp/plugins/plugin.py new file mode 100644 index 000000000..004cf12f7 --- /dev/null +++ b/openlp/plugins/plugin.py @@ -0,0 +1,33 @@ +class Plugin(object): + """Base class for openlp plugins to inherit from. + + Basic attributes are: + name: the name that should appear in the plugins list + version: The version number of this iteration of the plugin (just an incrementing integer!) + paint_context: A list of paint contexts? + """ + name="Base Plugin" + version=0 + + + def __init__(self): + self.paint_context=None + self.prefshandler=None # this will be a PrefsPage object if it needs one + self.media_manager_item=None # this will be a MediaManagerItem if it needs one + def write_oos_data(self, data): + """OOS data is passed to this function, which should return a string which can be written to the OOS file""" + pass + def read_oos_data(self, str): + """data from the OOS file is passed in. This function parses and sets up the internals of the plugin""" + pass + + def render(self, screen=None): + """render the screenth screenful of data to self.paint_conext""" + pass + + def __repr__(self): + return '' % ( + self.__class__.__name__ + ) + + diff --git a/openlp/plugins/plugin_manager.py b/openlp/plugins/plugin_manager.py new file mode 100644 index 000000000..2996e1817 --- /dev/null +++ b/openlp/plugins/plugin_manager.py @@ -0,0 +1,43 @@ +# import openlp.plugins +from openlp.plugins import Plugin +import os, sys +import logging +class PluginManager: + global log + log=logging.getLogger("PluginMgr") + log.info("Plugin manager loaded") + def __init__(self, dir): + log.info("Plugin manager initing") + if not dir in sys.path: + log.debug("Inserting %s into sys.path", dir) + sys.path.insert(0, dir) + self.basepath=os.path.abspath(dir) + self.plugins=[] + self.find_plugins(dir) + log.info("Plugin manager done init") + def find_plugins(self, dir): + """Scan the directory dir for objects inheriting from openlp.plugin""" + log.debug("find plugins" + str(dir)) + for root,dirs, files in os.walk(dir): + for name in files: + if name.endswith(".py") and not name.startswith("__"): + path=os.path.abspath(os.path.join(root,name)) + modulename,pyext = os.path.splitext(path) + prefix=os.path.commonprefix([self.basepath, path]) + # hack off the plugin base path + modulename=modulename[len(prefix)+1:] + modulename=modulename.replace('/','.') + + log.debug("Importing "+modulename+" from "+path) + try: + __import__(modulename, globals(), locals(), []) + except ImportError: + pass + self.plugins=Plugin.__subclasses__() + self.plugin_by_name={} + for p in self.plugins: + self.plugin_by_name[p.name]=p; + + + + diff --git a/openlp/plugins/test/test_plugin_manager.py b/openlp/plugins/test/test_plugin_manager.py new file mode 100644 index 000000000..1a585032f --- /dev/null +++ b/openlp/plugins/test/test_plugin_manager.py @@ -0,0 +1,39 @@ +import logging +logging.basicConfig(level=logging.DEBUG, + format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', + datefmt='%m-%d %H:%M', + filename='plugins.log', + filemode='w') + +console=logging.StreamHandler() +# set a format which is simpler for console use +formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') +# tell the handler to use this format +console.setFormatter(formatter) +logging.getLogger('').addHandler(console) +log=logging.getLogger('') + +logging.info("Logging started") +import os, sys +mypath=os.path.split(os.path.abspath(__file__))[0] + +sys.path.insert(0,(os.path.join(mypath, '..' ,'..', '..'))) +from openlp.plugins import PluginManager + +class TestPluginManager: + def test_init(self): + p=PluginManager("..") + assert (len(p.plugins)==2); + # get list of the names of the plugins + names=[plugin.name for plugin in p.plugins] + assert ("testplugin1" in names) + assert ("testplugin2" in names) + assert ("testplugin3" not in names) + assert (p.plugin_by_name["testplugin1"].version==0) + assert (p.plugin_by_name["testplugin2"].version==1) + +if __name__=="__main__": + log.debug("Starting") + p=PluginManager("..") + for plugin in p.plugins: + log.debug("Plugin %s, name=%s (version=%d)"%(str(plugin), plugin.name, plugin.version)) diff --git a/openlp/plugins/testplugin1.py b/openlp/plugins/testplugin1.py new file mode 100644 index 000000000..991e88291 --- /dev/null +++ b/openlp/plugins/testplugin1.py @@ -0,0 +1,12 @@ +from openlp.plugins import Plugin +import logging + +class testplugin1(Plugin): + name="testplugin1" + version=0 + global log + log=logging.getLogger("testplugin1") + log.info("Started") + def __init__(self): + pass + diff --git a/openlp/plugins/testplugin2/__init__.py b/openlp/plugins/testplugin2/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/openlp/plugins/testplugin2/testplugin2.py b/openlp/plugins/testplugin2/testplugin2.py new file mode 100644 index 000000000..0df5d2ecc --- /dev/null +++ b/openlp/plugins/testplugin2/testplugin2.py @@ -0,0 +1,7 @@ +from openlp.plugins import Plugin + +class testplugin2(Plugin): + name="testplugin2" + version=1 + def __init__(self): + pass