]> kaliko git repositories - mpd-goodies.git/commitdiff
* [mfade] convert mfade to use the new framework
authorkaliko <kaliko@azylum.org>
Fri, 11 Jun 2010 16:28:37 +0000 (16:28 +0000)
committerkaliko <kaliko@azylum.org>
Fri, 11 Jun 2010 16:28:37 +0000 (16:28 +0000)
 * [crop] updates crop acoording to changes in MPDClass
 * [mpdclass.py] some changes

crop
lib/mpdclass.py
lib/startop.py
mfade

diff --git a/crop b/crop
index 4a77bbbdcdccc2a3ff006b8d95b9ec1807d082b6..9a08eb1e0b2c743f9e784e1850a4b3e4330db5d5 100755 (executable)
--- a/crop
+++ b/crop
@@ -32,7 +32,7 @@ CROP_OPTS = list([
         'sw': ['-n', '--nbtracks'],
         'type': 'int',
         'dest': 'nb_tracks',
-        'default': 60,
+        'default': 6,
         'metavar': '<n>',
         'help': 'keep <n> tracks before currently played.'},
     ])
@@ -42,20 +42,20 @@ class Crop(StartOpt, MPDClass):
     """
     script_info = dict({
         'version': VERSION,
-        'prog_name': 'crop',
+        'prog_name': NAME,
         'description': 'Keep <n> tracks before currently played, removed others.',
         })
 
     def __init__(self):
         """"""
-        StartOpt.__init__(self, Crop.script_info, CROP_OPTS)
+        StartOpt.__init__(self, self.__class__.script_info, CROP_OPTS)
         MPDClass.__init__(self)
         self._run()
 
     def _run(self):
         """"""
         print 'Connecting %s:%i' % (self.cli_options.host, self.cli_options.port)
-        self.mpdConnect({'host':self.cli_options.host, 'port':self.cli_options.port})
+        self.mpdConnect()
         current_pos = int(self.client.currentsong().get('pos'))
         if current_pos <=  self.cli_options.nb_tracks:
             self.client.disconnect()
index 5a8f4f9f127fac9c24bd4188ace42f38990dd831..63a2f06d3a87355bd59ae63f7b1ab245717f0634 100755 (executable)
@@ -18,11 +18,11 @@ class MPDClass(object):
     def __init__(self):
         self.client = MPDClient()
 
-    def mpdConnect(self, con_id):#{{{
+    def mpdConnect(self):#{{{
         """
         Simple wrapper to connect MPD.
-        con_id = {'host': 'MPD_server', 'port': 'MPD_server port'}
         """
+        con_id = dict({'host':self.cli_options.host, 'port':self.cli_options.port})
         try:
             self.client.connect(**con_id)
         except SocketError:
index 0ede89a20d0b2900564626796903ba459973763d..44cb64a00a25ef7b8b4f0fe7a45a2e02ec5efdb7 100644 (file)
@@ -66,8 +66,12 @@ class StartOpt(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,
+                                   usage=usage,
                                    prog=prog,
                                    description=des)
         con_id, passwd = get_mpd_environ()
diff --git a/mfade b/mfade
index 6b8616b56e7f529abef6cd24dd3808c3bac44a77..6666c8d4ca03c9f37697d49ee4295b744f9303eb 100755 (executable)
--- a/mfade
+++ b/mfade
@@ -29,94 +29,87 @@ import sys
 
 from time import sleep
 
-from lib.mpdutils import mconnect
-
-USAGE = """Usage:
-
-mfade [time [min|max]]
-
-   * time in seconds
-   * min|max in percentage of volume
-
-when MPD is:
-- not playing: fade in from 0% to max over time
-               default 10 minutes / 50%
--     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.
-"""
-
-
-class Sleep(object):
-    """
-    """
-
-    def __init__(self):
+from lib.mpdclass import MPDClass
+from lib.startop import StartOpt
+
+NAME = 'mfade'
+VERSION = '0.1'
+USAGE = 'USAGE:  %prog [--help] [options] [<time> [<final volume level>]'
+DESC = """Fade in/out to <final volume level> over <time>. From 0% to 50% when
+paused or stopped and from current volume to 10th of it if playing, both over 10
+minutes."""
+
+
+class Sleep(StartOpt, MPDClass):
+    """"""
+    script_info = dict({
+        'version': VERSION,
+        'prog_name': NAME,
+        'description': DESC,
+        'usage': USAGE,
+        })
+
+    def __init__(self):#{{{
         """"""
+        StartOpt.__init__(self, self.__class__.script_info, [])
+        MPDClass.__init__(self)
         self.tempo = int(10)
         self.volum = None
-        self._consume_sopt()
-        self.cli = mconnect()
-        self._run()
-        self.cli.disconnect()
+        self._consume_args()
+        self._run()#}}}
 
-    def _consume_sopt(self):
+    def _consume_args(self):#{{{
         """"""
-        # TODO: use optparse?
-        options = sys.argv
-        if len(sys.argv) > 1 and sys.argv[1] in ['-h', '--help']:
-            sys.stderr.write(USAGE)
-            sys.exit(1)
+        if (len(self.cli_args) < 1 or
+                len(self.cli_args) > 2):
+            self.parser.error('%s requires at least an argument and no more than two!')
         try:
-            self.tempo = int(options.pop(1))
-            self.volum = int(options.pop(1))
+            self.tempo = int(self.cli_args[0])
+            self.volum = int(self.cli_args[1])
         except IndexError:
             pass
         except ValueError, err:
-            sys.stderr.write('Error: wrong option passed: %s' % err)
-            sys.stdout.write(USAGE)
-            sys.exit(1)
+            self.parser.error('wrong option passed (%s)' % err)#}}}
 
-    def _run(self):
+    def _run(self):#{{{
         """"""
-        self.mpd_state = str(self.cli.status().get('state'))
-        self.mpd_vol = int(self.cli.status().get('volume'))
+        self.mpdConnect()
+        self.mpd_state = str(self.client.status().get('state'))
+        self.mpd_vol = int(self.client.status().get('volume'))
         if self.mpd_state == 'play':
             if not self.volum:
                 self.volum = self.mpd_vol / 10
             if 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)
+                self.parser.error('Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol))
             print >> sys.stdout, 'fading down from %d%% to %d%% over %smin' % (self.mpd_vol, self.volum, self.tempo)
             if self.fade():
-                self.cli.stop()
+                self.client.stop()
         if self.mpd_state in ['stop', 'pause']:
             if not self.volum:
                 self.volum = int(50)
             print >> sys.stdout, 'fading up from 0%% to %d%% over %smin' % (self.volum, self.tempo)
-            self.cli.setvol(0)
+            self.client.setvol(0)
             self.mpd_vol = 0
-            self.cli.play()
+            self.client.play()
             self.fade()
-        sleep(1)
+        sleep(1)#}}}
 
-    def fade(self):
+    def fade(self):#{{{
         """"""
         # TODO: handle possible lost connections
         span = float(self.volum - self.mpd_vol)
         step = span / float(60 * self.tempo)
         vol = self.mpd_vol
         while 42:
-            if int(vol) != int(self.cli.status().get('volume')):
+            if int(vol) != int(self.client.status().get('volume')):
                 sys.stderr.write('Warning: external volume change, aborting!\n')
                 return False
             vol += step
-            self.cli.setvol(int(vol))
+            self.client.setvol(int(vol))
             if abs(vol - self.volum) < 1:
-                self.cli.setvol(self.volum)
+                self.client.setvol(self.volum)
                 return True
-            sleep(1)
+            sleep(1)#}}}
 
 
 # Script starts here