core: Use library name instead of URI scheme in browse()

This commit is contained in:
Stein Magnus Jodal 2014-01-02 23:10:09 +01:00
parent 7dba0dafa5
commit 048c3bb544
2 changed files with 44 additions and 31 deletions

View File

@ -1,6 +1,7 @@
from __future__ import unicode_literals
import collections
import re
import urlparse
import pykka
@ -62,32 +63,37 @@ class LibraryController(object):
"""
if not path.startswith('/'):
return []
backends = {
backend.library.name.get(): backend
for backend in self.backends.with_library.values()
if backend.library.browse('/').get()}
if path == '/':
library_names = [
backend.library.name.get()
for backend in self.backends.with_library.values()
if backend.library.browse('/').get()]
return [
Ref(uri='/%s' % name, name=name, type='directory')
for name in library_names]
uri_scheme = path.split('/', 2)[1]
backend = self.backends.with_library.get(uri_scheme, None)
if backend:
backend_path = path.replace('/%s' % uri_scheme, '')
if not backend_path.startswith('/'):
backend_path = '/%s' % backend_path
refs = backend.library.browse(backend_path).get()
result = []
for ref in refs:
if ref.type == 'directory':
result.append(
ref.copy(uri='/%s%s' % (uri_scheme, ref.uri)))
else:
result.append(ref)
return result
else:
for name in backends.keys()]
groups = re.match('/(?P<library>[^/]+)(?P<path>.*)', path).groupdict()
library_name = groups['library']
backend_path = groups['path']
if not backend_path.startswith('/'):
backend_path = '/%s' % backend_path
backend = backends.get(library_name, None)
if not backend:
return []
refs = backend.library.browse(backend_path).get()
result = []
for ref in refs:
if ref.type == 'directory':
result.append(
ref.copy(uri='/%s%s' % (library_name, ref.uri)))
else:
result.append(ref)
return result
def find_exact(self, query=None, uris=None, **kwargs):
"""
Search the library for tracks where ``field`` is ``values``.

View File

@ -31,11 +31,10 @@ class CoreLibraryTest(unittest.TestCase):
self.backend1, self.backend2, self.backend3])
def test_browse_root_returns_dir_ref_for_each_library_with_content(self):
result1 = [
self.library1.browse().get.return_value = [
Ref(uri='/foo/bar', name='bar', type='directory'),
Ref(uri='dummy1:/foo/baz.mp3', name='Baz', type='track'),
]
self.library1.browse().get.return_value = result1
self.library1.browse.reset_mock()
self.library2.browse().get.return_value = []
self.library2.browse.reset_mock()
@ -57,29 +56,37 @@ class CoreLibraryTest(unittest.TestCase):
self.assertFalse(self.library2.browse.called)
def test_browse_dummy1_selects_dummy1_backend(self):
self.library1.browse().get.return_value = []
self.library1.browse().get.return_value = [
Ref(uri='/foo/bar', name='bar', type='directory'),
Ref(uri='dummy1:/foo/baz.mp3', name='Baz', type='track'),
]
self.library1.browse.reset_mock()
self.core.library.browse('/dummy1/foo')
self.library1.browse.assert_called_once_with('/foo')
self.assertFalse(self.library2.browse.called)
self.assertEqual(self.library1.browse.call_count, 2)
self.assertEqual(self.library2.browse.call_count, 1)
self.library1.browse.assert_called_with('/foo')
def test_browse_dummy2_selects_dummy2_backend(self):
self.library2.browse().get.return_value = []
self.library2.browse().get.return_value = [
Ref(uri='/bar/quux', name='quux', type='directory'),
Ref(uri='dummy2:/foo/baz.mp3', name='Baz', type='track'),
]
self.library2.browse.reset_mock()
self.core.library.browse('/dummy2/bar')
self.assertFalse(self.library1.browse.called)
self.library2.browse.assert_called_once_with('/bar')
self.assertEqual(self.library1.browse.call_count, 1)
self.assertEqual(self.library2.browse.call_count, 2)
self.library2.browse.assert_called_with('/bar')
def test_browse_dummy3_returns_nothing(self):
result = self.core.library.browse('/dummy3')
self.assertEqual(result, [])
self.assertFalse(self.library1.browse.called)
self.assertFalse(self.library2.browse.called)
self.assertEqual(self.library1.browse.call_count, 1)
self.assertEqual(self.library2.browse.call_count, 1)
def test_browse_dir_returns_subdirs_and_tracks(self):
result1 = [