openlp/openlp/core/state.py

170 lines
5.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-01 10:10:57 +00:00
# Copyright (c) 2008-2022 OpenLP Developers #
2019-04-13 13:00:22 +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, 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/>. #
##########################################################################
"""
The :mod:`core` module provides state management
2019-01-08 19:30:28 +00:00
All the core functions of the OpenLP application including the GUI, settings, logging and a plugin framework are
contained within the openlp.core module.
"""
import logging
2019-07-20 12:27:28 +00:00
from openlp.core.common import Singleton
2018-10-26 18:30:59 +00:00
from openlp.core.common.registry import Registry
2018-10-20 14:41:32 +00:00
from openlp.core.common.mixins import LogMixin
from openlp.core.lib.plugin import PluginStatus
log = logging.getLogger()
2018-10-20 14:41:32 +00:00
class StateModule(LogMixin):
def __init__(self):
"""
2019-01-08 19:30:28 +00:00
Holder of State information per module
2018-10-20 14:41:32 +00:00
"""
super(StateModule, self).__init__()
self.name = None
self.order = 0
2018-10-26 18:30:59 +00:00
self.is_plugin = None
2018-10-20 14:41:32 +00:00
self.status = PluginStatus.Inactive
2018-11-04 17:13:56 +00:00
self.pass_preconditions = False
2018-10-20 14:41:32 +00:00
self.requires = None
self.required_by = None
2018-11-04 17:13:56 +00:00
self.text = None
2018-10-20 14:41:32 +00:00
2019-07-20 12:27:28 +00:00
class State(LogMixin, metaclass=Singleton):
def load_settings(self):
self.modules = {}
def save_settings(self):
pass
2018-10-26 18:30:59 +00:00
def add_service(self, name, order, is_plugin=False, status=PluginStatus.Active, requires=None):
2018-10-20 14:41:32 +00:00
"""
2019-01-08 19:30:28 +00:00
Add a module to the array and load dependencies. There will only be one item per module
2018-10-20 14:41:32 +00:00
:param name: Module name
2018-10-26 18:30:59 +00:00
:param order: Order to display
:param is_plugin: Am I a plugin
2018-10-20 14:41:32 +00:00
:param status: The active status
:param requires: Module name this requires
:return:
"""
if name not in self.modules:
2018-10-20 14:41:32 +00:00
state = StateModule()
state.name = name
state.order = order
2018-10-26 18:30:59 +00:00
state.is_plugin = is_plugin
2018-10-20 14:41:32 +00:00
state.status = status
state.requires = requires
state.required_by = []
self.modules[name] = state
if requires and requires in self.modules:
if requires not in self.modules[requires].required_by:
self.modules[requires].required_by.append(name)
2018-11-04 17:13:56 +00:00
def missing_text(self, name, text):
"""
Updates the preconditions state of a module
:param name: Module name
:param text: Module missing text
:return:
"""
self.modules[name].text = text
2018-11-04 20:23:25 +00:00
def get_text(self):
"""
return an string of error text
:return: a string of text
"""
error_text = ''
for mod in self.modules:
if self.modules[mod].text:
error_text = error_text + self.modules[mod].text + '\n'
return error_text
2018-10-20 14:41:32 +00:00
def update_pre_conditions(self, name, status):
"""
Updates the preconditions state of a module
2018-10-21 15:35:59 +00:00
2018-10-20 14:41:32 +00:00
:param name: Module name
:param status: Module new status
:return:
"""
self.modules[name].pass_preconditions = status
2018-10-26 18:30:59 +00:00
if self.modules[name].is_plugin:
plugin = Registry().get('{mod}_plugin'.format(mod=name))
if status:
self.log_debug('Plugin {plugin} active'.format(plugin=str(plugin.name)))
plugin.set_status()
else:
plugin.status = PluginStatus.Disabled
2018-10-20 14:41:32 +00:00
def flush_preconditions(self):
"""
Now all modules are loaded lets update all the preconditions.
2018-10-21 15:35:59 +00:00
2018-10-20 14:41:32 +00:00
:return:
"""
for mods in self.modules:
for req in self.modules[mods].required_by:
2018-10-21 15:35:59 +00:00
self.modules[req].pass_preconditions = self.modules[mods].pass_preconditions
2018-10-26 18:30:59 +00:00
plugins_list = sorted(self.modules, key=lambda state: self.modules[state].order)
mdl = {}
for pl in plugins_list:
mdl[pl] = self.modules[pl]
self.modules = mdl
2018-10-20 14:41:32 +00:00
def is_module_active(self, name):
return self.modules[name].status == PluginStatus.Active
2018-10-21 15:35:59 +00:00
def check_preconditions(self, name):
"""
2019-01-08 19:30:28 +00:00
Checks if a modules preconditions have been met.
2018-10-21 15:35:59 +00:00
:param name: Module name
2019-01-08 19:30:28 +00:00
:return: Have the preconditions been met.
:rtype: bool
2018-10-21 15:35:59 +00:00
"""
try:
if self.modules[name].requires is None:
return self.modules[name].pass_preconditions
else:
mod = self.modules[name].requires
return self.modules[mod].pass_preconditions
except KeyError:
# Module is missing so therefore not found.
return False
2018-10-25 16:37:12 +00:00
2019-01-04 18:21:38 +00:00
def list_plugins(self):
2018-11-04 20:23:25 +00:00
"""
Return a list of plugins
:return: an array of plugins
"""
2018-10-25 16:37:12 +00:00
plugins = []
for mod in self.modules:
2019-01-04 18:11:15 +00:00
if self.modules[mod].is_plugin:
2018-10-26 18:30:59 +00:00
plugins.append(Registry().get('{mod}_plugin'.format(mod=mod)))
2018-10-25 16:37:12 +00:00
return plugins