From 9b6b8c4ca00efe9e9b640bb1c73fddc86411d26c Mon Sep 17 00:00:00 2001 From: Michele Dolfi Date: Wed, 28 Aug 2024 13:14:21 +0200 Subject: [PATCH] raise a failure if examples fail Signed-off-by: Michele Dolfi --- examples/batch_convert.py | 10 +++++++++- examples/custom_convert.py | 11 ++++++++++- examples/export_figures.py | 10 ++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/examples/batch_convert.py b/examples/batch_convert.py index aaf5c49f..5d739b4b 100644 --- a/examples/batch_convert.py +++ b/examples/batch_convert.py @@ -49,6 +49,7 @@ def export_documents( f"of which {failure_count} failed " f"and {partial_success_count} were partially converted." ) + return success_count, partial_success_count, failure_count def main(): @@ -73,12 +74,19 @@ def main(): start_time = time.time() 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 _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__": main() diff --git a/examples/custom_convert.py b/examples/custom_convert.py index 2aaab377..df8d3642 100644 --- a/examples/custom_convert.py +++ b/examples/custom_convert.py @@ -42,6 +42,8 @@ def export_documents( f"Processed {success_count + failure_count} docs, of which {failure_count} failed" ) + return success_count, failure_count + def main(): logging.basicConfig(level=logging.INFO) @@ -114,12 +116,19 @@ def main(): start_time = time.time() 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 _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__": main() diff --git a/examples/export_figures.py b/examples/export_figures.py index 277e1c26..1928e43a 100644 --- a/examples/export_figures.py +++ b/examples/export_figures.py @@ -41,10 +41,13 @@ def main(): conv_results = doc_converter.convert(input_files) + success_count = 0 + failure_count = 0 output_dir.mkdir(parents=True, exist_ok=True) for conv_res in conv_results: if conv_res.status != ConversionStatus.SUCCESS: _log.info(f"Document {conv_res.input.file} failed to convert.") + failure_count += 1 continue doc_filename = conv_res.input.file.stem @@ -66,10 +69,17 @@ def main(): with element_image_filename.open("wb") as fp: image.save(fp, "PNG") + success_count += 1 + end_time = time.time() - start_time _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__": main()