Skip to content

Commit

Permalink
move dump logic to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed May 6, 2024
1 parent 8aadc07 commit 3860837
Show file tree
Hide file tree
Showing 20 changed files with 1,492 additions and 1,429 deletions.
61 changes: 55 additions & 6 deletions rose/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
AudioTags,
RoseDate,
UnsupportedFiletypeError,
UnsupportedTagValueTypeError,
)
from rose.cache import (
Collage,
Expand All @@ -23,13 +24,16 @@
get_release,
get_track,
get_tracks_of_release,
get_tracks_of_releases,
label_exists,
list_artists,
list_collages,
list_descriptors,
list_genres,
list_labels,
list_playlists,
list_releases,
list_tracks,
lock,
make_release_logtext,
make_track_logtext,
Expand All @@ -48,6 +52,9 @@
update_cache_for_releases,
)
from rose.collages import (
CollageAlreadyExistsError,
CollageDoesNotExistError,
DescriptionMismatchError,
add_release_to_collage,
create_collage,
delete_collage,
Expand All @@ -65,8 +72,16 @@
sanitize_dirname,
sanitize_filename,
)
from rose.config import Config
from rose.config import (
Config,
ConfigDecodeError,
ConfigNotFoundError,
InvalidConfigValueError,
MissingConfigKeyError,
)
from rose.playlists import (
PlaylistAlreadyExistsError,
PlaylistDoesNotExistError,
add_track_to_playlist,
create_playlist,
delete_playlist,
Expand All @@ -77,33 +92,46 @@
set_playlist_cover_art,
)
from rose.releases import (
InvalidCoverArtFileError,
ReleaseDoesNotExistError,
ReleaseEditFailedError,
UnknownArtistRoleError,
create_single_release,
delete_release,
delete_release_cover_art,
edit_release,
find_releases_matching_rule,
run_actions_on_release,
set_release_cover_art,
toggle_release_new,
)
from rose.rule_parser import MetadataAction, MetadataMatcher, MetadataRule
from rose.rules import execute_metadata_rule, execute_stored_metadata_rules
from rose.rule_parser import InvalidRuleError, MetadataAction, MetadataMatcher, MetadataRule
from rose.rules import (
InvalidReplacementValueError,
TrackTagNotAllowedError,
execute_metadata_rule,
execute_stored_metadata_rules,
)
from rose.templates import (
InvalidPathTemplateError,
PathContext,
PathTemplate,
evaluate_release_template,
evaluate_track_template,
get_sample_music,
)
from rose.tracks import run_actions_on_track
from rose.tracks import TrackDoesNotExistError, find_tracks_matching_rule, run_actions_on_track

__all__ = [
# Plumbing
"initialize_logging",
"VERSION",
# Errors
"RoseError",
"RoseExpectedError",
"UnsupportedFiletypeError",
"DescriptionMismatchError",
"InvalidCoverArtFileError",
"ReleaseDoesNotExistError",
"ReleaseEditFailedError",
# Utilities
"sanitize_dirname",
"sanitize_filename",
Expand All @@ -112,6 +140,10 @@
"SUPPORTED_AUDIO_EXTENSIONS",
# Configuration
"Config",
"ConfigNotFoundError",
"ConfigDecodeError",
"MissingConfigKeyError",
"InvalidConfigValueError",
# Cache
"maybe_invalidate_cache_database",
"update_cache",
Expand All @@ -129,6 +161,8 @@
# Tagging
"AudioTags",
"RoseDate",
"UnsupportedFiletypeError",
"UnsupportedTagValueTypeError",
# Rule Engine
"MetadataAction",
"MetadataMatcher",
Expand All @@ -137,31 +171,42 @@
"execute_stored_metadata_rules",
"run_actions_on_release",
"run_actions_on_track",
"InvalidRuleError",
"InvalidReplacementValueError",
"TrackTagNotAllowedError",
# Path Templates
"PathContext",
"PathTemplate",
"evaluate_release_template",
"evaluate_track_template",
"get_sample_music",
"InvalidPathTemplateError",
# Releases
"Release",
"create_single_release",
"delete_release",
"delete_release_cover_art",
"edit_release",
"list_releases",
"find_releases_matching_rule",
"get_release",
"set_release_cover_art",
"toggle_release_new",
# Tracks
"Track",
"get_track",
"find_tracks_matching_rule",
"list_tracks",
"get_tracks_of_release",
"get_tracks_of_releases",
"track_within_release",
"TrackDoesNotExistError",
# Artists
"Artist",
"ArtistMapping",
"artist_exists",
"list_artists",
"UnknownArtistRoleError",
# Genres
"GenreEntry",
"list_genres",
Expand All @@ -186,6 +231,8 @@
"remove_release_from_collage",
"release_within_collage",
"rename_collage",
"CollageDoesNotExistError",
"CollageAlreadyExistsError",
# Playlists
"Playlist",
"add_track_to_playlist",
Expand All @@ -200,6 +247,8 @@
"remove_track_from_playlist",
"rename_playlist",
"set_playlist_cover_art",
"PlaylistDoesNotExistError",
"PlaylistAlreadyExistsError",
]

initialize_logging(__name__)
68 changes: 0 additions & 68 deletions rose/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,32 +225,6 @@ class Release:
releaseartists: ArtistMapping
metahash: str

def dump(self) -> dict[str, Any]:
return {
"id": self.id,
"source_path": str(self.source_path.resolve()),
"cover_image_path": str(self.cover_image_path.resolve())
if self.cover_image_path
else None,
"added_at": self.added_at,
"releasetitle": self.releasetitle,
"releasetype": self.releasetype,
"releasedate": str(self.releasedate) if self.releasedate else None,
"originaldate": str(self.originaldate) if self.originaldate else None,
"compositiondate": str(self.compositiondate) if self.compositiondate else None,
"catalognumber": self.catalognumber,
"edition": self.edition,
"new": self.new,
"disctotal": self.disctotal,
"genres": self.genres,
"parent_genres": self.parent_genres,
"secondary_genres": self.secondary_genres,
"parent_secondary_genres": self.parent_secondary_genres,
"descriptors": self.descriptors,
"labels": self.labels,
"releaseartists": self.releaseartists.dump(),
}


def cached_release_from_view(c: Config, row: dict[str, Any], aliases: bool = True) -> Release:
secondary_genres = _split(row["secondary_genres"]) if row["secondary_genres"] else []
Expand Down Expand Up @@ -298,48 +272,6 @@ class Track:

release: Release

def dump(self, with_release_info: bool = True) -> dict[str, Any]:
r = {
"id": self.id,
"source_path": str(self.source_path.resolve()),
"tracktitle": self.tracktitle,
"tracknumber": self.tracknumber,
"tracktotal": self.tracktotal,
"discnumber": self.discnumber,
"duration_seconds": self.duration_seconds,
"trackartists": self.trackartists.dump(),
}
if with_release_info:
r.update(
{
"release_id": self.release.id,
"added_at": self.release.added_at,
"releasetitle": self.release.releasetitle,
"releasetype": self.release.releasetype,
"disctotal": self.release.disctotal,
"releasedate": str(self.release.releasedate)
if self.release.releasedate
else None,
"originaldate": str(self.release.originaldate)
if self.release.originaldate
else None,
"compositiondate": str(self.release.compositiondate)
if self.release.compositiondate
else None,
"catalognumber": self.release.catalognumber,
"edition": self.release.edition,
"new": self.release.new,
"genres": self.release.genres,
"parent_genres": self.release.parent_genres,
"secondary_genres": self.release.secondary_genres,
"parent_secondary_genres": self.release.parent_secondary_genres,
"descriptors": self.release.descriptors,
"labels": self.release.labels,
"releaseartists": self.release.releaseartists.dump(),
}
)
return r


def cached_track_from_view(
c: Config,
Expand Down
33 changes: 1 addition & 32 deletions rose/collages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
The collages module provides functions for interacting with collages.
"""

import json
import logging
from pathlib import Path
from typing import Any
Expand All @@ -14,16 +13,14 @@

from rose.cache import (
collage_lock_name,
get_collage,
get_collage_releases,
get_release_logtext,
list_collages,
lock,
update_cache_evict_nonexistent_collages,
update_cache_for_collages,
)
from rose.common import RoseExpectedError
from rose.config import Config
from rose.releases import ReleaseDoesNotExistError

logger = logging.getLogger(__name__)

Expand All @@ -40,10 +37,6 @@ class CollageAlreadyExistsError(RoseExpectedError):
pass


class ReleaseDoesNotExistError(RoseExpectedError):
pass


def create_collage(c: Config, name: str) -> None:
(c.music_source_dir / "!collages").mkdir(parents=True, exist_ok=True)
path = collage_path(c, name)
Expand Down Expand Up @@ -145,30 +138,6 @@ def add_release_to_collage(
update_cache_for_collages(c, [collage_name], force=True)


def dump_collage(c: Config, collage_name: str) -> str:
collage = get_collage(c, collage_name)
if collage is None:
raise CollageDoesNotExistError(f"Collage {collage_name} does not exist")
collage_releases = get_collage_releases(c, collage_name)
releases: list[dict[str, Any]] = []
for idx, rls in enumerate(collage_releases):
releases.append({"position": idx + 1, **rls.dump()})
return json.dumps({"name": collage_name, "releases": releases})


def dump_all_collages(c: Config) -> str:
out: list[dict[str, Any]] = []
for name in list_collages(c):
collage = get_collage(c, name)
assert collage is not None
collage_releases = get_collage_releases(c, name)
releases: list[dict[str, Any]] = []
for idx, rls in enumerate(collage_releases):
releases.append({"position": idx + 1, **rls.dump()})
out.append({"name": name, "releases": releases})
return json.dumps(out)


def edit_collage_in_editor(c: Config, collage_name: str) -> None:
path = collage_path(c, collage_name)
if not path.exists():
Expand Down
Loading

0 comments on commit 3860837

Please sign in to comment.