Make library.lookup() return a list of tracks

This commit is contained in:
Stein Magnus Jodal 2012-11-19 21:08:41 +01:00
parent 32639ea8de
commit eab399357f
10 changed files with 39 additions and 31 deletions

View File

@ -141,6 +141,10 @@ backends:
- :meth:`mopidy.core.TracklistController.remove` can now remove multiple
tracks, and returns the tracks it removed.
- :meth:`mopidy.core.LibraryController.lookup` now returns a list of tracks.
This makes it possible to support lookup of artist or album URIs which then
can expand to a list of tracks.
**Bug fixes**
- :issue:`218`: The MPD commands ``listplaylist`` and ``listplaylistinfo`` now

View File

@ -42,9 +42,7 @@ class DummyLibraryProvider(base.BaseLibraryProvider):
return []
def lookup(self, uri):
matches = filter(lambda t: uri == t.uri, self.dummy_library)
if matches:
return matches[0]
return filter(lambda t: uri == t.uri, self.dummy_library)
def refresh(self, uri=None):
pass

View File

@ -30,10 +30,10 @@ class LocalLibraryProvider(base.BaseLibraryProvider):
def lookup(self, uri):
try:
return self._uri_mapping[uri]
return [self._uri_mapping[uri]]
except KeyError:
logger.debug('Failed to lookup %r', uri)
return None
return []
def find_exact(self, **query):
self._validate_query(query)

View File

@ -55,7 +55,7 @@ class LocalPlaylistsProvider(base.BasePlaylistsProvider):
try:
# TODO We must use core.library.lookup() to support tracks
# from other backends
tracks.append(self.backend.library.lookup(track_uri))
tracks += self.backend.library.lookup(track_uri)
except LookupError as ex:
logger.error('Playlist item could not be added: %s', ex)

View File

@ -57,10 +57,10 @@ class SpotifyLibraryProvider(base.BaseLibraryProvider):
def lookup(self, uri):
try:
return SpotifyTrack(uri)
return [SpotifyTrack(uri)]
except SpotifyError as e:
logger.debug('Failed to lookup "%s": %s', uri, e)
return None
return []
def refresh(self, uri=None):
pass # TODO

View File

@ -41,17 +41,20 @@ class LibraryController(object):
def lookup(self, uri):
"""
Lookup track with given URI. Returns :class:`None` if not found.
Lookup the given URI.
If the URI expands to multiple tracks, the returned list will contain
them all.
:param uri: track URI
:type uri: string
:rtype: :class:`mopidy.models.Track` or :class:`None`
:rtype: list of :class:`mopidy.models.Track`
"""
backend = self._get_backend(uri)
if backend:
return backend.library.lookup(uri).get()
else:
return None
return []
def refresh(self, uri=None):
"""

View File

@ -22,9 +22,9 @@ def add(context, uri):
"""
if not uri:
return
track = context.core.library.lookup(uri).get()
if track:
context.core.tracklist.add(track)
tracks = context.core.library.lookup(uri).get()
if tracks:
context.core.tracklist.append(tracks)
return
raise MpdNoExistError('directory or file not found', command='add')
@ -52,13 +52,19 @@ def addid(context, uri, songpos=None):
raise MpdNoExistError('No such song', command='addid')
if songpos is not None:
songpos = int(songpos)
track = context.core.library.lookup(uri).get()
if track is None:
tracks = context.core.library.lookup(uri).get()
if not tracks:
raise MpdNoExistError('No such song', command='addid')
if songpos and songpos > context.core.tracklist.length.get():
raise MpdArgError('Bad song index', command='addid')
tl_track = context.core.tracklist.add(track, at_position=songpos).get()
return ('Id', tl_track.tlid)
first_tl_track = None
for track in tracks:
tl_track = context.core.tracklist.add(track, at_position=songpos).get()
if songpos is not None:
songpos += 1
if first_tl_track is None:
first_tl_track = tl_track
return ('Id', first_tl_track.tlid)
@handle_request(r'^delete "(?P<start>\d+):(?P<end>\d+)*"$')

View File

@ -279,13 +279,10 @@ class MprisObject(dbus.service.Object):
return
# NOTE Check if URI has MIME type known to the backend, if MIME support
# is added to the backend.
uri_schemes = self.core.uri_schemes.get()
if not any([uri.startswith(uri_scheme) for uri_scheme in uri_schemes]):
return
track = self.core.library.lookup(uri).get()
if track is not None:
tl_track = self.core.tracklist.add(track).get()
self.core.playback.play(tl_track)
tracks = self.core.library.lookup(uri).get()
if tracks:
tl_tracks = self.core.tracklist.append(tracks).get()
self.core.playback.play(tl_tracks[0])
else:
logger.debug('Track with URI "%s" not found in library.', uri)

View File

@ -43,12 +43,12 @@ class LibraryControllerTest(object):
pass
def test_lookup(self):
track = self.library.lookup(self.tracks[0].uri)
self.assertEqual(track, self.tracks[0])
tracks = self.library.lookup(self.tracks[0].uri)
self.assertEqual(tracks, self.tracks[0:1])
def test_lookup_unknown_track(self):
track = self.library.lookup('fake uri')
self.assertEquals(track, None)
tracks = self.library.lookup('fake uri')
self.assertEqual(tracks, [])
def test_find_exact_no_hits(self):
result = self.library.find_exact(track=['unknown track'])

View File

@ -41,10 +41,10 @@ class CoreLibraryTest(unittest.TestCase):
self.assertFalse(self.library1.lookup.called)
self.library2.lookup.assert_called_once_with('dummy2:a')
def test_lookup_fails_for_dummy3_track(self):
def test_lookup_returns_nothing_for_dummy3_track(self):
result = self.core.library.lookup('dummy3:a')
self.assertIsNone(result)
self.assertEqual(result, [])
self.assertFalse(self.library1.lookup.called)
self.assertFalse(self.library2.lookup.called)