From aeacbd03b6efcd9fc163560030fdd2e7676a5794 Mon Sep 17 00:00:00 2001 From: kaliko Date: Tue, 15 Dec 2020 11:24:44 +0100 Subject: [PATCH] Tags plugin configuration check and test Also improved internal Exception handling --- sima/launch.py | 14 +++++++------- sima/plugins/internal/tags.py | 28 +++++++++++++++++++++------- sima/utils/utils.py | 12 ++++-------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/sima/launch.py b/sima/launch.py index 538238b..68ba4e0 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 kaliko # # This file is part of sima # @@ -37,7 +37,7 @@ from .lib.logger import set_logger 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 @@ -72,11 +72,7 @@ def load_plugins(sima, source): 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): @@ -146,10 +142,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() diff --git a/sima/plugins/internal/tags.py b/sima/plugins/internal/tags.py index 9000c37..4b1d764 100644 --- a/sima/plugins/internal/tags.py +++ b/sima/plugins/internal/tags.py @@ -25,11 +25,12 @@ Add titles based on tags import random # third parties components +from musicpd import CommandError # local import from ...lib.plugin import Plugin from ...lib.track import Track -from ...utils.utils import PluginConfException, PluginException +from ...utils.utils import PluginException def forge_filter(cfg): tags = set(cfg.keys()) & Tags.supported_tags @@ -66,17 +67,24 @@ class Tags(Plugin): def _control_conf(self): sup_tags = Tags.supported_tags + config_tags = {k for k, v in self.plugin_conf.items() + if (v and k not in ['filter', 'priority'])} if not self.plugin_conf.get('filter', None) and \ - self.plugin_conf.keys().isdisjoint(sup_tags): - self.log.error( - 'Found no config for %s plugin! Need at least "filter" or a supported tag' % self) + config_tags.isdisjoint(sup_tags): + self.log.error('Found no config for %s plugin! ' + 'Need at least "filter" or a supported tag', self) self.log.info('Supported Tags are : %s', ', '.join(sup_tags)) - raise PluginConfException('plugin misconfiguration') + raise PluginException('plugin misconfiguration') + if config_tags.difference(sup_tags): + self.log.error('Found unsupported tag in config: %s', + config_tags.difference(sup_tags)) + raise PluginException('plugin misconfiguration') def _setup_tagsneeded(self): + config_tags = {k for k, v in self.plugin_conf.items() if v} self.log.debug('%s plugin needs the followinng metadata: %s', - self, set(self.plugin_conf.keys()) & Tags.supported_tags) - tags = set(self.plugin_conf.keys()) & Tags.supported_tags + self, config_tags & Tags.supported_tags) + tags = config_tags & Tags.supported_tags self.player.needed_tags |= tags def _get_history(self): @@ -94,6 +102,12 @@ class Tags(Plugin): self.log.error('Need at least MPD 0.21 to use Tags plugin (filters required)') self.player.disconnect() raise PluginException('MPD >= 0.21 required') + # Check filter is valid + try: + self.player.find(self.plugin_conf['filter']) + except CommandError: + raise PluginException('Badly formated filter in tags plugin configuration: "%s"' + % self.plugin_conf['filter']) def callback_need_track(self): candidates = [] diff --git a/sima/utils/utils.py b/sima/utils/utils.py index 19603ba..429b0f9 100644 --- a/sima/utils/utils.py +++ b/sima/utils/utils.py @@ -165,9 +165,11 @@ class Throttle: return result return wrapper -# http client exceptions (for webservices) +class MPDSimaException(Exception): + pass -class WSError(Exception): +# http client exceptions (for webservices) +class WSError(MPDSimaException): pass class WSNotFound(WSError): @@ -179,14 +181,8 @@ class WSTimeout(WSError): class WSHTTPError(WSError): pass -class MPDSimaException(Exception): - pass - class PluginException(MPDSimaException): pass -class PluginConfException(MPDSimaException): - pass - # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab -- 2.39.2