openlp/openlp/core/lib/pluginmanager.py

219 lines
8.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2009 Raoul Snyman #
# Portions copyright (c) 2008-2009 Martin Thompson, Tim Bentley, Carsten #
# Tinggaard, Jon Tibble, Jonathan Corwin, Maikel Stuivenberg, Scott Guerrieri #
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
2008-12-01 18:36:53 +00:00
import os
import sys
import logging
2009-09-21 17:56:36 +00:00
from openlp.core.lib import Plugin, PluginStatus
class PluginManager(object):
"""
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')
log.info(u'Plugin manager loaded')
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.
"""
log.info(u'Plugin manager initing')
if not dir in sys.path:
log.debug(u'Inserting %s into sys.path', dir)
sys.path.insert(0, dir)
self.basepath = os.path.abspath(dir)
2009-05-20 20:17:20 +00:00
log.debug(u'Base path %s ', self.basepath)
self.plugins = []
# this has to happen after the UI is sorted self.find_plugins(dir)
2009-05-20 20:17:20 +00:00
log.info(u'Plugin manager done init')
2009-08-26 05:00:19 +00:00
def find_plugins(self, dir, plugin_helpers):
"""
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.
"""
self.plugin_helpers = plugin_helpers
2009-05-20 20:17:20 +00:00
startdepth = len(os.path.abspath(dir).split(os.sep))
log.debug(u'find plugins %s at depth %d', unicode(dir), startdepth)
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'__'):
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
continue
modulename, pyext = os.path.splitext(path)
prefix = os.path.commonprefix([self.basepath, path])
# hack off the plugin base path
modulename = modulename[len(prefix) + 1:]
modulename = modulename.replace(os.path.sep, '.')
# import the modules
2009-09-19 23:05:30 +00:00
log.debug(u'Importing %s from %s. Depth %d',
modulename, path, thisdepth)
try:
__import__(modulename, globals(), locals(), [])
except ImportError, e:
log.error(u'Failed to import module %s on path %s for reason %s', modulename, path, e.args[0])
plugin_classes = Plugin.__subclasses__()
2008-12-01 18:36:53 +00:00
self.plugins = []
plugin_objects = []
for p in plugin_classes:
try:
plugin = p(self.plugin_helpers)
log.debug(u'Loaded plugin %s with helpers', unicode(p))
plugin_objects.append(plugin)
except TypeError:
log.error(u'loaded plugin %s has no helpers', unicode(p))
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-10-02 19:06:07 +00:00
if plugin.can_be_disabled():
plugin.set_status()
else:
plugin.status = PluginStatus.Active
else:
plugin.status = PluginStatus.Disabled
2009-09-18 17:37:11 +00:00
self.plugins.append(plugin)
def order_by_weight(self, x, y):
"""
Sort two plugins and order them by their weight.
``x``
The first plugin.
``y``
The second plugin.
"""
return cmp(x.weight, y.weight)
def hook_media_manager(self, mediatoolbox):
"""
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.
``mediatoolbox``
The Media Manager itself.
"""
2008-12-01 18:36:53 +00:00
for plugin in self.plugins:
2009-10-02 19:06:07 +00:00
media_manager_item = plugin.get_media_manager_item()
if media_manager_item is not None:
log.debug(u'Inserting media manager item from %s' % \
plugin.name)
mediatoolbox.addItem(media_manager_item, plugin.icon,
media_manager_item.title)
if plugin.status == PluginStatus.Inactive:
media_manager_item.hide()
def hook_settings_tabs(self, settingsform=None):
"""
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.
"""
for plugin in self.plugins:
2009-09-18 17:37:11 +00:00
settings_tab = plugin.get_settings_tab()
if settings_tab is not None:
2009-09-18 17:37:11 +00:00
log.debug(u'Inserting settings tab item from %s' % plugin.name)
settingsform.addTab(settings_tab)
else:
2009-09-18 17:37:11 +00:00
log.debug(u'No settings in %s' % plugin.name)
def hook_import_menu(self, import_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 import menu.
``import_menu``
The Import menu.
"""
for plugin in self.plugins:
2009-10-02 19:06:07 +00:00
plugin.add_import_menu_item(import_menu)
if plugin.status == PluginStatus.Inactive:
import_menu.hide()
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.
"""
for plugin in self.plugins:
2009-10-02 19:06:07 +00:00
plugin.add_export_menu_item(export_menu)
if plugin.status == PluginStatus.Inactive:
export_menu.hide()
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-02 19:06:07 +00:00
plugin.add_tools_menu_item(tools_menu)
if plugin.status == PluginStatus.Inactive:
tools_menu.hide()
2009-09-17 18:24:13 +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.
"""
for plugin in self.plugins:
2009-09-18 17:37:11 +00:00
if plugin.status == PluginStatus.Active:
plugin.initialise()
def finalise_plugins(self):
"""
Loop through all the plugins and give them an opportunity to
clean themselves up
"""
for plugin in self.plugins:
2009-09-18 17:37:11 +00:00
if plugin.status == PluginStatus.Active:
plugin.finalise()