]> kaliko git repositories - mpd-goodies.git/blobdiff - bin/mcrop
Massive refactoring
[mpd-goodies.git] / bin / mcrop
similarity index 55%
rename from src/crop
rename to bin/mcrop
index 4829a65faf2e82a9d924ec1d2b4a7c06647b823b..ad2fecf8ea5affa2bc48d34480f7981f1e1ef4cc 100755 (executable)
--- a/src/crop
+++ b/bin/mcrop
@@ -1,7 +1,7 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
-# Copyright (c) 2009, 2010, 2012 Kaliko Jack <efrim@azylum.org>
+# Copyright (c) 2009,2010,2012,2019 kaliko <kaliko@azylum.org>
 #
 #   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
 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+import argparse
 import sys
 
-from lib.goodies import Goodie
+from os.path import basename
 
-NAME = 'crop'
-VERSION = '0.1'
-USAGE = 'USAGE:  %prog [--help] | [ <n> ]'
+import musicpd
 
+VERSION = '0.2'
 
-class Crop(Goodie):
-    """
-    """
+
+class Crop(musicpd.MPDClient):
     script_info = dict({
-        'version': VERSION,
-        'prog_name': NAME,
+        'prog': basename(__file__),
         'description': 'Keep <n> tracks before currently played, removed others. Default is to keep 6 tracks.',
-        'usage': USAGE,
-        })
+        'epilog': 'Set MPD host/port in env. var'
+    })
 
     def __init__(self):
         """"""
-        Goodie.__init__(self, self.__class__.script_info)
+        musicpd.MPDClient.__init__(self)
         self.nb_tracks = 6
-        self._get_arg()
+        self._get_args()
         self._run()
 
-    def _get_arg(self):
+    def _get_args(self):
         """"""
-        if not self.cli_args:
-            return True
-        try:
-            self.nb_tracks = int(self.cli_args[0])
-        except ValueError, err:
-            self.parser.error('invalid argument, not a natural number? (%s)' % err)
+        parser = argparse.ArgumentParser(**self.__class__.script_info)
+        parser.add_argument('--version', action='version',
+                            version='v%s' % VERSION)
+        parser.add_argument('n', type=int, nargs='?',
+                            help='how many titles to crop')
+        args = parser.parse_args()
+        if args.n:
+            self.nb_tracks = args.n
 
     def _run(self):
         """"""
-        print 'Connecting %s:%i' % (self.cli_options.host, self.cli_options.port)
-        print 'Keeping %i tracks' % self.nb_tracks
-        self.mpdConnect()
-        current_pos = int(self.client.currentsong().get('pos', 0))
+        self.connect()
+        current_pos = int(self.currentsong().get('pos', 0))
         if current_pos <= self.nb_tracks:
-            self.client.disconnect()
+            self.disconnect()
             sys.exit(0)
+        print('Keeping %i tracks' % self.nb_tracks)
         while current_pos > self.nb_tracks:
-            self.client.delete(0)
-            current_pos = int(self.client.currentsong().get('pos', 0))
-        self.client.disconnect()
+            self.delete(0)
+            current_pos = int(self.currentsong().get('pos', 0))
+        self.disconnect()
         sys.exit(0)