Skip to content

Commit

Permalink
refactor loop
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaucasau committed Dec 3, 2024
1 parent b30929e commit c1fdd8d
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions tools/verify_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -75,16 +80,16 @@ 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")
sys.exit(0)

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)
Expand Down

0 comments on commit c1fdd8d

Please sign in to comment.