From a31f17bb9d44c961b01a3d7492162962c661ee5d Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Sat, 21 Oct 2023 01:34:41 -0700 Subject: [PATCH] Update comments and make worker functions private --- src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py | 10 ++++++---- src/ocrmypdf/_pipelines/ocr.py | 5 ++--- src/ocrmypdf/_pipelines/pdf_to_hocr.py | 5 +++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py b/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py index 7781b5a2..a0cd67dd 100644 --- a/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py +++ b/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py @@ -39,7 +39,8 @@ from ocrmypdf.exceptions import ExitCode log = logging.getLogger(__name__) -def exec_hocrtransform_sync(page_context: PageContext) -> HOCRResult: +def _exec_hocrtransform_sync(page_context: PageContext) -> HOCRResult: + """Process each page.""" hocr_json = page_context.get_path('hocr.json') if not hocr_json.exists(): # No hOCR file, so no OCR was performed on this page. @@ -52,7 +53,7 @@ def exec_hocrtransform_sync(page_context: PageContext) -> HOCRResult: def exec_hocr_to_ocr_pdf(context: PdfContext, executor: Executor) -> Sequence[str]: - """Execute the OCR pipeline concurrently and output hOCR.""" + """Convert hOCR files to OCR PDF.""" # Run exec_page_sync on every page options = context.options max_workers = min(len(context.pdfinfo), options.jobs) @@ -62,7 +63,7 @@ def exec_hocr_to_ocr_pdf(context: PdfContext, executor: Executor) -> Sequence[st ocrgraft = OcrGrafter(context) def graft_page(result: HOCRResult, pbar: ProgressBar): - """After OCR is complete for a page, update the PDF.""" + """Graft text only PDF on to main PDF's page.""" try: set_thread_pageno(result.pageno + 1) pbar.update() @@ -87,7 +88,7 @@ def exec_hocr_to_ocr_pdf(context: PdfContext, executor: Executor) -> Sequence[st disable=not options.progress_bar, ), worker_initializer=partial(worker_init, PIL.Image.MAX_IMAGE_PIXELS), - task=exec_hocrtransform_sync, + task=_exec_hocrtransform_sync, task_arguments=context.get_page_context_args(), task_finished=graft_page, ) @@ -109,6 +110,7 @@ def run_hocr_to_ocr_pdf_pipeline( *, plugin_manager: OcrmypdfPluginManager, ) -> ExitCode: + """Run pipeline to convert hOCR to final output PDF.""" with manage_work_folder( work_folder=options.work_folder, retain=True, print_location=False ) as work_folder: diff --git a/src/ocrmypdf/_pipelines/ocr.py b/src/ocrmypdf/_pipelines/ocr.py index 1b6fbc97..cb37bf0d 100644 --- a/src/ocrmypdf/_pipelines/ocr.py +++ b/src/ocrmypdf/_pipelines/ocr.py @@ -69,7 +69,7 @@ def _image_to_ocr_text( return ocr_out, text_out -def exec_page_sync(page_context: PageContext) -> PageResult: +def _exec_page_sync(page_context: PageContext) -> PageResult: """Execute a pipeline for a single page synchronously.""" set_thread_pageno(page_context.pageno + 1) @@ -92,7 +92,6 @@ def exec_page_sync(page_context: PageContext) -> PageResult: def exec_concurrent(context: PdfContext, executor: Executor) -> Sequence[str]: """Execute the OCR pipeline concurrently.""" - # Run exec_page_sync on every page options = context.options max_workers = min(len(context.pdfinfo), options.jobs) if max_workers > 1: @@ -128,7 +127,7 @@ def exec_concurrent(context: PdfContext, executor: Executor) -> Sequence[str]: disable=not options.progress_bar, ), worker_initializer=partial(worker_init, PIL.Image.MAX_IMAGE_PIXELS), - task=exec_page_sync, + task=_exec_page_sync, task_arguments=context.get_page_context_args(), task_finished=update_page, ) diff --git a/src/ocrmypdf/_pipelines/pdf_to_hocr.py b/src/ocrmypdf/_pipelines/pdf_to_hocr.py index 891878e6..fdc3b687 100644 --- a/src/ocrmypdf/_pipelines/pdf_to_hocr.py +++ b/src/ocrmypdf/_pipelines/pdf_to_hocr.py @@ -39,7 +39,7 @@ from ocrmypdf._validation import ( log = logging.getLogger(__name__) -def exec_page_hocr_sync(page_context: PageContext) -> HOCRResult: +def _exec_page_hocr_sync(page_context: PageContext) -> HOCRResult: """Execute a pipeline for a single page hOCR.""" set_thread_pageno(page_context.pageno + 1) @@ -80,7 +80,7 @@ def exec_pdf_to_hocr(context: PdfContext, executor: Executor) -> None: disable=not options.progress_bar, ), worker_initializer=partial(worker_init, PIL.Image.MAX_IMAGE_PIXELS), - task=exec_page_hocr_sync, + task=_exec_page_hocr_sync, task_arguments=context.get_page_context_args(), ) @@ -90,6 +90,7 @@ def run_hocr_pipeline( *, plugin_manager: OcrmypdfPluginManager, ) -> None: + """Run pipeline to output hOCR.""" with manage_work_folder( work_folder=options.output_folder, retain=True, print_location=False ) as work_folder: