From c1fdd8d483c3c2211221f355c5cb2bc9dc21beac Mon Sep 17 00:00:00 2001 From: Arnau Casau Date: Tue, 3 Dec 2024 14:27:09 +0100 Subject: [PATCH] refactor loop --- tools/verify_images.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tools/verify_images.py b/tools/verify_images.py index 02862b6..6bbc378 100644 --- a/tools/verify_images.py +++ b/tools/verify_images.py @@ -55,13 +55,18 @@ def validate_image(file_path: str) -> tuple[str, list[str]]: options: list[str] = [] for line_index, line in enumerate(lines): - if image_found and is_option(line): - options.append(line) - continue - - if image_found and not is_valid_image(options): - image_line = line_index - len(options) - invalid_images.append(f"- Error in line {image_line}: {lines[image_line-1].strip()}") + if image_found: + if is_option(line): + options.append(line) + continue + + # Else, the prior image_found has no more options so we should determine if it was valid. + # + # Note that, either way, we do not early exit out of the loop iteration because this `line` + # might be the start of a new image. + if not is_valid_image(options): + image_line = line_index - len(options) + invalid_images.append(f"- Error in line {image_line}: {lines[image_line-1].strip()}") image_found = is_image(line) options = [] @@ -75,7 +80,7 @@ def main() -> None: with multiprocessing.Pool() as pool: results = pool.map(validate_image, files) - failed_files = [x for x in results if len(x[1])] + failed_files = {file: image_errors for file, image_errors in results if image_errors} if not len(failed_files): print("✅ All images have alt text") @@ -83,8 +88,8 @@ def main() -> None: print("💔 Some images are missing the alt text", file=sys.stderr) - for filename, image_errors in failed_files: - print(f"\nErrors found in {filename}:", file=sys.stderr) + for file, image_errors in failed_files.items(): + print(f"\nErrors found in {file}:", file=sys.stderr) for image_error in image_errors: print(image_error, file=sys.stderr)