Subversion Repositories basico

Rev

Rev 16 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 16 Rev 74
Line 3... Line 3...
3
# File: basico.py
3
# File: basico.py
4
# Author: Tomás Vírseda
4
# Author: Tomás Vírseda
5
# License: GPL v3
5
# License: GPL v3
6
# Description: Main entry point por Basico app
6
# Description: Main entry point por Basico app
7
import os
7
import os
-
 
8
# -*- coding: utf-8 -*-
8
import sys
9
import sys
-
 
10
import codecs
-
 
11
sys.stdout = codecs.getwriter("iso-8859-1")(sys.stdout, 'xmlcharrefreplace')
9
import traceback as tb
12
import traceback as tb
10
import imp
13
import imp
11
import signal
14
import signal
-
 
15
from pprint import pprint
12
from os.path import abspath, sep as SEP
16
from os.path import abspath, sep as SEP
13
from configparser import SafeConfigParser, ExtendedInterpolation
17
from configparser import SafeConfigParser, ExtendedInterpolation
-
 
18
# import urllib.request
-
 
19
-
 
20
import selenium
14
21
15
import gi
22
import gi
16
gi.require_version('Gtk', '3.0')
23
gi.require_version('Gtk', '3.0')
17
from gi.repository import Gtk
24
from gi.repository import Gtk
18
25
-
 
26
from .utils import Utils
19
from .log import get_logger
27
from .log import get_logger
20
from .gui import GUI
28
from .gui import GUI
21
from .iconmanager import IconManager
29
from .iconmanager import IconManager
22
from .sap import SAP
30
from .sap import SAP
23
from .settings import Settings
31
from .settings import Settings
Line 26... Line 34...
26
from .projects import Projects
34
from .projects import Projects
27
from .tasks import Tasks
35
from .tasks import Tasks
28
from .plugins import Plugins
36
from .plugins import Plugins
29
from .callbacks import Callback
37
from .callbacks import Callback
30
from .notify import Notification
38
from .notify import Notification
31
from .stats import Stats
-
 
-
 
39
#from .stats import Stats
-
 
40
from .database import Database
-
 
41
from .driver import SeleniumDriver
32
from .env import ROOT, APP, LPATH, GPATH, FILE
42
from .env import ROOT, APP, LPATH, GPATH, FILE
33
43
34
44
35
class Basico:
45
class Basico:
36
    def __init__(self):
46
    def __init__(self):
37
        """Main class: the entry point for Basico.
47
        """Main class: the entry point for Basico.
38
        It stands for Controller.
48
        It stands for Controller.
39
        """
49
        """
40
        self.__set_env()
-
 
-
 
50
-
 
51
        # Create local paths if they do not exist
-
 
52
        for entry in LPATH:
-
 
53
            if not os.path.exists(LPATH[entry]):
-
 
54
                os.makedirs(LPATH[entry])
-
 
55
-
 
56
41
        self.log = get_logger(self.__class__.__name__, FILE['LOG'])
57
        self.log = get_logger(self.__class__.__name__, FILE['LOG'])
42
        self.log.info("Starting Basico")
-
 
43
        self.__init_config()
-
 
-
 
58
        #~ self.log.info("Starting Basico")
-
 
59
44
        self.services = {}
60
        self.services = {}
45
        try:
61
        try:
46
            services = {
62
            services = {
47
                'GUI'       :   GUI(),
63
                'GUI'       :   GUI(),
-
 
64
                'Utils'     :   Utils(),
48
                'UIF'       :   UIFuncs(),
65
                'UIF'       :   UIFuncs(),
49
                'Menus'     :   Menus(),
66
                'Menus'     :   Menus(),
50
                'SAP'       :   SAP(),
67
                'SAP'       :   SAP(),
51
                'Settings'  :   Settings(),
68
                'Settings'  :   Settings(),
52
                'Notify'    :   Notification(),
69
                'Notify'    :   Notification(),
53
                'Tasks'     :   Tasks(),
70
                'Tasks'     :   Tasks(),
54
                'IM'        :   IconManager(),
71
                'IM'        :   IconManager(),
55
                'Plugins'   :   Plugins(),
72
                'Plugins'   :   Plugins(),
56
                'Callbacks' :   Callback(),
73
                'Callbacks' :   Callback(),
57
                'Stats'     :   Stats(),
-
 
-
 
74
                #'Stats'     :   Stats(),
-
 
75
                'DB'        :   Database(),
-
 
76
                'Driver'    :   SeleniumDriver()
58
            }
77
            }
59
            self.register_services(services)
78
            self.register_services(services)
60
        except Exception as error:
79
        except Exception as error:
61
            self.log.error(error)
80
            self.log.error(error)
62
            raise
81
            raise
63
82
64
83
65
    def __set_env(self):
-
 
66
        # Create local paths if they do not exist
-
 
67
        for DIR in LPATH:
-
 
68
            if not os.path.exists(LPATH[DIR]):
-
 
69
                os.makedirs(LPATH[DIR])
-
 
-
 
84
    def setup(self):
-
 
85
        """
-
 
86
        Setup Basico Envrionment
-
 
87
        Info about ConfigParser options:
-
 
88
        https://docs.python.org/3/library/configparser.html#interpolation-of-values
-
 
89
        https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.optionxform
-
 
90
        """
-
 
91
        self.log.debug("Setting up Basico environment")
-
 
92
        self.log.debug("Global path: %s" % GPATH['ROOT'])
-
 
93
        self.log.debug("Local path: %s" % LPATH['ROOT'])
70
94
71
-
 
72
    def __init_config(self):
-
 
73
        #~ self.log.debug("ROOT: %s" % ROOT)
-
 
74
        # Set up config
95
        # Set up config
75
        CONFIG_FILE = self.get_file('CNF')
96
        CONFIG_FILE = self.get_file('CNF')
76
-
 
77
        #~ https://docs.python.org/3/library/configparser.html#interpolation-of-values
-
 
78
        self.config = SafeConfigParser(interpolation=ExtendedInterpolation())
97
        self.config = SafeConfigParser(interpolation=ExtendedInterpolation())
79
-
 
80
        #~ https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.optionxform
-
 
81
        self.config.optionxform = str
98
        self.config.optionxform = str
82
99
83
        # Save config
100
        # Save config
84
        if not os.path.exists(CONFIG_FILE):
101
        if not os.path.exists(CONFIG_FILE):
85
            self.log.debug('Configuration file not found. Creating a new one')
102
            self.log.debug('Configuration file not found. Creating a new one')
Line 135... Line 152...
135
                service.start(self, name)
152
                service.start(self, name)
136
                return service
153
                return service
137
        except KeyError as service:
154
        except KeyError as service:
138
            self.log.error("Service %s not registered or not found" % service)
155
            self.log.error("Service %s not registered or not found" % service)
139
            raise
156
            raise
-
 
157
140
158
141
    def register_services(self, services):
159
    def register_services(self, services):
142
        """Register a list of services
160
        """Register a list of services
143
        @type services: dict
161
        @type services: dict
144
        @param services: a dictionary of name:class for each service
162
        @param services: a dictionary of name:class for each service
Line 154... Line 172...
154
        @type service: class
172
        @type service: class
155
        @param service: class which contains the code
173
        @param service: class which contains the code
156
        """
174
        """
157
        try:
175
        try:
158
            self.services[name] = service
176
            self.services[name] = service
159
            #~ self.log.debug("Service '%s' loaded successfully" % name)
-
 
160
        except Exception as error:
177
        except Exception as error:
161
            self.log.error(error)
178
            self.log.error(error)
162
179
163
180
164
    def deregister_service(self, name):
181
    def deregister_service(self, name):
Line 166... Line 183...
166
        @type name: string
183
        @type name: string
167
        @param name: name of the service
184
        @param name: name of the service
168
        """
185
        """
169
        self.services[name].end()
186
        self.services[name].end()
170
        self.services[name] = None
187
        self.services[name] = None
-
 
188
        self.log.debug("Service %s stopped" % name)
171
189
172
190
173
    def check_gtk_version(self):
191
    def check_gtk_version(self):
174
        vmajor = Gtk.get_major_version()
192
        vmajor = Gtk.get_major_version()
175
        vminor = Gtk.get_minor_version()
193
        vminor = Gtk.get_minor_version()
Line 180... Line 198...
180
            return True
198
            return True
181
        else:
199
        else:
182
            return False
200
            return False
183
201
184
    def check(self):
202
    def check(self):
185
        GTK_VERSION = self.check_gtk_version()
-
 
-
 
203
        '''
-
 
204
        Check Basico environment
-
 
205
        '''
-
 
206
        utils = self.get_service("Utils")
-
 
207
        uif = self.get_service('UIF')
-
 
208
        driver = self.get_service('Driver')
186
209
187
        if GTK_VERSION:
-
 
188
            self.log.debug("GTK+ version supported")
-
 
189
            run = True
-
 
-
 
210
        # Show Selenium version
-
 
211
        self.log.debug("Selenium version: %s" % selenium.__version__)
-
 
212
-
 
213
        # Check proper GTK version
-
 
214
        GTK_VERSION = uif.check_gtk_version()
-
 
215
-
 
216
        # Check Gecko webdrver
-
 
217
        GECKO_DRIVER = driver.check()
-
 
218
-
 
219
        run = GTK_VERSION and GECKO_DRIVER
-
 
220
-
 
221
        if run:
-
 
222
            self.log.info("Basico environment ready!")
-
 
223
            return True
190
        else:
224
        else:
191
            self.log.error("Please, install a modern version of GTK+ (>= 3.18)")
-
 
192
            run = False
-
 
-
 
225
            self.log.error("Error(s) found checking Basico environment")
-
 
226
            return False
-
 
227
193
228
194
        return run
-
 
195
        #~ sap = self.get_service('SAP')
-
 
196
        #~ found = sap.check_webdriver()
-
 
197
        #~ if not found:
-
 
198
            #~ self.log.error("No webdriver found. Exiting.")
-
 
199
        #~ return found
-
 
200
229
201
230
202
    def stop(self):
231
    def stop(self):
203
        """For each service registered, it executes the 'end' method
232
        """For each service registered, it executes the 'end' method
204
        (if any) to finalize them properly.
233
        (if any) to finalize them properly.
205
        """
234
        """
206
235
207
        # Deregister all services loaded
236
        # Deregister all services loaded
208
        self.deregister_service('GUI')
237
        self.deregister_service('GUI')
209
-
 
210
        for name in self.services:
238
        for name in self.services:
211
            try:
239
            try:
212
                self.deregister_service(name)
-
 
213
            except: pass
-
 
214
        # Bye bye
-
 
-
 
240
                if name != 'GUI':
-
 
241
                    self.deregister_service(name)
-
 
242
            except Exception as error:
-
 
243
                self.log.error(self.get_traceback())
-
 
244
                raise
-
 
245
215
        self.log.info("Basico finished")
246
        self.log.info("Basico finished")
216
247
217
248
218
    def get_traceback(self):
249
    def get_traceback(self):
219
        return tb.format_exc()
250
        return tb.format_exc()
220
251
221
252
222
    def run(self):
253
    def run(self):
223
        try:
254
        try:
224
            self.gui = self.get_service('GUI')
255
            self.gui = self.get_service('GUI')
225
            #~ self.log.debug("Basico ready to start")
-
 
226
            self.gui.run()
256
            self.gui.run()
227
        except:
257
        except:
228
            self.log.error(self.get_traceback())
258
            self.log.error(self.get_traceback())
229
259
230
260
231
def main():
261
def main():
232
    #DOC: http://stackoverflow.com/questions/16410852/keyboard-interrupt-with-with-python-gtk
262
    #DOC: http://stackoverflow.com/questions/16410852/keyboard-interrupt-with-with-python-gtk
233
    signal.signal(signal.SIGINT, signal.SIG_DFL)
263
    signal.signal(signal.SIGINT, signal.SIG_DFL)
234
    basico = Basico()
264
    basico = Basico()
235
    run = basico.check()
-
 
236
    if run:
-
 
-
 
265
    basico.setup()
-
 
266
    ok = basico.check()
-
 
267
    if ok:
237
        basico.run()
268
        basico.run()
238
    sys.exit(0)
269
    sys.exit(0)