From acdfff5b61643ae62d5df2e141a7e6d6266bbd6f Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Sun, 7 Feb 2010 23:36:53 +0100 Subject: [PATCH] Add get_by_id() and get_by_uri() to BaseCurrentPlaylistController --- docs/api/backends.rst | 14 ++++++++++++++ mopidy/backends/__init__.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/docs/api/backends.rst b/docs/api/backends.rst index afcded4d..fe5a96e7 100644 --- a/docs/api/backends.rst +++ b/docs/api/backends.rst @@ -52,6 +52,20 @@ Clear the current playlist. + .. method:: get_by_id(id) + + Get track by ID. Raises :class:`KeyError` if not found. + + :param id: track ID + :type id: int + + .. method:: get_by_uri(uri) + + Get track by URI. Raises :class:`KeyError` if not found. + + :param uri: track URI + :type uri: string + .. method:: load(playlist) Replace the current playlist with the given playlist. diff --git a/mopidy/backends/__init__.py b/mopidy/backends/__init__.py index 99e8b2d6..416a64a0 100644 --- a/mopidy/backends/__init__.py +++ b/mopidy/backends/__init__.py @@ -37,6 +37,20 @@ class BaseCurrentPlaylistController(object): self.backend.playback.stop() self.playlist = Playlist() + def get_by_id(self, id): + matches = filter(lambda t: t.id == id, self._playlist.tracks) + if matches: + return matches[0] + else: + raise KeyError('Track with ID "%s" not found' % id) + + def get_by_uri(self, uri): + matches = filter(lambda t: t.uri == uri, self._playlist.tracks) + if matches: + return matches[0] + else: + raise KeyError('Track with URI "%s" not found' % uri) + def load(self, playlist): self.playlist = playlist self.version = 0