From 1cb3b44761a6b51dd8ac5a0c8279e46e41b91847 Mon Sep 17 00:00:00 2001 From: kaliko Date: Fri, 11 Jun 2010 14:54:02 +0000 Subject: [PATCH] * crop fully functional * StarOpt/MPDClass ready for new command --- crop | 28 ++++++++------ lib/mpdclass.py | 26 +------------ lib/startop.py | 100 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 37 deletions(-) create mode 100644 lib/startop.py diff --git a/crop b/crop index 70e01ea..4a77bbb 100755 --- a/crop +++ b/crop @@ -26,21 +26,18 @@ from lib.startop import StartOpt NAME = 'crop' VERSION = '0.1' -USAGE = """Usage: - -crop -S -n -""" 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': '', + 'help': 'keep tracks before currently played.'}, ]) -class Crop(StartOpt): +class Crop(StartOpt, MPDClass): """ """ script_info = dict({ @@ -52,15 +49,22 @@ class Crop(StartOpt): 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 diff --git a/lib/mpdclass.py b/lib/mpdclass.py index 40765fb..5a8f4f9 100755 --- a/lib/mpdclass.py +++ b/lib/mpdclass.py @@ -11,28 +11,6 @@ except ImportError, err: 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 """ @@ -43,6 +21,7 @@ class MPDClass(object): 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) @@ -58,9 +37,6 @@ class MPDClass(object): return False return True#}}} - def connect(self): - """Connect & auth""" - pass def main(): pass diff --git a/lib/startop.py b/lib/startop.py new file mode 100644 index 0000000..0ede89a --- /dev/null +++ b/lib/startop.py @@ -0,0 +1,100 @@ +# -*- 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 + -- 2.39.5