# -*- coding: utf-8 -*-
-# Copyright (c) 2013, 2014, 2015 kaliko <kaliko@azylum.org>
+# Copyright (c) 2013, 2014, 2015, 2020,2021 kaliko <kaliko@azylum.org>
#
# This file is part of sima
#
from importlib import __import__ as sima_import
from os.path import isfile
+from os import rename
##
# third parties components
from .lib.simadb import SimaDB
from .utils.config import ConfMan
from .utils.startopt import StartOpt
-from .utils.utils import exception_log, SigHup, PluginConfException
+from .utils.utils import exception_log, SigHup, MPDSimaException
# core plugins
from .plugins.core.history import History
from .plugins.core.mpdoptions import MpdOptions
sys.exit(1)
logger.info('Loading {0} plugin: {name} ({doc})'.format(
source, **plugin_obj.info()))
- try:
- sima.register_plugin(plugin_obj)
- except PluginConfException as err:
- logger.error(err)
- sys.exit(2)
+ sima.register_plugin(plugin_obj)
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)
+ SimaDB(db_path=db_file).create_db()
+ # Migration from v0.17.0
+ dbinfo = SimaDB(db_path=db_file).get_info()
+ if not dbinfo: # v0.17.0 → v0.18+ migration
+ logger.warning('Backing up database!')
+ rename(db_file, db_file + '-old-version-backup')
+ logger.info('Creating an new database in "%s"', db_file)
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)
+ 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)
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()