raise a failure if examples fail

Signed-off-by: Michele Dolfi <dol@zurich.ibm.com>
This commit is contained in:
Michele Dolfi 2024-08-28 13:14:21 +02:00
parent 2ad85fdf97
commit 9b6b8c4ca0
3 changed files with 29 additions and 2 deletions

View File

@ -49,6 +49,7 @@ def export_documents(
f"of which {failure_count} failed " f"of which {failure_count} failed "
f"and {partial_success_count} were partially converted." f"and {partial_success_count} were partially converted."
) )
return success_count, partial_success_count, failure_count
def main(): def main():
@ -73,12 +74,19 @@ def main():
start_time = time.time() start_time = time.time()
conv_results = doc_converter.convert(input) conv_results = doc_converter.convert(input)
export_documents(conv_results, output_dir=Path("./scratch")) success_count, partial_success_count, failure_count = export_documents(
conv_results, output_dir=Path("./scratch")
)
end_time = time.time() - start_time end_time = time.time() - start_time
_log.info(f"All documents were converted in {end_time:.2f} seconds.") _log.info(f"All documents were converted in {end_time:.2f} seconds.")
if failure_count > 0:
raise RuntimeError(
f"The example failed converting {failure_count} on {len(input_doc_paths)}."
)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -42,6 +42,8 @@ def export_documents(
f"Processed {success_count + failure_count} docs, of which {failure_count} failed" f"Processed {success_count + failure_count} docs, of which {failure_count} failed"
) )
return success_count, failure_count
def main(): def main():
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
@ -114,12 +116,19 @@ def main():
start_time = time.time() start_time = time.time()
conv_results = doc_converter.convert(input) conv_results = doc_converter.convert(input)
export_documents(conv_results, output_dir=Path("./scratch")) success_count, failure_count = export_documents(
conv_results, output_dir=Path("./scratch")
)
end_time = time.time() - start_time end_time = time.time() - start_time
_log.info(f"All documents were converted in {end_time:.2f} seconds.") _log.info(f"All documents were converted in {end_time:.2f} seconds.")
if failure_count > 0:
raise RuntimeError(
f"The example failed converting {failure_count} on {len(input_doc_paths)}."
)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -41,10 +41,13 @@ def main():
conv_results = doc_converter.convert(input_files) conv_results = doc_converter.convert(input_files)
success_count = 0
failure_count = 0
output_dir.mkdir(parents=True, exist_ok=True) output_dir.mkdir(parents=True, exist_ok=True)
for conv_res in conv_results: for conv_res in conv_results:
if conv_res.status != ConversionStatus.SUCCESS: if conv_res.status != ConversionStatus.SUCCESS:
_log.info(f"Document {conv_res.input.file} failed to convert.") _log.info(f"Document {conv_res.input.file} failed to convert.")
failure_count += 1
continue continue
doc_filename = conv_res.input.file.stem doc_filename = conv_res.input.file.stem
@ -66,10 +69,17 @@ def main():
with element_image_filename.open("wb") as fp: with element_image_filename.open("wb") as fp:
image.save(fp, "PNG") image.save(fp, "PNG")
success_count += 1
end_time = time.time() - start_time end_time = time.time() - start_time
_log.info(f"All documents were converted in {end_time:.2f} seconds.") _log.info(f"All documents were converted in {end_time:.2f} seconds.")
if failure_count > 0:
raise RuntimeError(
f"The example failed converting {failure_count} on {len(input_doc_paths)}."
)
if __name__ == "__main__": if __name__ == "__main__":
main() main()