From fda997b6a4107767ccff761df343bdbf1e0eef87 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 23 Feb 2010 19:46:56 +0100 Subject: [PATCH] Add BaseStoredPlaylistController.get_by_name() --- mopidy/backends/__init__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mopidy/backends/__init__.py b/mopidy/backends/__init__.py index f0d700c9..e10dd120 100644 --- a/mopidy/backends/__init__.py +++ b/mopidy/backends/__init__.py @@ -430,6 +430,10 @@ class BaseStoredPlaylistsController(object): """List of :class:`mopidy.models.Playlist`.""" return copy(self._playlists) + @playlists.setter + def playlists(self, playlists): + self._playlists = playlists + def create(self, name): """ Create a new playlist. @@ -449,6 +453,24 @@ class BaseStoredPlaylistsController(object): """ raise NotImplementedError + def get_by_name(self, name): + """ + Get playlist with given name from the set of stored playlists. + + Raises :exc:`KeyError` if not a unique match is found. + + :param name: playlist name + :type name: string + :rtype: :class:`mopidy.models.Playlist` + """ + matches = filter(lambda p: name == p.name, self._playlists) + if len(matches) == 1: + return matches[0] + elif len(matches) == 0: + raise KeyError('Name "%s" not found' % name) + else: + raise KeyError('Name "%s" matched multiple elements' % name) + def lookup(self, uri): """ Lookup playlist with given URI in both the set of stored playlists and