X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=launch;h=a4eb951a1f2ba7fb4fd120deb1b7c9a7f87c4d54;hb=6893eb469c11924c9480b15385cedaf37908dd02;hp=83ba3bad9208d6e0cbe6cc1f9c14b4b60d1d079f;hpb=1cc879f39941fc302f9a841a532c9f749797cca4;p=mpd-sima.git diff --git a/launch b/launch index 83ba3ba..a4eb951 100755 --- a/launch +++ b/launch @@ -1,21 +1,117 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +"""Sima +""" + +# standart library import +import logging +import sys + +from importlib import __import__ +from os.path import isfile +## + +# third parties components +## + +# local import +from sima import core +from sima.lib.logger import set_logger +from sima.lib.simadb import SimaDB +from sima.utils.config import ConfMan +from sima.utils.startopt import StartOpt +from sima.utils.utils import exception_log +## + + +def load_plugins(sima, source): + """Handles internal/external plugins + sima: sima.core.Sima instance + source: ['internal', 'contrib'] + """ + if not sima.config.get('sima', source ): + return + logger = logging.getLogger('sima') + for plugin in sima.config.get('sima', source).split(','): + plugin = plugin.strip(' \n') + module = 'sima.plugins.{0}.{1}'.format(source, plugin.lower()) + try: + mod_obj = __import__(module, fromlist=[plugin]) + except ImportError as err: + logger.error('Failed to load plugin\'s module: {0} ({1})'.format(module, err)) + sima.shutdown() + try: + plugin_obj = getattr(mod_obj, plugin) + except AttributeError as err: + logger.error('Failed to load plugin {0} ({1})'.format(plugin, err)) + sima.shutdown() + logger.info('Loading {0} plugin: {name} ({doc})'.format(source, **plugin_obj.info())) + sima.register_plugin(plugin_obj) + def main(): - from sima import core - from sima.plugins.crop import Crop - m = core.Sima() - m.register_plugin(Crop) + """Entry point, deal w/ CLI and starts application + """ + info = dict({'version': core.__version__, + 'prog': 'sima'}) + # StartOpt gathers options from command line call (in StartOpt().options) + sopt = StartOpt(info) + # set logger + verbosity = sopt.options.get('verbosity', 'warning') + logfile = sopt.options.get('logfile', None) + cli_loglevel = getattr(logging, verbosity.upper()) + set_logger(level=verbosity, logfile=logfile) + logger = logging.getLogger('sima') + logger.setLevel(cli_loglevel) + # loads configuration + config = ConfMan(logger, sopt.options).config + logger.setLevel(getattr(logging, + config.get('log', 'verbosity').upper())) # pylint: disable=E1103 + + logger.debug('Command line say: {0}'.format(sopt.options)) + # Create Database + db_file = config.get('sima', 'db_file') + if (sopt.options.get('create_db', None) + or not isfile(db_file)): + logger.info('Creating database in "{}"'.format(db_file)) + open(db_file, 'a').close() + SimaDB(db_path=db_file).create_db() + if sopt.options.get('create_db', None): + logger.info('Done, bye...') + sys.exit(0) + + logger.info('Starting...') + sima = core.Sima(config) + + # Loading internal plugins + load_plugins(sima, 'internal') + + # Loading contrib plugins + load_plugins(sima, 'contrib') + + # Run as a daemon + if config.getboolean('daemon', 'daemon'): + sima.start() + try: - m.run() + sima.foreground() except KeyboardInterrupt: - m.shutdown() + logger.info('Caught KeyboardInterrupt, stopping') + #sima.shutdown() + +def run(): + """Catching broad exception + """ + # pylint: disable=broad-except + try: + main() + except Exception: + exception_log() # Script starts here if __name__ == '__main__': - main() - + run() # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab