]> kaliko git repositories - mpd-goodies.git/blob - lib/startop.py
* nalbum : new fade out/in feature for album transition
[mpd-goodies.git] / lib / startop.py
1 # -*- coding: utf-8 -*-
2
3 from optparse import (OptionParser, OptionValueError, SUPPRESS_HELP)
4 from os import environ
5
6 # Options list
7 # pop out 'sw' value before creating OptionParser object.
8 ##{{{
9 OPTS = list([
10     {
11         'sw': ['-S', '--hostname'],
12         'type': 'string',
13         'dest': 'host',
14         'help': 'Hostname MPD in running on'},
15     {
16         'sw': ['-P', '--port'],
17         'type': 'int',
18         'dest': 'port',
19         'help': 'Port MPD in listening on'},
20 ])
21 #}}}
22
23 USAGE = u"""USAGE:  %prog [--help] [options]"""
24
25
26 def get_mpd_environ():#{{{
27     """
28     Retrieve MPD env. var.
29     """
30     con_id = dict({'host': 'localhost',
31                    'port': int(6600)})
32     passwd = None
33     mpd_host_env = environ.get('MPD_HOST')
34     mpd_port = environ.get('MPD_PORT')
35     if mpd_host_env:
36         # If password is set:
37         # mpd_host_env = ['pass', 'host'] because MPD_HOST=pass@host
38         mpd_host_env = mpd_host_env.split('@')
39         mpd_host_env.reverse()
40         if len(mpd_host_env[0]) > 0:
41                 con_id.update({'host': mpd_host_env[0]})
42         if len(mpd_host_env) > 1:
43             print 'passwd set in MPD_HOST'
44             passwd = mpd_host_env[1]
45     if mpd_port:
46         con_id.update({'port': int(mpd_port)})
47     return (con_id, passwd)#}}}
48
49
50 class StartOpt(object):
51     """
52     """
53
54     def __init__(self, script_info, child_options):#{{{
55         self.parser = None
56         self.cli_options = dict({})
57         self.cli_args = dict({})
58         self.info = dict(script_info)
59         # options allows to add new cli options within child objects calling
60         # parent __init__()
61         self.options = list(child_options + OPTS)
62         self.main()#}}}
63
64     def declare_opts(self):#{{{
65         """
66         Declare options in OptionParser object.
67         """
68         version = self.info.get('version')
69         prog = self.info.get('prog_name')
70         des = self.info.get('description')
71         if 'usage' in self.info:
72             usage = self.info.get('usage')
73         else:
74             usage = USAGE
75         self.parser = OptionParser(version=version,
76                                    usage=usage,
77                                    prog=prog,
78                                    description=des)
79         con_id, passwd = get_mpd_environ()
80         # Add all options declare in OPTS
81         for opt in self.options:
82             opt_names = opt.pop('sw')
83             self.parser.add_option(*opt_names, **opt)
84             #set defaults for host/port according to env var
85             dest = opt.get('dest')
86             if dest in ['host']:
87                 self.parser.set_defaults(host=con_id.get('host'))
88             if dest in ['port']:
89                 self.parser.set_defaults(port=con_id.get('port'))#}}}
90
91     def main(self):
92         """declare options, parse command line"""
93         self.declare_opts()
94         (self.cli_options, self.cli_args) = self.parser.parse_args()
95
96
97 def main():
98     pass
99
100 # Script starts here
101 if __name__ == '__main__':
102     main()
103
104 # VIM MODLINE
105 # vim: ai ts=4 sw=4 sts=4 expandtab
106