Subversion Repositories basico

Rev

Rev 341 | Rev 363 | 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
25
from basico.services.srv_db import Database
26
from basico.services.srv_driver import SeleniumDriver
27
from basico.services.srv_cols import Collections
28
from basico.services.srv_annot import Annotation
355 t00mlabs 29
from basico.services.srv_attachment import Attachment
340 t00mlabs 30
from basico.services.srv_notify import Notification
31
 
32
 
33
#DOC: http://stackoverflow.com/questions/16410852/keyboard-interrupt-with-with-python-gtk
34
signal.signal(signal.SIGINT, signal.SIG_DFL)
35
 
36
class Basico(object):
37
    """
38
    Basico Application class
39
    """
40
    def __init__(self):
41
        """
42
        Basico class
43
        """
44
        self.setup_environment()
45
        self.setup_logging()
46
        self.setup_services()
47
 
48
 
49
    def setup_environment(self):
50
        """
51
        Setup Basico environment
52
        """
53
        # Add webdriver path to System PATH
54
        os.environ["PATH"] += os.pathsep + LPATH['DRIVERS']
55
 
56
        # Create local paths if they do not exist
57
        for entry in LPATH:
58
            if not os.path.exists(LPATH[entry]):
59
                os.makedirs(LPATH[entry])
60
 
61
 
62
    def setup_logging(self):
63
        """
64
        Setup Basico logging
65
        """
66
        # Truncate existing log file
67
        if os.path.exists(FILE['LOG']):
68
            with open(FILE['LOG'], 'w') as flog:
69
                pass
70
 
71
        #Initialize logging        
72
        self.log = get_logger(__class__.__name__)
73
        self.log.info("Basico %s started", APP['version'])
74
        self.log.debug("Global path: %s", GPATH['ROOT'])
75
        self.log.debug("Local path: %s", LPATH['ROOT'])
76
        self.log.debug("Logging all messages to file: %s", FILE['LOG'])
77
        self.log.debug("Logging only events: %s", FILE['EVENTS'])
78
 
79
 
80
    def setup_services(self):
81
        """
82
        Setup Basico Services
83
        """
84
 
85
        # Declare and register services
86
        self.services = {}
87
        try:
88
            services = {
89
                'GUI'           :   GUI(),
90
                'Utils'         :   Utils(),
91
                'UIF'           :   UIFuncs(),
92
                'SAP'           :   SAP(),
93
                'Settings'      :   Settings(),
94
                'Notify'        :   Notification(),
95
                'IM'            :   IconManager(),
96
                'Callbacks'     :   Callback(),
97
                'DB'            :   Database(),
98
                'Driver'        :   SeleniumDriver(),
99
                'Collections'   :   Collections(),
100
                'Annotation'    :   Annotation(),
355 t00mlabs 101
                'Attachment'    :   Attachment(),
340 t00mlabs 102
                'BNR'           :   BackupRestoreMan(),
103
                'Notify'        :   Notification()
104
            }
105
 
106
            for name in services:
107
                self.register_service(name, services[name])
108
        except Exception as error:
109
            self.log.error(error)
110
            raise
111
 
112
 
113
    def get_service(self, name):
114
        """
115
        Get/Start a registered service
116
        """
117
        try:
118
            service = self.services[name]
119
            logname = service.__class__.__name__
120
            if not service.is_started():
121
                service.start(self, logname, name)
122
            return service
123
        except KeyError as service:
124
            self.log.error("Service %s not registered or not found", service)
125
            raise
126
 
127
 
128
    def register_service(self, name, service):
129
        """
130
        Register a new service
131
        """
132
        try:
133
            self.services[name] = service
134
        except KeyError as error:
135
            self.log.error(error)
136
 
137
 
138
    def deregister_service(self, name):
139
        """
140
        Deregister a running service
141
        """
142
        self.services[name].end()
143
        self.services[name] = None
144
 
145
 
146
    def stop(self):
147
        """
148
        For each service registered, it executes the 'end' method
149
        (if any) to finalize them properly.
150
        """
151
 
152
        # Deregister all services loaded starting by the GUI service
153
        self.deregister_service('GUI')
154
        for name in self.services:
155
            try:
156
                if name != 'GUI':
157
                    self.deregister_service(name)
158
            except Exception as error:
159
                self.log.error(error)
160
                raise
161
        self.log.info("Basico %s finished", APP['version'])
162
 
163
 
164
    def run(self):
165
        """
166
        Start Basico
167
        """
168
        self.srvdrv = self.get_service('Driver')
169
        self.srvgui = self.get_service('GUI')
170
        self.srvgui.run()
171
 
172
 
173
def main():
174
    """
175
    Entry point
176
    """
177
    basico = Basico()
178
    basico.run()