Subversion Repositories basico

Rev

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

Rev Author Line No. Line
243 t00m 1
"""
255 t00m 2
#!/usr/bin/python 3
4 t00mlabs 3
# -*- coding: utf-8 -*-
4
# File: basico.py
5
# Author: Tomás Vírseda
6
# License: GPL v3
7
# Description: Main entry point por Basico app
243 t00m 8
"""
9
 
4 t00mlabs 10
import os
11
import sys
12
import traceback as tb
13
import signal
126 t00mlabs 14
from datetime import datetime
248 t00m 15
# ~ import selenium
16
from basico.core.mod_env import LPATH, GPATH, FILE
243 t00m 17
from basico.core.mod_log import get_logger
244 t00m 18
from basico.services.srv_utils import Utils
19
from basico.services.srv_gui import GUI
20
from basico.services.srv_iconmgt import IconManager
21
from basico.services.srv_bnr import BackupRestoreMan
22
from basico.services.srv_sap import SAP
23
from basico.services.srv_settings import Settings
24
from basico.services.srv_uif import UIFuncs
25
from basico.services.srv_cb import Callback
26
from basico.services.srv_notify import Notification
27
from basico.services.srv_db import Database
28
from basico.services.srv_driver import SeleniumDriver
29
from basico.services.srv_cols import Collections
30
from basico.services.srv_annot import Annotation
74 t00mlabs 31
 
4 t00mlabs 32
 
132 t00mlabs 33
class Basico(object):
243 t00m 34
    """
35
    Basico class
36
    """
4 t00mlabs 37
    def __init__(self):
38
        """Main class: the entry point for Basico.
39
        It stands for Controller.
40
        """
248 t00m 41
        # ~ STATS['BASICO_STARTUP_START'] = datetime.now()
74 t00mlabs 42
 
43
        # Create local paths if they do not exist
44
        for entry in LPATH:
45
            if not os.path.exists(LPATH[entry]):
46
                os.makedirs(LPATH[entry])
47
 
150 t00m 48
        # Truncate existing log file
49
        if os.path.exists(FILE['LOG']):
50
            flog = open(FILE['LOG'], 'w')
51
            flog.close()
74 t00mlabs 52
 
150 t00m 53
        # Create logger
4 t00mlabs 54
        self.log = get_logger(self.__class__.__name__, FILE['LOG'])
133 t00mlabs 55
        self.debug("Starting Basico")
74 t00mlabs 56
 
4 t00mlabs 57
        self.services = {}
58
        try:
59
            services = {
229 t00m 60
                'GUI'           :   GUI(),
61
                'Utils'         :   Utils(),
62
                'UIF'           :   UIFuncs(),
63
                'SAP'           :   SAP(),
64
                'Settings'      :   Settings(),
65
                'Notify'        :   Notification(),
66
                'IM'            :   IconManager(),
67
                'Callbacks'     :   Callback(),
68
                'DB'            :   Database(),
69
                'Driver'        :   SeleniumDriver(),
70
                'Collections'   :   Collections(),
236 t00m 71
                'Annotation'    :   Annotation(),
72
                'BNR'           :   BackupRestoreMan()
4 t00mlabs 73
            }
252 t00m 74
 
75
            for name in services:
76
                self.register_service(name, services[name])
4 t00mlabs 77
        except Exception as error:
78
            self.log.error(error)
79
            raise
80
 
236 t00m 81
 
254 t00m 82
    def print_traceback(self):
83
        self.debug(tb.format_exc())
84
 
85
 
133 t00mlabs 86
    def debug(self, message):
243 t00m 87
        """
88
        debug
89
        """
90
        self.log.debug("%18s | %s", self.__class__.__name__, message)
4 t00mlabs 91
 
236 t00m 92
 
93
    def error(self, message):
243 t00m 94
        """
95
        error
96
        """
97
        self.log.error("%18s | %s", self.__class__.__name__, message)
236 t00m 98
 
99
 
74 t00mlabs 100
    def setup(self):
101
        """
102
        Setup Basico Envrionment
103
        Info about ConfigParser options:
104
        https://docs.python.org/3/library/configparser.html#interpolation-of-values
105
        https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.optionxform
106
        """
133 t00mlabs 107
        self.debug("Setting up Basico environment")
108
        self.debug("Global path: %s" % GPATH['ROOT'])
109
        self.debug("Local path: %s" % LPATH['ROOT'])
4 t00mlabs 110
 
111
 
112
    def list_services(self):
113
        """Return a dictionary of services"""
114
        return self.services
115
 
116
 
117
    def get_service(self, name):
118
        """Get/Start a registered service
119
        @type name: name of the service
120
        @param name: given a service name it returns the associated
121
        class. If service was not running it is started.
122
        """
123
        try:
124
            service = self.services[name]
243 t00m 125
            if not service.is_started():
4 t00mlabs 126
                service.start(self, name)
243 t00m 127
            return service
4 t00mlabs 128
        except KeyError as service:
243 t00m 129
            self.log.error("Service %s not registered or not found", service)
4 t00mlabs 130
            raise
131
 
74 t00mlabs 132
 
4 t00mlabs 133
    def register_service(self, name, service):
134
        """Register a new service
135
        @type name: string
136
        @param name: name of the service
137
        @type service: class
138
        @param service: class which contains the code
139
        """
140
        try:
141
            self.services[name] = service
243 t00m 142
        except KeyError as error:
4 t00mlabs 143
            self.log.error(error)
144
 
145
 
146
    def deregister_service(self, name):
147
        """Deregister a running service
148
        @type name: string
149
        @param name: name of the service
150
        """
151
        self.services[name].end()
152
        self.services[name] = None
243 t00m 153
        # ~ self.debug("Service %s stopped" % name)
4 t00mlabs 154
 
155
 
156
    def check(self):
74 t00mlabs 157
        '''
158
        Check Basico environment
159
        '''
243 t00m 160
        # ~ utils = self.get_service("Utils")
74 t00mlabs 161
        uif = self.get_service('UIF')
162
        driver = self.get_service('Driver')
4 t00mlabs 163
 
74 t00mlabs 164
        # Show Selenium version
248 t00m 165
        # ~ self.debug("Selenium version: %s" % selenium.__version__)
74 t00mlabs 166
 
167
        # Check proper GTK version
243 t00m 168
        gtk_version = uif.check_gtk_version()
74 t00mlabs 169
 
170
        # Check Gecko webdrver
243 t00m 171
        gecko_driver = driver.check()
74 t00mlabs 172
 
243 t00m 173
        run = gtk_version and gecko_driver
74 t00mlabs 174
 
175
        if run:
133 t00mlabs 176
            self.debug("Basico environment ready!")
16 t00mlabs 177
        else:
74 t00mlabs 178
            self.log.error("Error(s) found checking Basico environment")
4 t00mlabs 179
 
243 t00m 180
        return run
16 t00mlabs 181
 
182
 
74 t00mlabs 183
 
243 t00m 184
 
4 t00mlabs 185
    def stop(self):
186
        """For each service registered, it executes the 'end' method
187
        (if any) to finalize them properly.
188
        """
189
 
190
        # Deregister all services loaded
191
        self.deregister_service('GUI')
192
        for name in self.services:
193
            try:
74 t00mlabs 194
                if name != 'GUI':
195
                    self.deregister_service(name)
243 t00m 196
            except Exception:
74 t00mlabs 197
                self.log.error(self.get_traceback())
198
                raise
199
 
133 t00mlabs 200
        self.debug("Basico finished")
4 t00mlabs 201
 
202
 
203
    def get_traceback(self):
243 t00m 204
        """
205
        get traceback
206
        """
4 t00mlabs 207
        return tb.format_exc()
208
 
209
 
210
    def run(self):
243 t00m 211
        """
212
        Start Basico
213
        """
214
        bnr = self.get_service('BNR')
215
        gui = self.get_service('GUI')
216
        bnr.backup()
217
        gui.run()
4 t00mlabs 218
 
219
 
220
def main():
243 t00m 221
    """
222
    Entry point
223
    """
4 t00mlabs 224
    #DOC: http://stackoverflow.com/questions/16410852/keyboard-interrupt-with-with-python-gtk
225
    signal.signal(signal.SIGINT, signal.SIG_DFL)
226
    basico = Basico()
74 t00mlabs 227
    basico.setup()
243 t00m 228
    if basico.check():
4 t00mlabs 229
        basico.run()
236 t00m 230
        sys.exit(0)
231
    else:
243 t00m 232
        sys.exit(-1)