Skip to content

Commit dd1130d

Browse files
committed
Add an --all flag to fail if all images cannot be redacted
This affects the plan and run commands.
1 parent 840f633 commit dd1130d

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

imagedephi/main.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
click.option(
7171
"-r", "--recursive", is_flag=True, help="Apply the command to images in subdirectories"
7272
),
73+
click.option(
74+
"-a", "--require-all", "--all", is_flag=True,
75+
help="Return a failure code if not all images can be redacted; do not"
76+
" redact any images in this case."
77+
),
7378
]
7479

7580

@@ -79,7 +84,7 @@ def global_options(func):
7984
return func
8085

8186

82-
def _check_parent_params(ctx, profile, override_rules, recursive, quiet, verbose, log_file):
87+
def _check_parent_params(ctx, profile, override_rules, recursive, require_all, quiet, verbose, log_file):
8388
params = {
8489
"override_rules": (
8590
ctx.parent.params["override_rules"]
@@ -92,6 +97,9 @@ def _check_parent_params(ctx, profile, override_rules, recursive, quiet, verbose
9297
"recursive": (
9398
ctx.parent.params["recursive"] if ctx.parent.params["recursive"] else recursive
9499
),
100+
"require_all": (
101+
ctx.parent.params["require_all"] if ctx.parent.params["require_all"] else require_all
102+
),
95103
"quiet": ctx.parent.params["quiet"] if ctx.parent.params["quiet"] else quiet,
96104
"verbose": ctx.parent.params["verbose"] if ctx.parent.params["verbose"] else verbose,
97105
"log_file": ctx.parent.params["log_file"] if ctx.parent.params["log_file"] else log_file,
@@ -129,6 +137,7 @@ def imagedephi(
129137
override_rules: Path | None,
130138
profile: str,
131139
recursive: bool,
140+
require_all: bool,
132141
) -> None:
133142
"""Redact microscopy whole slide images."""
134143
if verbose or quiet or log_file:
@@ -173,6 +182,7 @@ def run(
173182
override_rules: Path | None,
174183
profile: str,
175184
recursive: bool,
185+
require_all: bool,
176186
rename: bool,
177187
quiet,
178188
verbose,
@@ -182,7 +192,7 @@ def run(
182192
file_list: Path,
183193
):
184194
"""Perform the redaction of images."""
185-
params = _check_parent_params(ctx, profile, override_rules, recursive, quiet, verbose, log_file)
195+
params = _check_parent_params(ctx, profile, override_rules, recursive, require_all, quiet, verbose, log_file)
186196
if params["verbose"] or params["quiet"] or params["log_file"]:
187197
set_logging_config(params["verbose"], params["quiet"], params["log_file"])
188198
file_contents = {}
@@ -193,6 +203,7 @@ def run(
193203
# cf_rename will only be checked if rename is false so default should be false.
194204
cf_rename: bool = False
195205
cf_recursive: bool = False
206+
cf_require_all: bool = False
196207
cf_index: int = 1
197208
if command_file:
198209
if command_file.suffix not in [".yaml", ".yml"]:
@@ -221,6 +232,9 @@ def run(
221232
cf_recursive = (
222233
bool(file_contents.get("recursive")) if "recursive" in file_contents else False
223234
)
235+
cf_require_all = (
236+
bool(file_contents.get("require_all")) if "require_all" in file_contents else False
237+
)
224238
cf_index = int(file_contents["index"]) if "index" in file_contents else 1
225239
if not input_paths and not command_inputs:
226240
raise click.BadParameter("At least one input path must be provided.")
@@ -246,6 +260,16 @@ def run(
246260
output_dir = Path.cwd()
247261
if not input_paths and not file_input_paths:
248262
raise click.BadParameter("At least one input path must be provided.")
263+
if params["require_all"] or cf_require_all:
264+
plan = show_redaction_plan(
265+
input_paths or command_inputs or file_input_paths,
266+
override_rules=params["override_rules"] or cf_override_rules,
267+
recursive=params["recursive"] or cf_recursive,
268+
profile=params["profile"] or cf_profile,
269+
require_all=True,
270+
)
271+
if plan.missing_rules:
272+
sys.exit(1)
249273
redact_images(
250274
input_paths or command_inputs or file_input_paths,
251275
output_dir or command_output,
@@ -282,14 +306,15 @@ def plan(
282306
profile: str,
283307
override_rules: Path | None,
284308
recursive: bool,
309+
require_all: bool,
285310
quiet,
286311
verbose,
287312
log_file,
288313
command_file: Path,
289314
file_list: Path,
290315
) -> None:
291316
"""Print the redaction plan for images."""
292-
params = _check_parent_params(ctx, profile, override_rules, recursive, quiet, verbose, log_file)
317+
params = _check_parent_params(ctx, profile, override_rules, recursive, require_all, quiet, verbose, log_file)
293318

294319
# Even if the user doesn't use the verbose flag, ensure logging level is set to
295320
# show info output of this command.
@@ -331,7 +356,7 @@ def plan(
331356
if not input_paths and not file_input_paths:
332357
raise click.BadParameter("At least one input path must be provided.")
333358

334-
show_redaction_plan(
359+
plan = show_redaction_plan(
335360
input_paths or command_inputs or file_input_paths,
336361
override_rules=(
337362
params["override_rules"] or command_params.get("override_rules")
@@ -349,6 +374,8 @@ def plan(
349374
else None
350375
),
351376
)
377+
if require_all and plan.missing_rules:
378+
sys.exit(1)
352379

353380

354381
@imagedephi.command

imagedephi/redact/redact.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ def _sort_data(data):
370370
def show_redaction_plan(
371371
input_paths: list[Path],
372372
override_rules: Path | None = None,
373-
recursive=False,
373+
recursive: bool = False,
374+
require_all: bool = False,
374375
profile="",
375376
limit: int | None = None,
376377
offset: int | None = None,
@@ -425,6 +426,8 @@ def _create_redaction_plan_report():
425426
redaction_plan_report.update(redaction_plan.report_plan()) # type: ignore
426427
if not redaction_plan.is_comprehensive():
427428
missing_rules = True
429+
if require_all:
430+
break
428431

429432
if not update:
430433
global redaction_plan_report

0 commit comments

Comments
 (0)