NAME = 'crop'
VERSION = '0.1'
-USAGE = """Usage:
-
-crop -S <server> -n <nb_tracks>
-"""
CROP_OPTS = list([
{
'sw': ['-n', '--nbtracks'],
'type': 'int',
'dest': 'nb_tracks',
- 'default': 3,
- 'help': 'Number of tracks to keep before the current one.'},
+ 'default': 60,
+ 'metavar': '<n>',
+ 'help': 'keep <n> tracks before currently played.'},
])
-class Crop(StartOpt):
+class Crop(StartOpt, MPDClass):
"""
"""
script_info = dict({
def __init__(self):
""""""
StartOpt.__init__(self, Crop.script_info, CROP_OPTS)
+ MPDClass.__init__(self)
self._run()
def _run(self):
""""""
- pass
-
- def crop(self):
- """"""
- NotImplemented
+ print 'Connecting %s:%i' % (self.cli_options.host, self.cli_options.port)
+ self.mpdConnect({'host':self.cli_options.host, 'port':self.cli_options.port})
+ current_pos = int(self.client.currentsong().get('pos'))
+ if current_pos <= self.cli_options.nb_tracks:
+ self.client.disconnect()
+ sys.exit(0)
+ while current_pos > self.cli_options.nb_tracks:
+ self.client.delete(0)
+ current_pos = int(self.client.currentsong().get('pos'))
+ self.client.disconnect()
+ sys.exit(0)
# Script starts here
sys.exit(1)
-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')
- 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()
- 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({'port': int(environ.get('MPD_PORT', con_id.get('port')))})
- return (con_id, passwd)#}}}
-
-
class MPDClass(object):
"""Connect to MPD server
"""
def mpdConnect(self, con_id):#{{{
"""
Simple wrapper to connect MPD.
+ con_id = {'host': 'MPD_server', 'port': 'MPD_server port'}
"""
try:
self.client.connect(**con_id)
return False
return True#}}}
- def connect(self):
- """Connect & auth"""
- pass
def main():
pass
--- /dev/null
+# -*- 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
+