From 4f0a708411dd22a395bf6876b61064f7da479ec1 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 13 Nov 2012 11:34:48 +0100 Subject: [PATCH] mpd: Allow 'file' key to 'search' and 'find' --- docs/changes.rst | 3 +++ mopidy/frontends/mpd/protocol/music_db.py | 12 ++++++---- tests/frontends/mpd/protocol/music_db_test.py | 24 +++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/docs/changes.rst b/docs/changes.rst index 81a27d33..cd8fd814 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -99,6 +99,9 @@ backends: - The settings validator will now allow any setting prefixed with ``CUSTOM_`` to exist in the settings file. +- The MPD commands ``search`` and ``find`` now allows the key ``file``, which + is used by ncmpcpp instead of ``filename``. + **Bug fixes** - :issue:`218`: The MPD commands ``listplaylist`` and ``listplaylistinfo`` now diff --git a/mopidy/frontends/mpd/protocol/music_db.py b/mopidy/frontends/mpd/protocol/music_db.py index 49c52d34..d867de58 100644 --- a/mopidy/frontends/mpd/protocol/music_db.py +++ b/mopidy/frontends/mpd/protocol/music_db.py @@ -13,10 +13,10 @@ def _build_query(mpd_query): Parses a MPD query string and converts it to the Mopidy query format. """ query_pattern = ( - r'"?(?:[Aa]lbum|[Aa]rtist|[Ff]ilename|[Tt]itle|[Aa]ny)"? "[^"]+"') + r'"?(?:[Aa]lbum|[Aa]rtist|[Ff]ile[name]*|[Tt]itle|[Aa]ny)"? "[^"]+"') query_parts = re.findall(query_pattern, mpd_query) query_part_pattern = ( - r'"?(?P([Aa]lbum|[Aa]rtist|[Ff]ilename|[Tt]itle|[Aa]ny))"? ' + r'"?(?P([Aa]lbum|[Aa]rtist|[Ff]ile[name]*|[Tt]itle|[Aa]ny))"? ' r'"(?P[^"]+)"') query = {} for query_part in query_parts: @@ -24,6 +24,8 @@ def _build_query(mpd_query): field = m.groupdict()['field'].lower() if field == 'title': field = 'track' + elif field == 'file': + field = 'filename' field = str(field) # Needed for kwargs keys on OS X and Windows what = m.groupdict()['what'].lower() if field in query: @@ -47,7 +49,7 @@ def count(context, tag, needle): @handle_request( - r'^find (?P("?([Aa]lbum|[Aa]rtist|[Dd]ate|[Ff]ilename|' + r'^find (?P("?([Aa]lbum|[Aa]rtist|[Dd]ate|[Ff]ile[name]*|' r'[Tt]itle|[Aa]ny)"? "[^"]+"\s?)+)$') def find(context, mpd_query): """ @@ -72,6 +74,7 @@ def find(context, mpd_query): *ncmpcpp:* - also uses the search type "date". + - uses "file" instead of "filename". """ query = _build_query(mpd_query) return playlist_to_mpd_format( @@ -320,7 +323,7 @@ def rescan(context, uri=None): @handle_request( - r'^search (?P("?([Aa]lbum|[Aa]rtist|[Dd]ate|[Ff]ilename|' + r'^search (?P("?([Aa]lbum|[Aa]rtist|[Dd]ate|[Ff]ile[name]*|' r'[Tt]itle|[Aa]ny)"? "[^"]+"\s?)+)$') def search(context, mpd_query): """ @@ -348,6 +351,7 @@ def search(context, mpd_query): *ncmpcpp:* - also uses the search type "date". + - uses "file" instead of "filename". """ query = _build_query(mpd_query) return playlist_to_mpd_format( diff --git a/tests/frontends/mpd/protocol/music_db_test.py b/tests/frontends/mpd/protocol/music_db_test.py index e1c571f5..7059c855 100644 --- a/tests/frontends/mpd/protocol/music_db_test.py +++ b/tests/frontends/mpd/protocol/music_db_test.py @@ -75,6 +75,22 @@ class MusicDatabaseFindTest(protocol.BaseTestCase): self.sendRequest('find artist "what"') self.assertInResponse('OK') + def test_find_filename(self): + self.sendRequest('find "filename" "afilename"') + self.assertInResponse('OK') + + def test_find_filename_without_quotes(self): + self.sendRequest('find filename "afilename"') + self.assertInResponse('OK') + + def test_find_file(self): + self.sendRequest('find "file" "afilename"') + self.assertInResponse('OK') + + def test_find_file_without_quotes(self): + self.sendRequest('find file "afilename"') + self.assertInResponse('OK') + def test_find_title(self): self.sendRequest('find "title" "what"') self.assertInResponse('OK') @@ -313,6 +329,14 @@ class MusicDatabaseSearchTest(protocol.BaseTestCase): self.sendRequest('search filename "afilename"') self.assertInResponse('OK') + def test_search_file(self): + self.sendRequest('search "file" "afilename"') + self.assertInResponse('OK') + + def test_search_file_without_quotes(self): + self.sendRequest('search file "afilename"') + self.assertInResponse('OK') + def test_search_title(self): self.sendRequest('search "title" "atitle"') self.assertInResponse('OK')