1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Jack 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, SUPPRESS)
25 from .utils import Wfile, Rfile, Wdir
27 USAGE = """USAGE: %prog [--help] [options]"""
29 sima automagicaly queue new tracks in MPD playlist.
30 All command line options will override their equivalent in configuration
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):
43 # pop out 'sw' value before creating Parser object.
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.
56 'help': 'file to log message to, default is stdout/stderr'},
58 'sw':['-v', '--log-level'],
61 'choices': ['debug', 'info', 'warning', 'error'],
62 'help': 'Log messages verbosity, default is info'},
64 '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'],
86 'help': 'Configuration file to load'},
91 'help': 'Directory to store var content (ie. database, cache)'},
93 'sw': ['--create-db'],
94 'action': 'store_true',
96 'help': '''Create database and exit, use destination
97 specified in --var_dir or standard location.'''},
99 'sw':['--queue-mode', '-q'],
100 'dest': 'queue_mode',
101 'choices': ['track', 'top', 'album'],
102 #'help': 'Queue mode in [track, top, album]',
105 'sw':['--purge_history'],
106 'action': 'store_true',
107 'dest': 'do_purge_history',
112 class StartOpt(object):
113 """Command line management.
116 def __init__(self, script_info,):
118 self.info = dict(script_info)
119 self.options = dict()
122 def declare_opts(self):
124 Declare options in ArgumentParser object.
126 self.parser = ArgumentParser(description=DESCRIPTION,
127 usage='%(prog)s [options]',
128 prog=self.info.get('prog'),
129 epilog='Happy Listening',
131 self.parser.add_argument('--version', action='version',
132 version='%(prog)s {version}'.format(**self.info))
133 # Add all options declare in OPTS
135 opt_names = opt.pop('sw')
136 self.parser.add_argument(*opt_names, **opt)
140 Look for env. var and parse command line.
143 options = vars(self.parser.parse_args())
144 # Set log file to os.devnull in daemon mode to avoid logging to
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)
155 # vim: ai ts=4 sw=4 sts=4 expandtab