]> kaliko git repositories - mpd-sima.git/blob - sima/utils/startopt.py
Fix help message for --log-level
[mpd-sima.git] / sima / utils / startopt.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009, 2010, 2011, 2012, 2013 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 import sys
23 from argparse import (ArgumentParser, SUPPRESS)
24
25
26 from .utils import Obsolete, Wfile, Rfile, Wdir
27
28 USAGE = """USAGE:  %prog [--help] [options]"""
29 DESCRIPTION = """
30 sima automagicaly queue new tracks in MPD playlist.
31 All command line options will override their equivalent in configuration
32 file.
33 """
34
35
36 def clean_dict(to_clean):
37     """Remove items which values are set to None/False"""
38     for k in list(to_clean.keys()):
39         if not to_clean.get(k):
40             to_clean.pop(k)
41
42
43 # OPTIONS LIST
44 # pop out 'sw' value before creating Parser object.
45 # PAY ATTENTION:
46 #   If an option has to override its dual in conf file, the destination
47 #   identifier "dest" is to be named after that option in the conf file.
48 #   The supersedes_config_with_cmd_line_options method in ConfMan() (config.py)
49 #   is looking for command line option names identical to config file option
50 #   name it is meant to override.
51 OPTS = [
52     {
53         'sw':['-l', '--log'],
54         'type': str,
55         'dest': 'logfile',
56         'action': Wfile,
57         'help': 'file to log message to, default is stdout/stderr'},
58     {
59         'sw':['-v', '--log-level'],
60         'type': str,
61         'dest': 'verbosity',
62         'choices': ['debug', 'info', 'warning', 'error'],
63         'help': 'Log messages verbosity, default is info'},
64     {
65         'sw': ['-p', '--pid'],
66         'dest': 'pidfile',
67         'action': Wfile,
68         'help': 'file to save PID to, default is not to store pid'},
69     {
70         'sw': ['-d', '--daemon'],
71         'dest': 'daemon',
72         'action': 'store_true',
73         'help': 'Daemonize process.'},
74     {
75         'sw': ['-S', '--host'],
76         'dest': 'host',
77         'help': 'Host MPD in running on (IP or FQDN)'},
78     {
79         'sw': ['-P', '--port'],
80         'type': int,
81         'dest': 'port',
82         'help': 'Port MPD in listening on'},
83     {
84         'sw':['-c', '--config'],
85         'dest': 'conf_file',
86         'action': Rfile,
87         'help': 'Configuration file to load'},
88     {
89         'sw':['--var_dir'],
90         'dest': 'var_dir',
91         'action': Wdir,
92         'help': 'Directory to store var content (ie. database, cache)'},
93     {
94         'sw': ['--create-db'],
95         'action': 'store_true',
96         'dest': 'create_db',
97         'help': '''Create database and exit, use destination
98                    specified in --var_dir or standard location.'''},
99     {
100         'sw':['--queue-mode', '-q'],
101         'dest': 'queue_mode',
102         'choices': ['track', 'top', 'album'],
103         #'help': 'Queue mode in [track, top, album]',
104         'help': SUPPRESS, },
105     {
106         'sw':['--purge_history'],
107         'action': 'store_true',
108         'dest': 'do_purge_history',
109         'help': SUPPRESS},
110 ]
111
112
113 class StartOpt(object):
114     """Command line management.
115     """
116
117     def __init__(self, script_info,):
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