Skip to content

Commit

Permalink
core: Warn if backend does not implement as_list()
Browse files Browse the repository at this point in the history
Fixes #1080
  • Loading branch information
jodal committed Mar 24, 2015
1 parent 08c7f31 commit a8e6cd2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
24 changes: 18 additions & 6 deletions mopidy/core/playlists.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, unicode_literals

import itertools
import logging
import urlparse

import pykka
Expand All @@ -10,6 +10,9 @@
from mopidy.utils.deprecation import deprecated_property


logger = logging.getLogger(__name__)


class PlaylistsController(object):
pykka_traversable = True

Expand All @@ -29,11 +32,20 @@ def as_list(self):
.. versionadded:: 1.0
"""
futures = [
b.playlists.as_list()
for b in self.backends.with_playlists.values()]
results = pykka.get_all(futures)
return list(itertools.chain(*results))
futures = {
b.actor_ref.actor_class.__name__: b.playlists.as_list()
for b in set(self.backends.with_playlists.values())}

results = []
for backend_name, future in futures.items():
try:
results.extend(future.get())
except NotImplementedError:
logger.warning(
'%s does not implement playlists.as_list(). '
'Please upgrade it.', backend_name)

return results

def get_items(self, uri):
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/core/test_playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ def setUp(self): # noqa: N802
self.sp2.lookup.return_value.get.side_effect = [self.pl2a, self.pl2b]

self.backend1 = mock.Mock()
self.backend1.actor_ref.actor_class.__name__ = 'Backend1'
self.backend1.uri_schemes.get.return_value = ['dummy1']
self.backend1.playlists = self.sp1

self.backend2 = mock.Mock()
self.backend2.actor_ref.actor_class.__name__ = 'Backend2'
self.backend2.uri_schemes.get.return_value = ['dummy2']
self.backend2.playlists = self.sp2

Expand All @@ -55,6 +57,15 @@ def test_as_list_combines_result_from_backends(self):
self.assertIn(self.plr2a, result)
self.assertIn(self.plr2b, result)

def test_as_list_ignores_backends_that_dont_support_it(self):
self.sp2.as_list.return_value.get.side_effect = NotImplementedError

result = self.core.playlists.as_list()

self.assertEqual(len(result), 2)
self.assertIn(self.plr1a, result)
self.assertIn(self.plr1b, result)

def test_get_items_selects_the_matching_backend(self):
ref = Ref.track()
self.sp2.get_items.return_value.get.return_value = [ref]
Expand Down

0 comments on commit a8e6cd2

Please sign in to comment.