Skip to content

Commit

Permalink
implement dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Nov 2, 2023
1 parent c9d9377 commit 373a1a0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 63 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ select = [
ignore = [
# Allow shadowing builtins on attributes.
"A003",
# Do not enforce max line length via linter. We have an autoformatter, and sometimes we
# intentionally turn formatting off.
"E501",
# Fixtures that do not return anything do not need leading underscore.
"PT004",
]
Expand Down
20 changes: 13 additions & 7 deletions rose/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,29 +396,35 @@ def remove_cover2(ctx: Context, playlist: str) -> None:

@cli.group()
def metadata() -> None:
"""Run metadata improvement tools"""
"""Run metadata improvement tools."""


@metadata.command()
# fmt: off
@click.argument("matcher", type=str, nargs=1)
@click.argument("actions", type=str, nargs=-1)
@click.option("--dry-run", "-d", is_flag=True, help="Display intended changes without applying them.") # noqa: E501
@click.option("--yes", "-y", is_flag=True, help="Bypass confirmation prompts.")
# fmt: on
@click.pass_obj
def run_rule(ctx: Context, matcher: str, actions: list[str], yes: bool) -> None:
"""Run an ad hoc metadata rule"""
def run_rule(ctx: Context, matcher: str, actions: list[str], dry_run: bool, yes: bool) -> None:
"""Run an ad hoc metadata rule."""
if not actions:
logger.info("No-Op: No actions passed")
return
rule = MetadataRule.parse(matcher, actions)
execute_metadata_rule(ctx.config, rule, confirm_yes=not yes)
execute_metadata_rule(ctx.config, rule, dry_run=dry_run, confirm_yes=not yes)


@metadata.command()
@click.option(
"--dry-run", "-d", is_flag=True, help="Display intended changes without applying them."
) # noqa: E501
@click.option("--yes", "-y", is_flag=True, help="Bypass confirmation prompts.")
@click.pass_obj
def run_stored_rules(ctx: Context, yes: bool) -> None:
"""Run the stored metadata rules defined in the config"""
execute_stored_metadata_rules(ctx.config, confirm_yes=not yes)
def run_stored_rules(ctx: Context, dry_run: bool, yes: bool) -> None:
"""Run the stored metadata rules defined in the config."""
execute_stored_metadata_rules(ctx.config, dry_run=dry_run, confirm_yes=not yes)


@cli.command()
Expand Down
57 changes: 34 additions & 23 deletions rose/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,23 @@ class InvalidReplacementValueError(RoseError):
pass


def execute_stored_metadata_rules(c: Config, confirm_yes: bool = False) -> None:
def execute_stored_metadata_rules(
c: Config,
*,
dry_run: bool = False,
confirm_yes: bool = False,
) -> None:
for rule in c.stored_metadata_rules:
click.secho(f"Executing stored metadata rule {rule}", dim=True)
click.echo()
execute_metadata_rule(c, rule, confirm_yes)
execute_metadata_rule(c, rule, dry_run=dry_run, confirm_yes=confirm_yes)


def execute_metadata_rule(
c: Config,
rule: MetadataRule,
*,
dry_run: bool = False,
confirm_yes: bool = False,
enter_number_to_confirm_above_count: int = 25,
) -> None:
Expand Down Expand Up @@ -223,29 +230,33 @@ def execute_metadata_rule(
click.echo()
return

# === Step 4: Ask user confirmation on the changes ===
# === Step 4: Display changes and ask for user confirmation ===

# Compute the text to display:
todisplay: list[tuple[str, list[Changes]]] = []
maxpathwidth = 0
for tags, changes in actionable_audiotags:
pathtext = str(tags.path).lstrip(str(c.music_source_dir) + "/")
if len(pathtext) >= 120:
pathtext = pathtext[:59] + ".." + pathtext[-59:]
maxpathwidth = max(maxpathwidth, len(pathtext))
todisplay.append((pathtext, changes))

# And then display it.
for pathtext, changes in todisplay:
click.secho(pathtext, underline=True)
for name, old, new in changes:
click.echo(f" {name}: ", nl=False)
click.secho(old, fg="red", nl=False)
click.echo(" -> ", nl=False)
click.secho(new, fg="green", bold=True)

# If we're dry-running, then abort here.
if dry_run:
return

# And then let's go for the confirmation.
if confirm_yes:
# Compute the text to display:
todisplay: list[tuple[str, list[Changes]]] = []
maxpathwidth = 0
for tags, changes in actionable_audiotags:
pathtext = str(tags.path).lstrip(str(c.music_source_dir) + "/")
if len(pathtext) >= 120:
pathtext = pathtext[:59] + ".." + pathtext[-59:]
maxpathwidth = max(maxpathwidth, len(pathtext))
todisplay.append((pathtext, changes))

# And then display it.
for pathtext, changes in todisplay:
click.secho(pathtext, underline=True)
for name, old, new in changes:
click.echo(f" {name}: ", nl=False)
click.secho(old, fg="red", nl=False)
click.echo(" -> ", nl=False)
click.secho(new, fg="green", bold=True)

# And then let's go for the confirmation.
click.echo()
if len(actionable_audiotags) > enter_number_to_confirm_above_count:
while True:
Expand Down
Loading

0 comments on commit 373a1a0

Please sign in to comment.