Subversion Repositories basico

Rev

Rev 150 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: service.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Service class
7
 
8
import sys
80 t00mlabs 9
import traceback as tb
4 t00mlabs 10
 
233 t00m 11
from basico.core.env import FILE
4 t00mlabs 12
 
13
class Service(object):
14
    """
15
    Service class is the base class for the rest of main classes used in
16
    the application.
17
    Different modules (GUI, Database, Ask, etc...) share same methods
18
    which is useful to start/stop them, simplify logging and, comunicate
19
    each other easily.
20
    """
21
 
22
    def __init__(self, app=None):
23
        """Initialize Service instance
24
        @type app: Basico instance
25
        @param app: current Basico instance reference
26
        """
27
        self.started = False
28
 
29
 
30
    def is_started(self):
31
        """Return True or False if service is running / not running
32
        """
33
        return self.started
34
 
35
 
80 t00mlabs 36
    def print_traceback(self):
133 t00mlabs 37
        self.debug(tb.format_exc())
80 t00mlabs 38
 
39
 
4 t00mlabs 40
    def start(self, app, logname=None):
41
        """Start service.
42
        Use initialize for writting a custom init method
43
        @type app: basico
44
        @param app: basico Class pointer.
45
        @type logname: string
46
        @param logname: name of associated logger. It is used aswell to
47
        identify configuration section name
48
        """
49
        self.started = True
50
        self.app = app
134 t00mlabs 51
        self.log = self.app.log
4 t00mlabs 52
        self.section = logname
126 t00mlabs 53
        self.init_section(self.section)
4 t00mlabs 54
 
55
        try:
56
            self.initialize()
133 t00mlabs 57
            self.debug("Service %s loaded" % logname)
4 t00mlabs 58
        except Exception as error:
133 t00mlabs 59
            self.debug (self.get_traceback())
4 t00mlabs 60
 
61
 
62
    def get_var(self, name, scope='global'):
63
        return self.app.get_var(name, scope)
64
 
65
 
66
    def get_app_info(self, name):
67
        return self.app.get_app_info(name)
68
 
69
 
70
    def get_file(self, name):
71
        return self.app.get_file(name)
72
 
73
 
74
    def end(self):
75
        """End service
76
        Use finalize for writting a custom end method
77
        """
78
        self.started = False
79
        try:
80
            self.finalize()
81
        except Exception as error:
133 t00mlabs 82
            self.debug (self.get_traceback())
4 t00mlabs 83
 
84
 
85
    def initialize(self):
86
        """Initialize service.
87
        All clases derived from Service class must implement this method
88
        """
89
        pass
90
 
91
 
92
    def finalize(self):
93
        """Finalize service.
94
        All clases derived from Service class must implement this method
95
        """
96
        pass
97
 
98
 
99
    def init_section(self, section):
100
        """Check if section exists in config. If not, create it"""
126 t00mlabs 101
        self.settings = self.app.get_service('Settings')
102
        config = self.settings.load()
103
        try:
104
            config[section]
105
        except:
106
            config[section] = {}
107
            self.settings.save(config)
133 t00mlabs 108
            self.debug("Section '%s' initialized in config file" % section)
4 t00mlabs 109
 
133 t00mlabs 110
    def debug(self, message):
150 t00m 111
        self.app.log.debug("%18s | %s" % (self.__class__.__name__, message))
133 t00mlabs 112
 
4 t00mlabs 113
    def get_traceback(self):
114
        return self.app.get_traceback()
115
 
116
 
117
    def get_service(self, name):
118
        return self.app.get_service(name)