openlp/openlp/core/lib/pluginmanager.py

259 lines
11 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2012-12-28 22:06:43 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# 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 #
# 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, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
# --------------------------------------------------------------------------- #
# 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
import logging
import imp
2013-01-22 21:09:43 +00:00
from openlp.core.lib import Plugin, PluginStatus, Registry
2013-02-17 12:57:52 +00:00
from openlp.core.utils import AppLocation
2010-02-27 15:31:23 +00:00
log = logging.getLogger(__name__)
2013-02-01 19:58:18 +00:00
class PluginManager(object):
"""
This is the Plugin manager, which loads all the plugins,
and executes all the hooks, as and when necessary.
"""
log.info(u'Plugin manager loaded')
2011-10-11 18:10:53 +00:00
2013-02-17 12:57:52 +00:00
def __init__(self):
"""
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-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-21 21:18:26 +00:00
Registry().register_function(u'bootstrap_initialise', self.bootstrap_initialise)
2013-02-17 12:57:52 +00:00
self.base_path = os.path.abspath(AppLocation.get_directory(AppLocation.PluginsDir))
log.debug(u'Base path %s ', self.base_path)
self.plugins = []
2010-02-06 17:13:55 +00:00
log.info(u'Plugin manager Initialised')
2013-02-21 21:18:26 +00:00
def bootstrap_initialise(self):
2013-02-18 21:36:36 +00:00
"""
Bootstrap all the plugin manager functions
"""
2013-02-22 07:15:07 +00:00
log.info(u'bootstrap_initialise')
2013-02-18 21:36:36 +00:00
self.find_plugins()
# hook methods have to happen after find_plugins. Find plugins needs
# the controllers hence the hooks have moved from setupUI() to here
# Find and insert settings tabs
log.info(u'hook settings')
self.hook_settings_tabs()
# Find and insert media manager items
log.info(u'hook media')
self.hook_media_manager()
# Call the hook method to pull in import menus.
log.info(u'hook menus')
self.hook_import_menu()
# Call the hook method to pull in export menus.
self.hook_export_menu()
# Call the hook method to pull in tools menus.
self.hook_tools_menu()
# Call the initialise method to setup plugins.
log.info(u'initialise plugins')
self.initialise_plugins()
def find_plugins(self):
"""
Scan a directory for objects inheriting from the ``Plugin`` class.
"""
log.info(u'Finding plugins')
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
for name in files:
2009-05-20 20:17:20 +00:00
if name.endswith(u'.py') and not name.startswith(u'__'):
path = os.path.abspath(os.path.join(root, name))
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
module_name = name[:-3]
# import the modules
log.debug(u'Importing %s from %s. Depth %d', module_name, root, this_depth)
try:
# 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
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()
imp.load_module(module_name, fp, path_name, description)
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])
plugin_classes = Plugin.__subclasses__()
2008-12-01 18:36:53 +00:00
plugin_objects = []
for p in plugin_classes:
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))
plugin_objects.append(plugin)
except TypeError:
2011-03-12 08:12:02 +00:00
log.exception(u'Failed to load plugin %s', unicode(p))
plugins_list = sorted(plugin_objects, key=lambda plugin: plugin.weight)
for plugin in plugins_list:
2010-07-05 16:00:48 +00:00
if plugin.checkPreConditions():
log.debug(u'Plugin %s active', unicode(plugin.name))
2013-03-19 17:53:32 +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)
def hook_media_manager(self):
"""
Create the plugins' media manager items.
"""
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:
2013-03-19 17:53:32 +00:00
plugin.create_media_manager_item()
2013-02-18 20:41:08 +00:00
def hook_settings_tabs(self):
"""
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
"""
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)
2013-02-18 20:57:14 +00:00
def hook_import_menu(self):
"""
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.
"""
for plugin in self.plugins:
2009-10-03 11:07:58 +00:00
if plugin.status is not PluginStatus.Disabled:
2013-02-18 20:57:14 +00:00
plugin.addImportMenuItem(self.main_window.file_import_menu)
2013-02-18 20:57:14 +00:00
def hook_export_menu(self):
"""
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.
"""
for plugin in self.plugins:
2009-10-03 11:07:58 +00:00
if plugin.status is not PluginStatus.Disabled:
2013-02-18 20:57:14 +00:00
plugin.addExportMenuItem(self.main_window.file_export_menu)
2013-02-18 20:57:14 +00:00
def hook_tools_menu(self):
2009-09-17 18:24:13 +00:00
"""
Loop through all the plugins and give them an opportunity to add an
item to the tools menu.
"""
for plugin in self.plugins:
2009-10-03 11:07:58 +00:00
if plugin.status is not PluginStatus.Disabled:
2013-02-18 20:57:14 +00:00
plugin.addToolsMenuItem(self.main_window.tools_menu)
2009-09-17 18:24:13 +00:00
def hook_upgrade_plugin_settings(self, settings):
"""
Loop through all the plugins and give them an opportunity to upgrade their settings.
``settings``
The Settings object containing the old settings.
"""
for plugin in self.plugins:
if plugin.status is not PluginStatus.Disabled:
plugin.upgrade_settings(settings)
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.
"""
2011-03-12 17:35:27 +00:00
log.info(u'Initialise Plugins - Started')
for plugin in self.plugins:
2013-03-19 17:53:32 +00:00
log.info(u'initialising plugins %s in a %s state' % (plugin.name, plugin.is_active()))
if plugin.is_active():
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')
def finalise_plugins(self):
"""
Loop through all the plugins and give them an opportunity to
clean themselves up
"""
log.info(u'finalising plugins')
for plugin in self.plugins:
2013-03-19 17:53:32 +00:00
if plugin.is_active():
plugin.finalise()
log.info(u'Finalisation Complete for %s ' % plugin.name)
def get_plugin_by_name(self, name):
"""
Return the plugin which has a name with value ``name``.
"""
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:
2013-03-19 17:53:32 +00:00
if plugin.is_active():
2013-02-03 09:07:31 +00:00
plugin.new_service_created()
2013-02-18 20:57:14 +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)
def _get_main_window(self):
"""
Adds the main window to the class dynamically
"""
if not hasattr(self, u'_main_window'):
self._main_window = Registry().get(u'main_window')
return self._main_window
2013-02-18 20:41:08 +00:00
2013-02-18 20:57:14 +00:00
main_window = property(_get_main_window)
2013-02-18 20:41:08 +00:00