diff options
Diffstat (limited to 'solo-tool-project/src')
| -rw-r--r-- | solo-tool-project/src/solo_tool/session_manager.py | 31 | 
1 files changed, 28 insertions, 3 deletions
| diff --git a/solo-tool-project/src/solo_tool/session_manager.py b/solo-tool-project/src/solo_tool/session_manager.py index 4fae19f..ece67d8 100644 --- a/solo-tool-project/src/solo_tool/session_manager.py +++ b/solo-tool-project/src/solo_tool/session_manager.py @@ -1,9 +1,28 @@ +from typing import BinaryIO, Protocol +from abc import abstractmethod  from glob import glob  import json  from pathlib import Path  from . import SoloTool -class FileSystemSessionManager: +class SessionManager(Protocol): +    @abstractmethod +    def getSessions(self) -> list[str]: +        raise NotImplementedError + +    @abstractmethod +    def loadSession(self, key: str, player=None) -> SoloTool: +        raise NotImplementedError + +    @abstractmethod +    def saveSession(self, soloTool: SoloTool, key: str) -> None: +        raise NotImplementedError + +    @abstractmethod +    def addSong(self, name: str, content: BinaryIO) -> None: +        raise NotImplementedError + +class _FileSystemSessionManager(SessionManager):      def __init__(self, songPool: str, sessionPath: str):          self._songPool = Path(songPool)          self._sessionPath = Path(sessionPath) @@ -39,5 +58,11 @@ class FileSystemSessionManager:          with open(self._sessionPath / f"{key}.json", "w") as f:              json.dump(session, f) -def getSessionManager(songPool: str, sessionPath: str) -> FileSystemSessionManager: -    return FileSystemSessionManager(songPool, sessionPath) +    def addSong(self, name: str, content: BinaryIO): +        from shutil import copyfileobj +        newSong = self._songPool / name +        with open(newSong, "wb") as f: +            copyfileobj(content, f) + +def getSessionManager(songPool: str, sessionPath: str) -> SessionManager: +    return _FileSystemSessionManager(songPool, sessionPath) | 
