]> kaliko git repositories - mpd-sima.git/blob - sima/utils/configtest.py
Cleanup Exceptions
[mpd-sima.git] / sima / utils / configtest.py
1 # coding: utf-8
2
3 import sys
4
5 from logging import getLogger
6
7 from ..mpdclient import MPD
8 from ..mpdclient import PlayerError
9
10 from ..plugins.internal.tags import forge_filter, control_config
11
12 log = getLogger('sima')
13
14
15 def tags_config_test(cli, config):
16     tags_cfg = config['tags']
17     if not control_config(tags_cfg):
18         return
19     filt = forge_filter(tags_cfg)
20     log.info('Trying tags and filter config:')
21     log.info('%s', filt)
22     try:
23         # Use window to limit reponse size
24         res = cli.find(filt, 'window', (0, 300))
25     except PlayerError as err:
26         cli.disconnect()
27         print('filter error: %s' % err, file=sys.stderr)
28         sys.exit(1)
29     artists = list({trk.albumartist for trk in res if trk.albumartist})
30     if not artists:
31         log.info('Tags config correct but got nothing from MPD\'s library')
32         return
33     log.info('Got results, here are some of the artists found:')
34     log.info('%s', ' / '.join(artists[:6]))
35
36
37 def config_test(config):
38     cli = MPD(config)
39     log.info('Trying to connect MPD: %s:%s',
40              config.get('MPD', 'host'),
41              config.get('MPD', 'port'))
42     try:
43         cli.connect()
44     except PlayerError as err:
45         print(err, file=sys.stderr)
46         sys.exit(1)
47     tags_config_test(cli, config)
48
49
50 # VIM MODLINE
51 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8