From f903dca83b8628faf433c3cf3fd0b8959bcfc79f Mon Sep 17 00:00:00 2001 From: kaliko Date: Tue, 8 Dec 2009 23:08:37 +0000 Subject: [PATCH] * add new goodies mtopls * some clean up in mfade --- mfade | 30 +++++++-------- mtopls | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 15 deletions(-) create mode 100755 mtopls diff --git a/mfade b/mfade index 22d2f3a..3ed1756 100755 --- a/mfade +++ b/mfade @@ -38,9 +38,10 @@ mfade [time [min|max]] * time in seconds * min|max in percentag +when MPD is: - not palying: fade in from 0% to max over time default 10 minutes / 50% -- if playing: fade out from current volume to min over time +- playing: fade out from current volume to min over time default 10 minutes / 1/10 of current vol Manual or any external volume change will abort the script. @@ -62,9 +63,10 @@ class Sleep(object): def _consume_sopt(self): """""" + # TODO: use optparse? options = sys.argv - if len(sys.argv) >1 and sys.argv[1] in ['-h', '--help']: - print USAGE + if len(sys.argv) > 1 and sys.argv[1] in ['-h', '--help']: + sys.stderr.write(USAGE) sys.exit(1) try: self.tempo = int(options.pop(1)) @@ -72,8 +74,8 @@ class Sleep(object): except IndexError: pass except ValueError, err: - print 'Error: wrong option passed: %s' % err - print USAGE + sys.stderr.write('Error: wrong option passed: %s' % err) + sys.stdout.write(USAGE) sys.exit(1) def _run(self): @@ -84,17 +86,17 @@ class Sleep(object): if not self.volum: self.volum = self.mpd_vol / 10 if self.volum > self.mpd_vol: - print 'Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol) + sys.stderr.write('Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol)) sys.exit(1) - print 'fading down from %d%% to %d%% over %smin' % (self.mpd_vol, - self.volum, self.tempo) + sys.stdout.write('fading down from %d%% to %d%% over %smin' % (self.mpd_vol, + self.volum, self.tempo)) self.fade() self.cli.stop() if self.mpd_state in ['stop', 'pause']: if not self.volum: self.volum = int(50) - print 'fading up from 0%% to %d%% over %smin' % (self.volum, - self.tempo) + sys.stdout.write('fading up from 0%% to %d%% over %smin' % (self.volum, + self.tempo)) self.cli.setvol(0) self.mpd_vol = 0 self.cli.play() @@ -103,13 +105,13 @@ class Sleep(object): def fade(self): """""" + # TODO: handle possible lost connections span = float(self.volum - self.mpd_vol) step = span / float(60 * self.tempo) - print step vol = self.mpd_vol while 42: if int(vol) != int(self.cli.status().get('volume')): - print 'Warning: external volume change, aborting!' + sys.stdout.write('Warning: external volume change, aborting!') break vol += step self.cli.setvol(int(vol)) @@ -121,12 +123,10 @@ class Sleep(object): # Script starts here if __name__ == '__main__': - options = [14, 11] - #main(options) try: Sleep() except KeyboardInterrupt: - print 'exit' + sys.stdout.write('exit') # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab diff --git a/mtopls b/mtopls new file mode 100755 index 0000000..d447889 --- /dev/null +++ b/mtopls @@ -0,0 +1,120 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright (c) 2009 Efrim {{{ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# }}} + +""" +DOC: +""" + +import sys + +from os import (access, F_OK, W_OK) +from os.path import (dirname, isfile, join, abspath) + +from lib.mpdutils import mconnect + +USAGE = """Usage: + +mtopls [] [--help | -h | help] + + If no playlist is specifed a new one is created in /var/lib/mpd/playlist/ + named after genre of current playlist. + +""" + +class MtoPls(object): + """ + """ + + def __init__(self): + """""" + self.pls_path = None + self.cli = mconnect() + self.current = self.cli.currentsong() + self.cli.disconnect() + print self.current + self._consume_sopt() + self._create_playlist() + self._controls_perm() + self._run() + + def _consume_sopt(self): + """""" + if len(sys.argv) > 2 or \ + len(sys.argv) == 2 and \ + sys.argv[1] in ['-h', '--help', 'help']: + sys.stdout.write(USAGE) + sys.exit(1) + if len(sys.argv) == 2: + self.pls_path = sys.argv[1] + if not dirname(self.pls_path): + self.pls_path = abspath(sys.argv[1]) + sys.stdout.write('Playlist set to "%s"\n' % self.pls_path) + return + if len(sys.argv) == 1: + self._set_playlist() + return + sys.stdout.write(USAGE) + sys.exit(1) + + def _set_playlist(self): + """Set playlist is none is given""" + + mpd_playlists = '/var/lib/mpd/playlists/' + if not access(mpd_playlists, F_OK): + sys.stderr.write('Error: No access to "%s"' % self.pls_path) + sys.exit(1) + genre = self.current.get('genre', None) + if not genre: + sys.stderr.write('Error: No genre set in %s' % + self.current.get('file')) + sys.stdout.write('Please provide a playlist.') + sys.exit(1) + genre += '.m3u' + self.pls_path = join('/var/lib/mpd/playlists/', genre) + + def _create_playlist(self): + if not isfile(self.pls_path): + sys.stdout.write('Create new playlist: %s\n' % self.pls_path) + open(self.pls_path, 'a').close() + + def _controls_perm(self): + if not access(dirname(self.pls_path), F_OK): + sys.stderr.write('Error: Not existing path: "%s"' % self.pls_path) + sys.exit(1) + if not access(self.pls_path, W_OK): + sys.stderr.write('Error: No write access to path: "%s"' % self.pls_path) + sys.exit(1) + if not isfile(self.pls_path): + sys.stderr.write('Error: Not a regular file: "%s"' % self.pls_path) + sys.exit(1) + return True + + def _run(self): + """""" + fd = open(self.pls_path, 'a') + fd.write(self.current.get('file')) + fd.close() + +# Script starts here +if __name__ == '__main__': + MtoPls() + +# VIM MODLINE +# vim: ai ts=4 sw=4 sts=4 expandtab -- 2.39.5