1
# -*- coding: utf-8 -*-
2
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
4
###############################################################################
5
# OpenLP - Open Source Lyrics Projection #
6
# --------------------------------------------------------------------------- #
7
# Copyright (c) 2008-2019 OpenLP Developers #
8
# --------------------------------------------------------------------------- #
9
# This program is free software; you can redistribute it and/or modify it #
10
# under the terms of the GNU General Public License as published by the Free #
11
# Software Foundation; version 2 of the License. #
13
# This program is distributed in the hope that it will be useful, but WITHOUT #
14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
15
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
18
# You should have received a copy of the GNU General Public License along #
19
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
21
###############################################################################
24
The :mod:`core` module provides state management
26
All the core functions of the OpenLP application including the GUI, settings, logging and a plugin framework are
27
contained within the openlp.core module.
31
from openlp.core.common.registry import Registry
32
from openlp.core.common.mixins import LogMixin
33
from openlp.core.lib.plugin import PluginStatus
36
log = logging.getLogger()
39
class StateModule(LogMixin):
42
Holder of State information per module
44
super(StateModule, self).__init__()
48
self.status = PluginStatus.Inactive
49
self.pass_preconditions = False
51
self.required_by = None
55
class State(LogMixin):
61
Re-implement the __new__ method to make sure we create a true singleton.
63
if not cls.__instance__:
64
cls.__instance__ = object.__new__(cls)
65
return cls.__instance__
67
def load_settings(self):
70
def save_settings(self):
73
def add_service(self, name, order, is_plugin=False, status=PluginStatus.Active, requires=None):
75
Add a module to the array and load dependencies. There will only be one item per module
76
:param name: Module name
77
:param order: Order to display
78
:param is_plugin: Am I a plugin
79
:param status: The active status
80
:param requires: Module name this requires
83
if name not in self.modules:
87
state.is_plugin = is_plugin
89
state.requires = requires
90
state.required_by = []
91
self.modules[name] = state
92
if requires and requires in self.modules:
93
if requires not in self.modules[requires].required_by:
94
self.modules[requires].required_by.append(name)
96
def missing_text(self, name, text):
98
Updates the preconditions state of a module
100
:param name: Module name
101
:param text: Module missing text
104
self.modules[name].text = text
108
return an string of error text
109
:return: a string of text
112
for mod in self.modules:
113
if self.modules[mod].text:
114
error_text = error_text + self.modules[mod].text + '\n'
117
def update_pre_conditions(self, name, status):
119
Updates the preconditions state of a module
121
:param name: Module name
122
:param status: Module new status
125
self.modules[name].pass_preconditions = status
126
if self.modules[name].is_plugin:
127
plugin = Registry().get('{mod}_plugin'.format(mod=name))
129
self.log_debug('Plugin {plugin} active'.format(plugin=str(plugin.name)))
132
plugin.status = PluginStatus.Disabled
134
def flush_preconditions(self):
136
Now all modules are loaded lets update all the preconditions.
140
for mods in self.modules:
141
for req in self.modules[mods].required_by:
142
self.modules[req].pass_preconditions = self.modules[mods].pass_preconditions
143
plugins_list = sorted(self.modules, key=lambda state: self.modules[state].order)
145
for pl in plugins_list:
146
mdl[pl] = self.modules[pl]
149
def is_module_active(self, name):
150
return self.modules[name].status == PluginStatus.Active
152
def check_preconditions(self, name):
154
Checks if a modules preconditions have been met.
156
:param name: Module name
157
:return: Have the preconditions been met.
160
if self.modules[name].requires is None:
161
return self.modules[name].pass_preconditions
163
mod = self.modules[name].requires
164
return self.modules[mod].pass_preconditions
166
def list_plugins(self):
168
Return a list of plugins
169
:return: an array of plugins
172
for mod in self.modules:
173
if self.modules[mod].is_plugin:
174
plugins.append(Registry().get('{mod}_plugin'.format(mod=mod)))