From 9a6150ae535221345a6f69e720bb3fa97440badd Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Sun, 27 Oct 2024 12:09:33 -0700 Subject: [PATCH] Refactor get_pdfinfo common code --- src/ocrmypdf/_pipelines/_common.py | 13 +++++++++++++ src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py | 12 ++---------- src/ocrmypdf/_pipelines/ocr.py | 13 ++----------- src/ocrmypdf/_pipelines/pdf_to_hocr.py | 15 ++++----------- 4 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/ocrmypdf/_pipelines/_common.py b/src/ocrmypdf/_pipelines/_common.py index 24077bdf..0044d0a4 100644 --- a/src/ocrmypdf/_pipelines/_common.py +++ b/src/ocrmypdf/_pipelines/_common.py @@ -33,6 +33,7 @@ from ocrmypdf._pipeline import ( generate_postscript_stub, get_orientation_correction, get_pdf_save_settings, + get_pdfinfo, optimize_pdf, preprocess_clean, preprocess_deskew, @@ -315,6 +316,18 @@ def setup_pipeline( return executor +def do_get_pdfinfo(pdf, executor, options): + return get_pdfinfo( + pdf, + executor=executor, + detailed_analysis=options.redo_ocr, + progbar=options.progress_bar, + max_workers=options.jobs, + use_threads=options.use_threads, + check_pages=options.pages, + ) + + def preprocess( page_context: PageContext, image: Path, diff --git a/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py b/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py index 910915a0..c6fb5e38 100644 --- a/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py +++ b/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py @@ -19,11 +19,11 @@ from ocrmypdf._graft import OcrGrafter from ocrmypdf._jobcontext import PageContext, PdfContext from ocrmypdf._pipeline import ( copy_final, - get_pdfinfo, render_hocr_page, ) from ocrmypdf._pipelines._common import ( HOCRResult, + do_get_pdfinfo, manage_work_folder, postprocess, report_output_pdf, @@ -117,15 +117,7 @@ def run_hocr_to_ocr_pdf_pipeline( origin_pdf = work_folder / 'origin.pdf' # Gather pdfinfo and create context - pdfinfo = get_pdfinfo( - origin_pdf, - executor=executor, - detailed_analysis=options.redo_ocr, - progbar=options.progress_bar, - max_workers=options.jobs, - use_threads=options.use_threads, - check_pages=options.pages, - ) + pdfinfo = do_get_pdfinfo(origin_pdf, executor, options) context = PdfContext(options, work_folder, origin_pdf, pdfinfo, plugin_manager) plugin_manager.hook.check_options(options=options) optimize_messages = exec_hocr_to_ocr_pdf(context, executor) diff --git a/src/ocrmypdf/_pipelines/ocr.py b/src/ocrmypdf/_pipelines/ocr.py index d6dc00a0..e742dce0 100644 --- a/src/ocrmypdf/_pipelines/ocr.py +++ b/src/ocrmypdf/_pipelines/ocr.py @@ -21,7 +21,6 @@ from ocrmypdf._graft import OcrGrafter from ocrmypdf._jobcontext import PageContext, PdfContext from ocrmypdf._pipeline import ( copy_final, - get_pdfinfo, is_ocr_required, merge_sidecars, ocr_engine_hocr, @@ -33,6 +32,7 @@ from ocrmypdf._pipeline import ( from ocrmypdf._pipelines._common import ( PageResult, cli_exception_handler, + do_get_pdfinfo, manage_debug_log_handler, manage_work_folder, postprocess, @@ -171,16 +171,7 @@ def _run_pipeline( ) # Gather pdfinfo and create context - pdfinfo = get_pdfinfo( - origin_pdf, - executor=executor, - detailed_analysis=options.redo_ocr, - progbar=options.progress_bar, - max_workers=options.jobs, - use_threads=options.use_threads, - check_pages=options.pages, - ) - + pdfinfo = do_get_pdfinfo(origin_pdf, executor, options) context = PdfContext(options, work_folder, origin_pdf, pdfinfo, plugin_manager) # Validate options are okay for this pdf diff --git a/src/ocrmypdf/_pipelines/pdf_to_hocr.py b/src/ocrmypdf/_pipelines/pdf_to_hocr.py index 7005d4ff..457d144c 100644 --- a/src/ocrmypdf/_pipelines/pdf_to_hocr.py +++ b/src/ocrmypdf/_pipelines/pdf_to_hocr.py @@ -17,13 +17,13 @@ import PIL from ocrmypdf._concurrent import Executor from ocrmypdf._jobcontext import PageContext, PdfContext from ocrmypdf._pipeline import ( - get_pdfinfo, is_ocr_required, ocr_engine_hocr, validate_pdfinfo_options, ) from ocrmypdf._pipelines._common import ( HOCRResult, + do_get_pdfinfo, manage_work_folder, process_page, set_thread_pageno, @@ -94,18 +94,11 @@ def run_hocr_pipeline( work_folder=options.output_folder, retain=True, print_location=False ) as work_folder: executor = setup_pipeline(options, plugin_manager) - shutil.copy2(options.input_file, work_folder / 'origin.pdf') + origin_pdf = work_folder / 'origin.pdf' + shutil.copy2(options.input_file, origin_pdf) # Gather pdfinfo and create context - pdfinfo = get_pdfinfo( - options.input_file, - executor=executor, - detailed_analysis=options.redo_ocr, - progbar=options.progress_bar, - max_workers=options.jobs, - use_threads=options.use_threads, - check_pages=options.pages, - ) + pdfinfo = do_get_pdfinfo(origin_pdf, executor, options) context = PdfContext( options, work_folder, options.input_file, pdfinfo, plugin_manager )