def samples()

in community/identity-form-autofiller-python/src/main.py [0:0]


def samples():
    def get_samples(
        parent_path: Path,
        rel_dir: str = "",
    ) -> Iterator[tuple[str, list[str]]]:
        paged_samples: list[str] = []
        for path in parent_path.glob("*"):
            if path.is_dir():
                sub_dir = f"{rel_dir}/{path.name}" if rel_dir else path.name
                yield from get_samples(path, sub_dir)
            elif path.suffix[1:].lower() in SUFFIXES:
                if rel_dir:
                    paged_samples.append(f"{rel_dir}/{path.name}")
                else:
                    yield path.stem, [path.name]
        if paged_samples:
            yield parent_path.name, sorted(paged_samples)

    # See https://cloud.google.com/document-ai/docs/file-types#other_processors
    SUFFIXES = {"pdf", "tiff", "tif", "gif", "jpeg", "jpg", "png", "bmp", "webp"}
    samples = dict(get_samples(SAMPLES_PATH))

    # Note: jsonify sorts dictionaries by key by default
    return jsonify(samples=samples)