curds: Add basic playlist class
The intention is that different playlist types will create a subclass of this class to do whatever work they need. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
99f103d345
commit
e0763ec727
|
@ -0,0 +1 @@
|
|||
# Copyright 2019 (c) Anna Schumaker.
|
|
@ -0,0 +1,20 @@
|
|||
# Copyright 2019 (c) Anna Schumaker.
|
||||
|
||||
class Playlist(list):
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
def add(self, track):
|
||||
if track is not None:
|
||||
self.append(track)
|
||||
|
||||
def runtime(self):
|
||||
m, s = divmod(sum([ track.length for track in self ]), 60)
|
||||
h, m = divmod(m, 60)
|
||||
d, h = divmod(h, 24)
|
||||
res = [ ]
|
||||
if d > 0: res.append(f"{int(d)} day{'s' if d != 1 else ''}")
|
||||
if h > 0: res.append(f"{int(h)} hour{'s' if h != 1 else ''}")
|
||||
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)
|
|
@ -0,0 +1,57 @@
|
|||
# Copyright 2019 (c) Anna Schumaker.
|
||||
from . import playlist
|
||||
from .. import tags
|
||||
import os
|
||||
import unittest
|
||||
|
||||
test_library = os.path.abspath("./trier/Test Library")
|
||||
|
||||
class TestPlaylist(unittest.TestCase):
|
||||
def setUp(self):
|
||||
tags.tag_map.clear()
|
||||
|
||||
def test_playlist_init(self):
|
||||
plist = playlist.Playlist("Test Playlist")
|
||||
self.assertIsInstance(plist, list)
|
||||
self.assertEqual(plist.name, "Test Playlist")
|
||||
self.assertEqual(len(plist), 0)
|
||||
self.assertEqual(plist.runtime(), "")
|
||||
|
||||
def test_playlist_add(self):
|
||||
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")))
|
||||
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")))
|
||||
self.assertEqual(len(plist), 2)
|
||||
self.assertEqual(plist.runtime(), "3 seconds")
|
||||
|
||||
plist.add(None)
|
||||
self.assertEqual(len(plist), 2)
|
||||
self.assertEqual(plist.runtime(), "3 seconds")
|
||||
self.assertNotIn(None, plist)
|
||||
|
||||
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"))
|
||||
plist = playlist.Playlist("Test Playlist")
|
||||
|
||||
plist.add(track)
|
||||
self.assertEqual(plist.runtime(), "1 second")
|
||||
track.length = 2
|
||||
self.assertEqual(plist.runtime(), "2 seconds")
|
||||
track.length = 60
|
||||
self.assertEqual(plist.runtime(), "1 minute")
|
||||
track.length = 120
|
||||
self.assertEqual(plist.runtime(), "2 minutes")
|
||||
track.length = 3600
|
||||
self.assertEqual(plist.runtime(), "1 hour")
|
||||
track.length = 7200
|
||||
self.assertEqual(plist.runtime(), "2 hours")
|
||||
track.length = 86400
|
||||
self.assertEqual(plist.runtime(), "1 day")
|
||||
track.length = 172800
|
||||
self.assertEqual(plist.runtime(), "2 days")
|
|
@ -8,3 +8,5 @@ class TestEmmental(unittest.TestCase):
|
|||
self.assertIsNotNone(curds.data)
|
||||
self.assertIsNotNone(curds.tags)
|
||||
self.assertIsNotNone(curds.threadqueue)
|
||||
self.assertIsNotNone(curds.playlist)
|
||||
self.assertIsNotNone(curds.playlist.playlist)
|
||||
|
|
Loading…
Reference in New Issue