From 1beb3cf869e73bac416624ba2a5fe14013b13552 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 11 Mar 2019 17:09:36 -0400 Subject: [PATCH] curds: Replace track.add() with track.lookup() Now that we have a "new-track" notification, we can replace the add function with a lookup function that more closely mirrors how we look up album tags. Signed-off-by: Anna Schumaker --- curds/playlist/library.py | 5 ++--- curds/playlist/test_library.py | 8 -------- curds/playlist/test_playlist.py | 8 ++++---- curds/tags.py | 5 ++--- curds/test_tags.py | 4 ++-- curds/test_tags_track.py | 6 +++--- 6 files changed, 13 insertions(+), 23 deletions(-) diff --git a/curds/playlist/library.py b/curds/playlist/library.py index 9352bb9..2eef900 100644 --- a/curds/playlist/library.py +++ b/curds/playlist/library.py @@ -16,10 +16,9 @@ class LibraryPlaylist(playlist.Playlist): library_thread.push(self.thread_scan) def thread_add(self, path): - track = tags.Track.add(path) - if track != None: + track = tags.Track.lookup(path) + if not track in self: self.add(track) - notify.Notify.notify("on-scan", self, track) def thread_scan(self): for dirname, subdirs, files in os.walk(self.name): diff --git a/curds/playlist/test_library.py b/curds/playlist/test_library.py index 581a168..bf3e7c8 100644 --- a/curds/playlist/test_library.py +++ b/curds/playlist/test_library.py @@ -18,7 +18,6 @@ class TestLibraryPlaylist(unittest.TestCase): library.library_thread.stop() library.library_thread = threadqueue.ThreadQueue() tags.tag_map.clear() - self.scan_count = 0 def tearDownClass(): library.library_thread.stop() @@ -30,20 +29,13 @@ class TestLibraryPlaylist(unittest.TestCase): self.assertIsInstance(plist, playlist.Playlist) self.assertEqual(plist.name, test_library) - def on_scan(self, plist, track): - self.scan_count += 1 - def test_playlist_library_scan(self): - notify.Notify.notify_me("on-scan", self.on_scan) - plist = library.LibraryPlaylist(test_library) self.assertGreater(library.library_thread.qsize(), 0) library.library_thread.join() self.assertEqual(len(plist), 1250) - self.assertEqual(self.scan_count, 1250) self.assertEqual(plist.runtime(), "1 hour, 54 minutes, 35 seconds") plist.scan() library.library_thread.join() self.assertEqual(len(plist), 1250) - self.assertEqual(self.scan_count, 1250) diff --git a/curds/playlist/test_playlist.py b/curds/playlist/test_playlist.py index 782df9d..9c6e344 100644 --- a/curds/playlist/test_playlist.py +++ b/curds/playlist/test_playlist.py @@ -25,11 +25,11 @@ class TestPlaylist(unittest.TestCase): path = os.path.join(test_library, "Test Artist 01", "Test Album 1") plist = playlist.Playlist("Test Playlist") - plist.add(tags.Track.add(os.path.join(path, "01 - Test Track 01.ogg"))) + plist.add(tags.Track.lookup(os.path.join(path, "01 - Test Track 01.ogg"))) self.assertEqual(len(plist), 1) self.assertEqual(plist.runtime(), "1 second") - plist.add(tags.Track.add(os.path.join(path, "02 - Test Track 02.ogg"))) + plist.add(tags.Track.lookup(os.path.join(path, "02 - Test Track 02.ogg"))) self.assertEqual(len(plist), 2) self.assertEqual(plist.runtime(), "3 seconds") @@ -40,7 +40,7 @@ class TestPlaylist(unittest.TestCase): def test_playlist_runtime(self): path = os.path.join(test_library, "Test Artist 01", "Test Album 1") - track = tags.Track.add(os.path.join(path, "01 - Test Track 01.ogg")) + track = tags.Track.lookup(os.path.join(path, "01 - Test Track 01.ogg")) plist = playlist.Playlist("Test Playlist") plist.add(track) @@ -66,7 +66,7 @@ class TestPlaylist(unittest.TestCase): def test_playlist_notifications(self): path = os.path.join(test_library, "Test Artist 01", "Test Album 1") - track = tags.Track.add(os.path.join(path, "01 - Test Track 01.ogg")) + track = tags.Track.lookup(os.path.join(path, "01 - Test Track 01.ogg")) plist = playlist.Playlist("Test Playlist") notify.Notify.notify_me("on-add", self.on_add) diff --git a/curds/tags.py b/curds/tags.py index c83f998..dc353e8 100644 --- a/curds/tags.py +++ b/curds/tags.py @@ -69,9 +69,8 @@ class Track(Tag): return hash((hash(self.album), self.title, self.artist, self.tracknumber, self.discnumber)) - def add(path): - track = Track(path) - return track if Tag.lookup(track, "new-track") == track else None + def lookup(path): + return Tag.lookup(Track(path), "new-track") def save(): diff --git a/curds/test_tags.py b/curds/test_tags.py index 93b0270..1bfc3a9 100644 --- a/curds/test_tags.py +++ b/curds/test_tags.py @@ -20,7 +20,7 @@ class TestTags(unittest.TestCase): tags.load() self.assertEqual(len(tags.tag_map), 0) - t = tags.Track.add(os.path.join(test_tracks, "01 - Test Track.ogg")) + t = tags.Track.lookup(os.path.join(test_tracks, "01 - Test Track.ogg")) self.assertFalse(dfile.exists()) tags.save() self.assertTrue(dfile.exists()) @@ -44,7 +44,7 @@ class TestTags(unittest.TestCase): for dirname, subdirs, files in os.walk(test_library): save = False for f in files: - tq.push(tags.Track.add, os.path.join(dirname, f)) + tq.push(tags.Track.lookup, os.path.join(dirname, f)) tracks += 1 save = True if save == True: diff --git a/curds/test_tags_track.py b/curds/test_tags_track.py index 7bc679e..ce5e314 100644 --- a/curds/test_tags_track.py +++ b/curds/test_tags_track.py @@ -61,7 +61,7 @@ class TestTrackTag(unittest.TestCase): def test_track_add(self): notify.Notify.notify_me("new-track", self.on_new_track) - t = tags.Track.add(os.path.join(test_tracks, "01 - Test Track.ogg")) + t = tags.Track.lookup(os.path.join(test_tracks, "01 - Test Track.ogg")) self.assertIsNotNone(t) self.assertIn(t, tags.tag_map.values()) self.assertIn(t.album, tags.tag_map.values()) @@ -69,8 +69,8 @@ class TestTrackTag(unittest.TestCase): self.cb_track = None for i in range(10): - u = tags.Track.add(os.path.join(test_tracks, "01 - Test Track.ogg")) - self.assertIsNone(u) + u = tags.Track.lookup(os.path.join(test_tracks, "01 - Test Track.ogg")) + self.assertEqual(t, u) self.assertIsNone(self.cb_track) self.assertEqual(len(tags.tag_map), 2)