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 #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2009-12-31 12:52:01 +00:00
|
|
|
# Copyright (c) 2008-2010 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
|
2009-11-30 20:29:26 +00:00
|
|
|
# Gorven, Scott Guerrieri, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
|
|
|
|
# Carsten Tinggaard #
|
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 #
|
|
|
|
###############################################################################
|
2009-07-14 13:51:27 +00:00
|
|
|
|
2008-12-01 18:36:53 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2008-11-22 09:28:03 +00:00
|
|
|
import logging
|
|
|
|
|
2009-09-21 17:56:36 +00:00
|
|
|
from openlp.core.lib import Plugin, PluginStatus
|
2008-11-22 09:28:03 +00:00
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
global log
|
2009-05-20 20:17:20 +00:00
|
|
|
log = logging.getLogger(u'PluginMgr')
|
2009-03-23 19:17:07 +00:00
|
|
|
log.info(u'Plugin manager loaded')
|
2008-11-22 09:28:03 +00:00
|
|
|
|
|
|
|
def __init__(self, dir):
|
|
|
|
"""
|
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.
|
|
|
|
|
|
|
|
``dir``
|
|
|
|
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')
|
2008-11-22 09:28:03 +00:00
|
|
|
if not dir in sys.path:
|
2009-06-16 18:21:24 +00:00
|
|
|
log.debug(u'Inserting %s into sys.path', dir)
|
2008-11-22 09:28:03 +00:00
|
|
|
sys.path.insert(0, dir)
|
2008-12-02 20:09:08 +00:00
|
|
|
self.basepath = os.path.abspath(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 = []
|
2009-03-12 20:19:24 +00:00
|
|
|
# this has to happen after the UI is sorted self.find_plugins(dir)
|
2010-02-03 17:23:33 +00:00
|
|
|
log.warn(u'Plugin manager Initialised')
|
2009-02-28 23:19:45 +00:00
|
|
|
|
2009-08-26 05:00:19 +00:00
|
|
|
def find_plugins(self, dir, plugin_helpers):
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
2009-09-04 22:50:19 +00:00
|
|
|
Scan the directory ``dir`` for objects inheriting from the ``Plugin``
|
|
|
|
class.
|
2009-07-10 13:16:15 +00:00
|
|
|
|
|
|
|
``dir``
|
|
|
|
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
|
2009-05-20 20:17:20 +00:00
|
|
|
startdepth = len(os.path.abspath(dir).split(os.sep))
|
2009-07-08 05:12:16 +00:00
|
|
|
log.debug(u'find plugins %s at depth %d', unicode(dir), startdepth)
|
2009-02-28 23:19:45 +00:00
|
|
|
|
2008-11-22 09:28:03 +00:00
|
|
|
for root, dirs, files in os.walk(dir):
|
|
|
|
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))
|
|
|
|
if thisdepth-startdepth > 2:
|
|
|
|
# skip anything lower down
|
2009-01-20 21:20:05 +00:00
|
|
|
continue
|
2008-11-22 09:28:03 +00:00
|
|
|
modulename, pyext = os.path.splitext(path)
|
|
|
|
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:
|
2009-10-11 19:31:46 +00:00
|
|
|
log.exception(u'Failed to import module %s on path %s for reason %s',
|
2009-10-10 04:56:06 +00:00
|
|
|
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:
|
2009-07-08 05:12:16 +00:00
|
|
|
log.error(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:
|
|
|
|
if plugin.check_pre_conditions():
|
|
|
|
log.debug(u'Plugin %s active', unicode(plugin.name))
|
2009-11-07 07:42:18 +00:00
|
|
|
plugin.set_status()
|
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:
|
2009-10-03 13:08:18 +00:00
|
|
|
plugin.media_item = plugin.get_media_manager_item()
|
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:
|
2009-10-03 19:02:40 +00:00
|
|
|
plugin.settings_tab = plugin.get_settings_tab()
|
2009-11-03 18:14:25 +00:00
|
|
|
if plugin.settings_tab:
|
|
|
|
log.debug(u'Inserting settings tab item from %s' %
|
|
|
|
plugin.name)
|
2009-10-07 05:09:35 +00:00
|
|
|
settingsform.addTab(plugin.name, plugin.settings_tab)
|
2009-10-03 11:07:58 +00:00
|
|
|
else:
|
2009-10-03 13:08:18 +00:00
|
|
|
log.debug(u'No tab settings in %s' % plugin.name)
|
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:
|
|
|
|
plugin.add_import_menu_item(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:
|
|
|
|
plugin.add_export_menu_item(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:
|
|
|
|
plugin.add_tools_menu_item(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'
|
|
|
|
% (plugin.name, plugin.is_active()))
|
2009-10-03 13:08:18 +00:00
|
|
|
if plugin.is_active():
|
2009-09-18 17:37:11 +00:00
|
|
|
plugin.initialise()
|
2010-02-03 17:23:33 +00:00
|
|
|
log.warn(u'Initialisation Complete for %s ' % plugin.name)
|
2009-10-07 05:09:35 +00:00
|
|
|
if not plugin.is_active():
|
2009-10-03 18:39:44 +00:00
|
|
|
plugin.remove_toolbox_item()
|
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:
|
2009-10-03 13:08:18 +00:00
|
|
|
if plugin.is_active():
|
2010-02-03 17:23:33 +00:00
|
|
|
plugin.finalise()
|
|
|
|
log.warn(u'Finalisation Complete for %s ' % plugin.name)
|