1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013, 2014 Jack Kaliko <kaliko@azylum.org>
4 # This file is part of sima
6 # sima is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # sima is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with sima. If not, see <http://www.gnu.org/licenses/>.
23 # standard library import
27 from importlib import __import__
28 from os.path import isfile
31 # third parties components
35 from . import core, info
36 from .lib.logger import set_logger
37 from .lib.simadb import SimaDB
38 from .utils.config import ConfMan
39 from .utils.startopt import StartOpt
40 from .utils.utils import exception_log, SigHup
42 from .plugins.core.history import History
43 from .plugins.core.mpdoptions import MpdOptions
47 def load_plugins(sima, source):
48 """Handles internal/external plugins
49 sima: sima.core.Sima instance
50 source: ['internal', 'contrib']
52 if not sima.config.get('sima', source ):
54 logger = logging.getLogger('sima')
55 for plugin in sima.config.get('sima', source).split(','):
56 plugin = plugin.strip(' \n')
57 module = 'sima.plugins.{0}.{1}'.format(source, plugin.lower())
59 mod_obj = __import__(module, fromlist=[plugin])
60 except ImportError as err:
61 logger.error('Failed to load plugin\'s module: {0} ({1})'.format(module, err))
65 plugin_obj = getattr(mod_obj, plugin)
66 except AttributeError as err:
67 logger.error('Failed to load plugin {0} ({1})'.format(plugin, err))
70 logger.info('Loading {0} plugin: {name} ({doc})'.format(source, **plugin_obj.info()))
71 sima.register_plugin(plugin_obj)
74 def start(sopt, restart=False):
78 verbosity = sopt.options.get('verbosity', 'warning')
79 logfile = sopt.options.get('logfile', None)
80 cli_loglevel = getattr(logging, verbosity.upper())
81 set_logger(level=verbosity, logfile=logfile)
82 logger = logging.getLogger('sima')
83 logger.setLevel(cli_loglevel)
85 config = ConfMan(logger, sopt.options).config
86 logger.setLevel(getattr(logging,
87 config.get('log', 'verbosity').upper())) # pylint: disable=E1103
89 logger.debug('Command line say: {0}'.format(sopt.options))
91 db_file = config.get('sima', 'db_file')
92 if (sopt.options.get('create_db', None)
93 or not isfile(db_file)):
94 logger.info('Creating database in "{}"'.format(db_file))
95 open(db_file, 'a').close()
96 SimaDB(db_path=db_file).create_db()
97 if sopt.options.get('create_db', None):
98 logger.info('Done, bye...')
101 logger.info('Starting...')
102 sima = core.Sima(config)
104 # required core plugins
105 sima.register_plugin(History)
106 sima.register_plugin(MpdOptions)
108 # Loading internal plugins
109 load_plugins(sima, 'internal')
111 # Loading contrib plugins
112 load_plugins(sima, 'contrib')
114 if config.getboolean('daemon', 'daemon'):
118 logger.info('Daemonize process...')
123 except KeyboardInterrupt:
124 logger.info('Caught KeyboardInterrupt, stopping')
128 def run(sopt, restart=False):
130 Handles SigHup exception
131 Catches Unhandled exception
133 # pylint: disable=broad-except
136 except SigHup as err: # SigHup inherit from Exception
138 except Exception: # Unhandled exception
143 nfo = dict({'version': info.__version__,
145 # StartOpt gathers options from command line call (in StartOpt().options)
150 if __name__ == '__main__':
154 # vim: ai ts=4 sw=4 sts=4 expandtab