Subversion Repositories basico

Rev

Rev 365 | Rev 376 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
340 t00mlabs 1
#!/usr/bin/python 3
2
# -*- coding: utf-8 -*-
3
"""
4
# File: basico.py
5
# Author: Tomás Vírseda
6
# License: GPL v3
7
# Description: Main entry point por Basico app
8
"""
9
 
10
import os
11
import sys
12
import signal
13
from gi.repository import GObject
14
from basico.core.mod_env import APP, LPATH, GPATH, FILE
15
from basico.core.mod_log import get_logger
16
from basico.services.srv_utils import Utils
17
from basico.services.srv_gui import GUI
18
from basico.services.srv_iconmgt import IconManager
19
from basico.services.srv_bnr import BackupRestoreMan
20
from basico.services.srv_sap import SAP
21
from basico.services.srv_settings import Settings
22
from basico.services.srv_uif import UIFuncs
23
from basico.services.srv_callbacks import Callback
24
from basico.services.srv_notify import Notification
364 t00mlabs 25
from basico.services.srv_database import Database
340 t00mlabs 26
from basico.services.srv_driver import SeleniumDriver
363 t00mlabs 27
from basico.services.srv_collections import Collections
28
from basico.services.srv_annotations import Annotation
355 t00mlabs 29
from basico.services.srv_attachment import Attachment
340 t00mlabs 30
from basico.services.srv_notify import Notification
365 t00mlabs 31
from basico.services.srv_asciidoctor import Asciidoctor
374 t00mlabs 32
from basico.widgets.wdg_splash import Splash
340 t00mlabs 33
 
34
#DOC: http://stackoverflow.com/questions/16410852/keyboard-interrupt-with-with-python-gtk
35
signal.signal(signal.SIGINT, signal.SIG_DFL)
36
 
37
class Basico(object):
38
    """
39
    Basico Application class
40
    """
41
    def __init__(self):
42
        """
43
        Basico class
44
        """
374 t00mlabs 45
 
46
        self.setup_splash()
340 t00mlabs 47
        self.setup_environment()
48
        self.setup_logging()
49
        self.setup_services()
50
 
51
 
374 t00mlabs 52
    def get_splash(self):
53
        return self.splash
54
 
55
 
56
    def setup_splash(self):
57
        self.splash = Splash(title="Basico\n0.4", font='Roboto Slab 18', font_weight='bold', font_color="#FFFFFF", background_image=FILE['SPLASH'])
58
        self.splash.start()
59
 
340 t00mlabs 60
    def setup_environment(self):
61
        """
62
        Setup Basico environment
63
        """
64
        # Add webdriver path to System PATH
65
        os.environ["PATH"] += os.pathsep + LPATH['DRIVERS']
374 t00mlabs 66
 
340 t00mlabs 67
        # Create local paths if they do not exist
68
        for entry in LPATH:
69
            if not os.path.exists(LPATH[entry]):
70
                os.makedirs(LPATH[entry])
71
 
72
 
73
    def setup_logging(self):
74
        """
75
        Setup Basico logging
76
        """
77
        # Truncate existing log file
78
        if os.path.exists(FILE['LOG']):
79
            with open(FILE['LOG'], 'w') as flog:
80
                pass
374 t00mlabs 81
 
82
        #Initialize logging
340 t00mlabs 83
        self.log = get_logger(__class__.__name__)
84
        self.log.info("Basico %s started", APP['version'])
85
        self.log.debug("Global path: %s", GPATH['ROOT'])
86
        self.log.debug("Local path: %s", LPATH['ROOT'])
87
        self.log.debug("Logging all messages to file: %s", FILE['LOG'])
88
        self.log.debug("Logging only events: %s", FILE['EVENTS'])
89
 
90
 
91
    def setup_services(self):
92
        """
93
        Setup Basico Services
94
        """
374 t00mlabs 95
 
340 t00mlabs 96
        # Declare and register services
97
        self.services = {}
98
        try:
99
            services = {
100
                'GUI'           :   GUI(),
101
                'Utils'         :   Utils(),
102
                'UIF'           :   UIFuncs(),
103
                'SAP'           :   SAP(),
104
                'Settings'      :   Settings(),
105
                'Notify'        :   Notification(),
106
                'IM'            :   IconManager(),
107
                'Callbacks'     :   Callback(),
108
                'DB'            :   Database(),
109
                'Driver'        :   SeleniumDriver(),
110
                'Collections'   :   Collections(),
111
                'Annotation'    :   Annotation(),
355 t00mlabs 112
                'Attachment'    :   Attachment(),
340 t00mlabs 113
                'BNR'           :   BackupRestoreMan(),
365 t00mlabs 114
                'Notify'        :   Notification(),
115
                'Asciidoctor'   :   Asciidoctor()
340 t00mlabs 116
            }
117
 
118
            for name in services:
119
                self.register_service(name, services[name])
120
        except Exception as error:
121
            self.log.error(error)
122
            raise
123
 
124
 
125
    def get_service(self, name):
126
        """
127
        Get/Start a registered service
128
        """
129
        try:
130
            service = self.services[name]
131
            logname = service.__class__.__name__
132
            if not service.is_started():
133
                service.start(self, logname, name)
134
            return service
135
        except KeyError as service:
136
            self.log.error("Service %s not registered or not found", service)
137
            raise
138
 
139
 
140
    def register_service(self, name, service):
141
        """
142
        Register a new service
143
        """
144
        try:
145
            self.services[name] = service
146
        except KeyError as error:
147
            self.log.error(error)
148
 
149
 
150
    def deregister_service(self, name):
151
        """
152
        Deregister a running service
153
        """
154
        self.services[name].end()
155
        self.services[name] = None
156
 
157
 
158
    def stop(self):
159
        """
160
        For each service registered, it executes the 'end' method
161
        (if any) to finalize them properly.
162
        """
163
 
374 t00mlabs 164
        self.splash.show()
340 t00mlabs 165
        # Deregister all services loaded starting by the GUI service
166
        self.deregister_service('GUI')
167
        for name in self.services:
168
            try:
169
                if name != 'GUI':
170
                    self.deregister_service(name)
171
            except Exception as error:
172
                self.log.error(error)
173
                raise
374 t00mlabs 174
        self.splash.destroy()
340 t00mlabs 175
        self.log.info("Basico %s finished", APP['version'])
176
 
177
 
178
    def run(self):
179
        """
180
        Start Basico
181
        """
182
        self.srvdrv = self.get_service('Driver')
183
        self.srvgui = self.get_service('GUI')
184
        self.srvgui.run()
185
 
186
 
187
def main():
188
    """
189
    Entry point
190
    """
191
    basico = Basico()
192
    basico.run()