Subversion Repositories basico

Rev

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