Subversion Repositories basico

Rev

Rev 4 | 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
9
 
10
from .log import get_logger
11
 
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
 
36
    def start(self, app, logname=None):
37
        """Start service.
38
        Use initialize for writting a custom init method
39
        @type app: basico
40
        @param app: basico Class pointer.
41
        @type logname: string
42
        @param logname: name of associated logger. It is used aswell to
43
        identify configuration section name
44
        """
45
        self.started = True
46
        self.app = app
47
        logfile = self.app.get_file('LOG')
48
        self.log = get_logger(logname, logfile)
49
        self.config = self.app.get_config()
50
        self.section = logname
51
        self.init_section(logname)
52
 
53
        try:
54
            self.initialize()
55
            self.log.debug("Service %s loaded" % logname)
56
        except Exception as error:
57
            self.log.error (self.get_traceback())
58
 
59
 
60
    def get_var(self, name, scope='global'):
61
        return self.app.get_var(name, scope)
62
 
63
 
64
    def get_app_info(self, name):
65
        return self.app.get_app_info(name)
66
 
67
 
68
    def get_file(self, name):
69
        return self.app.get_file(name)
70
 
71
 
72
    def end(self):
73
        """End service
74
        Use finalize for writting a custom end method
75
        """
76
        self.started = False
77
        try:
78
            self.finalize()
79
        except Exception as error:
80
            self.log.error (self.get_traceback())
81
 
82
 
83
    def initialize(self):
84
        """Initialize service.
85
        All clases derived from Service class must implement this method
86
        """
87
        pass
88
 
89
 
90
    def finalize(self):
91
        """Finalize service.
92
        All clases derived from Service class must implement this method
93
        """
94
        pass
95
 
96
 
97
    def get_config_value(self, key):
98
        """Get value for a given param in section for this service
99
        @type param: string
100
        @param param: parameter name
101
        """
102
        self.config = self.app.get_config()
103
        if self.config.has_section(self.section):
104
            if self.config.has_option(self.section, key):
105
                return self.config.get(self.section, key)
106
 
107
        return None
108
 
109
 
110
    def set_config_value(self, key, value):
111
        """Set value for a given param in section for this service
112
        @type param: string
113
        @param param: parameter name
114
        @type value: string
115
        @param param: new value for this parameter
116
        """
117
        self.config[self.section][key] = value
118
        self.log.debug("CONFIG[%s][%s] = %s" % (self.section, key, value))
119
        self.save_config()
120
 
121
 
122
    def init_section(self, section):
123
        """Check if section exists in config. If not, create it"""
124
        if not self.config.has_section(section):
125
            self.config.add_section(section)
126
            self.log.debug("CONFIG[%s] section created" % section)
127
        self.save_config()
128
 
129
 
130
    def save_config(self):
131
        CONFIG_FILE = self.get_file('CNF')
132
        with open(CONFIG_FILE, 'w') as configfile:
133
            self.config.write(configfile)
134
 
135
 
136
    def get_traceback(self):
137
        return self.app.get_traceback()
138
 
139
 
140
    def get_service(self, name):
141
        return self.app.get_service(name)