+# -*- coding: utf-8 -*-
+
+from optparse import (OptionParser, OptionValueError, SUPPRESS_HELP)
+from os import environ
+
+# Options list
+# pop out 'sw' value before creating OptionParser object.
+##{{{
+OPTS = list([
+ {
+ 'sw': ['-S', '--hostname'],
+ 'type': 'string',
+ 'dest': 'host',
+ 'help': 'Hostname MPD in running on'},
+ {
+ 'sw': ['-P', '--port'],
+ 'type': 'int',
+ 'dest': 'port',
+ 'help': 'Port MPD in listening on'},
+])
+#}}}
+
+USAGE = u"""USAGE: %prog [--help] [options]"""
+
+
+def get_mpd_environ():#{{{
+ """
+ Retrieve MPD env. var.
+ """
+ con_id = dict({'host': 'localhost',
+ 'port': int(6600)})
+ passwd = None
+ mpd_host_env = environ.get('MPD_HOST')
+ mpd_port = environ.get('MPD_PORT')
+ if mpd_host_env:
+ # If password is set:
+ # mpd_host_env = ['pass', 'host'] because MPD_HOST=pass@host
+ mpd_host_env = mpd_host_env.split('@')
+ mpd_host_env.reverse()
+ if len(mpd_host_env[0]) > 0:
+ con_id.update({'host': mpd_host_env[0]})
+ if len(mpd_host_env) > 1:
+ print 'passwd set in MPD_HOST'
+ passwd = mpd_host_env[1]
+ if mpd_port:
+ con_id.update({'port': int(mpd_port)})
+ return (con_id, passwd)#}}}
+
+
+class StartOpt(object):
+ """
+ """
+
+ def __init__(self, script_info, options):#{{{
+ self.parser = None
+ self.cli_options = dict({})
+ self.cli_args = dict({})
+ self.info = dict(script_info)
+ self.options = list(options + OPTS)
+ self.main()#}}}
+
+ def declare_opts(self):#{{{
+ """
+ Declare options in OptionParser object.
+ """
+ version = self.info.get('version')
+ prog = self.info.get('prog_name')
+ des = self.info.get('description')
+ self.parser = OptionParser(version=version,
+ usage=USAGE,
+ prog=prog,
+ description=des)
+ con_id, passwd = get_mpd_environ()
+ # Add all options declare in OPTS
+ for opt in self.options:
+ opt_names = opt.pop('sw')
+ self.parser.add_option(*opt_names, **opt)
+ #set defaults for host/port according to env var
+ dest = opt.get('dest')
+ if dest in ['host']:
+ self.parser.set_defaults(host=con_id.get('host'))
+ if dest in ['port']:
+ self.parser.set_defaults(port=con_id.get('port'))#}}}
+
+ def main(self):
+ """declare options, parse command line"""
+ self.declare_opts()
+ (self.cli_options, self.cli_args) = self.parser.parse_args()
+
+
+def main():
+ pass
+
+# Script starts here
+if __name__ == '__main__':
+ main()
+
+# VIM MODLINE
+# vim: ai ts=4 sw=4 sts=4 expandtab
+