Make local stored playlists set and use URIs (#217)

This commit is contained in:
Stein Magnus Jodal 2012-10-31 11:39:13 +01:00
parent d8378e9284
commit 51aab4f138
3 changed files with 57 additions and 42 deletions

View File

@ -20,7 +20,9 @@ class LocalStoredPlaylistsProvider(base.BaseStoredPlaylistsProvider):
self.refresh()
def lookup(self, uri):
pass # TODO
for playlist in self._playlists:
if playlist.uri == uri:
return playlist
def refresh(self):
playlists = []
@ -46,7 +48,9 @@ class LocalStoredPlaylistsProvider(base.BaseStoredPlaylistsProvider):
self.playlists = playlists
def create(self, name):
playlist = Playlist(name=name)
file_path = os.path.join(self._folder, name + '.m3u')
uri = path.path_to_uri(file_path)
playlist = Playlist(uri=uri, name=name)
self.save(playlist)
return playlist
@ -74,9 +78,10 @@ class LocalStoredPlaylistsProvider(base.BaseStoredPlaylistsProvider):
shutil.move(src, dst)
def save(self, playlist):
file_path = os.path.join(self._folder, playlist.name + '.m3u')
assert playlist.uri, 'Cannot save playlist without URI'
# FIXME this should be a save_m3u function, not inside save
file_path = playlist.uri[len('file://'):]
with open(file_path, 'w') as file_handle:
for track in playlist.tracks:
if track.uri.startswith('file://'):
@ -84,4 +89,7 @@ class LocalStoredPlaylistsProvider(base.BaseStoredPlaylistsProvider):
else:
file_handle.write(track.uri + '\n')
original_playlist = self.lookup(playlist.uri)
if original_playlist is not None:
self._playlists.remove(original_playlist)
self._playlists.append(playlist)

View File

@ -30,11 +30,15 @@ class StoredPlaylistsControllerTest(object):
settings.runtime.clear()
def test_create(self):
def test_create_returns_playlist_with_name_set(self):
playlist = self.stored.create('test')
self.assertEqual(playlist.name, 'test')
def test_create_in_playlists(self):
def test_create_returns_playlist_with_uri_set(self):
playlist = self.stored.create('test')
self.assert_(playlist.uri)
def test_create_adds_playlist_to_playlists_collection(self):
playlist = self.stored.create('test')
self.assert_(self.stored.playlists)
self.assertIn(playlist, self.stored.playlists)
@ -88,9 +92,12 @@ class StoredPlaylistsControllerTest(object):
except LookupError as e:
self.assertEqual(u'"name=c" match no playlists', e[0])
@unittest.SkipTest
def test_lookup(self):
pass
def test_lookup_finds_playlist_by_uri(self):
original_playlist = self.stored.create('test')
looked_up_playlist = self.stored.lookup(original_playlist.uri)
self.assertEqual(original_playlist, looked_up_playlist)
@unittest.SkipTest
def test_refresh(self):
@ -106,11 +113,14 @@ class StoredPlaylistsControllerTest(object):
test = lambda: self.stored.get(name='test2')
self.assertRaises(LookupError, test)
def test_save(self):
# FIXME should we handle playlists without names?
playlist = Playlist(name='test')
self.stored.save(playlist)
self.assertIn(playlist, self.stored.playlists)
def test_save_replaces_playlist_with_updated_playlist(self):
playlist1 = self.stored.create('test1')
self.assertIn(playlist1, self.stored.playlists)
playlist2 = playlist1.copy(name='test2')
self.stored.save(playlist2)
self.assertNotIn(playlist1, self.stored.playlists)
self.assertIn(playlist2, self.stored.playlists)
@unittest.SkipTest
def test_playlist_with_unknown_track(self):

View File

@ -2,7 +2,7 @@ import os
from mopidy import settings
from mopidy.backends.local import LocalBackend
from mopidy.models import Playlist, Track
from mopidy.models import Track
from mopidy.utils.path import path_to_uri
from tests import unittest, path_to_data_dir
@ -23,38 +23,43 @@ class LocalStoredPlaylistsControllerTest(
self.assert_(os.path.exists(path))
def test_saved_playlist_is_persisted(self):
path = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test2.m3u')
self.assert_(not os.path.exists(path))
self.stored.save(Playlist(name='test2'))
self.assert_(os.path.exists(path))
path1 = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test.m3u')
path2 = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test2.m3u')
playlist = self.stored.create('test')
self.assertFalse(os.path.exists(path2))
self.stored.rename(playlist, 'test2')
self.assertFalse(os.path.exists(path1))
self.assertTrue(os.path.exists(path2))
def test_deleted_playlist_is_removed(self):
playlist = self.stored.create('test')
self.stored.delete(playlist)
path = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test.m3u')
self.assert_(not os.path.exists(path))
def test_renamed_playlist_is_moved(self):
self.assertFalse(os.path.exists(path))
playlist = self.stored.create('test')
file1 = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test.m3u')
file2 = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test2.m3u')
self.assert_(not os.path.exists(file2))
self.stored.rename(playlist, 'test2')
self.assert_(not os.path.exists(file1))
self.assert_(os.path.exists(file2))
self.assertTrue(os.path.exists(path))
self.stored.delete(playlist)
self.assertFalse(os.path.exists(path))
def test_playlist_contents_is_written_to_disk(self):
track = Track(uri=generate_song(1))
uri = track.uri[len('file://'):]
playlist = Playlist(tracks=[track], name='test')
path = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test.m3u')
track_path = track.uri[len('file://'):]
playlist = self.stored.create('test')
playlist_path = playlist.uri[len('file://'):]
playlist = playlist.copy(tracks=[track])
self.stored.save(playlist)
with open(path) as playlist_file:
with open(playlist_path) as playlist_file:
contents = playlist_file.read()
self.assertEqual(uri, contents.strip())
self.assertEqual(track_path, contents.strip())
def test_playlists_are_loaded_at_startup(self):
playlist_path = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test.m3u')
@ -82,11 +87,3 @@ class LocalStoredPlaylistsControllerTest(
@unittest.SkipTest
def test_playlist_folder_is_createad(self):
pass
@unittest.SkipTest
def test_create_sets_playlist_uri(self):
pass
@unittest.SkipTest
def test_save_sets_playlist_uri(self):
pass