From 8b0c226826344a3f74e1f690fc0326a0514f2d1b Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Sat, 22 Nov 2008 09:28:03 +0000 Subject: [PATCH] Moved plugin.py and pluginmanager.py to the core module, removed from the plugins module. bzr-revno: 110 --- copyright.txt | 4 ++ openlp.pyw | 4 +- openlp/core/__init__.py | 4 ++ openlp/core/plugin.py | 87 +++++++++++++++++++++++++------- openlp/core/pluginmanager.py | 87 ++++++++++++++++++++++++++++++++ openlp/plugins/plugin.py | 33 ------------ openlp/plugins/plugin_manager.py | 45 ----------------- 7 files changed, 166 insertions(+), 98 deletions(-) create mode 100644 openlp/core/pluginmanager.py delete mode 100644 openlp/plugins/plugin.py delete mode 100644 openlp/plugins/plugin_manager.py diff --git a/copyright.txt b/copyright.txt index c44e62bf2..3fc2584eb 100644 --- a/copyright.txt +++ b/copyright.txt @@ -1,3 +1,6 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=80 +""" OpenLP - Open Source Lyrics Projection Copyright (c) 2008 Raoul Snyman Portions copyright (c) 2008 Martin Thompson, Tim Bentley, @@ -13,3 +16,4 @@ PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +""" diff --git a/openlp.pyw b/openlp.pyw index b0762ac11..8c4d90723 100755 --- a/openlp.pyw +++ b/openlp.pyw @@ -1,10 +1,10 @@ #!/usr/bin/env pythonw +# -*- coding: utf-8 -*- # vim: autoindent shiftwidth=4 expandtab textwidth=80 - """ OpenLP - Open Source Lyrics Projection Copyright (c) 2008 Raoul Snyman -Portions copyright (c) 2008 Martin Thompson, Tim Bentley +Portions copyright (c) 2008 Martin Thompson, Tim Bentley, This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software diff --git a/openlp/core/__init__.py b/openlp/core/__init__.py index caeb519e9..25bc9d548 100644 --- a/openlp/core/__init__.py +++ b/openlp/core/__init__.py @@ -18,3 +18,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA from openlp.core.render import Renderer from openlp.core.settingsmanager import SettingsManager +from openlp.core.plugin import Plugin +from openlp.core.pluginmanager import PluginManager + +__all__ = ['Renderer', 'SettingsManager', 'Plugin'] diff --git a/openlp/core/plugin.py b/openlp/core/plugin.py index 534c66de1..551424194 100644 --- a/openlp/core/plugin.py +++ b/openlp/core/plugin.py @@ -1,19 +1,70 @@ -""" - -Base Plugin class - -""" - -class Plugin(Interface): - "Plugin type" - - def __init__(self, mediaManager, *args, **kwargs): - "Plugin constructor called with mediaManager argument which allows adding to the - mediaManager - generally adding a page to mediaManager.Notebook" - - def GetName(): - "Return the plugins name for future plugin manager" - - - +# -*- coding: utf-8 -*- # vim: autoindent shiftwidth=4 expandtab textwidth=80 +""" +OpenLP - Open Source Lyrics Projection +Copyright (c) 2008 Raoul Snyman +Portions copyright (c) 2008 Martin Thompson, Tim Bentley, + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA +""" + +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? + """ + + def __init__(self, name=None, version=None): + """ + This is the constructor for the plugin object. This provides an easy + way for descendent plugins to populate common data. + """ + if name is not None: + self.Name = name + else: + self.Name = 'Plugin' + if version is not None: + self.__version__ = version + # this will be a MediaManagerItem if it needs one + self.MediaManagerItem = None + # this will be a PrefsPage object if it needs one + self.SettingsTab = None + #self.paint_context = None + + def save(self, data): + """ + Service item data is passed to this function, which should return a + string which can be written to the service file + """ + pass + def load(self, str): + """ + A string from the service 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/core/pluginmanager.py b/openlp/core/pluginmanager.py new file mode 100644 index 000000000..bb3103cf2 --- /dev/null +++ b/openlp/core/pluginmanager.py @@ -0,0 +1,87 @@ +""" +OpenLP - Open Source Lyrics Projection +Copyright (c) 2008 Raoul Snyman +Portions copyright (c) 2008 Martin Thompson, Tim Bentley, + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA +""" +# vim: autoindent shiftwidth=4 expandtab textwidth=80 + +# import openlp.plugins +import os, sys +import logging + +# Shouldn't this be a core class? i.e. from openlp.core import Plugin +from openlp.core import Plugin + +# Not sure what this is for. I prefer keeping as much code in the class as possible. +mypath=os.path.split(os.path.abspath(__file__))[0] +sys.path.insert(0,(os.path.join(mypath, '..' ,'..'))) + +class PluginManager: + """ + This is the Plugin manager, which loads all the plugins, + and executes all the hooks, as and when necessary. + """ + global log + log=logging.getLogger("PluginMgr") + log.info("Plugin manager loaded") + + def __init__(self, dir): + """ + The constructor for the plugin manager. This does a, b and c. + """ + 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('/', '.') + # import the modules + log.debug("Importing %s from %s." % (modulename, 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; + + def hook_media_manager(self, MediaToolBox): + """ + Loop through all the plugins. If a plugin has a valid media manager item, + add it to the media manager. + """ + for plugin in self.plugins: + if plugin.MediaManagerItem is not None: + log.debug('Inserting media manager item from %s' % plugin.name) + MediaToolBox.addItem(plugin.MediaManagerItem, plugin.Icon, plugin.Name) + diff --git a/openlp/plugins/plugin.py b/openlp/plugins/plugin.py deleted file mode 100644 index 004cf12f7..000000000 --- a/openlp/plugins/plugin.py +++ /dev/null @@ -1,33 +0,0 @@ -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 deleted file mode 100644 index d3828a3e6..000000000 --- a/openlp/plugins/plugin_manager.py +++ /dev/null @@ -1,45 +0,0 @@ -# import openlp.plugins -import os, sys -import logging -mypath=os.path.split(os.path.abspath(__file__))[0] -sys.path.insert(0,(os.path.join(mypath, '..' ,'..'))) -from openlp.plugins.plugin import Plugin -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; - - - -