Skip to content

Commit

Permalink
Move common CLI args and add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhelba committed Jan 27, 2023
1 parent 4b06804 commit b93bc1f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
54 changes: 27 additions & 27 deletions imagedephi/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
from dataclasses import dataclass
from pathlib import Path
from typing import TextIO
import webbrowser
Expand All @@ -13,11 +14,30 @@
from imagedephi.async_utils import run_coroutine, wait_for_port
from imagedephi.gui import app, shutdown_event
from imagedephi.redact import RuleSource, build_ruleset, redact_images, show_redaction_plan
from imagedephi.rules import RuleSet


@dataclass
class ImagedephiContext:
override_rule_set: RuleSet | None = None


@click.group
def imagedephi() -> None:
@click.option(
"-r",
"--override-rules",
type=click.File("r"),
help="Specify user-defined rules to override defaults",
)
@click.pass_context
def imagedephi(ctx: click.Context, override_rules: TextIO | None) -> None:
"""Redact microscopy whole slide images."""
obj = ImagedephiContext()
# Store separately, to preserve the type of "obj"
ctx.obj = obj

if override_rules:
obj.override_rule_set = build_ruleset(yaml.safe_load(override_rules), RuleSource.OVERRIDE)


@imagedephi.command
Expand All @@ -28,38 +48,18 @@ def imagedephi() -> None:
"output-dir",
type=click.Path(exists=True, file_okay=False, readable=True, writable=True, path_type=Path),
)
@click.option(
"-r",
"--override-rules",
type=click.File("r"),
help="Specify user-defined rules to override defaults",
)
def run(input_dir: Path, output_dir: Path, override_rules: TextIO | None):
@click.pass_obj
def run(obj: ImagedephiContext, input_dir: Path, output_dir: Path):
"""Redact images in a folder according to given rule sets."""
override_rule_set = (
build_ruleset(yaml.safe_load(override_rules), RuleSource.OVERRIDE)
if override_rules
else None
)
redact_images(input_dir, output_dir, override_rule_set)
redact_images(input_dir, output_dir, obj.override_rule_set)


@imagedephi.command
@click.argument("image", type=click.Path())
@click.option(
"-r",
"--override-rules",
type=click.File("r"),
help="Specify user-defined rules to override defaults",
)
def redaction_plan(image: click.Path, override_rules: TextIO | None) -> None:
@click.pass_obj
def plan(obj: ImagedephiContext, image: click.Path) -> None:
"""Print the redaction plan for a given image and rules."""
override_rule_set = (
build_ruleset(yaml.safe_load(override_rules), RuleSource.OVERRIDE)
if override_rules
else None
)
show_redaction_plan(image, override_rule_set)
show_redaction_plan(image, obj.override_rule_set)


@imagedephi.command
Expand Down
19 changes: 17 additions & 2 deletions tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def test_e2e_run(runner: CliRunner, data_dir: Path, tmp_path: Path) -> None:
result = runner.invoke(
main.imagedephi,
[
"--override-rules",
str(data_dir / "rules" / "example_user_rules.yml"),
"run",
str(data_dir / "input"),
str(tmp_path),
"-r",
str(data_dir / "rules" / "example_user_rules.yml"),
],
)
assert result.exit_code == 0
Expand All @@ -44,6 +44,21 @@ def test_e2e_run(runner: CliRunner, data_dir: Path, tmp_path: Path) -> None:
assert b"Redacted by ImageDePHI" in output_file_bytes


@pytest.mark.timeout(5)
def test_e2e_plan(runner: CliRunner, data_dir: Path, tmp_path: Path) -> None:
result = runner.invoke(
main.imagedephi,
[
"--override-rules",
str(data_dir / "rules" / "example_user_rules.yml"),
"plan",
str(data_dir / "input" / "test_image.tif"),
],
)
assert result.exit_code == 0
assert "Replace ImageDescription" in result.stdout


@pytest.mark.timeout(5)
def test_e2e_gui(
runner: CliRunner,
Expand Down

0 comments on commit b93bc1f

Please sign in to comment.