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