[Pkg-mpd-commits] [python-mpd] 175/262: Better support for fetching stickers from MPD

Simon McVittie smcv at debian.org
Sun May 22 18:16:43 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 0ec1066055f0bf1a077a9ea7a27314d16161834e
Author: Jonathan Ballet <jon at multani.info>
Date:   Fri Jan 4 19:30:46 2013 +0800

    Better support for fetching stickers from MPD
    
    MPDClient now parses the output of MPD and provide a more convenient way
    of accessing stickers.
---
 mpd.py  | 21 +++++++++++++++++++--
 test.py | 29 +++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/mpd.py b/mpd.py
index a450c84..da02daf 100644
--- a/mpd.py
+++ b/mpd.py
@@ -149,10 +149,10 @@ _commands = {
     "update":             "_fetch_item",
     "rescan":             "_fetch_item",
     # Sticker Commands
-    "sticker get":        "_fetch_item",
+    "sticker get":        "_fetch_sticker",
     "sticker set":        "_fetch_nothing",
     "sticker delete":     "_fetch_nothing",
-    "sticker list":       "_fetch_list",
+    "sticker list":       "_fetch_stickers",
     "sticker find":       "_fetch_songs",
     # Connection Commands
     "close":              None,
@@ -319,6 +319,15 @@ class MPDClient(object):
             self._command_list = None
         self._fetch_nothing()
 
+    def _read_stickers(self):
+        for key, sticker in self._read_pairs():
+            value = sticker.split('=', 1)
+
+            if len(value) < 2:
+                raise ProtocolError("Could not parse sticker: %r" % sticker)
+
+            yield tuple(value)
+
     def _iterator_wrapper(self, iterator):
         try:
             for item in iterator:
@@ -343,6 +352,14 @@ class MPDClient(object):
             return
         return pairs[0][1]
 
+    def _fetch_sticker(self):
+        # Either we get one or we get an error while reading the line
+        key, value = list(self._read_stickers())[0]
+        return value
+
+    def _fetch_stickers(self):
+        return dict(self._read_stickers())
+
     def _fetch_list(self):
         return self._wrap_iterator(self._read_list())
 
diff --git a/test.py b/test.py
index 7810971..1a73257 100755
--- a/test.py
+++ b/test.py
@@ -301,5 +301,34 @@ class TestMPDClient(unittest.TestCase):
                          # otherwise we get all the readline() & co...
                          self.client._sock.makefile.call_args_list[0:2])
 
+    def test_read_stickers(self):
+        self.MPDWillReturn("sticker: foo=bar\n", "OK\n")
+        res = self.client._read_stickers()
+        self.assertEqual([('foo', 'bar')], list(res))
+
+        self.MPDWillReturn("sticker: foo=bar\n", "sticker: l=b\n", "OK\n")
+        res = self.client._read_stickers()
+        self.assertEqual([('foo', 'bar'), ('l', 'b')], list(res))
+
+    def test_parse_sticket_get_one(self):
+        self.MPDWillReturn("sticker: foo=bar\n", "OK\n")
+        res = self.client.sticker_get('song', 'baz', 'foo')
+        self.assertEqual('bar', res)
+
+    def test_parse_sticket_get_no_sticker(self):
+        self.MPDWillReturn("ACK [50 at 0] {sticker} no such sticker\n")
+        self.assertRaises(mpd.CommandError,
+                          self.client.sticker_get, 'song', 'baz', 'foo')
+
+    def test_parse_sticker_list(self):
+        self.MPDWillReturn("sticker: foo=bar\n", "sticker: lom=bok\n", "OK\n")
+        res = self.client.sticker_list('song', 'baz')
+        self.assertEqual({'foo': 'bar', 'lom': 'bok'}, res)
+
+        # Even with only one sticker, we get a dict
+        self.MPDWillReturn("sticker: foo=bar\n", "OK\n")
+        res = self.client.sticker_list('song', 'baz')
+        self.assertEqual({'foo': 'bar'}, res)
+
 if __name__ == '__main__':
     unittest.main()

-- 
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