+++ /dev/null
-# -*- coding: utf-8 -*-
-
-from locale import getpreferredencoding
-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 (default: localhost or MPD_HOST if set)'},
- {
- 'sw': ['-P', '--port'],
- 'type': 'int',
- 'dest': 'port',
- 'help': 'Port MPD in listening on (default: 6600 or MPD_PORT if set)'},
-])
-
-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]
- con_id.update({'passwd': passwd})
- if mpd_port:
- con_id.update({'port': int(mpd_port)})
- return (con_id, passwd)
-
-
-class StartOpt(object):
- """
- """
-
- def __init__(self, script_info, child_options):
- # Strong assumption?
- self.localencoding = 'utf8'
- self.parser = None
- self.cli_options = dict({})
- # TODO: dict ?!!? isn't it a list instead?
- self.cli_args = dict({})
- self.info = dict(script_info)
- # options allows to add new cli options within child objects calling
- # parent __init__()
- self.options = list(child_options + OPTS)
- self.main()
-
- def _get_encoding(self):
- """Get local encoding"""
- self.localencoding = getpreferredencoding()
-
- def _u8_convert(self, string):
- """Convert CLI input string to UTF8 (mpd standart)"""
- return unicode(string, self.localencoding).encode('UTF-8')
-
- 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')
- if 'usage' in self.info:
- usage = self.info.get('usage')
- else:
- usage = USAGE
- 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._get_encoding()
- (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