]> kaliko git repositories - mpd-sima.git/blob - sima/utils/configtest.py
Cleanup PlayerError exception wrapper
[mpd-sima.git] / sima / utils / configtest.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2021 kaliko <kaliko@azylum.org>
3 #
4 #  This file is part of sima
5 #
6 #  sima is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  sima is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with sima.  If not, see <http://www.gnu.org/licenses/>.
18
19 import re
20
21 from logging import getLogger
22
23 from ..mpdclient import MPD, PlayerError
24
25 from ..plugins.internal.tags import forge_filter, control_config
26
27 log = getLogger('sima')
28
29
30 def tags_config_test(cli, config):
31     tags_cfg = config['tags']
32     if not control_config(tags_cfg):
33         return
34     filt = forge_filter(tags_cfg, log)
35     log.info('Trying tags and filter config')
36     log.debug('Complete filter (tag+filter): %s', filt)
37     try:
38         # Use window to limit reponse size
39         res = cli.find(filt, 'window', (0, 300))
40     except PlayerError as err:
41         msg = re.split('{find}', str(err))
42         if len(msg) == 2 and tags_cfg.get('filter'):
43             log.info('user filter: %s', tags_cfg.get('filter'))
44             log.error('failed to find tracks, error: %s', msg[1])
45         raise PlayerError(err) from err
46     artists = list({trk.albumartist for trk in res if trk.albumartist})
47     if not artists:
48         log.info('Tags config correct but got nothing from MPD\'s library')
49         return
50     log.info('Got results, here are some of the artists found:')
51     log.info('%s', ' / '.join(artists[:6]))
52
53
54 def config_test(config):
55     cli = MPD(config)
56     log.info('Trying to connect MPD: %s:%s',
57              config.get('MPD', 'host'),
58              config.get('MPD', 'port'))
59     cli.connect()
60     if 'Tags' in config.get('sima', 'internal'):
61         tags_config_test(cli, config)
62
63
64 # VIM MODLINE
65 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8