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 #
|
|
|
|
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
|
2010-07-24 22:10:47 +00:00
|
|
|
# Gorven, Scott Guerrieri, Meinert Jordan, Andreas Preikschat, Christian #
|
|
|
|
# Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
|
|
|
|
# Carsten Tinggaard, 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
|
|
|
|
|
2010-09-16 21:10:36 +00:00
|
|
|
from openlp.core.lib import Plugin, StringContent, 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
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
"""
|
2009-03-22 07:13:34 +00:00
|
|
|
log.info(u'Plugin manager initing')
|
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)
|
2010-05-27 14:41:47 +00:00
|
|
|
self.plugin_helpers = []
|
2008-11-22 09:28:03 +00:00
|
|
|
self.plugins = []
|
2010-05-27 14:41:47 +00:00
|
|
|
# this has to happen after the UI is sorted
|
|
|
|
# self.find_plugins(plugin_dir)
|
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
|
|
|
"""
|
2009-03-22 07:13:34 +00:00
|
|
|
self.plugin_helpers = plugin_helpers
|
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
|
2009-01-20 21:20:05 +00:00
|
|
|
continue
|
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:
|
2009-03-22 07:13:34 +00:00
|
|
|
plugin = p(self.plugin_helpers)
|
2009-08-15 06:54:37 +00:00
|
|
|
log.debug(u'Loaded plugin %s with helpers', unicode(p))
|
|
|
|
plugin_objects.append(plugin)
|
2009-02-28 23:19:45 +00:00
|
|
|
except TypeError:
|
2010-02-12 18:42:09 +00:00
|
|
|
log.exception(u'loaded plugin %s has no helpers', unicode(p))
|
2009-08-15 06:54:37 +00:00
|
|
|
plugins_list = sorted(plugin_objects, self.order_by_weight)
|
|
|
|
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
|
|
|
|
2008-12-14 15:49:54 +00:00
|
|
|
def order_by_weight(self, x, y):
|
2009-07-08 05:12:16 +00:00
|
|
|
"""
|
|
|
|
Sort two plugins and order them by their weight.
|
|
|
|
|
|
|
|
``x``
|
|
|
|
The first plugin.
|
|
|
|
|
|
|
|
``y``
|
|
|
|
The second plugin.
|
|
|
|
"""
|
2008-12-14 15:49:54 +00:00
|
|
|
return cmp(x.weight, y.weight)
|
2008-11-22 09:28:03 +00:00
|
|
|
|
2009-10-07 05:09:35 +00:00
|
|
|
def hook_media_manager(self, mediadock):
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
2009-07-10 13:16:15 +00:00
|
|
|
Loop through all the plugins. If a plugin has a valid media manager
|
|
|
|
item, add it to the media manager.
|
2009-07-08 05:12:16 +00:00
|
|
|
|
|
|
|
``mediatoolbox``
|
|
|
|
The Media Manager itself.
|
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:
|
2010-07-05 16:00:48 +00:00
|
|
|
plugin.mediaItem = plugin.getMediaManagerItem()
|
2009-02-28 23:19:45 +00:00
|
|
|
|
|
|
|
def hook_settings_tabs(self, settingsform=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
|
|
|
|
|
|
|
``settingsform``
|
|
|
|
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:
|
2010-07-05 16:00:48 +00:00
|
|
|
plugin.settings_tab = plugin.getSettingsTab()
|
2010-09-16 21:10:36 +00:00
|
|
|
visible_title = plugin.getString(StringContent.VisibleName)
|
2009-11-03 18:14:25 +00:00
|
|
|
if plugin.settings_tab:
|
|
|
|
log.debug(u'Inserting settings tab item from %s' %
|
2010-09-16 21:10:36 +00:00
|
|
|
visible_title[u'title'])
|
2010-11-03 18:03:28 +00:00
|
|
|
settingsform.addTab(visible_title[u'title'],
|
|
|
|
plugin.settings_tab)
|
2009-10-03 11:07:58 +00:00
|
|
|
else:
|
2010-11-03 18:03:28 +00:00
|
|
|
log.debug(
|
|
|
|
u'No tab settings in %s' % visible_title[u'title'])
|
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
|
|
|
"""
|
|
|
|
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)
|
2010-07-05 16:00:48 +00:00
|
|
|
if not plugin.isActive():
|
|
|
|
plugin.removeToolboxItem()
|
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()
|
2010-12-26 11:04:47 +00:00
|
|
|
log.info(u'Finalisation Complete for %s ' % plugin.name)
|