X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sima%2Flaunch.py;h=0ff7da3aa38e220ad0fb54777559cb0681f434e5;hb=69c5f0029e140e4471f38e8cae5d07b649263a08;hp=1ae3c190bdd4d804aa13764c70710af1e8d2c596;hpb=5155de9f17342ee68f19196d7658751883f4145a;p=mpd-sima.git diff --git a/sima/launch.py b/sima/launch.py index 1ae3c19..0ff7da3 100644 --- a/sima/launch.py +++ b/sima/launch.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2013, 2014, 2015 kaliko +# Copyright (c) 2013, 2014, 2015, 2020,2021 kaliko # # This file is part of sima # @@ -34,11 +34,10 @@ from os.path import isfile # local import from . import core, info from .lib.logger import set_logger -from .lib.meta import Meta from .lib.simadb import SimaDB from .utils.config import ConfMan from .utils.startopt import StartOpt -from .utils.utils import exception_log, SigHup +from .utils.utils import exception_log, SigHup, MPDSimaException # core plugins from .plugins.core.history import History from .plugins.core.mpdoptions import MpdOptions @@ -80,27 +79,49 @@ def start(sopt, restart=False): """starts application """ # loads configuration - config = ConfMan(sopt.options).config + cfg_mgmt = ConfMan(sopt.options) + config = cfg_mgmt.config # set logger logger = logging.getLogger('sima') logfile = config.get('log', 'logfile', fallback=None) verbosity = config.get('log', 'verbosity') set_logger(verbosity, logfile) logger.debug('Command line say: %s', sopt.options) - # Create Database + + # Create database if not present db_file = config.get('sima', 'db_file') - if (sopt.options.get('create_db', None) - or not isfile(db_file)): - logger.info('Creating database in "%s"', db_file) - open(db_file, 'a').close() + if not isfile(db_file): + logger.debug('Creating database in "%s"', db_file) + open(db_file, 'a').close() # TODO: to remove with new simadb in v0.18 SimaDB(db_path=db_file).create_db() - if sopt.options.get('create_db', None): + + if sopt.options.get('command'): + cmd = sopt.options.get('command') + if cmd == "generate-config": + config.write(sys.stdout, space_around_delimiters=True) + sys.exit(0) + logger.info('Running "%s" and exit' % cmd) + if cmd == "config-test": + logger.info('Config location: "%s"', cfg_mgmt.conf_file) + from .utils.configtest import config_test + config_test(config) + sys.exit(0) + if cmd == "create-db": + if not isfile(db_file): + logger.info('Creating database in "%s"', db_file) + open(db_file, 'a').close() # TODO: to remove with new simadb in v0.18 + SimaDB(db_path=db_file).create_db() + else: + logger.info('Database already there, not overwriting %s', db_file) logger.info('Done, bye...') sys.exit(0) - - if sopt.options.get('generate_config'): - config.write(sys.stdout, space_around_delimiters=True) - sys.exit(0) + if cmd == "purge-history": + db_file = config.get('sima', 'db_file') + if not isfile(db_file): + logger.warning('No db found: %s', db_file) + sys.exit(1) + SimaDB(db_path=db_file).purge_history(duration=0) + sys.exit(0) logger.info('Starting (%s)...', info.__version__) sima = core.Sima(config) @@ -112,20 +133,15 @@ def start(sopt, restart=False): core_plugins = [History, MpdOptions] config['sima']['musicbrainzid'] = 'False' for cplgn in core_plugins: - logger.debug('Register core {name} ({doc})'.format(**cplgn.info())) + logger.debug('Register core %(name)s (%(doc)s)', cplgn.info()) sima.register_core_plugin(cplgn) - logger.debug('core loaded, prioriy: {}'.format(' > '.join(map(str, sima.core_plugins)))) + logger.debug('core loaded, prioriy: %s', ' > '.join(map(str, sima.core_plugins))) # Loading internal plugins load_plugins(sima, 'internal') - # Loading contrib plugins load_plugins(sima, 'contrib') - logger.info('plugins loaded, prioriy: {}'.format(' > '.join(map(str, sima.plugins)))) - # Set use of MusicBrainzIdentifier - if not config.getboolean('sima', 'musicbrainzid'): - logger.info('Disabling MusicBrainzIdentifier') - Meta.use_mbid = False + logger.info('plugins loaded, prioriy: %s', ' > '.join(map(str, sima.plugins))) # Run as a daemon if config.getboolean('daemon', 'daemon'): @@ -148,10 +164,14 @@ def run(sopt, restart=False): Catches Unhandled exception """ # pylint: disable=broad-except + logger = logging.getLogger('sima') try: start(sopt, restart) except SigHup: # SigHup inherit from Exception run(sopt, True) + except MPDSimaException as err: + logger.error(err) + sys.exit(2) except Exception: # Unhandled exception exception_log()