Skip to content

Commit

Permalink
trigger cache update after rule application
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Nov 2, 2023
1 parent 2dae4cb commit ca09df6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
9 changes: 9 additions & 0 deletions rose/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,15 @@ def get_release_source_path_from_id(c: Config, uuid: str) -> Path | None:
return None


def get_release_source_paths_from_ids(c: Config, uuids: list[str]) -> list[Path]:
with connect(c) as conn:
cursor = conn.execute(
f"SELECT source_path FROM releases WHERE id IN ({','.join(['?']*len(uuids))})",
uuids,
)
return [Path(r["source_path"]) for r in cursor]


def get_track_filename(c: Config, uuid: str) -> str | None:
with connect(c) as conn:
cursor = conn.execute(
Expand Down
9 changes: 9 additions & 0 deletions rose/cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get_release,
get_release_id_from_virtual_dirname,
get_release_source_path_from_id,
get_release_source_paths_from_ids,
get_release_virtual_dirname_from_id,
get_track_filename,
label_exists,
Expand Down Expand Up @@ -1155,6 +1156,14 @@ def test_get_release_source_path_dirname_from_id(config: Config) -> None:
assert str(get_release_source_path_from_id(config, "r1")).endswith("/source/r1")


@pytest.mark.usefixtures("seeded_cache")
def test_get_release_source_paths_dirname_from_ids(config: Config) -> None:
assert get_release_source_paths_from_ids(config, ["r1", "r2"]) == [
config.music_source_dir / "r1",
config.music_source_dir / "r2",
]


@pytest.mark.usefixtures("seeded_cache")
def test_get_track_filename(config: Config) -> None:
assert get_track_filename(config, "t1") == "01.m4a"
Expand Down
19 changes: 16 additions & 3 deletions rose/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
import click

from rose.audiotags import AudioTags
from rose.cache import connect
from rose.cache import (
connect,
get_release_source_paths_from_ids,
update_cache_for_releases,
)
from rose.common import RoseError
from rose.config import Config
from rose.rule_parser import (
Expand Down Expand Up @@ -257,7 +261,7 @@ def execute_metadata_rule(
break
else:
if not click.confirm(
f"Write changes to {click.style(len(actionable_audiotags), bold=True)} tracks? ",
f"Write changes to {click.style(len(actionable_audiotags), bold=True)} tracks?",
default=True,
prompt_suffix="",
):
Expand All @@ -267,16 +271,25 @@ def execute_metadata_rule(

# === Step 5: Flush writes to disk ===

changed_release_ids: set[str] = set()
for tags, changes in actionable_audiotags:
if tags.release_id:
changed_release_ids.add(tags.release_id)
logger.info(f"Writing tag changes {tags.path} (rule {rule}).")
pathtext = str(tags.path).lstrip(str(c.music_source_dir) + "/")
logger.debug(
f"{tags.path} changes: {' //// '.join([str(x)+' -> '+str(y) for _, x, y in changes])}"
f"{pathtext} changes: {' //// '.join([str(x)+' -> '+str(y) for _, x, y in changes])}"
)
tags.flush()

click.echo()
click.echo(f"Applied tag changes to {len(actionable_audiotags)} tracks!")

# == Step 6: Trigger cache update ===

source_paths = get_release_source_paths_from_ids(c, list(changed_release_ids))
update_cache_for_releases(c, source_paths)


def matches_pattern(pattern: str, value: str | int | None) -> bool:
value = str(value) if value is not None else ""
Expand Down

0 comments on commit ca09df6

Please sign in to comment.