]> kaliko git repositories - mpd-sima.git/blob - launch
Add handling of external plugins
[mpd-sima.git] / launch
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """Sima
4 """
5
6 # standart library import
7 import logging
8 import sys
9
10 from importlib import __import__
11 from os.path import isfile, basename
12 ##
13
14 # third parties components
15 ##
16
17 # local import
18 from sima import core
19 from sima.plugins.crop import Crop
20 from sima.plugins.addhist import History
21 from sima.lib.logger import set_logger
22 from sima.lib.simadb import SimaDB
23 from sima.utils.config import ConfMan
24 from sima.utils.startopt import StartOpt
25 from sima.utils.utils import exception_log
26 ##
27
28 # official plugins to start
29 PLUGINS = (Crop, History)
30
31
32 def load_contrib_plugins(sima):
33     """Handles contrib/external plugins
34     """
35     if not sima.config.has_option('sima', 'plugins'):
36         return
37     logger = logging.getLogger('sima')
38     for plugin in sima.config.get('sima','plugins').split(','):
39         plugin = plugin.strip(' \n')
40         module = 'sima.plugins.contrib.{}'.format(plugin.lower())
41         try:
42             mod_obj = __import__(module, fromlist=[plugin])
43         except ImportError as err:
44             logger.error('Failed to load plugin\'s module: {0} ({1})'.format(module, err))
45             sima.shutdown()
46         try:
47             plugin_obj = getattr(mod_obj, plugin)
48         except AttributeError as err:
49             logger.error('Failed to load plugin {0} ({1})'.format(plugin, err))
50             sima.shutdown()
51         logger.info('Loading contrib plugin: {name} ({doc})'.format(**plugin_obj.info()))
52         sima.register_plugin(plugin_obj)
53
54
55 def main():
56     """Entry point, deal w/ CLI and starts application
57     """
58     info = dict({'version': core.__version__,})
59     # StartOpt gathers options from command line call (in StartOpt().options)
60     sopt = StartOpt(info)
61     # set logger
62     verbosity = sopt.options.get('verbosity', 'warning')
63     cli_loglevel = getattr(logging, verbosity.upper())
64     set_logger(level=verbosity)
65     logger = logging.getLogger('sima')
66     logger.setLevel(cli_loglevel)
67     # loads configuration
68     conf_manager = ConfMan(logger, sopt.options)
69     config = conf_manager.config
70     logger.setLevel(getattr(logging,
71                     config.get('log', 'verbosity').upper()))  # pylint: disable=E1103
72
73     logger.debug('Command line say: {0}'.format(sopt.options))
74
75     # Create Database
76     if (sopt.options.get('create_db', None)
77        or not isfile(conf_manager.db_file)):
78         logger.info('Creating database in "{}"'.format(conf_manager.db_file))
79         open(conf_manager.db_file, 'a').close()
80         SimaDB(db_path=conf_manager.db_file).create_db()
81         if sopt.options.get('create_db', None):
82             logger.info('Done, bye...')
83             sys.exit(0)
84
85     logger.info('Starting...')
86     sima = core.Sima(config, conf_manager.db_file)
87
88     #  Loading internal plugins
89     for plugin in PLUGINS:
90         logger.info('Loading internal plugin: {name} ({doc})'.format(**plugin.info()))
91         sima.register_plugin(plugin)
92
93     #  Loading contrib plugins
94     load_contrib_plugins(sima)
95     try:
96         sima.run()
97     except KeyboardInterrupt:
98         logger.info('Caught KeyboardInterrupt, stopping')
99         sima.shutdown()
100
101
102 # Script starts here
103 if __name__ == '__main__':
104     # pylint: disable=broad-except
105     try:
106         main()
107     except Exception:
108         exception_log()
109
110
111 # VIM MODLINE
112 # vim: ai ts=4 sw=4 sts=4 expandtab