From 00b2b9538e6a1d3dc9c1b938eed603f10127bbc8 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Sun, 1 Mar 2015 22:46:18 +0100 Subject: [PATCH] core: Add library.list_distinct for getting distinct field values --- docs/changelog.rst | 3 +++ mopidy/backend.py | 10 ++++++++++ mopidy/core/library.py | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 733d122c..03b25897 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -36,6 +36,9 @@ v0.20.0 (UNRELEASED) - When seeking in paused state, do not change to playing state. (Fixed :issue:`939`) +- Add :meth:`mopidy.core.LibraryController.list_distinct` for getting unique + values for a given field. (Fixes: :issue:`913`) + **Commands** - Make the ``mopidy`` command print a friendly error message if the diff --git a/mopidy/backend.py b/mopidy/backend.py index c713d083..38d4c5db 100644 --- a/mopidy/backend.py +++ b/mopidy/backend.py @@ -92,6 +92,16 @@ class LibraryProvider(object): """ return [] + def list_distinct(self, field, query=None): + """ + See :meth:`mopidy.core.LibraryController.list_distinct`. + + *MAY be implemented by subclass.* + + Default implementation will simply return an empty set. + """ + return set() + def get_images(self, uris): """ See :meth:`mopidy.core.LibraryController.get_images`. diff --git a/mopidy/core/library.py b/mopidy/core/library.py index 822836a6..4ccfd657 100644 --- a/mopidy/core/library.py +++ b/mopidy/core/library.py @@ -72,6 +72,27 @@ class LibraryController(object): return [] return backend.library.browse(uri).get() + def list_distinct(self, field, query=None): + """ + List distinct values for a given field from the library. + + This has mainly been added to support the list commands the MPD + protocol supports in a more sane fashion. Other frontends are not + recommended to use this method. + + :param string field: One of ``artist``, ``albumartist``, ``album``, + ``composer``, ``performer``, ``date``or ``genre``. + :param dict query: Query to use for limiting results, see + :method:`search` for details about the query format. + :rtype: set of values corresponding to the requested field type. + """ + futures = [b.library.list_distinct(field, query) + for b in self.backends.with_library.values()] + result = set() + for r in pykka.get_all(futures): + result.update(r) + return result + def get_images(self, uris): """Lookup the images for the given URIs