]> kaliko git repositories - mpd-sima.git/blob - sima/utils/startopt.py
Some clean-up (pylint audit)
[mpd-sima.git] / sima / utils / startopt.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Jack 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, SUPPRESS)
23
24
25 from .utils import Wfile, Rfile, Wdir
26
27 USAGE = """USAGE:  %prog [--help] [options]"""
28 DESCRIPTION = """
29 sima automagicaly queue new tracks in MPD playlist.
30 All command line options will override their equivalent in configuration
31 file.
32 """
33
34
35 def clean_dict(to_clean):
36     """Remove items which values are set to None/False"""
37     for k in list(to_clean.keys()):
38         if not to_clean.get(k):
39             to_clean.pop(k)
40
41
42 # OPTIONS LIST
43 # pop out 'sw' value before creating Parser object.
44 # PAY ATTENTION:
45 #   If an option has to override its dual in conf file, the destination
46 #   identifier "dest" is to be named after that option in the conf file.
47 #   The supersedes_config_with_cmd_line_options method in ConfMan() (config.py)
48 #   is looking for command line option names identical to config file option
49 #   name it is meant to override.
50 OPTS = [
51     {
52         'sw':['-l', '--log'],
53         'type': str,
54         'dest': 'logfile',
55         'action': Wfile,
56         'help': 'file to log message to, default is stdout/stderr'},
57     {
58         'sw':['-v', '--log-level'],
59         'type': str,
60         'dest': 'verbosity',
61         'choices': ['debug', 'info', 'warning', 'error'],
62         'help': 'Log messages verbosity, default is info'},
63     {
64         'sw': ['-p', '--pid'],
65         'dest': 'pidfile',
66         'action': Wfile,
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         'help': 'Configuration file to load'},
87     {
88         'sw':['--var_dir'],
89         'dest': 'var_dir',
90         'action': Wdir,
91         'help': 'Directory to store var content (ie. database, cache)'},
92     {
93         'sw': ['--create-db'],
94         'action': 'store_true',
95         'dest': 'create_db',
96         'help': '''Create database and exit, use destination
97                    specified in --var_dir or standard location.'''},
98     {
99         'sw':['--queue-mode', '-q'],
100         'dest': 'queue_mode',
101         'choices': ['track', 'top', 'album'],
102         #'help': 'Queue mode in [track, top, album]',
103         'help': SUPPRESS, },
104     {
105         'sw':['--purge_history'],
106         'action': 'store_true',
107         'dest': 'do_purge_history',
108         'help': SUPPRESS},
109 ]
110
111
112 class StartOpt(object):
113     """Command line management.
114     """
115
116     def __init__(self, script_info,):
117         self.parser = None
118         self.info = dict(script_info)
119         self.options = dict()
120         self.main()
121
122     def declare_opts(self):
123         """
124         Declare options in ArgumentParser object.
125         """
126         self.parser = ArgumentParser(description=DESCRIPTION,
127                                    usage='%(prog)s [options]',
128                                    prog=self.info.get('prog'),
129                                    epilog='Happy Listening',
130                 )
131         self.parser.add_argument('--version', action='version',
132                 version='%(prog)s {version}'.format(**self.info))
133         # Add all options declare in OPTS
134         for opt in OPTS:
135             opt_names = opt.pop('sw')
136             self.parser.add_argument(*opt_names, **opt)
137
138     def main(self):
139         """
140         Look for env. var and parse command line.
141         """
142         self.declare_opts()
143         options = vars(self.parser.parse_args())
144         # Set log file to os.devnull in daemon mode to avoid logging to
145         # std(out|err).
146         # TODO: Probably useless. To be checked
147         #if options.__dict__.get('daemon', False) and \
148         #        not options.__dict__.get('logfile', False):
149         #    options.__dict__['logfile'] = devnull
150         self.options.update(options)
151         clean_dict(self.options)
152
153
154 # VIM MODLINE
155 # vim: ai ts=4 sw=4 sts=4 expandtab