]> kaliko git repositories - mpd-sima.git/blob - sima/utils/startopt.py
Mainly use literal for list/dict and f-strings when possible
[mpd-sima.git] / sima / utils / startopt.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009-2015, 2021 kaliko <kaliko@azylum.org>
4 #
5 #  This file is part of sima
6 #
7 #  sima is free software: you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation, either version 3 of the License, or
10 #  (at your option) any later version.
11 #
12 #  sima is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with sima.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 #
21
22 from argparse import ArgumentParser, RawDescriptionHelpFormatter
23
24 from .utils import Wfile, Rfile, Wdir
25
26 DESCRIPTION = """
27 MPD_sima automagicaly queue new tracks in MPD playlist.
28
29 Command line options override their equivalent in configuration file.
30 If a positional arguments is provided MPD_sima execute the command and returns.
31 """
32
33
34 def clean_dict(to_clean):
35     """Remove items which values are set to None/False"""
36     for k in list(to_clean.keys()):
37         if not to_clean.get(k):
38             to_clean.pop(k)
39
40 # OPTIONS LIST
41 # pop out 'sw' value before creating Parser object.
42 # PAY ATTENTION:
43 #   If an option has to override its dual in conf file, the destination
44 #   identifier "dest" is to be named after that option in the conf file.
45 #   The supersedes_config_with_cmd_line_options method in ConfMan() (config.py)
46 #   is looking for command line option names identical to config file option
47 #   name it is meant to override.
48 OPTS = [
49     {
50         'sw': ['-l', '--log'],
51         'type': str,
52         'dest': 'logfile',
53         'action': Wfile,
54         'metavar': 'LOG',
55         'help': 'file to log message to, default is stdout/stderr'},
56     {
57         'sw': ['-v', '--log-level'],
58         'type': str,
59         'dest': 'verbosity',
60         'choices': ['debug', 'info', 'warning', 'error'],
61         'help': 'log messages verbosity, default is info'},
62     {
63         'sw': ['-p', '--pid'],
64         'dest': 'pidfile',
65         'action': Wfile,
66         'metavar': 'FILE',
67         'help': 'file to save PID to, default is not to store pid'},
68     {
69         'sw': ['-d', '--daemon'],
70         'dest': 'daemon',
71         'action': 'store_true',
72         'help': 'daemonize process'},
73     {
74         'sw': ['-S', '--host'],
75         'dest': 'host',
76         'help': 'host MPD in running on (IP or FQDN)'},
77     {
78         'sw': ['-P', '--port'],
79         'type': int,
80         'dest': 'port',
81         'help': 'port MPD in listening on'},
82     {
83         'sw': ['-c', '--config'],
84         'dest': 'conf_file',
85         'action': Rfile,
86         'metavar': 'CONFIG',
87         'help': 'configuration file to load'},
88     {
89         'sw': ['--var-dir', '--var_dir'],
90         'dest': 'var_dir',
91         'action': Wdir,
92         'help': 'directory to store var content (ie. database, cache)'},
93 ]
94 # Commands
95 CMDS = [
96         {'config-test': [{}], 'help': 'Test configuration (MPD connection and Tags plugin only)'},
97         {'create-db': [{}], 'help': 'Create the database'},
98         {'generate-config': [{}], 'help': 'Generate a configuration file to stdout'},
99         {'purge-history': [{}], 'help': 'Remove play history'},
100         {'bl-view': [{}], 'help': 'List blocklist IDs'},
101         {'bl-add-artist': [
102             {'name': 'artist', 'type': str, 'nargs': '?',
103              'help': 'If artist is provided use it else use currently playing value'}
104             ], 'help': 'Add artist to the blocklist'},
105         {'bl-add-album': [
106             {'name': 'album', 'type': str, 'nargs': '?',
107              'help': 'If album is provided use it else use currently playing value'}
108          ], 'help': 'Add album to the blocklist'},
109         {'bl-add-track': [
110             {'name': 'track', 'type': str, 'nargs': '?',
111              'help': 'If track is provided use it else use currently playing value'}
112          ], 'help': 'Add track to the blocklist'},
113         {'bl-delete': [
114             {'name': 'id', 'type': int, 'nargs': '?',
115              'help': 'blocklist ID to suppress (use bl-view list IDs)'}
116          ], 'help': 'Remove entries from the blocklist'},
117 ]
118
119
120 class StartOpt:
121     """Command line management.
122     """
123
124     def __init__(self, script_info,):
125         self.parser = None
126         self.info = dict(script_info)
127         self.options = {}
128         self.main()
129
130     def declare_opts(self):
131         """
132         Declare options in ArgumentParser object.
133         """
134         self.parser = ArgumentParser(description=DESCRIPTION,
135                                      prog=self.info.get('prog'),
136                                      epilog='Happy Listening',
137                                      formatter_class=RawDescriptionHelpFormatter,
138                                      )
139         self.parser.add_argument('--version', action='version',
140                         version='%(prog)s {version}'.format(**self.info))
141         # Add all options declare in OPTS
142         for opt in OPTS:
143             opt_names = opt.pop('sw')
144             self.parser.add_argument(*opt_names, **opt)
145         # Add sub commands
146         spa = self.parser.add_subparsers(
147                 title=f'{self.info["prog"]} commands as positional arguments',
148                 description=f"""Use them after optionnal arguments.\n"{self.info["prog"]} command -h" for more info.""",
149                 metavar='', dest='command')
150         for cmd in CMDS:
151             helpmsg = cmd.pop('help')
152             cmd, args = cmd.popitem()
153             _ = spa.add_parser(cmd, description=helpmsg, help=helpmsg)
154             for arg in args:
155                 name = arg.pop('name', None)
156                 if name:
157                     _.add_argument(name, **arg)
158
159     def main(self):
160         """
161         Look for env. var and parse command line.
162         """
163         self.declare_opts()
164         options = vars(self.parser.parse_args())
165         # Set log file to os.devnull in daemon mode to avoid logging to
166         # std(out|err).
167         # TODO: Probably useless. To be checked
168         #if options.__dict__.get('daemon', False) and \
169         #        not options.__dict__.get('logfile', False):
170         #    options.__dict__['logfile'] = devnull
171         self.options.update(options)
172         clean_dict(self.options)
173
174
175 # VIM MODLINE
176 # vim: ai ts=4 sw=4 sts=4 expandtab