From 8230d6de02c357d9c86c0c79a3a0be6d1f590b25 Mon Sep 17 00:00:00 2001 From: naglepuff Date: Wed, 3 Jan 2024 18:23:19 -0500 Subject: [PATCH 1/5] Allow redacting on subdirs --- imagedephi/main.py | 4 +++- imagedephi/redact/redact.py | 13 ++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/imagedephi/main.py b/imagedephi/main.py index 44496d3e..b8a83b36 100644 --- a/imagedephi/main.py +++ b/imagedephi/main.py @@ -112,12 +112,14 @@ 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, @@ -125,7 +127,7 @@ def run( """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 diff --git a/imagedephi/redact/redact.py b/imagedephi/redact/redact.py index 313aa4b7..deaa32e6 100644 --- a/imagedephi/redact/redact.py +++ b/imagedephi/redact/redact.py @@ -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 @@ -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: @@ -71,13 +73,14 @@ 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) @@ -97,10 +100,14 @@ 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, From 6910fdad413586407b954a607b1423aad4543a71 Mon Sep 17 00:00:00 2001 From: naglepuff Date: Thu, 4 Jan 2024 10:43:55 -0500 Subject: [PATCH 2/5] Format --- imagedephi/redact/redact.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/imagedephi/redact/redact.py b/imagedephi/redact/redact.py index deaa32e6..af4354bd 100644 --- a/imagedephi/redact/redact.py +++ b/imagedephi/redact/redact.py @@ -80,7 +80,9 @@ def redact_images( 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, recursive) 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) @@ -102,7 +104,9 @@ def redact_images( 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 = 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( From 9b388f2d334476d0ae7b7e1f650fcfbbb2230d4b Mon Sep 17 00:00:00 2001 From: naglepuff Date: Thu, 4 Jan 2024 10:56:03 -0500 Subject: [PATCH 3/5] Add test for recursive flag --- tests/test_e2e.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_e2e.py b/tests/test_e2e.py index bdbf6153..86b58af4 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -151,3 +151,17 @@ 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", [True, False]) +def test_e2e_recursive(cli_runner, data_dir: Path, tmp_path: Path, recursive: bool): + args = ["run", str(data_dir / "input"), "--output-dir", str(tmp_path)] + if recursive: + args.append("--recursive") + 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 From 71119f85ec548ec91bc73260534f8c404ae1c763 Mon Sep 17 00:00:00 2001 From: naglepuff Date: Tue, 9 Jan 2024 12:36:32 -0500 Subject: [PATCH 4/5] Write to proper directories when skipping rename --- imagedephi/redact/redact.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imagedephi/redact/redact.py b/imagedephi/redact/redact.py index af4354bd..b3db1316 100644 --- a/imagedephi/redact/redact.py +++ b/imagedephi/redact/redact.py @@ -117,7 +117,7 @@ def redact_images( 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: From 231f38c84f2cf9f6a4ced1d2283c217c73cc9564 Mon Sep 17 00:00:00 2001 From: naglepuff Date: Tue, 9 Jan 2024 12:45:14 -0500 Subject: [PATCH 5/5] Update recursive test for interaction with rename --- tests/test_e2e.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/test_e2e.py b/tests/test_e2e.py index 86b58af4..2be5ff08 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -155,13 +155,20 @@ def test_e2e_rename_flag(cli_runner, data_dir: Path, tmp_path: Path, rename: boo @freeze_time("2024-01-04 10:48:00") @pytest.mark.timeout(5) -@pytest.mark.parametrize("recursive", [True, False]) -def test_e2e_recursive(cli_runner, data_dir: Path, tmp_path: Path, recursive: bool): +@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()))