Skip to content

Commit

Permalink
Merge pull request #178 from DigitalSlideArchive/170-recursive-redaction
Browse files Browse the repository at this point in the history
Allow redacting on subdirs
  • Loading branch information
naglepuff authored Jan 10, 2024
2 parents d6434a8 + 231f38c commit 4442b52
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
4 changes: 3 additions & 1 deletion imagedephi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,22 @@ def imagedephi(
type=click.Path(exists=True, file_okay=False, readable=True, writable=True, path_type=Path),
)
@click.option("--rename/--skip-rename", default=True)
@click.option("-r", "--recursive", is_flag=True, help="Redact images in subdirectories.")
@click.pass_obj
def run(
obj: ImagedephiContext,
input_path: Path,
output_dir: Path,
rename: bool,
recursive: bool,
verbose,
quiet,
log_file,
):
"""Perform the redaction of images."""
if verbose or quiet or log_file:
set_logging_config(verbose, quiet, log_file)
redact_images(input_path, output_dir, obj.override_rule_set, rename)
redact_images(input_path, output_dir, obj.override_rule_set, rename, recursive=recursive)


@imagedephi.command
Expand Down
19 changes: 15 additions & 4 deletions imagedephi/redact/redact.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_base_rules():
return base_rule_set


def iter_image_files(directory: Path) -> Generator[Path, None, None]:
def iter_image_files(directory: Path, recursive: bool = False) -> Generator[Path, None, None]:
"""Given a directory return an iterable of available images."""
for child in sorted(directory.iterdir()):
# Use first four bits to check if its a tiff file
Expand All @@ -49,6 +49,8 @@ def iter_image_files(directory: Path) -> Generator[Path, None, None]:
else:
if data in (b"II\x2a\x00", b"MM\x00\x2a", b"II\x2b\x00", b"MM\x00\x2b"):
yield child
elif child.is_dir() and recursive:
yield from iter_image_files(child, recursive)


def create_redact_dir(base_output_dir: Path) -> Path:
Expand All @@ -71,13 +73,16 @@ def redact_images(
override_rules: Ruleset | None = None,
rename: bool = True,
overwrite: bool = False,
recursive: bool = False,
) -> None:
base_rules = get_base_rules()
output_file_name_base = (
override_rules.output_file_name if override_rules else base_rules.output_file_name
)
# Convert to a list in order to get the length
images_to_redact = list(iter_image_files(input_path) if input_path.is_dir() else [input_path])
images_to_redact = list(
iter_image_files(input_path, recursive) if input_path.is_dir() else [input_path]
)
output_file_counter = 1
output_file_max = len(images_to_redact)
redact_dir = create_redact_dir(output_dir)
Expand All @@ -97,16 +102,22 @@ def redact_images(
redaction_plan.report_missing_rules()
else:
redaction_plan.execute_plan()
output_parent_dir = redact_dir
if recursive:
output_parent_dir = Path(
str(image_file).replace(str(input_path), str(redact_dir), 1)
).parent
output_parent_dir.mkdir(parents=True, exist_ok=True)
output_path = (
_get_output_path(
image_file,
redact_dir,
output_parent_dir,
output_file_name_base,
output_file_counter,
output_file_max,
)
if rename
else redact_dir / image_file.name
else output_parent_dir / image_file.name
)
redaction_plan.save(output_path, overwrite)
if output_file_counter == output_file_max:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,24 @@ def test_e2e_rename_flag(cli_runner, data_dir: Path, tmp_path: Path, rename: boo
else tmp_path / "Redacted_2023-05-12_12-12-53" / "test_image.tif"
)
assert output_file_name.exists()


@freeze_time("2024-01-04 10:48:00")
@pytest.mark.timeout(5)
@pytest.mark.parametrize(
"recursive,rename", [(True, True), (True, False), (False, False), (False, True)]
)
def test_e2e_recursive(cli_runner, data_dir: Path, tmp_path: Path, recursive: bool, rename: bool):
args = ["run", str(data_dir / "input"), "--output-dir", str(tmp_path)]
if recursive:
args.append("--recursive")
if rename:
args.append("--skip-rename")
result = cli_runner.invoke(main.imagedephi, args)

assert result.exit_code == 0
output_subdir = tmp_path / "Redacted_2024-01-04_10-48-00" / "svs"
assert output_subdir.exists() == recursive

if recursive:
assert len(list(output_subdir.iterdir()))

0 comments on commit 4442b52

Please sign in to comment.