2008-11-28 14:05:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-12-28 22:06:43 +00:00
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 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 #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2012-12-29 20:56:56 +00:00
|
|
|
# Copyright (c) 2008-2013 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan #
|
2012-06-22 14:14:53 +00:00
|
|
|
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
|
2012-11-11 21:16:14 +00:00
|
|
|
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
|
2012-10-21 13:16:22 +00:00
|
|
|
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
|
|
|
|
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
|
|
|
|
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
|
2012-12-01 07:57:54 +00:00
|
|
|
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
|
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
|
2013-02-11 09:34:24 +00:00
|
|
|
import imp
|
2008-11-22 09:28:03 +00:00
|
|
|
|
2013-01-22 21:09:43 +00:00
|
|
|
from openlp.core.lib import Plugin, PluginStatus, Registry
|
2008-11-22 09:28:03 +00:00
|
|
|
|
2010-02-27 15:31:23 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2013-02-01 19:58:18 +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.
|
|
|
|
"""
|
2009-03-23 19:17:07 +00:00
|
|
|
log.info(u'Plugin manager loaded')
|
2011-10-11 18:10:53 +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.
|
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')
|
2013-01-22 21:09:43 +00:00
|
|
|
Registry().register(u'plugin_manager', self)
|
2013-02-11 09:34:24 +00:00
|
|
|
self.base_path = os.path.abspath(plugin_dir)
|
|
|
|
log.debug(u'Base path %s ', self.base_path)
|
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
|
|
|
|
2013-02-11 21:16:30 +00:00
|
|
|
def find_plugins(self):
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
2013-02-11 21:16:30 +00:00
|
|
|
Scan a directory for objects inheriting from the ``Plugin`` class.
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
2011-03-12 17:02:24 +00:00
|
|
|
log.info(u'Finding plugins')
|
2013-02-11 21:16:30 +00:00
|
|
|
start_depth = len(os.path.abspath(self.base_path).split(os.sep))
|
|
|
|
present_plugin_dir = os.path.join(self.base_path, 'presentations')
|
|
|
|
log.debug(u'finding plugins in %s at depth %d', unicode(self.base_path), start_depth)
|
|
|
|
for root, dirs, files in os.walk(self.base_path):
|
|
|
|
if sys.platform == 'darwin' and root.startswith(present_plugin_dir):
|
2013-02-11 13:46:08 +00:00
|
|
|
# TODO Presentation plugin is not yet working on Mac OS X.
|
|
|
|
# For now just ignore it. The following code will ignore files from the presentation plugin directory
|
|
|
|
# and thereby never import the plugin.
|
|
|
|
continue
|
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))
|
2013-02-11 09:34:24 +00:00
|
|
|
this_depth = len(path.split(os.sep))
|
|
|
|
if this_depth - start_depth > 2:
|
2009-05-20 20:17:20 +00:00
|
|
|
# skip anything lower down
|
2011-12-07 20:41:19 +00:00
|
|
|
break
|
2013-02-11 09:34:24 +00:00
|
|
|
module_name = name[:-3]
|
2008-11-22 09:28:03 +00:00
|
|
|
# import the modules
|
2013-02-11 09:34:24 +00:00
|
|
|
log.debug(u'Importing %s from %s. Depth %d', module_name, root, this_depth)
|
2008-11-22 09:28:03 +00:00
|
|
|
try:
|
2013-02-11 09:43:26 +00:00
|
|
|
# Use the "imp" library to try to get around a problem with the PyUNO library which
|
|
|
|
# monkey-patches the __import__ function to do some magic. This causes issues with our tests.
|
2013-02-11 17:41:32 +00:00
|
|
|
# First, try to find the module we want to import, searching the directory in root
|
2013-02-11 09:34:24 +00:00
|
|
|
fp, path_name, description = imp.find_module(module_name, [root])
|
2013-02-11 17:41:32 +00:00
|
|
|
# Then load the module (do the actual import) using the details from find_module()
|
2013-02-11 09:34:24 +00:00
|
|
|
imp.load_module(module_name, fp, path_name, description)
|
2008-12-01 20:12:34 +00:00
|
|
|
except ImportError, e:
|
2013-02-11 17:41:32 +00:00
|
|
|
log.exception(u'Failed to import module %s on path %s: %s', module_name, 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:
|
2013-01-23 21:05:25 +00:00
|
|
|
plugin = p()
|
2011-03-12 08:12:02 +00:00
|
|
|
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
|
|
|
|
2013-02-18 20:41:08 +00:00
|
|
|
def hook_settings_tabs(self):
|
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
|
|
|
|
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:
|
2013-02-18 20:41:08 +00:00
|
|
|
plugin.createSettingsTab(self.settings_form)
|
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:
|
2012-12-28 22:06:43 +00:00
|
|
|
log.info(u'initialising plugins %s in a %s state' % (plugin.name, plugin.isActive()))
|
2010-07-05 16:00:48 +00:00
|
|
|
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
|
2013-02-03 09:07:31 +00:00
|
|
|
|
|
|
|
def new_service_created(self):
|
|
|
|
"""
|
|
|
|
Loop through all the plugins and give them an opportunity to handle a new service
|
|
|
|
"""
|
|
|
|
log.info(u'plugins - new service created')
|
|
|
|
for plugin in self.plugins:
|
|
|
|
if plugin.isActive():
|
|
|
|
plugin.new_service_created()
|
|
|
|
|
2013-02-18 20:41:08 +00:00
|
|
|
#def _get_settings_form(self):
|
|
|
|
# """
|
|
|
|
# Adds the plugin manager to the class dynamically
|
|
|
|
# """
|
|
|
|
# if not hasattr(self, u'_settings_form'):
|
|
|
|
# self._settings_form = Registry().get(u'settings_form')
|
|
|
|
# return self._settings_form
|
|
|
|
|
|
|
|
#settings_form = property(_get_settings_form)
|
|
|
|
|