openlp/openlp/core/lib/pluginmanager.py

210 lines
8.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2019 OpenLP Developers #
# ---------------------------------------------------------------------- #
# 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, either version 3 of the License, or #
# (at your option) any later version. #
# #
# 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2010-06-10 01:57:59 +00:00
"""
Provide plugin management
"""
2008-12-01 18:36:53 +00:00
import os
2018-11-04 17:13:56 +00:00
from PyQt5 import QtWidgets
2018-10-25 16:37:12 +00:00
from openlp.core.state import State
2017-10-07 07:05:07 +00:00
from openlp.core.common import extension_loader
from openlp.core.common.applocation import AppLocation
2018-11-04 17:13:56 +00:00
from openlp.core.common.i18n import translate, UiStrings
2017-10-23 22:09:57 +00:00
from openlp.core.common.mixins import LogMixin, RegistryProperties
from openlp.core.common.registry import RegistryBase
from openlp.core.lib.plugin import Plugin, PluginStatus
2010-02-27 15:31:23 +00:00
2017-10-23 22:09:57 +00:00
class PluginManager(RegistryBase, LogMixin, RegistryProperties):
"""
This is the Plugin manager, which loads all the plugins,
and executes all the hooks, as and when necessary.
"""
2013-12-19 20:17:06 +00:00
def __init__(self, parent=None):
"""
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.
"""
2013-12-19 20:17:06 +00:00
super(PluginManager, self).__init__(parent)
self.log_info('Plugin manager Initialising')
2017-11-18 22:37:24 +00:00
self.log_debug('Base path {path}'.format(path=AppLocation.get_directory(AppLocation.PluginsDir)))
self.plugins = []
self.log_info('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
Scan a directory for objects inheriting from the ``Plugin`` class.
2013-02-18 21:36:36 +00:00
"""
glob_pattern = os.path.join('plugins', '*', '[!.]*plugin.py')
extension_loader(glob_pattern)
plugin_classes = Plugin.__subclasses__()
for p in plugin_classes:
try:
p()
self.log_debug('Loaded plugin {plugin}'.format(plugin=str(p)))
except TypeError:
self.log_exception('Failed to load plugin {plugin}'.format(plugin=str(p)))
2018-10-23 16:43:52 +00:00
def bootstrap_post_set_up(self):
"""
Bootstrap all the plugin manager functions
"""
2013-02-18 21:36:36 +00:00
self.hook_settings_tabs()
# Find and insert media manager items
self.hook_media_manager()
# Call the hook method to pull in import 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.
self.initialise_plugins()
2018-10-23 16:43:52 +00:00
def bootstrap_completion(self):
"""
Give all the plugins a chance to perform some tasks at startup
"""
self.application.process_events()
2018-12-02 10:19:07 +00:00
for plugin in State().list_plugins():
if plugin and plugin.is_active():
2018-10-23 16:43:52 +00:00
plugin.app_startup()
self.application.process_events()
@staticmethod
def hook_media_manager():
"""
Create the plugins' media manager items.
"""
2018-10-25 16:37:12 +00:00
for plugin in State().list_plugins():
2018-12-02 10:19:07 +00:00
if plugin and 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
"""
2018-10-26 18:30:59 +00:00
for plugin in State().list_plugins():
2018-12-02 10:19:07 +00:00
if plugin and plugin.status is not PluginStatus.Disabled:
2013-04-05 19:37:15 +00:00
plugin.create_settings_tab(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.
"""
2018-10-26 18:30:59 +00:00
for plugin in State().list_plugins():
2018-12-02 10:19:07 +00:00
if plugin and plugin.status is not PluginStatus.Disabled:
2013-03-19 19:43:22 +00:00
plugin.add_import_menu_item(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.
"""
2018-10-26 18:30:59 +00:00
for plugin in State().list_plugins():
2018-12-02 10:19:07 +00:00
if plugin and plugin.status is not PluginStatus.Disabled:
2014-04-12 20:19:22 +00:00
plugin.add_export_menu_item(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.
"""
2018-10-26 18:30:59 +00:00
for plugin in State().list_plugins():
2018-12-02 10:19:07 +00:00
if plugin and plugin.status is not PluginStatus.Disabled:
2013-03-19 19:43:22 +00:00
plugin.add_tools_menu_item(self.main_window.tools_menu)
2009-09-17 18:24:13 +00:00
2018-11-04 17:13:56 +00:00
@staticmethod
def hook_upgrade_plugin_settings(settings):
"""
Loop through all the plugins and give them an opportunity to upgrade their settings.
2014-03-17 19:05:55 +00:00
:param settings: The Settings object containing the old settings.
"""
2018-10-26 18:30:59 +00:00
for plugin in State().list_plugins():
2018-12-02 10:19:07 +00:00
if plugin and plugin.status is not PluginStatus.Disabled:
plugin.upgrade_settings(settings)
def initialise_plugins(self):
"""
2014-03-17 19:05:55 +00:00
Loop through all the plugins and give them an opportunity to initialise themselves.
"""
2018-11-04 17:13:56 +00:00
uninitialised_plugins = []
2019-01-27 14:42:23 +00:00
2018-10-26 18:30:59 +00:00
for plugin in State().list_plugins():
2018-12-02 09:20:06 +00:00
if plugin:
self.log_info('initialising plugins {plugin} in a {state} state'.format(plugin=plugin.name,
state=plugin.is_active()))
if plugin.is_active():
try:
plugin.initialise()
self.log_info('Initialisation Complete for {plugin}'.format(plugin=plugin.name))
except Exception:
uninitialised_plugins.append(plugin.name.title())
self.log_exception('Unable to initialise plugin {plugin}'.format(plugin=plugin.name))
2018-11-04 20:23:25 +00:00
display_text = ''
2019-01-27 14:42:23 +00:00
2018-11-04 17:13:56 +00:00
if uninitialised_plugins:
2019-01-04 20:39:05 +00:00
display_text = translate('OpenLP.PluginManager', 'Unable to initialise the following plugins:') + \
2019-01-04 20:50:40 +00:00
'\n\n'.join(uninitialised_plugins) + '\n\n'
2018-11-04 20:23:25 +00:00
error_text = State().get_text()
if error_text:
display_text = display_text + error_text + '\n'
2018-11-04 17:13:56 +00:00
if display_text:
2018-11-04 20:23:25 +00:00
display_text = display_text + translate('OpenLP.PluginManager', 'See the log file for more details')
2018-11-04 17:13:56 +00:00
QtWidgets.QMessageBox.critical(None, UiStrings().Error, display_text,
QtWidgets.QMessageBox.StandardButtons(QtWidgets.QMessageBox.Ok))
def finalise_plugins(self):
"""
2014-03-17 19:05:55 +00:00
Loop through all the plugins and give them an opportunity to clean themselves up
"""
2018-10-26 18:30:59 +00:00
for plugin in State().list_plugins():
2018-12-02 10:19:07 +00:00
if plugin and plugin.is_active():
plugin.finalise()
self.log_info('Finalisation Complete for {plugin}'.format(plugin=plugin.name))
2018-11-04 17:13:56 +00:00
@staticmethod
def get_plugin_by_name(name):
"""
Return the plugin which has a name with value ``name``.
"""
2018-10-26 18:30:59 +00:00
for plugin in State().list_plugins():
2018-12-02 10:19:07 +00:00
if plugin and plugin.name == name:
return plugin
return None
2013-02-03 09:07:31 +00:00
2018-11-04 17:13:56 +00:00
@staticmethod
def new_service_created():
2013-02-03 09:07:31 +00:00
"""
Loop through all the plugins and give them an opportunity to handle a new service
"""
2018-10-26 18:30:59 +00:00
for plugin in State().list_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()