From 6ac2c249b52d1a076aca3308f9caf52601bc2221 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Sat, 15 Dec 2012 00:33:11 +0100 Subject: [PATCH] mpd: Add 'findadd' command --- docs/changes.rst | 2 ++ mopidy/frontends/mpd/protocol/music_db.py | 30 +++++++++++-------- tests/frontends/mpd/protocol/music_db_test.py | 10 ++++++- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/docs/changes.rst b/docs/changes.rst index 300af3d3..4224c5d2 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -17,6 +17,8 @@ v0.11.0 (in development) - Add support for loading a range of tracks from a playlist to the ``load`` command, as added in MPD 0.17. +- Add support for the ``findadd`` command. + v0.10.0 (2012-12-12) ==================== diff --git a/mopidy/frontends/mpd/protocol/music_db.py b/mopidy/frontends/mpd/protocol/music_db.py index 00b9ec00..91f0dcf4 100644 --- a/mopidy/frontends/mpd/protocol/music_db.py +++ b/mopidy/frontends/mpd/protocol/music_db.py @@ -51,7 +51,8 @@ def count(context, tag, needle): @handle_request( - r'^find (?P("?([Aa]lbum|[Aa]rtist|[Dd]ate|[Ff]ile[name]*|' + r'^find ' + r'(?P("?([Aa]lbum|[Aa]rtist|[Dd]ate|[Ff]ile[name]*|' r'[Tt]itle|[Aa]ny)"? "[^"]*"\s?)+)$') def find(context, mpd_query): """ @@ -59,8 +60,10 @@ def find(context, mpd_query): ``find {TYPE} {WHAT}`` - Finds songs in the db that are exactly ``WHAT``. ``TYPE`` should be - ``album``, ``artist``, or ``title``. ``WHAT`` is what to find. + Finds songs in the db that are exactly ``WHAT``. ``TYPE`` can be any + tag supported by MPD, or one of the two special parameters - ``file`` + to search by full path (relative to database root), and ``any`` to + match against all available tags. ``WHAT`` is what to find. *GMPC:* @@ -82,26 +85,29 @@ def find(context, mpd_query): query = _build_query(mpd_query) except ValueError: return - return tracks_to_mpd_format( - context.core.library.find_exact(**query).get()) + result = context.core.library.find_exact(**query).get() + return tracks_to_mpd_format(result) @handle_request( r'^findadd ' - r'(?P("?([Aa]lbum|[Aa]rtist|[Ff]ilename|[Tt]itle|[Aa]ny)"? ' - r'"[^"]+"\s?)+)$') -def findadd(context, query): + r'(?P("?([Aa]lbum|[Aa]rtist|[Dd]ate|[Ff]ile[name]*|' + r'[Tt]itle|[Aa]ny)"? "[^"]*"\s?)+)$') +def findadd(context, mpd_query): """ *musicpd.org, music database section:* ``findadd {TYPE} {WHAT}`` Finds songs in the db that are exactly ``WHAT`` and adds them to - current playlist. ``TYPE`` can be any tag supported by MPD. - ``WHAT`` is what to find. + current playlist. Parameters have the same meaning as for ``find``. """ - # TODO Add result to current playlist - #result = context.find(query) + try: + query = _build_query(mpd_query) + except ValueError: + return + result = context.core.library.find_exact(**query).get() + context.core.tracklist.add(result) @handle_request( diff --git a/tests/frontends/mpd/protocol/music_db_test.py b/tests/frontends/mpd/protocol/music_db_test.py index 4539eb4c..7f50d169 100644 --- a/tests/frontends/mpd/protocol/music_db_test.py +++ b/tests/frontends/mpd/protocol/music_db_test.py @@ -13,7 +13,15 @@ class MusicDatabaseHandlerTest(protocol.BaseTestCase): self.assertInResponse('OK') def test_findadd(self): - self.sendRequest('findadd "album" "what"') + self.backend.library.dummy_find_exact_result = [ + Track(uri='dummy:a', name='A'), + ] + self.assertEqual(self.core.tracklist.length.get(), 0) + + self.sendRequest('findadd "title" "A"') + + self.assertEqual(self.core.tracklist.length.get(), 1) + self.assertEqual(self.core.tracklist.tracks.get()[0].uri, 'dummy:a') self.assertInResponse('OK') def test_listall(self):