adding playlist tests

This commit is contained in:
evilchili 2024-03-07 21:51:48 -08:00
parent 7417baeeb1
commit f3fd8215f0
2 changed files with 29 additions and 16 deletions

View File

@ -27,12 +27,12 @@ class Playlist:
@cached_property @cached_property
def path(self): def path(self):
return croaker.path.playlist_root() / self.name return self._get_path()
@cached_property @cached_property
def tracks(self): def tracks(self):
if not self.path.exists(): if not self.path.exists():
raise RuntimeError(f"Playlist {self.name} not found at {self.path}.") raise RuntimeError(f"Playlist {self.name} not found at {self.path}.") # pragma: no cover
entries = [] entries = []
theme = self.path / self.theme theme = self.path / self.theme
@ -49,13 +49,12 @@ class Playlist:
path = self.path path = self.path
logging.debug(f"Getting files matching {os.environ['MEDIA_GLOB']} from {path}") logging.debug(f"Getting files matching {os.environ['MEDIA_GLOB']} from {path}")
pats = os.environ["MEDIA_GLOB"].split(",") pats = os.environ["MEDIA_GLOB"].split(",")
return chain(*[list(path.glob(pat)) for pat in pats]) return chain(*[list(path.rglob(pat)) for pat in pats])
def _add_track(self, target: Path, source: Path, make_theme: bool = False): def _get_path(self):
if source.is_dir(): return croaker.path.playlist_root() / self.name
for file in self.get_audio_files(source):
self._add_track(self.path / _stripped(file.name), file) def _add_track(self, target: Path, source: Path):
return
if target.exists(): if target.exists():
if not target.is_symlink(): if not target.is_symlink():
logging.warning(f"{target}: target already exists and is not a symlink; skipping.") logging.warning(f"{target}: target already exists and is not a symlink; skipping.")
@ -63,14 +62,26 @@ class Playlist:
target.unlink() target.unlink()
target.symlink_to(source) target.symlink_to(source)
def add(self, tracks: List[Path], make_theme: bool = False): def add(self, paths: List[Path], make_theme: bool = False):
logger.debug(f"Adding everything from {paths = }")
self.path.mkdir(parents=True, exist_ok=True) self.path.mkdir(parents=True, exist_ok=True)
if make_theme: for path in paths:
target = self.path / "_theme.mp3" if path.is_dir():
source = tracks.pop(0) files = list(self.get_audio_files(path))
self._add_track(target, source, make_theme=True) if make_theme:
for track in tracks: logger.debug(f"Adding first file from dir as theme: {files[0] = }")
self._add_track(target=self.path / _stripped(track.name), source=track) self._add_track(self.path / "_theme.mp3", files.pop(0))
make_theme = False
for file in files:
logger.debug(f"Adding {file = }")
self._add_track(target=self.path / _stripped(file.name), source=file)
elif make_theme:
logger.debug(f"Adding path as theme: {path = }")
self._add_track(self.path / "_theme.mp3", path)
make_theme = False
else:
logger.debug(f"Adding {path = }")
self._add_track(target=self.path / _stripped(path.name), source=path)
return sorted(self.get_audio_files()) return sorted(self.get_audio_files())
def __repr__(self): def __repr__(self):
@ -79,7 +90,7 @@ class Playlist:
return "\n".join(lines) return "\n".join(lines)
def load_playlist(name: str): def load_playlist(name: str): # pragma: no cover
if name not in playlists: if name not in playlists:
playlists[name] = Playlist(name=name) playlists[name] = Playlist(name=name)
return playlists[name] return playlists[name]

View File

@ -29,6 +29,8 @@ croaker = "croaker.cli:app"
black = "^23.3.0" black = "^23.3.0"
isort = "^5.12.0" isort = "^5.12.0"
pyproject-autoflake = "^1.0.2" pyproject-autoflake = "^1.0.2"
pytest = "^7.2.0"
pytest-cov = "^4.0.0"
[tool.black] [tool.black]
line-length = 120 line-length = 120