From ca65647609ddffe3f37b1c70d2b1c261753dcfd6 Mon Sep 17 00:00:00 2001 From: kaliko Date: Tue, 16 Nov 2021 11:52:04 +0100 Subject: [PATCH] Cleanup code Update Exceptions (cf. 3b4178f) Add copyright header Better error message --- sima/utils/configtest.py | 41 ++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/sima/utils/configtest.py b/sima/utils/configtest.py index 82b49c4..f1c6a1a 100644 --- a/sima/utils/configtest.py +++ b/sima/utils/configtest.py @@ -1,11 +1,26 @@ -# coding: utf-8 +# -*- coding: utf-8 -*- +# Copyright (c) 2021 kaliko +# +# This file is part of sima +# +# sima is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# sima is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with sima. If not, see . -import sys +import re from logging import getLogger -from ..mpdclient import MPD -from ..mpdclient import PlayerError +from ..mpdclient import MPD, PlayerError from ..plugins.internal.tags import forge_filter, control_config @@ -17,15 +32,17 @@ def tags_config_test(cli, config): if not control_config(tags_cfg): return filt = forge_filter(tags_cfg) - log.info('Trying tags and filter config:') - log.info('%s', filt) + log.info('Trying tags and filter config') + log.debug('Complete filter (tag+filter): %s', filt) try: # Use window to limit reponse size res = cli.find(filt, 'window', (0, 300)) except PlayerError as err: - cli.disconnect() - print(f'filter error: {err}', file=sys.stderr) - sys.exit(1) + msg = re.split('{find}', str(err)) + if len(msg) == 2 and tags_cfg.get('filter'): + log.info('user filter: %s', tags_cfg.get('filter')) + log.error('failed to find tracks, error: %s', msg[1]) + raise PlayerError(err) from err artists = list({trk.albumartist for trk in res if trk.albumartist}) if not artists: log.info('Tags config correct but got nothing from MPD\'s library') @@ -39,11 +56,7 @@ def config_test(config): log.info('Trying to connect MPD: %s:%s', config.get('MPD', 'host'), config.get('MPD', 'port')) - try: - cli.connect() - except PlayerError as err: - print(err, file=sys.stderr) - sys.exit(1) + cli.connect() if 'Tags' in config.get('sima', 'internal'): tags_config_test(cli, config) -- 2.39.5