]> kaliko git repositories - mpd-sima.git/blob - sima/utils/testtags.py
Add test-tags-config helper
[mpd-sima.git] / sima / utils / testtags.py
1 # coding: utf-8
2 # Copyright (c) 2020 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 #
20 """Testing Tags plugin config
21 """
22
23 import argparse
24 import os.path
25 import sys
26
27 from configparser import ConfigParser
28
29 import musicpd
30
31 from ..plugins.internal.tags import forge_filter
32
33
34 def is_valid_file(parser, arg):
35     if not os.path.exists(arg) or not os.path.isfile(arg):
36         parser.error('The file "%s" does not exist!' % arg)
37     else:
38         return arg
39
40
41 def main():
42     parser = argparse.ArgumentParser(description='Tests Tags plugin config')
43     parser.add_argument('config', nargs=1,
44                         type=lambda x: is_valid_file(parser, x))
45     pargs = parser.parse_args(sys.argv[1:])
46     conf = ConfigParser()
47     conf.read(pargs.config)
48     if not conf['tags']:
49         print('Nothing in "tags" section', file=sys.stderr)
50         sys.exit(1)
51     tags_cfg = conf['tags']
52     filt = forge_filter(tags_cfg)
53     print(f'Filter forged: "{filt}"')
54     host = conf['MPD'].get('host', None)
55     port = conf['MPD'].get('port', None)
56     cli = musicpd.MPDClient()
57     try:
58         cli.connect(host=host, port=port)
59     except musicpd.ConnectionError as err:
60         print(err, file=sys.stderr)
61         sys.exit(1)
62     try:
63         res = cli.find(filt)
64     except musicpd.CommandError as err:
65         cli.disconnect()
66         print(err, file=sys.stderr)
67         sys.exit(1)
68     print({trk.get('artist', 'ukn') for trk in res})
69
70
71 # Script starts here
72 if __name__ == '__main__':
73     main()
74
75 # VIM MODLINE
76 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8