Skip to content

Commit

Permalink
add single print
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Nov 7, 2023
1 parent d6c9b62 commit e46241b
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Rosé's virtual filesystem organizes your music library by the metadata in the
music tags. The quality of the virtual filesystem depends on the quality of the
tags.

Thus, Rosé also provides the following functions to improv the tags of your
Thus, Rosé also provides the following functions to improve the tags of your
music library:

1. A text-based interface for manually modifying metadata,
Expand Down
90 changes: 90 additions & 0 deletions rose/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,96 @@ def list_releases(
)


def list_releases_and_tracks(
c: Config,
release_ids: str,
) -> list[tuple[CachedRelease, list[CachedTrack]]]:
rval: list[tuple[CachedRelease, list[CachedTrack]]] = []
tracksmap: dict[str, tuple[CachedRelease, list[CachedTrack]]] = {}
with connect(c) as conn:
cursor = conn.execute(
f"""
SELECT
id
, source_path
, cover_image_path
, added_at
, datafile_mtime
, title
, releasetype
, year
, multidisc
, new
, genres
, labels
, artist_names
, artist_roles
FROM releases_view
WHERE id IN ({','.join(['?']*len(release_ids))})
""",
release_ids,
)
for row in cursor:
release = CachedRelease(
id=row["id"],
source_path=Path(row["source_path"]),
cover_image_path=Path(row["cover_image_path"]) if row["cover_image_path"] else None,
added_at=row["added_at"],
datafile_mtime=row["datafile_mtime"],
title=row["title"],
releasetype=row["releasetype"],
year=row["year"],
multidisc=bool(row["multidisc"]),
new=bool(row["new"]),
genres=_split(row["genres"]),
labels=_split(row["labels"]),
artists=_unpack_artists(c, row["artist_names"], row["artist_roles"]),
)
tracks: list[CachedTrack] = []
tup = (release, tracks)
rval.append(tup)
tracksmap[release.id] = tup

cursor = conn.execute(
f"""
SELECT
t.id
, t.release_id
, t.source_path
, t.source_mtime
, t.title
, t.release_id
, t.tracknumber
, t.discnumber
, t.duration_seconds
, t.artist_names
, t.artist_roles
FROM tracks_view t
JOIN releases r ON r.id = t.release_id
WHERE id IN ({','.join(['?']*len(release_ids))})
ORDER BY t.release_id, FORMAT('%4d.%4d', t.discnumber, t.tracknumber)
""",
release_ids,
)
for row in cursor:
tracksmap[row["release_id"]][1].append(
CachedTrack(
id=row["id"],
source_path=Path(row["source_path"]),
source_mtime=row["source_mtime"],
title=row["title"],
release_id=row["release_id"],
tracknumber=row["tracknumber"],
discnumber=row["discnumber"],
duration_seconds=row["duration_seconds"],
artists=_unpack_artists(c, row["artist_names"], row["artist_roles"]),
release_multidisc=tracksmap[row["release_id"]][0].multidisc,
)
)

return rval


def get_release(c: Config, release_id: str) -> tuple[CachedRelease, list[CachedTrack]] | None:
with connect(c) as conn:
cursor = conn.execute(
Expand Down
37 changes: 32 additions & 5 deletions rose/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
add_release_to_collage,
create_collage,
delete_collage,
dump_collage,
dump_collages,
edit_collage_in_editor,
remove_release_from_collage,
Expand All @@ -32,6 +33,7 @@
create_playlist,
delete_playlist,
delete_playlist_cover_art,
dump_playlist,
dump_playlists,
edit_playlist_in_editor,
remove_track_from_playlist,
Expand All @@ -42,6 +44,7 @@
create_single_release,
delete_release,
delete_release_cover_art,
dump_release,
dump_releases,
edit_release,
run_actions_on_release,
Expand Down Expand Up @@ -192,14 +195,22 @@ def unmount(ctx: Context) -> None:
@cli.group()
def releases() -> None:
"""Manage releases."""
# TODO: print / extract-covers / add-metadata-url / search-metadata-urls / import
# TODO: extract-covers / add-metadata-url / search-metadata-urls / import


@releases.command(name="print")
@click.argument("release", type=str, nargs=1)
@click.pass_obj
def print1(ctx: Context, release: str) -> None:
"""Print a single release (in JSON). Accepts a release's UUID/path."""
click.echo(dump_release(ctx.config, release))


@releases.command(name="print-all")
@click.pass_obj
def print_all(ctx: Context) -> None:
"""Print all releases (in JSON)."""
print(dump_releases(ctx.config))
click.echo(dump_releases(ctx.config))


@releases.command(name="edit")
Expand Down Expand Up @@ -372,11 +383,19 @@ def edit(ctx: Context, collage: str) -> None:
edit_collage_in_editor(ctx.config, collage)


@collages.command(name="print")
@click.argument("collage", type=str, nargs=1)
@click.pass_obj
def print2(ctx: Context, collage: str) -> None:
"""Print a collage (in JSON). Accepts a collage's name."""
click.echo(dump_collage(ctx.config, collage))


@collages.command(name="print-all")
@click.pass_obj
def print_all1(ctx: Context) -> None:
"""Print all collages (in JSON)."""
print(dump_collages(ctx.config))
click.echo(dump_collages(ctx.config))


@cli.group()
Expand Down Expand Up @@ -441,11 +460,19 @@ def edit3(ctx: Context, playlist: str) -> None:
edit_playlist_in_editor(ctx.config, playlist)


@playlists.command(name="print")
@click.argument("playlist", type=str, nargs=1)
@click.pass_obj
def print3(ctx: Context, playlist: str) -> None:
"""Print a playlist (in JSON). Accepts a playlist's name."""
click.echo(dump_playlist(ctx.config, playlist))


@playlists.command(name="print-all")
@click.pass_obj
def print_all2(ctx: Context) -> None:
"""Print all playlists (in JSON.)"""
print(dump_playlists(ctx.config))
"""Print all playlists (in JSON)."""
click.echo(dump_playlists(ctx.config))


@playlists.command(name="set-cover")
Expand Down
10 changes: 10 additions & 0 deletions rose/collages.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ def add_release_to_collage(
update_cache_for_collages(c, [collage_name], force=True)


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


def dump_collages(c: Config) -> str:
out: list[dict[str, Any]] = []
for name in list_collages(c):
Expand Down
16 changes: 16 additions & 0 deletions rose/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ def add_track_to_playlist(
update_cache_for_playlists(c, [playlist_name], force=True)


def dump_playlist(c: Config, playlist_name: str) -> str:
pdata = get_playlist(c, playlist_name)
if pdata is None:
raise PlaylistDoesNotExistError(f"Playlist {playlist_name} does not exist")
tracks: list[dict[str, Any]] = []
for idx, trk in enumerate(pdata[1]):
tracks.append({"position": idx + 1, **trk.dump()})
return json.dumps(
{
"name": playlist_name,
"cover_image_path": str(pdata[0].cover_path) if pdata[0].cover_path else None,
"tracks": tracks,
}
)


def dump_playlists(c: Config) -> str:
out: list[dict[str, Any]] = []
for name in list_playlists(c):
Expand Down
8 changes: 8 additions & 0 deletions rose/releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class UnknownArtistRoleError(RoseExpectedError):
pass


def dump_release(c: Config, release_id: str) -> str:
try:
release, tracks = get_release(c, release_id) # type: ignore
except TypeError as e:
raise ReleaseDoesNotExistError(f"Release {release_id} does not exist") from e
return json.dumps({**release.dump(), "tracks": [t.dump() for t in tracks]})


def dump_releases(c: Config) -> str:
return json.dumps([r.dump() for r in list_releases(c)])

Expand Down

0 comments on commit e46241b

Please sign in to comment.