aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEddy Pedroni <eddy@0xf7.com>2021-12-21 08:16:55 +0100
committerEddy Pedroni <eddy@0xf7.com>2021-12-21 08:16:55 +0100
commit182a14f4b9aec4722f7f94ab44bf57356c6d71aa (patch)
treec2c3a683184a53ddc141fe33e02fc934b793d196
parent1ca1fbcc001e27f46fc033f44ad5459bd0351bc9 (diff)
Added AB controller with some tests, minor refactor to playlist
-rw-r--r--abcontroller.py38
-rw-r--r--abcontroller_unittest.py66
-rw-r--r--playlist.py26
-rw-r--r--playlist_unittest.py6
4 files changed, 117 insertions, 19 deletions
diff --git a/abcontroller.py b/abcontroller.py
new file mode 100644
index 0000000..8c14c0c
--- /dev/null
+++ b/abcontroller.py
@@ -0,0 +1,38 @@
+from collections import namedtuple
+
+_AB = namedtuple("_AB", ["a", "b"])
+
+class ABController:
+ def __init__(self):
+ self.setPositionCallback = None
+ self._limits = dict() # dictionary of all songs
+ self._songLimits = None # list of limits for selected song
+ self._currentLimits = None # a/b positions of selected limit
+
+ def setCurrentSong(self, path):
+ if path not in self._limits:
+ self._limits[path] = list()
+
+ self._songLimits = self._limits[path]
+ self._currentLimits = None
+
+ def addLimits(self, aLimit, bLimit):
+ if self._songLimits is None:
+ return
+
+ ab = _AB(aLimit, bLimit)
+ self._songLimits.append(ab)
+
+ def setCurrentLimits(self, index):
+ if not self._songLimits:
+ return
+
+ if index >= 0 and index < len(self._songLimits):
+ self._currentLimits = self._songLimits[index]
+
+ def positionChanged(self, position):
+ if not self._currentLimits:
+ return
+
+ if position > self._currentLimits.b and self.setPositionCallback:
+ self.setPositionCallback(self._currentLimits.a)
diff --git a/abcontroller_unittest.py b/abcontroller_unittest.py
new file mode 100644
index 0000000..0ee1554
--- /dev/null
+++ b/abcontroller_unittest.py
@@ -0,0 +1,66 @@
+from abcontroller import ABController
+from collections import namedtuple
+
+AB = namedtuple("AB", ["a", "b"])
+
+def checkLimits(uut, aLimit, bLimit):
+ Test = namedtuple("Test", ["currentPosition", "requestedPosition"])
+ tests = [
+ Test(aLimit - 0.1, None),
+ Test(aLimit, None),
+ Test(bLimit - 0.1, None),
+ Test(bLimit, None),
+ Test(bLimit + 0.1, aLimit)
+ ]
+
+ requestedPosition = None
+ def callback(newPosition):
+ nonlocal requestedPosition
+ requestedPosition = newPosition
+
+ originalCallback = uut.setPositionCallback
+ uut.setPositionCallback = callback
+
+ for t in tests:
+ uut.positionChanged(t.currentPosition)
+ assert requestedPosition == t.requestedPosition
+
+ uut.setPositionCallback = originalCallback
+
+def test_oneSetOfLimits():
+ song = "/path/to/song"
+ abLimits = AB(0.2, 0.4)
+
+ uut = ABController()
+ uut.setCurrentSong(song)
+ uut.addLimits(abLimits.a, abLimits.b)
+ uut.setCurrentLimits(0)
+
+ checkLimits(uut, abLimits.a, abLimits.b)
+
+def test_multipleSetsOfLimits():
+ song = "/path/to/song"
+ abLimits = [
+ AB(0.2, 0.4),
+ AB(0.3, 0.5),
+ AB(0.0, 1.2)
+ ]
+
+ uut = ABController()
+ uut.setCurrentSong(song)
+ for l in abLimits:
+ uut.addLimits(l.a, l.b)
+
+ for i, l in enumerate(abLimits):
+ uut.setCurrentLimits(i)
+ checkLimits(uut, l.a, l.b)
+
+def test_multipleSongs():
+ # different limits in each song
+ pass
+
+def test_disableAbRepeat():
+ pass
+
+def test_addLimitsToSong():
+ pass
diff --git a/playlist.py b/playlist.py
index b175a08..6e96534 100644
--- a/playlist.py
+++ b/playlist.py
@@ -1,24 +1,10 @@
import logging
-"""
-class PlaylistModel(QAbstractListModel):
- def __init__(self, medialist, *args, **kwargs):
- super(PlaylistModel, self).__init__(*args, **kwargs)
- self.medialist = medialist
-
- def data(self, index, role):
- if role == Qt.DisplayRole:
- return self.medialist.item_at_index(index.row()).get_mrl()
-
- def rowCount(self, index):
- return self.medialist.count()
-"""
-
class Playlist:
- def __init__(self, setSongCallback):
+ def __init__(self, callback):
self.songList = list()
self.currentSong = None
- self.callback = setSongCallback
+ self.setSongCallback = callback
def addSong(self, path):
self.songList.append(path)
@@ -27,8 +13,10 @@ class Playlist:
self.setCurrentSong(0)
def setCurrentSong(self, index):
- if index < len(self.songList):
+ if index >= 0 and index < len(self.songList):
self.currentSong = index
- self.callback(self.songList[index])
+ self.setSongCallback(self.songList[index])
logging.debug(f"Selected song: {self.currentSong}")
-
+
+ def getCurrentSong(self):
+ return self.currentSong
diff --git a/playlist_unittest.py b/playlist_unittest.py
index 685dcb0..b407e4d 100644
--- a/playlist_unittest.py
+++ b/playlist_unittest.py
@@ -13,6 +13,7 @@ def test_addAndSelectOneSong():
uut.setCurrentSong(0)
assert songAddedByUser == songSetByCallback
+ assert uut.getCurrentSong() == 0
def test_addTwoSongsAndSelectBoth():
songAddedByUser = ["/path/to/song", "/path/to/second/song"]
@@ -28,9 +29,11 @@ def test_addTwoSongsAndSelectBoth():
uut.setCurrentSong(0)
assert songAddedByUser[0] == songSetByCallback
+ assert uut.getCurrentSong() == 0
uut.setCurrentSong(1)
assert songAddedByUser[1] == songSetByCallback
+ assert uut.getCurrentSong() == 1
def test_firstAddedSongIsSelected():
songAddedByUser = "/path/to/song"
@@ -44,6 +47,7 @@ def test_firstAddedSongIsSelected():
uut.addSong(songAddedByUser)
assert songAddedByUser == songSetByCallback
+ assert uut.getCurrentSong() == 0
def test_invalidSongSelection():
songAddedByUser = "/path/to/song"
@@ -58,8 +62,10 @@ def test_invalidSongSelection():
uut.setCurrentSong(10)
assert songSetByCallback == None
+ assert uut.getCurrentSong() == None
uut.addSong(songAddedByUser)
uut.setCurrentSong(10)
assert songSetByCallback == songAddedByUser
+ assert uut.getCurrentSong() == 0