mpd: Limit use of fake tracks in 'find` responses

If searching for exact artist, don't include fake artist tracks.
If searching for exact album, don't include fake album tracks.

This makes sure that ncmpcpp's media library doesn't include the magic
artist-track in an artist's album listing, and that it doesn't include the
magic album-track in an album's track listing.
This commit is contained in:
Stein Magnus Jodal 2012-12-22 12:49:27 +01:00
parent 04be75ed97
commit 54662479ef
2 changed files with 54 additions and 12 deletions

View File

@ -39,13 +39,6 @@ def _artist_as_track(artist):
artists=[artist])
def _search_results_as_tracks(results):
albums = [_album_as_track(a) for a in _get_albums(results)]
artists = [_artist_as_track(a) for a in _get_artists(results)]
tracks = _get_tracks(results)
return artists + albums + tracks
@handle_request(r'^count "(?P<tag>[^"]+)" "(?P<needle>[^"]*)"$')
def count(context, tag, needle):
"""
@ -92,7 +85,13 @@ def find(context, mpd_query):
except ValueError:
return
results = context.core.library.find_exact(**query).get()
return translator.tracks_to_mpd_format(_search_results_as_tracks(results))
result_tracks = []
if 'artist' not in query:
result_tracks += [_artist_as_track(a) for a in _get_artists(results)]
if 'album' not in query:
result_tracks += [_album_as_track(a) for a in _get_albums(results)]
result_tracks += _get_tracks(results)
return translator.tracks_to_mpd_format(result_tracks)
@handle_request(r'^findadd ' + QUERY_RE)
@ -334,7 +333,10 @@ def search(context, mpd_query):
except ValueError:
return
results = context.core.library.search(**query).get()
return translator.tracks_to_mpd_format(_search_results_as_tracks(results))
artists = [_artist_as_track(a) for a in _get_artists(results)]
albums = [_album_as_track(a) for a in _get_albums(results)]
tracks = _get_tracks(results)
return translator.tracks_to_mpd_format(artists + albums + tracks)
@handle_request(r'^searchadd ' + QUERY_RE)

View File

@ -115,7 +115,7 @@ class MusicDatabaseHandlerTest(protocol.BaseTestCase):
class MusicDatabaseFindTest(protocol.BaseTestCase):
def test_find(self):
def test_find_includes_fake_artist_and_album_tracks(self):
self.backend.library.dummy_find_exact_result = SearchResult(
albums=[Album(uri='dummy:album:a', name='A', date='2001')],
artists=[Artist(uri='dummy:artist:b', name='B')],
@ -123,12 +123,52 @@ class MusicDatabaseFindTest(protocol.BaseTestCase):
self.sendRequest('find "any" "foo"')
self.assertInResponse('file: dummy:artist:b')
self.assertInResponse('Title: Artist: B')
self.assertInResponse('file: dummy:album:a')
self.assertInResponse('Title: Album: A')
self.assertInResponse('Date: 2001')
self.assertInResponse('file: dummy:artist:b')
self.assertInResponse('Title: Artist: B')
self.assertInResponse('file: dummy:track:c')
self.assertInResponse('Title: C')
self.assertInResponse('OK')
def test_find_artist_does_not_include_fake_artist_tracks(self):
self.backend.library.dummy_find_exact_result = SearchResult(
albums=[Album(uri='dummy:album:a', name='A', date='2001')],
artists=[Artist(uri='dummy:artist:b', name='B')],
tracks=[Track(uri='dummy:track:c', name='C')])
self.sendRequest('find "artist" "foo"')
self.assertNotInResponse('file: dummy:artist:b')
self.assertNotInResponse('Title: Artist: B')
self.assertInResponse('file: dummy:album:a')
self.assertInResponse('Title: Album: A')
self.assertInResponse('Date: 2001')
self.assertInResponse('file: dummy:track:c')
self.assertInResponse('Title: C')
self.assertInResponse('OK')
def test_find_artist_and_album_does_not_include_fake_tracks(self):
self.backend.library.dummy_find_exact_result = SearchResult(
albums=[Album(uri='dummy:album:a', name='A', date='2001')],
artists=[Artist(uri='dummy:artist:b', name='B')],
tracks=[Track(uri='dummy:track:c', name='C')])
self.sendRequest('find "artist" "foo" "album" "bar"')
self.assertNotInResponse('file: dummy:artist:b')
self.assertNotInResponse('Title: Artist: B')
self.assertNotInResponse('file: dummy:album:a')
self.assertNotInResponse('Title: Album: A')
self.assertNotInResponse('Date: 2001')
self.assertInResponse('file: dummy:track:c')
self.assertInResponse('Title: C')