aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEddy Pedroni <eddy@0xf7.com>2021-12-21 18:24:55 +0100
committerEddy Pedroni <eddy@0xf7.com>2021-12-21 18:24:55 +0100
commit6eb42e6d4468ad161281125c77a41063f93380e1 (patch)
tree35ad2edeb7058ad60f47affb1b27dfac3fa62bac
parentc1bfcc6064b3a22c76b986d9339daf6cbd403c80 (diff)
Added session manager, renamed solo-tool.py
-rw-r--r--abcontroller.py3
-rw-r--r--abcontroller_unittest.py4
-rw-r--r--playlist.py24
-rw-r--r--playlist_unittest.py21
-rw-r--r--session_manager.py39
-rw-r--r--session_manager_unittest.py137
-rw-r--r--solo_tool_qt.py (renamed from solo-tool.py)0
7 files changed, 209 insertions, 19 deletions
diff --git a/abcontroller.py b/abcontroller.py
index 302bc9e..9d232c1 100644
--- a/abcontroller.py
+++ b/abcontroller.py
@@ -48,3 +48,6 @@ class ABController:
def setEnable(self, enable):
self._enabled = enable
+
+ def getLimits(self, song):
+ return self._limits.get(song)
diff --git a/abcontroller_unittest.py b/abcontroller_unittest.py
index 839c88a..42eaabc 100644
--- a/abcontroller_unittest.py
+++ b/abcontroller_unittest.py
@@ -37,6 +37,7 @@ def test_oneSetOfLimits():
uut.setCurrentLimits(0)
checkLimits(uut, abLimits.a, abLimits.b)
+ assert uut.getLimits(song) == [abLimits]
def test_multipleSetsOfLimits():
song = "/path/to/song"
@@ -55,6 +56,8 @@ def test_multipleSetsOfLimits():
uut.setCurrentLimits(i)
checkLimits(uut, l.a, l.b)
+ assert uut.getLimits(song) == abLimits
+
def test_multipleSongs():
songs = [
"/path/to/song",
@@ -73,6 +76,7 @@ def test_multipleSongs():
uut.setCurrentLimits(0)
checkLimits(uut, abLimits[i].a, abLimits[i].b)
+ assert uut.getLimits(s) == [abLimits[i]]
def test_disableAbRepeat():
song = "/path/to/song"
diff --git a/playlist.py b/playlist.py
index 6e96534..5c52774 100644
--- a/playlist.py
+++ b/playlist.py
@@ -2,21 +2,23 @@ import logging
class Playlist:
def __init__(self, callback):
- self.songList = list()
- self.currentSong = None
- self.setSongCallback = callback
+ self._songList = list()
+ self._currentSong = None
+ self._setSongCallback = callback
def addSong(self, path):
- self.songList.append(path)
+ self._songList.append(path)
logging.debug(f"Added song: {path}")
- if self.currentSong is None:
- self.setCurrentSong(0)
def setCurrentSong(self, index):
- if index >= 0 and index < len(self.songList):
- self.currentSong = index
- self.setSongCallback(self.songList[index])
- logging.debug(f"Selected song: {self.currentSong}")
+ if index >= 0 and index < len(self._songList):
+ self._currentSong = index
+ self._setSongCallback(self._songList[index])
+ logging.debug(f"Selected song: {self._currentSong}")
def getCurrentSong(self):
- return self.currentSong
+ index = self._currentSong
+ return self._songList[index] if index is not None else None
+
+ def getSongs(self):
+ return self._songList
diff --git a/playlist_unittest.py b/playlist_unittest.py
index b407e4d..ff5a79c 100644
--- a/playlist_unittest.py
+++ b/playlist_unittest.py
@@ -13,7 +13,8 @@ def test_addAndSelectOneSong():
uut.setCurrentSong(0)
assert songAddedByUser == songSetByCallback
- assert uut.getCurrentSong() == 0
+ assert uut.getCurrentSong() == songAddedByUser
+ assert uut.getSongs() == [songAddedByUser]
def test_addTwoSongsAndSelectBoth():
songAddedByUser = ["/path/to/song", "/path/to/second/song"]
@@ -26,16 +27,17 @@ def test_addTwoSongsAndSelectBoth():
uut = Playlist(testCallback)
uut.addSong(songAddedByUser[0])
uut.addSong(songAddedByUser[1])
+ assert uut.getSongs() == songAddedByUser
uut.setCurrentSong(0)
assert songAddedByUser[0] == songSetByCallback
- assert uut.getCurrentSong() == 0
+ assert uut.getCurrentSong() == songAddedByUser[0]
uut.setCurrentSong(1)
assert songAddedByUser[1] == songSetByCallback
- assert uut.getCurrentSong() == 1
+ assert uut.getCurrentSong() == songAddedByUser[1]
-def test_firstAddedSongIsSelected():
+def test_firstAddedSongIsNotSelected():
songAddedByUser = "/path/to/song"
songSetByCallback = None
@@ -46,8 +48,9 @@ def test_firstAddedSongIsSelected():
uut = Playlist(testCallback)
uut.addSong(songAddedByUser)
- assert songAddedByUser == songSetByCallback
- assert uut.getCurrentSong() == 0
+ assert songSetByCallback == None
+ assert uut.getCurrentSong() == None
+ assert uut.getSongs() == [songAddedByUser]
def test_invalidSongSelection():
songAddedByUser = "/path/to/song"
@@ -59,6 +62,7 @@ def test_invalidSongSelection():
uut = Playlist(testCallback)
assert songSetByCallback == None
+ assert uut.getCurrentSong() == None
uut.setCurrentSong(10)
assert songSetByCallback == None
@@ -66,6 +70,7 @@ def test_invalidSongSelection():
uut.addSong(songAddedByUser)
uut.setCurrentSong(10)
- assert songSetByCallback == songAddedByUser
- assert uut.getCurrentSong() == 0
+ assert songSetByCallback == None
+ assert uut.getCurrentSong() == None
+ assert uut.getSongs() == [songAddedByUser]
diff --git a/session_manager.py b/session_manager.py
new file mode 100644
index 0000000..057d952
--- /dev/null
+++ b/session_manager.py
@@ -0,0 +1,39 @@
+import json
+
+class SessionManager:
+ def __init__(self, playlist, abController):
+ self._playlist = playlist
+ self._abController = abController
+
+ def addSong(self, path):
+ self._playlist.addSong(path)
+
+ def addLimits(self, aLimit, bLimit):
+ self._abController.addLimits(aLimit, bLimit)
+
+ def loadSession(self, file):
+ jsonStr = file.read()
+ session = json.loads(jsonStr)
+
+ for entry in session:
+ songPath = entry["path"]
+ abLimits = entry["ab_limits"]
+ self._playlist.addSong(songPath)
+
+ if abLimits is not None:
+ for l in abLimits:
+ self._abController.addLimits(l[0], l[1], songPath)
+
+ def saveSession(self, file):
+ songs = self._playlist.getSongs()
+ abLimits = self._abController.getLimits()
+ session = list()
+
+ for s in songs:
+ entry = {
+ "path": s,
+ "ab_limits" : abLimits.get(s)
+ }
+ session.append(entry)
+
+ file.write(json.dumps(session))
diff --git a/session_manager_unittest.py b/session_manager_unittest.py
new file mode 100644
index 0000000..0169132
--- /dev/null
+++ b/session_manager_unittest.py
@@ -0,0 +1,137 @@
+from session_manager import SessionManager
+from json import loads, dumps
+
+testSession = [
+ {
+ "path" : "/path/to/another/song",
+ "ab_limits" : None
+ },
+ {
+ "path" : "/path/to/song",
+ "ab_limits" : [
+ [0.1, 0.2],
+ [0.3, 0.4]
+ ]
+ },
+ {
+ "path" : "/path/to/something",
+ "ab_limits" : [
+ [0.1, 0.2]
+ ]
+ }
+]
+
+class PlaylistMock:
+ def __init__(self):
+ self.lastAddedSong = None
+ self.songs = list()
+
+ def addSong(self, s):
+ self.songs.append(s)
+ self.lastAddedSong = s
+
+ def getSongs(self):
+ return self.songs
+
+class ABControllerMock:
+ def __init__(self):
+ self.limits = dict()
+
+ def addLimits(self, aLimit, bLimit, song="current"):
+ if song not in self.limits:
+ self.limits[song] = list()
+ self.limits[song].append([aLimit, bLimit])
+
+ def getLimits(self):
+ return self.limits
+
+class MockFile:
+ def __init__(self, init=""):
+ self.contents = init
+
+ def open(self, *args):
+ pass
+
+ def write(self, s):
+ self.contents += s
+
+ def read(self):
+ return self.contents
+
+
+def test_addSongs():
+ songs = [
+ "/path/to/song",
+ "/path/to/another/song"
+ ]
+
+ playlistMock = PlaylistMock()
+ uut = SessionManager(playlistMock, None)
+
+ for s in songs:
+ uut.addSong(s)
+ assert playlistMock.lastAddedSong == s
+
+def test_addAbLimits():
+ abLimits = [
+ [0.1, 0.2],
+ [0.3, 0.4]
+ ]
+
+ abControllerMock = ABControllerMock()
+ uut = SessionManager(None, abControllerMock)
+
+ for i, ab in enumerate(abLimits):
+ uut.addLimits(ab[0], ab[1])
+ assert abControllerMock.limits["current"][i] == ab
+
+def test_loadSession():
+ playlistMock = PlaylistMock()
+ abControllerMock = ABControllerMock()
+ uut = SessionManager(playlistMock, abControllerMock)
+
+ sessionFile = MockFile(dumps(testSession))
+ uut.loadSession(sessionFile)
+
+ for i, entry in enumerate(testSession):
+ expectedSong = entry["path"]
+ expectedLimits = entry["ab_limits"]
+ loadedSong = playlistMock.songs[i]
+ loadedLimits = abControllerMock.limits.get(expectedSong)
+
+ assert loadedSong == expectedSong
+ assert loadedLimits == expectedLimits
+
+def test_saveSession():
+ playlistMock = PlaylistMock()
+ abControllerMock = ABControllerMock()
+ uut = SessionManager(playlistMock, abControllerMock)
+
+ for i, entry in enumerate(testSession):
+ song = entry["path"]
+ playlistMock.addSong(song)
+
+ abLimits = entry["ab_limits"]
+ if abLimits is not None:
+ for l in abLimits:
+ abControllerMock.addLimits(l[0], l[1], song)
+
+ sessionFile = MockFile()
+ uut.saveSession(sessionFile)
+
+ savedSession = loads(sessionFile.read())
+ assert savedSession == testSession
+
+def test_loadAndSaveEmptySession():
+ playlistMock = PlaylistMock()
+ abControllerMock = ABControllerMock()
+ uut = SessionManager(playlistMock, abControllerMock)
+
+ sessionFile = MockFile()
+
+ uut.saveSession(sessionFile)
+ assert loads(sessionFile.read()) == list()
+
+ uut.loadSession(sessionFile)
+ assert playlistMock.getSongs() == list()
+ assert abControllerMock.getLimits() == dict()
diff --git a/solo-tool.py b/solo_tool_qt.py
index 5db6d3f..5db6d3f 100644
--- a/solo-tool.py
+++ b/solo_tool_qt.py