1 # -*- coding: utf-8 -*-
5 # standard library import
9 from importlib import __import__
10 from os.path import isfile
13 # third parties components
17 from . import core, info
18 from .lib.logger import set_logger
19 from .lib.simadb import SimaDB
20 from .utils.config import ConfMan
21 from .utils.startopt import StartOpt
22 from .utils.utils import exception_log, SigHup
24 from .plugins.core.history import History
25 from .plugins.core.mpdoptions import MpdOptions
29 def load_plugins(sima, source):
30 """Handles internal/external plugins
31 sima: sima.core.Sima instance
32 source: ['internal', 'contrib']
34 if not sima.config.get('sima', source ):
36 logger = logging.getLogger('sima')
37 for plugin in sima.config.get('sima', source).split(','):
38 plugin = plugin.strip(' \n')
39 module = 'sima.plugins.{0}.{1}'.format(source, plugin.lower())
41 mod_obj = __import__(module, fromlist=[plugin])
42 except ImportError as err:
43 logger.error('Failed to load plugin\'s module: {0} ({1})'.format(module, err))
47 plugin_obj = getattr(mod_obj, plugin)
48 except AttributeError as err:
49 logger.error('Failed to load plugin {0} ({1})'.format(plugin, err))
52 logger.info('Loading {0} plugin: {name} ({doc})'.format(source, **plugin_obj.info()))
53 sima.register_plugin(plugin_obj)
56 def start(sopt, restart=False):
60 verbosity = sopt.options.get('verbosity', 'warning')
61 logfile = sopt.options.get('logfile', None)
62 cli_loglevel = getattr(logging, verbosity.upper())
63 set_logger(level=verbosity, logfile=logfile)
64 logger = logging.getLogger('sima')
65 logger.setLevel(cli_loglevel)
67 config = ConfMan(logger, sopt.options).config
68 logger.setLevel(getattr(logging,
69 config.get('log', 'verbosity').upper())) # pylint: disable=E1103
71 logger.debug('Command line say: {0}'.format(sopt.options))
73 db_file = config.get('sima', 'db_file')
74 if (sopt.options.get('create_db', None)
75 or not isfile(db_file)):
76 logger.info('Creating database in "{}"'.format(db_file))
77 open(db_file, 'a').close()
78 SimaDB(db_path=db_file).create_db()
79 if sopt.options.get('create_db', None):
80 logger.info('Done, bye...')
83 logger.info('Starting...')
84 sima = core.Sima(config)
86 # required core plugins
87 sima.register_plugin(History)
88 sima.register_plugin(MpdOptions)
90 # Loading internal plugins
91 load_plugins(sima, 'internal')
93 # Loading contrib plugins
94 load_plugins(sima, 'contrib')
96 if config.getboolean('daemon', 'daemon'):
100 logger.info('Daemonize process...')
105 except KeyboardInterrupt:
106 logger.info('Caught KeyboardInterrupt, stopping')
110 def run(sopt, restart=False):
112 Handles SigHup exception
113 Catches Unhandled exception
115 # pylint: disable=broad-except
118 except SigHup as err: # SigHup inherit from Exception
120 except Exception: # Unhandled exception
125 nfo = dict({'version': info.__version__,
127 # StartOpt gathers options from command line call (in StartOpt().options)
132 if __name__ == '__main__':
136 # vim: ai ts=4 sw=4 sts=4 expandtab