2008-11-28 14:05:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
2009-07-14 13:51:27 +00:00
|
|
|
|
2009-09-08 19:58:05 +00:00
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2010-12-26 11:04:47 +00:00
|
|
|
# Copyright (c) 2008-2011 Raoul Snyman #
|
2011-05-26 16:25:54 +00:00
|
|
|
# Portions copyright (c) 2008-2011 Tim Bentley, Gerald Britton, Jonathan #
|
|
|
|
# Corwin, Michael Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, #
|
2011-05-26 17:11:22 +00:00
|
|
|
# Armin Köhler, Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias #
|
2011-06-12 16:02:52 +00:00
|
|
|
# Põldaru, Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
|
2011-06-12 15:41:01 +00:00
|
|
|
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Frode Woldsund #
|
2009-09-08 19:58:05 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
2010-06-10 01:57:59 +00:00
|
|
|
"""
|
|
|
|
Provide plugin management
|
|
|
|
"""
|
2008-12-01 18:36:53 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2008-11-22 09:28:03 +00:00
|
|
|
import logging
|
|
|
|
|
2011-07-07 18:03:12 +00:00
|
|
|
from openlp.core.lib import Plugin, PluginStatus
|
2008-11-22 09:28:03 +00:00
|
|
|
|
2010-02-27 15:31:23 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2008-11-29 05:36:16 +00:00
|
|
|
class PluginManager(object):
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
|
|
|
This is the Plugin manager, which loads all the plugins,
|
|
|
|
and executes all the hooks, as and when necessary.
|
|
|
|
"""
|
2009-03-23 19:17:07 +00:00
|
|
|
log.info(u'Plugin manager loaded')
|
2008-11-22 09:28:03 +00:00
|
|
|
|
2011-10-11 18:10:53 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_instance():
|
2011-10-16 15:39:49 +00:00
|
|
|
"""
|
|
|
|
Obtain a single instance of class.
|
|
|
|
"""
|
2011-10-11 18:10:53 +00:00
|
|
|
return PluginManager.instance
|
|
|
|
|
2010-05-27 14:41:47 +00:00
|
|
|
def __init__(self, plugin_dir):
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
2009-07-10 13:16:15 +00:00
|
|
|
The constructor for the plugin manager. Passes the controllers on to
|
|
|
|
the plugins for them to interact with via their ServiceItems.
|
2011-05-14 12:26:05 +00:00
|
|
|
|
2010-05-27 14:41:47 +00:00
|
|
|
``plugin_dir``
|
2009-07-10 13:16:15 +00:00
|
|
|
The directory to search for plugins.
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
2011-03-12 17:35:27 +00:00
|
|
|
log.info(u'Plugin manager Initialising')
|
2011-10-11 18:10:53 +00:00
|
|
|
PluginManager.instance = self
|
2010-05-27 14:41:47 +00:00
|
|
|
if not plugin_dir in sys.path:
|
|
|
|
log.debug(u'Inserting %s into sys.path', plugin_dir)
|
|
|
|
sys.path.insert(0, plugin_dir)
|
|
|
|
self.basepath = os.path.abspath(plugin_dir)
|
2009-05-20 20:17:20 +00:00
|
|
|
log.debug(u'Base path %s ', self.basepath)
|
2008-11-22 09:28:03 +00:00
|
|
|
self.plugins = []
|
2010-02-06 17:13:55 +00:00
|
|
|
log.info(u'Plugin manager Initialised')
|
2009-02-28 23:19:45 +00:00
|
|
|
|
2010-05-27 14:41:47 +00:00
|
|
|
def find_plugins(self, plugin_dir, plugin_helpers):
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
2010-05-27 14:41:47 +00:00
|
|
|
Scan the directory ``plugin_dir`` for objects inheriting from the
|
|
|
|
``Plugin`` class.
|
2009-07-10 13:16:15 +00:00
|
|
|
|
2010-05-27 14:41:47 +00:00
|
|
|
``plugin_dir``
|
2009-07-10 13:16:15 +00:00
|
|
|
The directory to scan.
|
|
|
|
|
|
|
|
``plugin_helpers``
|
|
|
|
A list of helper objects to pass to the plugins.
|
|
|
|
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
2011-03-12 17:02:24 +00:00
|
|
|
log.info(u'Finding plugins')
|
2010-05-27 14:41:47 +00:00
|
|
|
startdepth = len(os.path.abspath(plugin_dir).split(os.sep))
|
|
|
|
log.debug(u'finding plugins in %s at depth %d',
|
|
|
|
unicode(plugin_dir), startdepth)
|
|
|
|
for root, dirs, files in os.walk(plugin_dir):
|
2008-11-22 09:28:03 +00:00
|
|
|
for name in files:
|
2009-05-20 20:17:20 +00:00
|
|
|
if name.endswith(u'.py') and not name.startswith(u'__'):
|
2008-11-22 09:28:03 +00:00
|
|
|
path = os.path.abspath(os.path.join(root, name))
|
2009-05-20 20:17:20 +00:00
|
|
|
thisdepth = len(path.split(os.sep))
|
2010-02-21 12:43:03 +00:00
|
|
|
if thisdepth - startdepth > 2:
|
2009-05-20 20:17:20 +00:00
|
|
|
# skip anything lower down
|
2011-12-07 20:41:19 +00:00
|
|
|
break
|
2010-05-27 14:41:47 +00:00
|
|
|
modulename = os.path.splitext(path)[0]
|
2008-11-22 09:28:03 +00:00
|
|
|
prefix = os.path.commonprefix([self.basepath, path])
|
|
|
|
# hack off the plugin base path
|
|
|
|
modulename = modulename[len(prefix) + 1:]
|
2008-12-03 18:13:27 +00:00
|
|
|
modulename = modulename.replace(os.path.sep, '.')
|
2008-11-22 09:28:03 +00:00
|
|
|
# import the modules
|
2009-09-19 23:05:30 +00:00
|
|
|
log.debug(u'Importing %s from %s. Depth %d',
|
|
|
|
modulename, path, thisdepth)
|
2008-11-22 09:28:03 +00:00
|
|
|
try:
|
|
|
|
__import__(modulename, globals(), locals(), [])
|
2008-12-01 20:12:34 +00:00
|
|
|
except ImportError, e:
|
2010-05-27 14:41:47 +00:00
|
|
|
log.exception(u'Failed to import module %s on path %s '
|
|
|
|
'for reason %s', modulename, path, e.args[0])
|
2009-08-13 20:02:38 +00:00
|
|
|
plugin_classes = Plugin.__subclasses__()
|
2008-12-01 18:36:53 +00:00
|
|
|
plugin_objects = []
|
2009-08-13 20:02:38 +00:00
|
|
|
for p in plugin_classes:
|
2009-02-20 21:13:04 +00:00
|
|
|
try:
|
2011-03-12 08:12:02 +00:00
|
|
|
plugin = p(plugin_helpers)
|
|
|
|
log.debug(u'Loaded plugin %s', unicode(p))
|
2009-08-15 06:54:37 +00:00
|
|
|
plugin_objects.append(plugin)
|
2009-02-28 23:19:45 +00:00
|
|
|
except TypeError:
|
2011-03-12 08:12:02 +00:00
|
|
|
log.exception(u'Failed to load plugin %s', unicode(p))
|
2011-12-07 20:25:12 +00:00
|
|
|
plugins_list = sorted(plugin_objects, key=lambda plugin: plugin.weight)
|
2009-08-15 06:54:37 +00:00
|
|
|
for plugin in plugins_list:
|
2010-07-05 16:00:48 +00:00
|
|
|
if plugin.checkPreConditions():
|
2009-08-15 06:54:37 +00:00
|
|
|
log.debug(u'Plugin %s active', unicode(plugin.name))
|
2010-07-05 16:00:48 +00:00
|
|
|
plugin.setStatus()
|
2009-10-02 19:06:07 +00:00
|
|
|
else:
|
|
|
|
plugin.status = PluginStatus.Disabled
|
2009-09-18 17:37:11 +00:00
|
|
|
self.plugins.append(plugin)
|
2009-08-15 06:54:37 +00:00
|
|
|
|
2011-12-07 20:25:12 +00:00
|
|
|
def hook_media_manager(self):
|
2009-07-08 05:12:16 +00:00
|
|
|
"""
|
2011-12-07 20:25:12 +00:00
|
|
|
Create the plugins' media manager items.
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
2008-12-01 18:36:53 +00:00
|
|
|
for plugin in self.plugins:
|
2009-10-03 11:07:58 +00:00
|
|
|
if plugin.status is not PluginStatus.Disabled:
|
2011-12-08 17:45:33 +00:00
|
|
|
plugin.createMediaManagerItem()
|
2009-02-28 23:19:45 +00:00
|
|
|
|
2011-04-14 18:30:53 +00:00
|
|
|
def hook_settings_tabs(self, settings_form=None):
|
2009-02-28 07:23:48 +00:00
|
|
|
"""
|
2009-07-10 13:16:15 +00:00
|
|
|
Loop through all the plugins. If a plugin has a valid settings tab
|
|
|
|
item, add it to the settings tab.
|
2009-09-05 08:52:01 +00:00
|
|
|
Tabs are set for all plugins not just Active ones
|
2009-07-10 13:16:15 +00:00
|
|
|
|
2011-12-07 20:25:12 +00:00
|
|
|
``settings_form``
|
2009-07-10 13:16:15 +00:00
|
|
|
Defaults to *None*. The settings form to add tabs to.
|
2009-02-28 07:23:48 +00:00
|
|
|
"""
|
|
|
|
for plugin in self.plugins:
|
2009-10-03 11:07:58 +00:00
|
|
|
if plugin.status is not PluginStatus.Disabled:
|
2011-12-08 17:45:33 +00:00
|
|
|
plugin.createSettingsTab(settings_form)
|
2011-04-14 18:30:53 +00:00
|
|
|
settings_form.plugins = self.plugins
|
2008-11-22 09:28:03 +00:00
|
|
|
|
2008-12-16 22:00:32 +00:00
|
|
|
def hook_import_menu(self, import_menu):
|
2008-12-17 12:38:02 +00:00
|
|
|
"""
|
2009-07-10 13:16:15 +00:00
|
|
|
Loop through all the plugins and give them an opportunity to add an
|
|
|
|
item to the import menu.
|
|
|
|
|
|
|
|
``import_menu``
|
|
|
|
The Import menu.
|
2008-12-17 12:38:02 +00:00
|
|
|
"""
|
2008-12-16 22:00:32 +00:00
|
|
|
for plugin in self.plugins:
|
2009-10-03 11:07:58 +00:00
|
|
|
if plugin.status is not PluginStatus.Disabled:
|
2010-07-05 16:00:48 +00:00
|
|
|
plugin.addImportMenuItem(import_menu)
|
2008-12-16 22:00:32 +00:00
|
|
|
|
2008-12-17 12:38:02 +00:00
|
|
|
def hook_export_menu(self, export_menu):
|
|
|
|
"""
|
2009-07-10 13:16:15 +00:00
|
|
|
Loop through all the plugins and give them an opportunity to add an
|
|
|
|
item to the export menu.
|
|
|
|
|
|
|
|
``export_menu``
|
|
|
|
The Export menu.
|
2008-12-17 12:38:02 +00:00
|
|
|
"""
|
|
|
|
for plugin in self.plugins:
|
2009-10-03 11:07:58 +00:00
|
|
|
if plugin.status is not PluginStatus.Disabled:
|
2010-07-05 16:00:48 +00:00
|
|
|
plugin.addExportMenuItem(export_menu)
|
2008-12-17 12:38:02 +00:00
|
|
|
|
2009-09-17 18:24:13 +00:00
|
|
|
def hook_tools_menu(self, tools_menu):
|
|
|
|
"""
|
|
|
|
Loop through all the plugins and give them an opportunity to add an
|
|
|
|
item to the tools menu.
|
|
|
|
|
|
|
|
``tools_menu``
|
|
|
|
The Tools menu.
|
|
|
|
"""
|
|
|
|
for plugin in self.plugins:
|
2009-10-03 11:07:58 +00:00
|
|
|
if plugin.status is not PluginStatus.Disabled:
|
2010-07-05 16:00:48 +00:00
|
|
|
plugin.addToolsMenuItem(tools_menu)
|
2009-09-17 18:24:13 +00:00
|
|
|
|
2009-03-22 07:13:34 +00:00
|
|
|
def initialise_plugins(self):
|
|
|
|
"""
|
2009-07-10 13:16:15 +00:00
|
|
|
Loop through all the plugins and give them an opportunity to
|
|
|
|
initialise themselves.
|
2009-03-22 07:13:34 +00:00
|
|
|
"""
|
2011-03-12 17:35:27 +00:00
|
|
|
log.info(u'Initialise Plugins - Started')
|
2009-03-22 07:13:34 +00:00
|
|
|
for plugin in self.plugins:
|
2009-10-07 05:09:35 +00:00
|
|
|
log.info(u'initialising plugins %s in a %s state'
|
2010-07-05 16:00:48 +00:00
|
|
|
% (plugin.name, plugin.isActive()))
|
|
|
|
if plugin.isActive():
|
2009-09-18 17:37:11 +00:00
|
|
|
plugin.initialise()
|
2010-02-06 17:12:03 +00:00
|
|
|
log.info(u'Initialisation Complete for %s ' % plugin.name)
|
2011-03-12 17:35:27 +00:00
|
|
|
log.info(u'Initialise Plugins - Finished')
|
2009-07-08 05:12:16 +00:00
|
|
|
|
2009-08-10 20:10:20 +00:00
|
|
|
def finalise_plugins(self):
|
2009-07-18 05:43:50 +00:00
|
|
|
"""
|
|
|
|
Loop through all the plugins and give them an opportunity to
|
|
|
|
clean themselves up
|
|
|
|
"""
|
2009-10-03 13:08:18 +00:00
|
|
|
log.info(u'finalising plugins')
|
2009-07-18 05:43:50 +00:00
|
|
|
for plugin in self.plugins:
|
2010-07-05 16:00:48 +00:00
|
|
|
if plugin.isActive():
|
2010-02-03 17:23:33 +00:00
|
|
|
plugin.finalise()
|
2011-02-24 05:47:38 +00:00
|
|
|
log.info(u'Finalisation Complete for %s ' % plugin.name)
|
2011-05-14 09:48:58 +00:00
|
|
|
|
|
|
|
def get_plugin_by_name(self, name):
|
|
|
|
"""
|
2011-12-08 17:45:33 +00:00
|
|
|
Return the plugin which has a name with value ``name``.
|
2011-05-14 09:48:58 +00:00
|
|
|
"""
|
|
|
|
for plugin in self.plugins:
|
|
|
|
if plugin.name == name:
|
|
|
|
return plugin
|
|
|
|
return None
|