Subversion Repositories MeX

Rev

Rev 1 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python 3
# -*- coding: utf-8 -*-
"""
# File: mex.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Main entry point por MeX app
"""


import os
import sys
import signal
import shutil

from mex.core.mod_env import APP, LPATH, GPATH, FILE
from mex.core.mod_log import get_logger
from mex.services.srv_plugins import MexPluginManager
from mex.services.srv_application import MexAppSrv
from mex.services.srv_utils import MexUtils

#DOC: http://stackoverflow.com/questions/16410852/keyboard-interrupt-with-with-python-gtk
signal.signal(signal.SIGINT, signal.SIG_DFL)

class MeX(object):
    """
    MeX Application class
    """

    def __init__(self):
        """
        """

        self.setup_environment()
        self.setup_logging()
        self.setup_services()


    def setup_environment(self):
        """
        Setup MeX environment
        """

        # Create local paths if they do not exist
        for entry in LPATH:
            if not os.path.exists(LPATH[entry]):
                os.makedirs(LPATH[entry])


    def setup_logging(self):
        """
        Setup MeX logging
        """

        # Truncate existing log file
        if os.path.exists(FILE['LOG']):
            with open(FILE['LOG'], 'w') as flog:
                pass

        #Initialize logging
        self.log = get_logger(__class__.__name__)
        self.log.info("MeX %s started", APP['version'])
        self.log.debug("Global path: %s", GPATH['ROOT'])
        self.log.debug("Local path: %s", LPATH['ROOT'])
        self.log.debug("Logging all messages to file: %s", FILE['LOG'])


    def setup_services(self):
        """
        Setup MeX Services
        """


        # Declare and register services
        self.services = {}
        try:
            services = {
                'Utils'         :   MexUtils(),
                'Application'   :   MexAppSrv(),
                'Plugins'       :   MexPluginManager(),

            }

            for name in services:
                self.register_service(name, services[name])
        except Exception as error:
            self.log.error(error)
            raise


    def get_service(self, name):
        """
        Get/Start a registered service
        """

        try:
            service = self.services[name]
            logname = service.__class__.__name__
            if not service.is_started():
                service.start(self, logname, name)
            return service
        except KeyError as service:
            self.log.error("Service %s not registered or not found", service)
            raise


    def register_service(self, name, service):
        """
        Register a new service
        """

        try:
            self.services[name] = service
        except KeyError as error:
            self.log.error(error)


    def deregister_service(self, name):
        """
        Deregister a running service
        """

        self.services[name].end()
        self.services[name] = None


    def stop(self):
        """
        For each service registered, it executes the 'end' method
        (if any) to finalize them properly.
        """


        # Deregister all services loaded
        for name in self.services:
            try:
                self.deregister_service(name)
            except Exception as error:
                self.log.error(error)
                raise
        self.log.info("MeX %s finished", APP['version'])


    def run(self):
        """
        Start MeX
        """

        app = self.get_service('Application')
        app.run()


def main():
    """
    Entry point
    """

    mex = MeX()
    mex.run()
    mex.stop()


if __name__ == '__main__':
    main()