models: Add Ref.type constants

This commit is contained in:
Stein Magnus Jodal 2014-01-03 22:16:22 +01:00
parent 048c3bb544
commit af3b0e40fd
5 changed files with 40 additions and 18 deletions

View File

@ -46,13 +46,13 @@ class LibraryController(object):
track's original URI. A matching pair of objects can look like this::
Track(uri='dummy:/foo.mp3', name='foo', artists=..., album=...)
Ref(uri='dummy:/foo.mp3', name='foo', type='track')
Ref(uri='dummy:/foo.mp3', name='foo', type=Ref.TRACK)
The :class:`~mopidy.models.Ref` objects representing directories has
plain paths, not including any URI schema. For example, the dummy
library's ``/bar`` directory is returned like this::
Ref(uri='/dummy/bar', name='bar', type='directory')
Ref(uri='/dummy/bar', name='bar', type=Ref.DIRECTORY)
Note to backend implementors: The ``/dummy`` part of the URI is added
by Mopidy core, not the individual backends.
@ -71,7 +71,7 @@ class LibraryController(object):
if path == '/':
return [
Ref(uri='/%s' % name, name=name, type='directory')
Ref(uri='/%s' % name, name=name, type=Ref.DIRECTORY)
for name in backends.keys()]
groups = re.match('/(?P<library>[^/]+)(?P<path>.*)', path).groupdict()
@ -87,7 +87,7 @@ class LibraryController(object):
refs = backend.library.browse(backend_path).get()
result = []
for ref in refs:
if ref.type == 'directory':
if ref.type == Ref.DIRECTORY:
result.append(
ref.copy(uri='/%s%s' % (library_name, ref.uri)))
else:

View File

@ -160,6 +160,21 @@ class Ref(ImmutableObject):
#: "directory". Read-only.
type = None
#: Constant used for comparision with the :attr:`type` field.
ALBUM = 'album'
#: Constant used for comparision with the :attr:`type` field.
ARTIST = 'artist'
#: Constant used for comparision with the :attr:`type` field.
DIRECTORY = 'directory'
#: Constant used for comparision with the :attr:`type` field.
PLAYLIST = 'playlist'
#: Constant used for comparision with the :attr:`type` field.
TRACK = 'track'
class Artist(ImmutableObject):
"""

View File

@ -32,8 +32,8 @@ class CoreLibraryTest(unittest.TestCase):
def test_browse_root_returns_dir_ref_for_each_library_with_content(self):
self.library1.browse().get.return_value = [
Ref(uri='/foo/bar', name='bar', type='directory'),
Ref(uri='dummy1:/foo/baz.mp3', name='Baz', type='track'),
Ref(uri='/foo/bar', name='bar', type=Ref.DIRECTORY),
Ref(uri='dummy1:/foo/baz.mp3', name='Baz', type=Ref.TRACK),
]
self.library1.browse.reset_mock()
self.library2.browse().get.return_value = []
@ -42,7 +42,7 @@ class CoreLibraryTest(unittest.TestCase):
result = self.core.library.browse('/')
self.assertEqual(result, [
Ref(uri='/dummy1', name='dummy1', type='directory'),
Ref(uri='/dummy1', name='dummy1', type=Ref.DIRECTORY),
])
self.assertTrue(self.library1.browse.called)
self.assertTrue(self.library2.browse.called)
@ -57,8 +57,8 @@ class CoreLibraryTest(unittest.TestCase):
def test_browse_dummy1_selects_dummy1_backend(self):
self.library1.browse().get.return_value = [
Ref(uri='/foo/bar', name='bar', type='directory'),
Ref(uri='dummy1:/foo/baz.mp3', name='Baz', type='track'),
Ref(uri='/foo/bar', name='bar', type=Ref.DIRECTORY),
Ref(uri='dummy1:/foo/baz.mp3', name='Baz', type=Ref.TRACK),
]
self.library1.browse.reset_mock()
@ -70,8 +70,8 @@ class CoreLibraryTest(unittest.TestCase):
def test_browse_dummy2_selects_dummy2_backend(self):
self.library2.browse().get.return_value = [
Ref(uri='/bar/quux', name='quux', type='directory'),
Ref(uri='dummy2:/foo/baz.mp3', name='Baz', type='track'),
Ref(uri='/bar/quux', name='quux', type=Ref.DIRECTORY),
Ref(uri='dummy2:/foo/baz.mp3', name='Baz', type=Ref.TRACK),
]
self.library2.browse.reset_mock()
@ -90,8 +90,8 @@ class CoreLibraryTest(unittest.TestCase):
def test_browse_dir_returns_subdirs_and_tracks(self):
result1 = [
Ref(uri='/foo/bar', name='bar', type='directory'),
Ref(uri='dummy1:/foo/baz.mp3', name='Baz', type='track'),
Ref(uri='/foo/bar', name='bar', type=Ref.DIRECTORY),
Ref(uri='dummy1:/foo/baz.mp3', name='Baz', type=Ref.TRACK),
]
self.library1.browse().get.return_value = result1
self.library1.browse.reset_mock()
@ -99,8 +99,8 @@ class CoreLibraryTest(unittest.TestCase):
result = self.core.library.browse('/dummy1/foo')
self.assertEqual(result, [
Ref(uri='/dummy1/foo/bar', name='bar', type='directory'),
Ref(uri='dummy1:/foo/baz.mp3', name='Baz', type='track'),
Ref(uri='/dummy1/foo/bar', name='bar', type=Ref.DIRECTORY),
Ref(uri='dummy1:/foo/baz.mp3', name='Baz', type=Ref.TRACK),
])
def test_lookup_selects_dummy1_backend(self):

View File

@ -87,6 +87,13 @@ class RefTest(unittest.TestCase):
ref2 = json.loads(serialized, object_hook=model_json_decoder)
self.assertEqual(ref1, ref2)
def test_type_constants(self):
self.assertEqual(Ref.ALBUM, 'album')
self.assertEqual(Ref.ARTIST, 'artist')
self.assertEqual(Ref.DIRECTORY, 'directory')
self.assertEqual(Ref.PLAYLIST, 'playlist')
self.assertEqual(Ref.TRACK, 'track')
class ArtistTest(unittest.TestCase):
def test_uri(self):

View File

@ -170,7 +170,7 @@ class MusicDatabaseHandlerTest(protocol.BaseTestCase):
def test_lsinfo_for_root_includes_dirs_for_each_lib_with_content(self):
self.backend.library.dummy_browse_result = [
Ref(uri='dummy:/a', name='a', type='track'),
Ref(uri='/foo', name='foo', type='directory'),
Ref(uri='/foo', name='foo', type=Ref.DIRECTORY),
]
self.sendRequest('lsinfo "/"')
@ -180,7 +180,7 @@ class MusicDatabaseHandlerTest(protocol.BaseTestCase):
def test_lsinfo_for_dir_with_and_without_leading_slash_is_the_same(self):
self.backend.library.dummy_browse_result = [
Ref(uri='dummy:/a', name='a', type='track'),
Ref(uri='/foo', name='foo', type='directory'),
Ref(uri='/foo', name='foo', type=Ref.DIRECTORY),
]
response1 = self.sendRequest('lsinfo "dummy"')
@ -202,7 +202,7 @@ class MusicDatabaseHandlerTest(protocol.BaseTestCase):
def test_lsinfo_for_dir_includes_subdirs(self):
self.backend.library.dummy_browse_result = [
Ref(uri='/foo', name='foo', type='directory'),
Ref(uri='/foo', name='foo', type=Ref.DIRECTORY),
]
self.sendRequest('lsinfo "/dummy"')