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
25 from .utils import Wfile, Rfile, Wdir
28 MPD_sima automagicaly queue new tracks in MPD playlist.
30 Command line options override their equivalent in configuration file.
31 If a positional arguments is provided MPD_sima execute the command and returns.
37 def clean_dict(to_clean):
38 """Remove items which values are set to None/False"""
39 for k in list(to_clean.keys()):
40 if not to_clean.get(k):
44 CMDS = {'config-test': 'Test configuration (MPD connection and Tags plugin only)',
45 'create-db': 'Create the database',
46 'generate-config': 'Generate a configuration file to stdout',
47 'purge-history': 'Remove play history'
50 # pop out 'sw' value before creating Parser object.
52 # If an option has to override its dual in conf file, the destination
53 # identifier "dest" is to be named after that option in the conf file.
54 # The supersedes_config_with_cmd_line_options method in ConfMan() (config.py)
55 # is looking for command line option names identical to config file option
56 # name it is meant to override.
59 'sw': ['-l', '--log'],
64 'help': 'file to log message to, default is stdout/stderr'},
66 'sw': ['-v', '--log-level'],
69 'choices': ['debug', 'info', 'warning', 'error'],
70 'help': 'log messages verbosity, default is info'},
72 'sw': ['-p', '--pid'],
76 'help': 'file to save PID to, default is not to store pid'},
78 'sw': ['-d', '--daemon'],
80 'action': 'store_true',
81 'help': 'daemonize process'},
83 'sw': ['-S', '--host'],
85 'help': 'host MPD in running on (IP or FQDN)'},
87 'sw': ['-P', '--port'],
90 'help': 'port MPD in listening on'},
92 'sw': ['-c', '--config'],
96 'help': 'configuration file to load'},
97 { # TODO: To remove eventually in next major realese v0.18
98 'sw': ['--generate-config'],
99 'dest': 'generate_config',
100 'action': 'store_true',
103 'sw': ['--var-dir', '--var_dir'],
106 'help': 'directory to store var content (ie. database, cache)'},
107 { # TODO: To remove eventually in next major realese v0.18
108 'sw': ['--create-db'],
109 'action': 'store_true',
115 'choices': CMDS.keys(),
116 'help': 'command to run (cf. description or unix manual for more)'},
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 cmds = '\n'.join([f' * {k}: {v}' for k, v in CMDS.items()])
135 self.parser = ArgumentParser(description=DESCRIPTION.format(cmds),
136 prog=self.info.get('prog'),
137 epilog='Happy Listening',
138 formatter_class=RawDescriptionHelpFormatter,
140 self.parser.add_argument('--version', action='version',
141 version='%(prog)s {version}'.format(**self.info))
142 # Add all options declare in OPTS
144 opt_names = opt.pop('sw')
145 self.parser.add_argument(*opt_names, **opt)
149 Look for env. var and parse command line.
152 options = vars(self.parser.parse_args())
153 # Set log file to os.devnull in daemon mode to avoid logging to
155 # TODO: Probably useless. To be checked
156 #if options.__dict__.get('daemon', False) and \
157 # not options.__dict__.get('logfile', False):
158 # options.__dict__['logfile'] = devnull
159 self.options.update(options)
160 clean_dict(self.options)
164 # vim: ai ts=4 sw=4 sts=4 expandtab