Skip to content

Commit

Permalink
move template preview to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed May 6, 2024
1 parent 19c6d2f commit 8aadc07
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 213 deletions.
22 changes: 5 additions & 17 deletions rose/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rose.audiotags import (
SUPPORTED_AUDIO_EXTENSIONS,
AudioTags,
RoseDate,
UnsupportedFiletypeError,
)
from rose.cache import (
Expand Down Expand Up @@ -50,8 +51,6 @@
add_release_to_collage,
create_collage,
delete_collage,
dump_all_collages,
dump_collage,
edit_collage_in_editor,
remove_release_from_collage,
rename_collage,
Expand All @@ -72,8 +71,6 @@
create_playlist,
delete_playlist,
delete_playlist_cover_art,
dump_all_playlists,
dump_playlist,
edit_playlist_in_editor,
remove_track_from_playlist,
rename_playlist,
Expand All @@ -83,8 +80,6 @@
create_single_release,
delete_release,
delete_release_cover_art,
dump_all_releases,
dump_release,
edit_release,
run_actions_on_release,
set_release_cover_art,
Expand All @@ -97,9 +92,9 @@
PathTemplate,
evaluate_release_template,
evaluate_track_template,
preview_path_templates,
get_sample_music,
)
from rose.tracks import dump_all_tracks, dump_track, run_actions_on_track
from rose.tracks import run_actions_on_track

__all__ = [
# Plumbing
Expand Down Expand Up @@ -133,6 +128,7 @@
"playlist_lock_name",
# Tagging
"AudioTags",
"RoseDate",
# Rule Engine
"MetadataAction",
"MetadataMatcher",
Expand All @@ -146,22 +142,18 @@
"PathTemplate",
"evaluate_release_template",
"evaluate_track_template",
"preview_path_templates",
"get_sample_music",
# Releases
"Release",
"create_single_release",
"delete_release",
"delete_release_cover_art",
"dump_all_releases",
"dump_release",
"edit_release",
"get_release",
"set_release_cover_art",
"toggle_release_new",
# Tracks
"Track",
"dump_all_tracks",
"dump_track",
"get_track",
"get_tracks_of_release",
"track_within_release",
Expand All @@ -187,8 +179,6 @@
"add_release_to_collage",
"create_collage",
"delete_collage",
"dump_all_collages",
"dump_collage",
"edit_collage_in_editor", # TODO: Move editor part to CLI, make this file-submissions.
"get_collage",
"get_collage_releases",
Expand All @@ -205,8 +195,6 @@
"delete_playlist_cover_art",
"get_playlist",
"get_playlist_tracks",
"dump_all_playlists",
"dump_playlist",
"edit_playlist_in_editor", # TODO: Move editor part to CLI, make this file-submissions.
"track_within_playlist",
"remove_track_from_playlist",
Expand Down
98 changes: 16 additions & 82 deletions rose/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from functools import cached_property
from typing import Any

import click
import jinja2

from rose.audiotags import RoseDate
Expand Down Expand Up @@ -366,46 +365,12 @@ def _collapse_spacing(x: str) -> str:
return COLLAPSE_SPACING_REGEX.sub(" ", x).strip()


def preview_path_templates(c: Config) -> None:
# fmt: off
_preview_release_template(c, "Source Directory - Release", c.path_templates.source.release)
_preview_track_template(c, "Source Directory - Track", c.path_templates.source.track)
click.echo()
_preview_release_template(c, "1. Releases - Release", c.path_templates.releases.release)
_preview_track_template(c, "1. Releases - Track", c.path_templates.releases.track)
click.echo()
_preview_release_template(c, "1. Releases (New) - Release", c.path_templates.releases_new.release)
_preview_track_template(c, "1. Releases (New) - Track", c.path_templates.releases_new.track)
click.echo()
_preview_release_template(c, "1. Releases (Added On) - Release", c.path_templates.releases_added_on.release)
_preview_track_template(c, "1. Releases (Added On) - Track", c.path_templates.releases_added_on.track)
click.echo()
_preview_release_template(c, "1. Releases (Released On) - Release", c.path_templates.releases_added_on.release)
_preview_track_template(c, "1. Releases (Released On) - Track", c.path_templates.releases_added_on.track)
click.echo()
_preview_release_template(c, "2. Artists - Release", c.path_templates.artists.release)
_preview_track_template(c, "2. Artists - Track", c.path_templates.artists.track)
click.echo()
_preview_release_template(c, "3. Genres - Release", c.path_templates.genres.release)
_preview_track_template(c, "3. Genres - Track", c.path_templates.genres.track)
click.echo()
_preview_release_template(c, "4. Descriptors - Release", c.path_templates.genres.release)
_preview_track_template(c, "4. Descriptors - Track", c.path_templates.genres.track)
click.echo()
_preview_release_template(c, "5. Labels - Release", c.path_templates.labels.release)
_preview_track_template(c, "5. Labels - Track", c.path_templates.labels.track)
click.echo()
_preview_release_template(c, "6. Collages - Release", c.path_templates.collages.release)
_preview_track_template(c, "6. Collages - Track", c.path_templates.collages.track)
click.echo()
_preview_track_template(c, "7. Playlists - Track", c.path_templates.playlists)
# fmt: on


def _get_preview_releases(c: Config) -> tuple[Release, Release, Release]:
from rose.cache import Release

kimlip = Release(
def get_sample_music(
c: Config,
) -> tuple[tuple[Release, Track], tuple[Release, Track], tuple[Release, Track]]:
from rose.cache import Release, Track

kimlip_rls = Release(
id="018b268e-ff1e-7a0c-9ac8-7bbb282761f2",
source_path=c.music_source_dir / "LOONA - 2017. Kim Lip",
cover_image_path=None,
Expand Down Expand Up @@ -442,8 +407,7 @@ def _get_preview_releases(c: Config) -> tuple[Release, Release, Release]:
releaseartists=ArtistMapping(main=[Artist("Kim Lip")]),
metahash="0",
)

youngforever = Release(
bts_rls = Release(
id="018b6021-f1e5-7d4b-b796-440fbbea3b13",
source_path=c.music_source_dir / "BTS - 2016. Young Forever (花樣年華)",
cover_image_path=None,
Expand Down Expand Up @@ -483,8 +447,7 @@ def _get_preview_releases(c: Config) -> tuple[Release, Release, Release]:
releaseartists=ArtistMapping(main=[Artist("BTS")]),
metahash="0",
)

debussy = Release(
debussy_rls = Release(
id="018b268e-de0c-7cb2-8ffa-bcc2083c94e6",
source_path=c.music_source_dir
/ "Debussy - 1907. Images performed by Cleveland Orchestra under Pierre Boulez (1992)",
Expand Down Expand Up @@ -514,31 +477,7 @@ def _get_preview_releases(c: Config) -> tuple[Release, Release, Release]:
metahash="0",
)

return kimlip, youngforever, debussy


def _preview_release_template(c: Config, label: str, template: PathTemplate) -> None:
# Import cycle trick :)
kimlip, youngforever, debussy = _get_preview_releases(c)
click.secho(f"{label}:", dim=True, underline=True)
click.secho(" Sample 1: ", dim=True, nl=False)
click.secho(evaluate_release_template(template, kimlip, position="1"))
click.secho(" Sample 2: ", dim=True, nl=False)
click.secho(evaluate_release_template(template, youngforever, position="2"))
click.secho(" Sample 3: ", dim=True, nl=False)
click.secho(evaluate_release_template(template, debussy, position="3"))


def _preview_track_template(c: Config, label: str, template: PathTemplate) -> None:
# Import cycle trick :)
from rose.cache import Track

kimlip, youngforever, debussy = _get_preview_releases(c)

click.secho(f"{label}:", dim=True, underline=True)

click.secho(" Sample 1: ", dim=True, nl=False)
track = Track(
kimlip_trk = Track(
id="018b268e-ff1e-7a0c-9ac8-7bbb282761f1",
source_path=c.music_source_dir / "LOONA - 2017. Kim Lip" / "01. Eclipse.opus",
source_mtime="999",
Expand All @@ -549,12 +488,9 @@ def _preview_track_template(c: Config, label: str, template: PathTemplate) -> No
duration_seconds=230,
trackartists=ArtistMapping(main=[Artist("Kim Lip")]),
metahash="0",
release=kimlip,
release=kimlip_rls,
)
click.secho(evaluate_track_template(template, track, position="1"))

click.secho(" Sample 2: ", dim=True, nl=False)
track = Track(
bts_trk = Track(
id="018b6021-f1e5-7d4b-b796-440fbbea3b15",
source_path=c.music_source_dir
/ "BTS - 2016. Young Forever (花樣年華)"
Expand All @@ -567,12 +503,9 @@ def _preview_track_template(c: Config, label: str, template: PathTemplate) -> No
duration_seconds=226,
trackartists=ArtistMapping(main=[Artist("BTS")]),
metahash="0",
release=youngforever,
release=bts_rls,
)
click.secho(evaluate_track_template(template, track, position="2"))

click.secho(" Sample 3: ", dim=True, nl=False)
track = Track(
debussy_trk = Track(
id="018b6514-6e65-78cc-94a5-fdb17418f090",
source_path=c.music_source_dir
/ "Debussy - 1907. Images performed by Cleveland Orchestra under Pierre Boulez (1992)"
Expand All @@ -589,6 +522,7 @@ def _preview_track_template(c: Config, label: str, template: PathTemplate) -> No
conductor=[Artist("Pierre Boulez")],
),
metahash="0",
release=debussy,
release=debussy_rls,
)
click.secho(evaluate_track_template(template, track, position="3"))

return (kimlip_rls, kimlip_trk), (bts_rls, bts_trk), (debussy_rls, debussy_trk)
117 changes: 3 additions & 114 deletions rose/templates_test.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
from copy import deepcopy
from pathlib import Path

import click
from click.testing import CliRunner

from rose.audiotags import RoseDate
from rose.cache import Release, Track
from rose.common import Artist, ArtistMapping
from rose.config import Config
from rose.templates import (
PathTemplate,
PathTemplateConfig,
_get_preview_releases,
evaluate_release_template,
evaluate_track_template,
preview_path_templates,
get_sample_music,
)

EMPTY_CACHED_RELEASE = Release(
Expand Down Expand Up @@ -114,114 +110,6 @@ def test_default_templates() -> None:
)


def test_preview_templates(config: Config) -> None:
runner = CliRunner()
with runner.isolated_filesystem(), runner.isolation() as out_streams:
preview_path_templates(config)
out_streams[0].seek(0)
output = click.unstyle(out_streams[0].read().decode())

assert (
output
== """\
Source Directory - Release:
Sample 1: Kim Lip - 2017. Kim Lip - Single [NEW]
Sample 2: BTS - 2016. Young Forever (花樣年華)
Sample 3: Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - 1992. Images
Source Directory - Track:
Sample 1: 01. Eclipse.opus
Sample 2: 02-05. House of Cards.opus
Sample 3: 01-01. Gigues: Modéré.opus
1. Releases - Release:
Sample 1: Kim Lip - 2017. Kim Lip - Single [NEW]
Sample 2: BTS - 2016. Young Forever (花樣年華)
Sample 3: Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - 1992. Images
1. Releases - Track:
Sample 1: 01. Eclipse.opus
Sample 2: 02-05. House of Cards.opus
Sample 3: 01-01. Gigues: Modéré.opus
1. Releases (New) - Release:
Sample 1: Kim Lip - 2017. Kim Lip - Single [NEW]
Sample 2: BTS - 2016. Young Forever (花樣年華)
Sample 3: Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - 1992. Images
1. Releases (New) - Track:
Sample 1: 01. Eclipse.opus
Sample 2: 02-05. House of Cards.opus
Sample 3: 01-01. Gigues: Modéré.opus
1. Releases (Added On) - Release:
Sample 1: [2023-04-20] Kim Lip - 2017. Kim Lip - Single [NEW]
Sample 2: [2023-06-09] BTS - 2016. Young Forever (花樣年華)
Sample 3: [2023-09-06] Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - 1992. Images
1. Releases (Added On) - Track:
Sample 1: 01. Eclipse.opus
Sample 2: 02-05. House of Cards.opus
Sample 3: 01-01. Gigues: Modéré.opus
1. Releases (Released On) - Release:
Sample 1: [2023-04-20] Kim Lip - 2017. Kim Lip - Single [NEW]
Sample 2: [2023-06-09] BTS - 2016. Young Forever (花樣年華)
Sample 3: [2023-09-06] Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - 1992. Images
1. Releases (Released On) - Track:
Sample 1: 01. Eclipse.opus
Sample 2: 02-05. House of Cards.opus
Sample 3: 01-01. Gigues: Modéré.opus
2. Artists - Release:
Sample 1: Kim Lip - 2017. Kim Lip - Single [NEW]
Sample 2: BTS - 2016. Young Forever (花樣年華)
Sample 3: Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - 1992. Images
2. Artists - Track:
Sample 1: 01. Eclipse.opus
Sample 2: 02-05. House of Cards.opus
Sample 3: 01-01. Gigues: Modéré.opus
3. Genres - Release:
Sample 1: Kim Lip - 2017. Kim Lip - Single [NEW]
Sample 2: BTS - 2016. Young Forever (花樣年華)
Sample 3: Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - 1992. Images
3. Genres - Track:
Sample 1: 01. Eclipse.opus
Sample 2: 02-05. House of Cards.opus
Sample 3: 01-01. Gigues: Modéré.opus
4. Descriptors - Release:
Sample 1: Kim Lip - 2017. Kim Lip - Single [NEW]
Sample 2: BTS - 2016. Young Forever (花樣年華)
Sample 3: Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - 1992. Images
4. Descriptors - Track:
Sample 1: 01. Eclipse.opus
Sample 2: 02-05. House of Cards.opus
Sample 3: 01-01. Gigues: Modéré.opus
5. Labels - Release:
Sample 1: Kim Lip - 2017. Kim Lip - Single [NEW]
Sample 2: BTS - 2016. Young Forever (花樣年華)
Sample 3: Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - 1992. Images
5. Labels - Track:
Sample 1: 01. Eclipse.opus
Sample 2: 02-05. House of Cards.opus
Sample 3: 01-01. Gigues: Modéré.opus
6. Collages - Release:
Sample 1: 1. Kim Lip - 2017. Kim Lip - Single [NEW]
Sample 2: 2. BTS - 2016. Young Forever (花樣年華)
Sample 3: 3. Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - 1992. Images
6. Collages - Track:
Sample 1: 01. Eclipse.opus
Sample 2: 02-05. House of Cards.opus
Sample 3: 01-01. Gigues: Modéré.opus
7. Playlists - Track:
Sample 1: 1. Kim Lip - Eclipse.opus
Sample 2: 2. BTS - House of Cards.opus
Sample 3: 3. Claude Debussy performed by Cleveland Orchestra under Pierre Boulez - Gigues: Modéré.opus
"""
)


def test_classical(config: Config) -> None:
"""Test a complicated classical template."""

Expand All @@ -236,7 +124,8 @@ def test_classical(config: Config) -> None:
"""
)

_, _, debussy = _get_preview_releases(config)
_, _, (debussy, _) = get_sample_music(config)

assert (
evaluate_release_template(template, debussy)
== "Debussy, Claude - 1907. Images performed by Cleveland Orchestra under Pierre Boulez (1992)"
Expand Down
Loading

0 comments on commit 8aadc07

Please sign in to comment.