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))
46 plugin_obj = getattr(mod_obj, plugin)
47 except AttributeError as err:
48 logger.error('Failed to load plugin {0} ({1})'.format(plugin, err))
50 logger.info('Loading {0} plugin: {name} ({doc})'.format(source, **plugin_obj.info()))
51 sima.register_plugin(plugin_obj)
54 def start(sopt, restart=False):
58 verbosity = sopt.options.get('verbosity', 'warning')
59 logfile = sopt.options.get('logfile', None)
60 cli_loglevel = getattr(logging, verbosity.upper())
61 set_logger(level=verbosity, logfile=logfile)
62 logger = logging.getLogger('sima')
63 logger.setLevel(cli_loglevel)
65 config = ConfMan(logger, sopt.options).config
66 logger.setLevel(getattr(logging,
67 config.get('log', 'verbosity').upper())) # pylint: disable=E1103
69 logger.debug('Command line say: {0}'.format(sopt.options))
71 db_file = config.get('sima', 'db_file')
72 if (sopt.options.get('create_db', None)
73 or not isfile(db_file)):
74 logger.info('Creating database in "{}"'.format(db_file))
75 open(db_file, 'a').close()
76 SimaDB(db_path=db_file).create_db()
77 if sopt.options.get('create_db', None):
78 logger.info('Done, bye...')
81 logger.info('Starting...')
82 sima = core.Sima(config)
84 # required core plugins
85 sima.register_plugin(History)
86 sima.register_plugin(MpdOptions)
88 # Loading internal plugins
89 load_plugins(sima, 'internal')
91 # Loading contrib plugins
92 load_plugins(sima, 'contrib')
94 if config.getboolean('daemon', 'daemon'):
98 logger.info('Daemonize process...')
103 except KeyboardInterrupt:
104 logger.info('Caught KeyboardInterrupt, stopping')
108 def run(sopt, restart=False):
110 Handles SigHup exception
111 Catches Unhandled exception
113 # pylint: disable=broad-except
116 except SigHup as err: # SigHup inherit from Exception
118 except Exception: # Unhandled exception
123 nfo = dict({'version': info.__version__,
125 # StartOpt gathers options from command line call (in StartOpt().options)
130 if __name__ == '__main__':
134 # vim: ai ts=4 sw=4 sts=4 expandtab