Subversion Repositories basico

Rev

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

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: service.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Service class

import sys
import traceback as tb

from basico.core.env import FILE

class Service(object):
    """
    Service class is the base class for the rest of main classes used in
    the application.
    Different modules (GUI, Database, Ask, etc...) share same methods
    which is useful to start/stop them, simplify logging and, comunicate
    each other easily.
    """


    def __init__(self, app=None):
        """Initialize Service instance
        @type app: Basico instance
        @param app: current Basico instance reference
        """

        self.started = False


    def is_started(self):
        """Return True or False if service is running / not running
        """

        return self.started


    def print_traceback(self):
        self.debug(tb.format_exc())


    def start(self, app, logname=None):
        """Start service.
        Use initialize for writting a custom init method
        @type app: basico
        @param app: basico Class pointer.
        @type logname: string
        @param logname: name of associated logger. It is used aswell to
        identify configuration section name
        """

        self.started = True
        self.app = app
        self.log = self.app.log
        self.section = logname
        self.init_section(self.section)

        try:
            self.initialize()
            self.debug("Service %s loaded" % logname)
        except Exception as error:
            self.debug (self.get_traceback())


    def get_var(self, name, scope='global'):
        return self.app.get_var(name, scope)


    def get_app_info(self, name):
        return self.app.get_app_info(name)


    def get_file(self, name):
        return self.app.get_file(name)


    def end(self):
        """End service
        Use finalize for writting a custom end method
        """

        self.started = False
        try:
            self.finalize()
        except Exception as error:
            self.debug (self.get_traceback())


    def initialize(self):
        """Initialize service.
        All clases derived from Service class must implement this method
        """

        pass


    def finalize(self):
        """Finalize service.
        All clases derived from Service class must implement this method
        """

        pass


    def init_section(self, section):
        """Check if section exists in config. If not, create it"""
        self.settings = self.app.get_service('Settings')
        config = self.settings.load()
        try:
            config[section]
        except:
            config[section] = {}
            self.settings.save(config)
            self.debug("Section '%s' initialized in config file" % section)

    def debug(self, message):
        self.app.log.debug("%18s | %s" % (self.__class__.__name__, message))

    def get_traceback(self):
        return self.app.get_traceback()


    def get_service(self, name):
        return self.app.get_service(name)