~tomasgroth/openlp/portable-path

« back to all changes in this revision

Viewing changes to openlp/core/state.py

  • Committer: Tomas Groth
  • Date: 2019-04-30 19:02:42 UTC
  • mfrom: (2829.2.32 openlp)
  • Revision ID: tomasgroth@yahoo.dk-20190430190242-6zwjk8724tyux70m
trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
 
3
 
 
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.                              #
 
12
#                                                                             #
 
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    #
 
16
# more details.                                                               #
 
17
#                                                                             #
 
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
###############################################################################
 
22
 
 
23
"""
 
24
The :mod:`core` module provides state management
 
25
 
 
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.
 
28
"""
 
29
import logging
 
30
 
 
31
from openlp.core.common.registry import Registry
 
32
from openlp.core.common.mixins import LogMixin
 
33
from openlp.core.lib.plugin import PluginStatus
 
34
 
 
35
 
 
36
log = logging.getLogger()
 
37
 
 
38
 
 
39
class StateModule(LogMixin):
 
40
    def __init__(self):
 
41
        """
 
42
        Holder of State information per module
 
43
        """
 
44
        super(StateModule, self).__init__()
 
45
        self.name = None
 
46
        self.order = 0
 
47
        self.is_plugin = None
 
48
        self.status = PluginStatus.Inactive
 
49
        self.pass_preconditions = False
 
50
        self.requires = None
 
51
        self.required_by = None
 
52
        self.text = None
 
53
 
 
54
 
 
55
class State(LogMixin):
 
56
 
 
57
    __instance__ = None
 
58
 
 
59
    def __new__(cls):
 
60
        """
 
61
        Re-implement the __new__ method to make sure we create a true singleton.
 
62
        """
 
63
        if not cls.__instance__:
 
64
            cls.__instance__ = object.__new__(cls)
 
65
        return cls.__instance__
 
66
 
 
67
    def load_settings(self):
 
68
        self.modules = {}
 
69
 
 
70
    def save_settings(self):
 
71
        pass
 
72
 
 
73
    def add_service(self, name, order, is_plugin=False, status=PluginStatus.Active, requires=None):
 
74
        """
 
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
 
81
        :return:
 
82
        """
 
83
        if name not in self.modules:
 
84
            state = StateModule()
 
85
            state.name = name
 
86
            state.order = order
 
87
            state.is_plugin = is_plugin
 
88
            state.status = status
 
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)
 
95
 
 
96
    def missing_text(self, name, text):
 
97
        """
 
98
        Updates the preconditions state of a module
 
99
 
 
100
        :param name: Module name
 
101
        :param text: Module missing text
 
102
        :return:
 
103
        """
 
104
        self.modules[name].text = text
 
105
 
 
106
    def get_text(self):
 
107
        """
 
108
        return an string of error text
 
109
        :return: a string of text
 
110
        """
 
111
        error_text = ''
 
112
        for mod in self.modules:
 
113
            if self.modules[mod].text:
 
114
                error_text = error_text + self.modules[mod].text + '\n'
 
115
        return error_text
 
116
 
 
117
    def update_pre_conditions(self, name, status):
 
118
        """
 
119
        Updates the preconditions state of a module
 
120
 
 
121
        :param name: Module name
 
122
        :param status: Module new status
 
123
        :return:
 
124
        """
 
125
        self.modules[name].pass_preconditions = status
 
126
        if self.modules[name].is_plugin:
 
127
            plugin = Registry().get('{mod}_plugin'.format(mod=name))
 
128
            if status:
 
129
                self.log_debug('Plugin {plugin} active'.format(plugin=str(plugin.name)))
 
130
                plugin.set_status()
 
131
            else:
 
132
                plugin.status = PluginStatus.Disabled
 
133
 
 
134
    def flush_preconditions(self):
 
135
        """
 
136
        Now all modules are loaded lets update all the preconditions.
 
137
 
 
138
        :return:
 
139
        """
 
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)
 
144
        mdl = {}
 
145
        for pl in plugins_list:
 
146
            mdl[pl] = self.modules[pl]
 
147
        self.modules = mdl
 
148
 
 
149
    def is_module_active(self, name):
 
150
        return self.modules[name].status == PluginStatus.Active
 
151
 
 
152
    def check_preconditions(self, name):
 
153
        """
 
154
        Checks if a modules preconditions have been met.
 
155
 
 
156
        :param name: Module name
 
157
        :return: Have the preconditions been met.
 
158
        :rtype: bool
 
159
        """
 
160
        if self.modules[name].requires is None:
 
161
            return self.modules[name].pass_preconditions
 
162
        else:
 
163
            mod = self.modules[name].requires
 
164
            return self.modules[mod].pass_preconditions
 
165
 
 
166
    def list_plugins(self):
 
167
        """
 
168
        Return a list of plugins
 
169
        :return: an array of plugins
 
170
        """
 
171
        plugins = []
 
172
        for mod in self.modules:
 
173
            if self.modules[mod].is_plugin:
 
174
                plugins.append(Registry().get('{mod}_plugin'.format(mod=mod)))
 
175
        return plugins