Commit 4c26ee73 authored by Tanguy Le Carrour's avatar Tanguy Le Carrour
Browse files

Implement Pydub Track and Tracks.

parent bd80c0f8
__pycache__/
splito.egg-info/
.envrc
data/a_new_track.mp3
File added
from pydub import AudioSegment
from splito.domain.audio_editing.model import Track, Tracks
class PydubTrack(Track):
@classmethod
def create(cls, a_path: str) -> Track:
try:
return cls(AudioSegment.from_file(a_path))
except FileNotFoundError:
raise KeyError()
def __init__(self, a_pydub_object: AudioSegment) -> None:
self.__segment = a_pydub_object
def export(self, path: str) -> None:
self.__segment.export(path)
class PydubTracks(Tracks):
def __getitem__(self, a_path: str) -> Track:
return PydubTrack.create(a_path)
def __setitem__(self, a_path: str, a_track: Track) -> None:
# Here we break LSP
if not isinstance(a_track, PydubTrack):
raise NotImplementedError("Repo only handles PydubTrack objects!")
a_track.export(a_path)
from mamba import describe, it, before # type: ignore
from robber import expect # type: ignore
from splito.infrastructure.pydub.tracks import PydubTrack, PydubTracks
with describe(PydubTrack):
with describe(PydubTrack.create):
with it("return a track object"):
track = PydubTrack.create("data/sample.mp3")
expect(track).to.be.instanceof(PydubTrack)
with describe(PydubTrack.export):
with it("is tested within PydubTracks.__setitem__"):
pass
with describe(PydubTracks):
with before.each as self:
self.tracks = PydubTracks()
with describe(PydubTracks.__getitem__):
with it("loads a track from disk"):
track = self.tracks["data/sample.mp3"]
expect(track).to.be.instanceof(PydubTrack)
with it("raises a KeyError if the track does not exist"):
def action() -> None:
_ = self.tracks["data/not_a_track.mp3"]
expect(action).to.be.throw(KeyError)
with describe(PydubTracks.__setitem__):
with it("saves a track to disk"):
track = PydubTrack.create("data/sample.mp3")
def action() -> None:
self.tracks["data/a_new_track.mp3"] = track
action()
expect(action).not_to.throw(Exception)
......@@ -28,6 +28,7 @@ def unit(c):
title(unit.__doc__)
c.run("mamba --format documentation splito/domain", pty=True)
c.run("mamba --format documentation splito/use_cases", pty=True)
c.run("mamba --format documentation splito/infrastructure", pty=True)
@task
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment