1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2009-2015, 2021 kaliko <kaliko@azylum.org>
5 # This file is part of sima
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.
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.
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/>.
22 from argparse import ArgumentParser, RawDescriptionHelpFormatter, SUPPRESS
24 from .utils import Wfile, Rfile, Wdir
27 MPD_sima automagicaly queue new tracks in MPD playlist.
29 Command line options override their equivalent in configuration file.
30 If a positional arguments is provided MPD_sima execute the command and returns.
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):
41 # pop out 'sw' value before creating Parser object.
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.
50 'sw': ['-l', '--log'],
55 'help': 'file to log message to, default is stdout/stderr'},
57 'sw': ['-v', '--log-level'],
60 'choices': ['debug', 'info', 'warning', 'error'],
61 'help': 'log messages verbosity, default is info'},
63 'sw': ['-p', '--pid'],
67 'help': 'file to save PID to, default is not to store pid'},
69 'sw': ['-d', '--daemon'],
71 'action': 'store_true',
72 'help': 'daemonize process'},
74 'sw': ['-S', '--host'],
76 'help': 'host MPD in running on (IP or FQDN)'},
78 'sw': ['-P', '--port'],
81 'help': 'port MPD in listening on'},
83 'sw': ['-c', '--config'],
87 'help': 'configuration file to load'},
89 'sw': ['--var-dir', '--var_dir'],
92 'help': 'directory to store var content (ie. database, cache)'},
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'},
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'},
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'},
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'},
114 {'name': 'id', 'type': int, 'nargs': '?',
115 'help': 'blocklist ID to suppress (use bl-view list IDs)'}
116 ], 'help': 'Remove entries from the blocklist'},
121 """Command line management.
124 def __init__(self, script_info,):
126 self.info = dict(script_info)
127 self.options = dict()
130 def declare_opts(self):
132 Declare options in ArgumentParser object.
134 self.parser = ArgumentParser(description=DESCRIPTION,
135 prog=self.info.get('prog'),
136 epilog='Happy Listening',
137 formatter_class=RawDescriptionHelpFormatter,
139 self.parser.add_argument('--version', action='version',
140 version='%(prog)s {version}'.format(**self.info))
141 # Add all options declare in OPTS
143 opt_names = opt.pop('sw')
144 self.parser.add_argument(*opt_names, **opt)
146 sp = 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')
151 helpmsg = cmd.pop('help')
152 cmd, args = cmd.popitem()
153 _ = sp.add_parser(cmd, description=helpmsg, help=helpmsg)
155 name = arg.pop('name', None)
157 _.add_argument(name, **arg)
161 Look for env. var and parse command line.
164 options = vars(self.parser.parse_args())
165 # Set log file to os.devnull in daemon mode to avoid logging to
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)
176 # vim: ai ts=4 sw=4 sts=4 expandtab