2008-11-28 14:05:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
|
|
|
OpenLP - Open Source Lyrics Projection
|
|
|
|
Copyright (c) 2008 Raoul Snyman
|
|
|
|
Portions copyright (c) 2008 Martin Thompson, Tim Bentley,
|
|
|
|
|
|
|
|
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
|
2008-11-22 09:28:03 +00:00
|
|
|
import logging
|
|
|
|
|
2008-12-01 14:25:08 +00:00
|
|
|
from openlp.core.lib import Plugin
|
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
|
|
|
|
log=logging.getLogger("PluginMgr")
|
|
|
|
log.info("Plugin manager loaded")
|
|
|
|
|
|
|
|
def __init__(self, dir):
|
|
|
|
"""
|
|
|
|
The constructor for the plugin manager. This does a, b and c.
|
|
|
|
"""
|
|
|
|
log.info("Plugin manager initing")
|
|
|
|
if not dir in sys.path:
|
|
|
|
log.debug("Inserting %s into sys.path", dir)
|
|
|
|
sys.path.insert(0, dir)
|
2008-12-02 20:09:08 +00:00
|
|
|
self.basepath = os.path.abspath(dir)
|
2008-12-01 05:58:25 +00:00
|
|
|
log.debug("Base path %s ", self.basepath)
|
2008-11-22 09:28:03 +00:00
|
|
|
self.plugins = []
|
|
|
|
self.find_plugins(dir)
|
|
|
|
log.info("Plugin manager done init")
|
|
|
|
|
|
|
|
def find_plugins(self, dir):
|
|
|
|
"""
|
|
|
|
Scan the directory dir for objects inheriting from openlp.plugin
|
|
|
|
"""
|
2008-12-01 05:58:25 +00:00
|
|
|
log.debug("find plugins " + str(dir))
|
2008-11-22 09:28:03 +00:00
|
|
|
for root, dirs, files in os.walk(dir):
|
|
|
|
for name in files:
|
|
|
|
if name.endswith(".py") and not name.startswith("__"):
|
|
|
|
path = os.path.abspath(os.path.join(root, name))
|
|
|
|
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('/', '.')
|
|
|
|
# import the modules
|
|
|
|
log.debug("Importing %s from %s." % (modulename, path))
|
|
|
|
try:
|
|
|
|
__import__(modulename, globals(), locals(), [])
|
2008-12-01 20:12:34 +00:00
|
|
|
except ImportError, e:
|
|
|
|
print e.message
|
2008-12-01 18:36:53 +00:00
|
|
|
self.plugin_classes = Plugin.__subclasses__()
|
|
|
|
self.plugins = []
|
|
|
|
plugin_objects = []
|
|
|
|
for p in self.plugin_classes:
|
2008-11-30 18:36:13 +00:00
|
|
|
plugin = p()
|
2008-12-01 18:36:53 +00:00
|
|
|
plugin_objects.append(plugin)
|
|
|
|
self.plugins = sorted(plugin_objects, self.orderByWeight)
|
|
|
|
|
|
|
|
def orderByWeight(self, x, y):
|
|
|
|
return cmp(x.Weight, y.Weight)
|
2008-11-22 09:28:03 +00:00
|
|
|
|
2008-12-01 13:15:31 +00:00
|
|
|
def hookMediaManager(self, mediatoolbox):
|
2008-11-22 09:28:03 +00:00
|
|
|
"""
|
|
|
|
Loop through all the plugins. If a plugin has a valid media manager item,
|
|
|
|
add it to the media manager.
|
|
|
|
"""
|
2008-12-01 18:36:53 +00:00
|
|
|
for plugin in self.plugins:
|
2008-11-30 20:01:19 +00:00
|
|
|
media_manager_item = plugin.getMediaManagerItem()
|
|
|
|
if media_manager_item is not None:
|
2008-11-29 05:36:16 +00:00
|
|
|
log.debug('Inserting media manager item from %s' % plugin.Name)
|
2008-12-01 18:36:53 +00:00
|
|
|
mediatoolbox.addItem(media_manager_item, plugin.Icon, media_manager_item.Title)
|
2008-11-22 09:28:03 +00:00
|
|
|
|
2008-12-01 13:15:31 +00:00
|
|
|
def hookHandleEvent(self, event):
|
|
|
|
pass
|