curds: Add a function for picking the next track
If "loop" is True, then we loop back to the beginning of the playlist when we reach the end. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
1beb3cf869
commit
2286665024
|
@ -3,13 +3,23 @@ from .. import notify
|
|||
|
||||
class Playlist(list):
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.name = name
|
||||
self.current = -1
|
||||
self.loop = False
|
||||
|
||||
def add(self, track):
|
||||
if track is not None:
|
||||
self.append(track)
|
||||
notify.Notify.notify("on-add", self, track)
|
||||
|
||||
def next(self):
|
||||
if self.loop == True and self.current >= len(self) - 1:
|
||||
self.current = -1
|
||||
if self.current < len(self) - 1:
|
||||
self.current += 1
|
||||
return self[self.current]
|
||||
return None
|
||||
|
||||
def runtime(self):
|
||||
m, s = divmod(sum([ track.length for track in self ]), 60)
|
||||
h, m = divmod(m, 60)
|
||||
|
@ -20,3 +30,7 @@ class Playlist(list):
|
|||
if m > 0: res.append(f"{int(m)} minute{'s' if m != 1 else ''}")
|
||||
if s > 0: res.append(f"{int(s)} second{'s' if s != 1 else ''}")
|
||||
return ", ".join(res)
|
||||
|
||||
def set_loop(self, loop):
|
||||
self.loop = loop
|
||||
return self.loop
|
||||
|
|
|
@ -20,6 +20,8 @@ class TestPlaylist(unittest.TestCase):
|
|||
self.assertEqual(plist.name, "Test Playlist")
|
||||
self.assertEqual(len(plist), 0)
|
||||
self.assertEqual(plist.runtime(), "")
|
||||
self.assertEqual(plist.current, -1)
|
||||
self.assertFalse(plist.loop)
|
||||
|
||||
def test_playlist_add(self):
|
||||
path = os.path.join(test_library, "Test Artist 01", "Test Album 1")
|
||||
|
@ -77,3 +79,43 @@ class TestPlaylist(unittest.TestCase):
|
|||
plist.add(None)
|
||||
self.assertEqual(self.cb_plist, plist)
|
||||
self.assertEqual(self.cb_track, track)
|
||||
|
||||
def test_playlist_next(self):
|
||||
path = os.path.join(test_library, "Test Artist 01", "Test Album 1")
|
||||
plist = playlist.Playlist("Test Playlist")
|
||||
tracks = [ ]
|
||||
|
||||
self.assertTrue(plist.set_loop(True))
|
||||
self.assertTrue(plist.loop)
|
||||
|
||||
for i in range(1, 9):
|
||||
track = tags.Track.lookup(os.path.join(path, f"0{i} - Test Track 0{i}.ogg"))
|
||||
tracks.append(track)
|
||||
plist.add(track)
|
||||
|
||||
self.assertEqual(plist.current, -1)
|
||||
for i in range(16):
|
||||
track = plist.next()
|
||||
self.assertEqual(track, tracks[i % 8])
|
||||
self.assertEqual(plist.current, i % 8)
|
||||
|
||||
track = tags.Track.lookup(os.path.join(path, "09 - Test Track 09.ogg"))
|
||||
tracks.append(track)
|
||||
plist.add(track)
|
||||
for i in [ 8, 0 ]:
|
||||
self.assertEqual(plist.next(), tracks[i])
|
||||
self.assertEqual(plist.current, i)
|
||||
|
||||
self.assertFalse(plist.set_loop(False))
|
||||
for i in range(1, 9):
|
||||
self.assertEqual(plist.next(), tracks[i])
|
||||
self.assertEqual(plist.current, i)
|
||||
|
||||
self.assertEqual(plist.current, 8)
|
||||
self.assertIsNone(plist.next())
|
||||
self.assertEqual(plist.current, 8)
|
||||
|
||||
track = tags.Track.lookup(os.path.join(path, "10 - Test Track 10.ogg"))
|
||||
plist.add(track)
|
||||
self.assertEqual(plist.next(), track)
|
||||
self.assertEqual(plist.current, 9)
|
||||
|
|
Loading…
Reference in New Issue