[Pkg-mpd-commits] [python-mpd] 192/262: add support for ranges

Simon McVittie smcv at debian.org
Sun May 22 18:16:46 UTC 2016


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch upstream
in repository python-mpd.

commit 0294880632b9d2deafd34b86c3f81fbf9d71d301
Author: Jörg Thalheim <joerg at higgsboson.tk>
Date:   Mon Feb 18 19:34:03 2013 +0100

    add support for ranges
---
 README.rst | 15 +++++++++++++++
 mpd.py     |  8 +++++++-
 test.py    | 19 +++++++++++++++++++
 3 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/README.rst b/README.rst
index 2866924..8e57a76 100644
--- a/README.rst
+++ b/README.rst
@@ -164,6 +164,21 @@ the idle command::
 Some more complex usage examples can be found
 `here <http://jatreuman.indefero.net/p/python-mpd/doc/>`_
 
+Range
+-----
+
+Some commands support integer ranges as argument.
+This is done in python-mpd2 by using two element tuple::
+
+    # move the first and the second songs
+    # after the third in the playlist
+    >>> client.move((1:2), 3)
+
+Second element can be omitted. MPD will assumes the biggest possible number then (don't forget the comma!)::
+
+    >>> client.delete((2,))     # delete all songs, but the first.
+
+
 Unicode Handling
 ----------------
 
diff --git a/mpd.py b/mpd.py
index c93b4bd..7a9a028 100644
--- a/mpd.py
+++ b/mpd.py
@@ -235,7 +235,13 @@ class MPDClient(object):
     def _write_command(self, command, args=[]):
         parts = [command]
         for arg in args:
-            parts.append('"%s"' % escape(encode_str(arg)))
+            if type(arg) is tuple:
+                if len(arg) == 1:
+                    parts.append('"%d:"' % int(arg[0]))
+                else:
+                    parts.append('"%d:%d"' % (int(arg[0]), int(arg[1])))
+            else:
+                parts.append('"%s"' % escape(encode_str(arg)))
         # Minimize logging cost if the logging is not activated.
         if logger.isEnabledFor(logging.DEBUG):
             if command == "password":
diff --git a/test.py b/test.py
index f216b29..14d5f0b 100755
--- a/test.py
+++ b/test.py
@@ -301,6 +301,25 @@ class TestMPDClient(unittest.TestCase):
                          # otherwise we get all the readline() & co...
                          self.client._sock.makefile.call_args_list[0:2])
 
+    def test_ranges_as_argument(self):
+        self.MPDWillReturn('OK\n')
+        self.client.move((1,2), 2)
+        self.assertMPDReceived('move "1:2" "2"\n')
+
+        self.MPDWillReturn('OK\n')
+        self.client.move((1,), 2)
+        self.assertMPDReceived('move "1:" "2"\n')
+
+        # old code still works!
+        self.MPDWillReturn('OK\n')
+        self.client.move("1:2", 2)
+        self.assertMPDReceived('move "1:2" "2"\n')
+
+        with self.assertRaises(ValueError):
+            self.MPDWillReturn('OK\n')
+            self.client.move((1,"garbage"), 2)
+            self.assertMPDReceived('move "1:" "2"\n')
+
     def test_read_stickers(self):
         self.MPDWillReturn("sticker: foo=bar\n", "OK\n")
         res = self.client._read_stickers()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mpd/python-mpd.git



More information about the Pkg-mpd-commits mailing list