trackdb: Give TrackAllocators an autoremove() function

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-06-22 17:05:11 -04:00
parent eb6eb0180b
commit 518e3cf9ad
2 changed files with 21 additions and 0 deletions

View File

@ -44,6 +44,15 @@ class TrackAllocator:
except Exception as e:
pass
def autoremove(self, lib):
with self.lock:
to_rm = [ trak for trak in self.tracks.values() \
if trak.library == lib and not trak.filepath().exists() ]
for trak in to_rm:
del self.tracks[trak.trackid]
self.Removed.publish(trak)
return to_rm
def list_tracks(self, lib):
with self.lock:
for (id, track) in self.tracks.items():

View File

@ -64,6 +64,18 @@ class TestTrackAllocator(unittest.TestCase):
self.assertEqual(alloc.Added.subscribers, set())
self.assertEqual(alloc.Removed.subscribers, set())
def test_allocator_autoremove(self):
lib = library.LibraryPath(test_tracks)
alloc = allocator.TrackAllocator()
alloc.Removed.register(self.on_track_removed)
track = alloc.allocate(lib, test_tracks / "01 - Test Track.ogg")
track2 = alloc.allocate(lib, test_tracks / "02 - Test {Disc 2}.ogg")
track2.path = pathlib.Path("No Such File")
self.assertEqual(alloc.autoremove(lib), [ track2 ])
self.assertEqual([ t for t in alloc.list_tracks(lib) ], [ track ])
def test_allocator_state(self):
lib = library.LibraryPath(test_tracks)
alloc = allocator.TrackAllocator()