]> kaliko git repositories - mpd-goodies.git/blob - lib/startop.py
44cb64a00a25ef7b8b4f0fe7a45a2e02ec5efdb7
[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, options):#{{{
55         self.parser = None
56         self.cli_options = dict({})
57         self.cli_args = dict({})
58         self.info = dict(script_info)
59         self.options = list(options + OPTS)
60         self.main()#}}}
61
62     def declare_opts(self):#{{{
63         """
64         Declare options in OptionParser object.
65         """
66         version = self.info.get('version')
67         prog = self.info.get('prog_name')
68         des = self.info.get('description')
69         if 'usage' in self.info:
70             usage = self.info.get('usage')
71         else:
72             usage = USAGE
73         self.parser = OptionParser(version=version,
74                                    usage=usage,
75                                    prog=prog,
76                                    description=des)
77         con_id, passwd = get_mpd_environ()
78         # Add all options declare in OPTS
79         for opt in self.options:
80             opt_names = opt.pop('sw')
81             self.parser.add_option(*opt_names, **opt)
82             #set defaults for host/port according to env var
83             dest = opt.get('dest')
84             if dest in ['host']:
85                 self.parser.set_defaults(host=con_id.get('host'))
86             if dest in ['port']:
87                 self.parser.set_defaults(port=con_id.get('port'))#}}}
88
89     def main(self):
90         """declare options, parse command line"""
91         self.declare_opts()
92         (self.cli_options, self.cli_args) = self.parser.parse_args()
93
94
95 def main():
96     pass
97
98 # Script starts here
99 if __name__ == '__main__':
100     main()
101
102 # VIM MODLINE
103 # vim: ai ts=4 sw=4 sts=4 expandtab
104