diff --git a/docs/changelog.rst b/docs/changelog.rst index 37716e96..b588011c 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -31,6 +31,10 @@ v1.0.0 (UNRELEASED) - Add :class:`mopidy.core.MixerController` which keeps track of volume and mute. (Fixes: :issue:`962`) +- Add ``uris`` argument to :method:`mopidy.core.LibraryController.lookup` + which allows for simpler lookup of multiple URIs. (Fixes: :issue:`1008`, + PR: :issue:`1047`) + - **Deprecated:** The old methods on :class:`mopidy.core.PlaybackController` for volume and mute management have been deprecated. (Fixes: :issue:`962`) diff --git a/mopidy/core/library.py b/mopidy/core/library.py index 49a4a796..f2a8b9bd 100644 --- a/mopidy/core/library.py +++ b/mopidy/core/library.py @@ -162,7 +162,7 @@ class LibraryController(object): in self._get_backends_to_uris(uris).items()] return [result for result in pykka.get_all(futures) if result] - def lookup(self, uri): + def lookup(self, uri=None, uris=None): """ Lookup the given URI. @@ -170,14 +170,39 @@ class LibraryController(object): them all. :param uri: track URI - :type uri: string - :rtype: list of :class:`mopidy.models.Track` + :type uri: string or :class:`None` + :param uris: track URIs + :type uris: list of string or :class:`None` + :rtype: list of :class:`mopidy.models.Track` if uri was set or + a {uri: list of :class:`mopidy.models.Track`} if uris was set. + + .. versionadded:: 1.0 + The ``uris`` argument. + + .. deprecated:: 1.0 + The ``uri`` argument. Use ``uris`` instead. """ - backend = self._get_backend(uri) - if backend: - return backend.library.lookup(uri).get() - else: - return [] + none_set = uri is None and uris is None + both_set = uri is not None and uris is not None + + if none_set or both_set: + raise ValueError("One of 'uri' or 'uris' must be set") + + futures = {} + result = {} + backends = self._get_backends_to_uris([uri] if uri else uris) + + # TODO: lookup(uris) to backend APIs + for backend, backend_uris in backends.items(): + for u in backend_uris or []: + futures[u] = backend.library.lookup(u) + + for u, future in futures.items(): + result[u] = future.get() + + if uri: + return result.get(uri, []) + return result def refresh(self, uri=None): """ diff --git a/tests/core/test_library.py b/tests/core/test_library.py index ccf1b349..b71e5de5 100644 --- a/tests/core/test_library.py +++ b/tests/core/test_library.py @@ -157,6 +157,17 @@ class CoreLibraryTest(unittest.TestCase): self.assertFalse(self.library1.lookup.called) self.library2.lookup.assert_called_once_with('dummy2:a') + def test_lookup_fails_with_uri_and_uris_set(self): + with self.assertRaises(ValueError): + self.core.library.lookup('dummy1:a', ['dummy2:a']) + + def test_lookup_can_handle_uris(self): + self.library1.lookup().get.return_value = [1234] + self.library2.lookup().get.return_value = [5678] + + result = self.core.library.lookup(uris=['dummy1:a', 'dummy2:a']) + self.assertEqual(result, {'dummy2:a': [5678], 'dummy1:a': [1234]}) + def test_lookup_returns_nothing_for_dummy3_track(self): result = self.core.library.lookup('dummy3:a')