curds: Replace string indexing with a lookup function

This matches how we handle playlist lookups for the other playlist
managers.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-04-02 10:07:10 -04:00
parent 6a0d793fa7
commit ebabf1a479
8 changed files with 82 additions and 82 deletions

View File

@ -65,30 +65,30 @@ class PlaylistManager(list):
self.append(previous.PreviousPlaylist())
self.append(GenreManager())
self.append(LibraryManager())
self.current = self["Collection"]
self.current = self.lookup("Collection")
self.track = None
def __getitem__(self, name):
def lookup(self, name):
name = name.title()
for plist in self:
if plist.name == name:
return plist
if plist.name == name: return plist
return None
def next(self):
self.track = self.current.next()
self["Previous"].add(self.track)
self.lookup("Previous").add(self.track)
return self.track
def parent(self, child):
parent = self
if isinstance(child, genre.GenrePlaylist):
parent = self["Genre"]
parent = self.lookup("Genre")
elif isinstance(child, library.LibraryPlaylist):
parent = self["Library"]
parent = self.lookup("Library")
return parent if child in parent else None
def previous(self):
self.track = self["Previous"].next()
self.track = self.lookup("Previous").next()
return self.track
def reset(self):

View File

@ -27,7 +27,7 @@ class TestPlaylistManager(unittest.TestCase):
self.cb_plist = None
def tearDown(self):
notify.cancel("new-track", self.playman["Collection"].add)
notify.cancel("new-track", self.playman.lookup("Collection").add)
notify.cancel("new-playlist", self.on_new_playlist)
library.stop()
@ -37,61 +37,61 @@ class TestPlaylistManager(unittest.TestCase):
def test_manager_init(self):
self.assertIsInstance(self.playman, manager.PlaylistManager)
self.assertIsInstance(self.playman, list)
self.assertIsInstance(self.playman["Collection"], collection.CollectionPlaylist)
self.assertIsInstance(self.playman["Previous"], previous.PreviousPlaylist)
self.assertIsInstance(self.playman.lookup("Collection"), collection.CollectionPlaylist)
self.assertIsInstance(self.playman.lookup("Previous"), previous.PreviousPlaylist)
self.assertIsInstance(self.playman["Genre"], manager.GenreManager)
self.assertIsInstance(self.playman["Genre"], manager.PlaylistAllocator)
self.assertIsInstance(self.playman["Genre"], list)
self.assertIsInstance(self.playman.lookup("Genre"), manager.GenreManager)
self.assertIsInstance(self.playman.lookup("Genre"), manager.PlaylistAllocator)
self.assertIsInstance(self.playman.lookup("Genre"), list)
self.assertIsInstance(self.playman["Library"], manager.LibraryManager)
self.assertIsInstance(self.playman["Library"], manager.PlaylistAllocator)
self.assertIsInstance(self.playman["Library"], list)
self.assertIsInstance(self.playman.lookup("Library"), manager.LibraryManager)
self.assertIsInstance(self.playman.lookup("Library"), manager.PlaylistAllocator)
self.assertIsInstance(self.playman.lookup("Library"), list)
self.assertEqual(self.playman.current, self.playman["Collection"])
self.assertEqual(self.playman.current, self.playman.lookup("Collection"))
self.assertEqual(self.playman.track, None)
def test_manager_library(self):
plist = self.playman["Library"].add(test_library + "/")
plist = self.playman.lookup("Library").add(test_library + "/")
self.assertEqual(self.cb_plist, plist)
self.assertEqual(str(self.playman["Library"]), "<big>Library</big>")
self.assertEqual(self.playman["Library"].name, "Library")
self.assertEqual(self.playman["Library"].icon, "folder-music")
self.assertEqual(str(self.playman.lookup("Library")), "<big>Library</big>")
self.assertEqual(self.playman.lookup("Library").name, "Library")
self.assertEqual(self.playman.lookup("Library").icon, "folder-music")
self.assertEqual(plist.name, test_library)
self.assertIsInstance(plist, library.LibraryPlaylist)
self.cb_plist = None
self.assertIsNone(self.playman["Library"].add(test_library))
self.assertIsNone(self.playman.lookup("Library").add(test_library))
self.assertIsNone(self.cb_plist)
alist = self.playman["Library"].add(test_album)
alist = self.playman.lookup("Library").add(test_album)
self.assertEqual(self.cb_plist, alist)
self.assertIsInstance(alist, library.LibraryPlaylist)
self.assertNotEqual(id(plist), id(alist))
self.assertIn(alist, self.playman["Library"])
self.assertIn(plist, self.playman["Library"])
self.assertIn(alist, self.playman.lookup("Library"))
self.assertIn(plist, self.playman.lookup("Library"))
self.assertEqual(self.playman["Library"].lookup(test_album), alist)
self.assertEqual(self.playman["Library"].lookup(test_library + "/"), plist)
self.assertIsNone(self.playman["Library"].lookup("No such library"))
self.assertEqual(self.playman.lookup("Library").lookup(test_album), alist)
self.assertEqual(self.playman.lookup("Library").lookup(test_library + "/"), plist)
self.assertIsNone(self.playman.lookup("Library").lookup("No such library"))
parent = self.playman.parent(plist)
self.assertEqual(parent, self.playman["Library"])
self.assertEqual(parent, self.playman.lookup("Library"))
parent = self.playman.parent(alist)
self.assertEqual(parent, self.playman["Library"])
parent = self.playman.parent(self.playman["Library"])
self.assertEqual(parent, self.playman.lookup("Library"))
parent = self.playman.parent(self.playman.lookup("Library"))
self.assertEqual(parent, self.playman)
self.playman.reset()
self.assertEqual(len(self.playman["Library"]), 0)
self.assertEqual(len(self.playman.lookup("Library")), 0)
def test_manager_genre(self):
genreman = self.playman["Genre"]
genreman = self.playman.lookup("Genre")
self.assertEqual(str(genreman), "<big>Genre</big>")
self.assertEqual(genreman.name, "Genre")
self.assertEqual(genreman.icon, "audio-x-generic-symbolic")
self.playman["Library"].add(test_library)
self.playman.lookup("Library").add(test_library)
library.join()
plist = genreman.lookup("Test Genre 1")
@ -113,31 +113,31 @@ class TestPlaylistManager(unittest.TestCase):
self.assertIn((genreman.add_track, False), notify.registered["new-track"])
def test_manager_on_scan(self):
plist = self.playman["Library"].add(test_library)
self.assertEqual(self.playman["Library"].index(plist), 0)
self.assertEqual(self.playman["Library"][0], plist)
plist = self.playman.lookup("Library").add(test_library)
self.assertEqual(self.playman.lookup("Library").index(plist), 0)
self.assertEqual(self.playman.lookup("Library")[0], plist)
alist = self.playman["Library"].add(test_album)
self.assertEqual(self.playman["Library"].index(alist), 0)
self.assertEqual(self.playman["Library"].index(plist), 1)
self.assertEqual(self.playman["Library"][0], alist)
self.assertEqual(self.playman["Library"][1], plist)
alist = self.playman.lookup("Library").add(test_album)
self.assertEqual(self.playman.lookup("Library").index(alist), 0)
self.assertEqual(self.playman.lookup("Library").index(plist), 1)
self.assertEqual(self.playman.lookup("Library")[0], alist)
self.assertEqual(self.playman.lookup("Library")[1], plist)
library.join()
self.assertEqual(len(plist), 1250)
self.assertEqual(len(alist), 12)
self.assertEqual(len(self.playman["Collection"]), len(alist) + len(plist))
self.assertEqual(len(self.playman.lookup("Collection")), len(alist) + len(plist))
self.playman.reset()
self.assertEqual(len(self.playman["Collection"]), 0)
self.assertEqual(len(self.playman.lookup("Collection")), 0)
def test_manager_next_prev(self):
self.assertIsNone(self.playman.next())
self.assertIsNone(self.playman.track)
clist = self.playman["Collection"]
prev = self.playman["Previous"]
plist = self.playman["Library"].add(test_library)
clist = self.playman.lookup("Collection")
prev = self.playman.lookup("Previous")
plist = self.playman.lookup("Library").add(test_library)
library.join()
track1 = self.playman.next()
@ -160,5 +160,5 @@ class TestPlaylistManager(unittest.TestCase):
self.assertEqual(self.playman.previous(), track1)
self.playman.reset()
self.assertEqual(len(self.playman["Previous"]), 0)
self.assertEqual(len(self.playman.lookup("Previous")), 0)
self.assertIsNone(self.playman.track)

View File

@ -6,7 +6,7 @@ import os
from gi.repository import GObject, Gtk, GLib
toplevel = [ "Collection", "Previous", None, "Genre", None, "Library" ]
level2 = [ curds.PlaylistManager["Genre"], curds.PlaylistManager["Library"] ]
level2 = [ curds.PlaylistManager.lookup("Genre"), curds.PlaylistManager.lookup("Library") ]
class ManagerModel(GObject.GObject, Gtk.TreeModel):
def __init__(self, *args, **kwargs):
@ -103,7 +103,7 @@ class ManagerModel(GObject.GObject, Gtk.TreeModel):
def iter_playlist(self, iter):
item = toplevel[iter.user_data - 1]
if item != None:
plist = curds.PlaylistManager[item]
plist = curds.PlaylistManager.lookup(item)
if iter.user_data2 > 0:
plist = plist[iter.user_data2 - 1]
return plist
@ -142,7 +142,7 @@ def library_cancel_clicked(self, *args):
def library_ok_clicked(self, *args):
path = LibraryChooser.get_filename()
LibraryPopover.popdown()
curds.PlaylistManager["Library"].add(path)
curds.PlaylistManager.lookup("Library").add(path)
LibraryAdd = gtk.Builder.get_object("library_add")

View File

@ -9,7 +9,7 @@ cols = [ "tracknumber", "title", "length", "artist", "album", "date", "genre" ]
class PlaylistModel(GObject.GObject, Gtk.TreeModel):
def __init__(self, *args, **kwargs):
GObject.GObject.__init__(self)
self.playlist = curds.PlaylistManager["Collection"]
self.playlist = curds.PlaylistManager.lookup("Collection")
curds.notify.register("add-track", self.on_add_track, queue=True)
RandomButton.connect("toggled", self.on_random_toggled)

View File

@ -121,19 +121,19 @@ class TestGst(unittest.TestCase):
gst.PrevButton.clicked()
self.assertIsNone(curds.PlaylistManager.track)
curds.PlaylistManager["Library"].add(test_album)
curds.PlaylistManager.lookup("Library").add(test_album)
curds.playlist.library.join()
self.assertEqual(curds.PlaylistManager["Collection"].current, -1)
self.assertEqual(curds.PlaylistManager.lookup("Collection").current, -1)
gst.NextButton.clicked()
track1 = curds.PlaylistManager.track
self.assertIsNotNone(track1)
self.assertEqual(curds.PlaylistManager["Collection"].current, 0)
self.assertEqual(curds.PlaylistManager.lookup("Collection").current, 0)
gst.NextButton.clicked()
self.assertNotEqual(track1, curds.PlaylistManager.track)
track2 = curds.PlaylistManager.track
self.assertEqual(curds.PlaylistManager["Collection"].current, 1)
self.assertEqual(curds.PlaylistManager.lookup("Collection").current, 1)
self.main_loop_until(Gst.State.PLAYING)
self.assertTrue( gst.PauseButton.is_visible())
@ -171,13 +171,13 @@ class TestGst(unittest.TestCase):
self.main_loop_until(Gst.State.PLAYING)
self.assertNotEqual(curds.PlaylistManager.track, track2)
track3 = curds.PlaylistManager.track
self.assertEqual(curds.PlaylistManager["Collection"].current, 2)
self.assertEqual(curds.PlaylistManager.lookup("Collection").current, 2)
gst.PrevButton.clicked()
self.main_loop_until(Gst.State.PLAYING)
self.assertEqual(curds.PlaylistManager.track, track2)
self.assertEqual(curds.PlaylistManager["Collection"].current, 2)
self.assertEqual(curds.PlaylistManager["Previous"].current, 1)
self.assertEqual(curds.PlaylistManager.lookup("Collection").current, 2)
self.assertEqual(curds.PlaylistManager.lookup("Previous").current, 1)
self.state = None
self.audio.seek(value=90)
@ -186,5 +186,5 @@ class TestGst(unittest.TestCase):
while self.stream == False:
Gtk.main_iteration_do(True)
self.main_loop_until(Gst.State.PLAYING)
self.assertEqual(curds.PlaylistManager["Collection"].current, 3)
self.assertEqual(curds.PlaylistManager.lookup("Collection").current, 3)
self.assertNotEqual(curds.PlaylistManager.track, track2)

View File

@ -12,12 +12,12 @@ plist_mgr = curds.PlaylistManager
test_album1 = os.path.abspath("./trier/Test Library/Test Artist 02/Test Album 1")
test_album2 = os.path.abspath("./trier/Test Library/Test Artist 02/Test Album 2")
toplevel = [ ("media-playback-start", "Collection\n0 Tracks", plist_mgr["Collection"]),
("edit-undo", "Previous\n0 Tracks", plist_mgr["Previous"]),
toplevel = [ ("media-playback-start", "Collection\n0 Tracks", plist_mgr.lookup("Collection")),
("edit-undo", "Previous\n0 Tracks", plist_mgr.lookup("Previous")),
("", "", None),
("audio-x-generic-symbolic", "<big>Genre</big>", plist_mgr["Genre"]),
("audio-x-generic-symbolic", "<big>Genre</big>", plist_mgr.lookup("Genre")),
("", "", None),
("folder-music", "<big>Library</big>", plist_mgr["Library"]) ]
("folder-music", "<big>Library</big>", plist_mgr.lookup("Library")) ]
class TestManager(unittest.TestCase):
def setUp(self):
@ -76,7 +76,7 @@ class TestManager(unittest.TestCase):
self.assertEqual(model.get_path(piter).get_indices(), [ i ])
iter = model.iter_next(iter)
iter = model.find_playlist(curds.PlaylistManager["Collection"])
iter = model.find_playlist(curds.PlaylistManager.lookup("Collection"))
track = curds.Track.lookup(os.path.join(test_album1, "01 - Test Track 01.ogg"))
self.assertEqual(model.get_value(iter, 1), "Collection\n1 Track")
track = curds.Track.lookup(os.path.join(test_album1, "02 - Test Track 02.ogg"))
@ -84,8 +84,8 @@ class TestManager(unittest.TestCase):
def do_test_model_level2(self, header, index):
model = manager.ManagerModel()
iter = model.find_playlist(plist_mgr[header])
pl_top = plist_mgr[header]
iter = model.find_playlist(plist_mgr.lookup(header))
pl_top = plist_mgr.lookup(header)
self.assertEqual(manager.toplevel[index], header)
self.assertEqual(iter.user_data, index + 1)
@ -94,8 +94,8 @@ class TestManager(unittest.TestCase):
self.assertFalse(model.iter_has_child(iter))
self.assertFalse(model.iter_n_children(iter), 0)
plist_mgr["Library"].add(test_album1)
plist_mgr["Library"].add(test_album2)
plist_mgr.lookup("Library").add(test_album1)
plist_mgr.lookup("Library").add(test_album2)
curds.playlist.library.join()
self.assertTrue(model.iter_has_child(iter))
self.assertEqual(model.iter_n_children(iter), len(pl_top))
@ -132,10 +132,10 @@ class TestManager(unittest.TestCase):
def test_manager_selection(self):
selection = manager.Treeview.get_selection()
model = manager.MgrModel
iter = model.find_playlist(plist_mgr["Collection"])
iter = model.find_playlist(plist_mgr.lookup("Collection"))
path = model.get_path(iter)
plist = plist_mgr["Library"].add(test_album1)
plist = plist_mgr.lookup("Library").add(test_album1)
curds.playlist.library.join()
while curds.notify.run_queued():
continue
@ -143,17 +143,17 @@ class TestManager(unittest.TestCase):
self.assertIsInstance(selection, Gtk.TreeSelection)
selection.select_path(path)
self.assertTrue(selection.path_is_selected(path))
self.assertEqual(playlist.PlistModel.playlist, plist_mgr["Collection"])
self.assertEqual(playlist.PlistModel.playlist, plist_mgr.lookup("Collection"))
path.next()
selection.select_path(path)
self.assertTrue(selection.path_is_selected(path))
self.assertEqual(playlist.PlistModel.playlist, plist_mgr["Previous"])
self.assertEqual(playlist.PlistModel.playlist, plist_mgr.lookup("Previous"))
path.next()
selection.select_path(path)
self.assertFalse(selection.path_is_selected(path))
self.assertEqual(playlist.PlistModel.playlist, plist_mgr["Previous"])
self.assertEqual(playlist.PlistModel.playlist, plist_mgr.lookup("Previous"))
path = model.get_path(model.find_playlist(plist))
selection.select_path(path)
@ -169,20 +169,20 @@ class TestManager(unittest.TestCase):
manager.LibraryAdd.clicked()
Gtk.main_iteration_do(True)
self.assertTrue(manager.LibraryPopover.is_visible())
self.assertEqual(len(plist_mgr["Library"]), 0)
self.assertEqual(len(plist_mgr.lookup("Library")), 0)
manager.LibraryCancel.clicked()
while Gtk.events_pending():
time.sleep(0.05)
Gtk.main_iteration_do(True)
self.assertFalse(manager.LibraryPopover.is_visible())
self.assertEqual(len(plist_mgr["Library"]), 0)
self.assertEqual(len(plist_mgr.lookup("Library")), 0)
manager.LibraryAdd.clicked()
Gtk.main_iteration_do(True)
self.assertEqual(manager.LibraryChooser.get_filename(), music_dir)
self.assertTrue(manager.LibraryPopover.is_visible())
self.assertEqual(len(plist_mgr["Library"]), 0)
self.assertEqual(len(plist_mgr.lookup("Library")), 0)
manager.LibraryChooser.set_filename(test_album1)
while Gtk.events_pending(): Gtk.main_iteration_do(True)
@ -192,5 +192,5 @@ class TestManager(unittest.TestCase):
while Gtk.events_pending():
time.sleep(0.05)
Gtk.main_iteration_do(True)
self.assertEqual(len(plist_mgr["Library"]), 1)
self.assertEqual(len(plist_mgr.lookup("Library")), 1)
self.assertFalse(manager.LibraryPopover.is_visible())

View File

@ -20,7 +20,7 @@ class TestPlaylist(unittest.TestCase):
def test_model_init(self):
model = playlist.PlaylistModel()
self.assertEqual(model.playlist, curds.PlaylistManager["Collection"])
self.assertEqual(model.playlist, curds.PlaylistManager.lookup("Collection"))
self.assertEqual(model.get_n_columns(), 7)
self.assertEqual(model.get_flags(), Gtk.TreeModelFlags.LIST_ONLY)
for col in range(model.get_n_columns()):

View File

@ -37,8 +37,8 @@ class TestEmmental(unittest.TestCase):
curds.reset()
self.assertEqual(curds.tags.tag_map, {})
self.assertTrue(curds.playlist.library.library_thread.is_alive())
self.assertEqual(len(curds.PlaylistManager["Collection"]), 0)
self.assertEqual(len(curds.PlaylistManager["Library"]), 0)
self.assertEqual(len(curds.PlaylistManager.lookup("Collection")), 0)
self.assertEqual(len(curds.PlaylistManager.lookup("Library")), 0)
curds.stop()
self.assertFalse(curds.playlist.library.library_thread.is_alive())