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
This commit is contained in:
Martin Thompson 2008-11-02 20:36:36 +00:00
parent cb7d2b04d7
commit 0485c4d6cb
7 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,2 @@
from plugin import Plugin
from plugin_manager import PluginManager

33
openlp/plugins/plugin.py Normal file
View File

@ -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 '<Plugin %s>' % (
self.__class__.__name__
)

View File

@ -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;

View File

@ -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))

View File

@ -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

View File

View File

@ -0,0 +1,7 @@
from openlp.plugins import Plugin
class testplugin2(Plugin):
name="testplugin2"
version=1
def __init__(self):
pass