From 109ad30a3fe5110745da3e2493fe3c4c8a464b36 Mon Sep 17 00:00:00 2001 From: Kevin Murphy Date: Tue, 31 Dec 2019 08:49:00 -0800 Subject: [PATCH] PydubLoader: Add helper class for SoundFileLoader to avoid repetition There needs to be some class sitting between SoundFileLoader and the concrete children since - each child shares a lot of code with siblings - each child needs the `pydub` external library, which may not exist on all systems See #122 for more context. --- ultratrace2/model/files/loaders/flac.py | 31 ++++-------------------- ultratrace2/model/files/loaders/mp3.py | 31 ++++-------------------- ultratrace2/model/files/loaders/ogg.py | 31 ++++-------------------- ultratrace2/model/files/loaders/pydub.py | 26 ++++++++++++++++++++ ultratrace2/model/files/loaders/wav.py | 31 ++++-------------------- 5 files changed, 46 insertions(+), 104 deletions(-) create mode 100644 ultratrace2/model/files/loaders/pydub.py diff --git a/ultratrace2/model/files/loaders/flac.py b/ultratrace2/model/files/loaders/flac.py index 6f59c4c..d41ba5f 100644 --- a/ultratrace2/model/files/loaders/flac.py +++ b/ultratrace2/model/files/loaders/flac.py @@ -1,28 +1,7 @@ -import pydub # type: ignore +from .pydub import PydubLoader -from .base import FileLoadError, SoundFileLoader - -class FLACLoader(SoundFileLoader): - def get_path(self) -> str: - return self._path - - def set_path(self, path) -> None: - self._path = path - - def __init__(self, path: str, audio_segment: pydub.AudioSegment): - self.set_path(path) - self.audio_segment = audio_segment - - def __len__(self) -> int: - return len(self.audio_segment) - - @classmethod - def from_file(cls, path: str) -> "FLACLoader": - try: - audio_segment = pydub.AudioSegment.from_file(path) - return FLACLoader(path, audio_segment) - except Exception as e: - raise FileLoadError( - f"Invalid FLAC ({path}), unable to read: {str(e)}" - ) from e +class FLACLoader(PydubLoader): + @staticmethod + def get_priority() -> int: + return 4 diff --git a/ultratrace2/model/files/loaders/mp3.py b/ultratrace2/model/files/loaders/mp3.py index 75595b3..8e6a537 100644 --- a/ultratrace2/model/files/loaders/mp3.py +++ b/ultratrace2/model/files/loaders/mp3.py @@ -1,28 +1,7 @@ -import pydub # type: ignore +from .pydub import PydubLoader -from .base import FileLoadError, SoundFileLoader - -class MP3Loader(SoundFileLoader): - def get_path(self) -> str: - return self._path - - def set_path(self, path) -> None: - self._path = path - - def __init__(self, path: str, audio_segment: pydub.AudioSegment): - self.set_path(path) - self.audio_segment = audio_segment - - def __len__(self) -> int: - return len(self.audio_segment) - - @classmethod - def from_file(cls, path: str) -> "MP3Loader": - try: - audio_segment = pydub.AudioSegment.from_file(path) - return MP3Loader(path, audio_segment) - except Exception as e: - raise FileLoadError( - f"Invalid MP3 ({path}), unable to read: {str(e)}" - ) from e +class MP3Loader(PydubLoader): + @staticmethod + def get_priority() -> int: + return 1 diff --git a/ultratrace2/model/files/loaders/ogg.py b/ultratrace2/model/files/loaders/ogg.py index ebc332f..80bcdb6 100644 --- a/ultratrace2/model/files/loaders/ogg.py +++ b/ultratrace2/model/files/loaders/ogg.py @@ -1,28 +1,7 @@ -import pydub # type: ignore +from .pydub import PydubLoader -from .base import FileLoadError, SoundFileLoader - -class OggLoader(SoundFileLoader): - def get_path(self) -> str: - return self._path - - def set_path(self, path) -> None: - self._path = path - - def __init__(self, path: str, audio_segment: pydub.AudioSegment): - self.set_path(path) - self.audio_segment = audio_segment - - def __len__(self) -> int: - return len(self.audio_segment) - - @classmethod - def from_file(cls, path: str) -> "OggLoader": - try: - audio_segment = pydub.AudioSegment.from_file(path) - return OggLoader(path, audio_segment) - except Exception as e: - raise FileLoadError( - f"Invalid Ogg ({path}), unable to read: {str(e)}" - ) from e +class OggLoader(PydubLoader): + @staticmethod + def get_priority() -> int: + return 2 diff --git a/ultratrace2/model/files/loaders/pydub.py b/ultratrace2/model/files/loaders/pydub.py new file mode 100644 index 0000000..470878c --- /dev/null +++ b/ultratrace2/model/files/loaders/pydub.py @@ -0,0 +1,26 @@ +import pydub # type: ignore + +from .base import FileLoadError, SoundFileLoader + + +class PydubLoader(SoundFileLoader): + def get_path(self) -> str: + return self._path + + def set_path(self, path) -> None: + self._path = path + + def __init__(self, path: str, audio_segment: pydub.AudioSegment): + self.set_path(path) + self.audio_segment = audio_segment + + def __len__(self) -> int: + return len(self.audio_segment) + + @classmethod + def from_file(cls, path: str) -> "PydubLoader": + try: + audio_segment = pydub.AudioSegment.from_file(path) + return PydubLoader(path, audio_segment) + except Exception as e: + raise FileLoadError(f"Invalid AudioSegment ({path}), unable to read") from e diff --git a/ultratrace2/model/files/loaders/wav.py b/ultratrace2/model/files/loaders/wav.py index cf931ce..1dcdce6 100644 --- a/ultratrace2/model/files/loaders/wav.py +++ b/ultratrace2/model/files/loaders/wav.py @@ -1,28 +1,7 @@ -import pydub # type: ignore +from .pydub import PydubLoader -from .base import FileLoadError, SoundFileLoader - -class WAVLoader(SoundFileLoader): - def get_path(self) -> str: - return self._path - - def set_path(self, path) -> None: - self._path = path - - def __init__(self, path: str, audio_segment: pydub.AudioSegment): - self.set_path(path) - self.audio_segment = audio_segment - - def __len__(self) -> int: - return len(self.audio_segment) - - @classmethod - def from_file(cls, path: str) -> "WAVLoader": - try: - audio_segment = pydub.AudioSegment.from_file(path) - return WAVLoader(path, audio_segment) - except Exception as e: - raise FileLoadError( - f"Invalid WAV ({path}), unable to read: {str(e)}" - ) from e +class WAVLoader(PydubLoader): + @staticmethod + def get_priority() -> int: + return 3