Reduces mypy errors in src/ocrmypdf and tests from 89 to 51. - Replace the `deprecation` package with stdlib `warnings.deprecated` (falling back to typing_extensions on <3.13); drop the dependency. - Add pypdfium2/uharfbuzz/pi_heif to mypy's ignore_missing_imports overrides (no upstream stubs); drop pluggy, which now ships py.typed. - Add a tests.* mypy override so test functions aren't required to annotate -> None. Real bugs found and fixed along the way, not just annotations: - OcrmypdfPluginManager had a `pluggy` property shadowing the `pluggy` module import within its own class body, breaking every `pluggy.PluginManager` annotation below it; renamed to `pluggy_manager`. - `_option_registry` was bolted onto OcrmypdfPluginManager from outside and read via `getattr(..., None)` instead of being a declared attribute; declared it properly. - ValidationCoordinator.__init__ was typed to accept a raw pluggy.PluginManager, but every caller passes the OcrmypdfPluginManager wrapper. - check_options_sidecar() did `options.output_file + '.txt'`, assuming output_file is always a str; would raise a raw TypeError if ever hit with a stream/bytes output. Added an explicit guard. - is_file_writable() called Path(test_file), which raises TypeError on a bytes path; fixed via os.fsdecode(). - copy_final() had a dead, unused `original_file` parameter; removed it. - run_hocr_pipeline() constructed PdfContext with the raw, untriaged input_file instead of the locally-copied origin_pdf, inconsistent with the other two pipelines. - _options.py had jbig2_threshold declared twice in the same model.
128 lines
4.3 KiB
Python
128 lines
4.3 KiB
Python
# SPDX-FileCopyrightText: 2019-2023 James R. Barlow
|
|
# SPDX-FileCopyrightText: 2019 Martin Wind
|
|
# SPDX-License-Identifier: MPL-2.0
|
|
|
|
"""Implements the concurrent and page synchronous parts of the pipeline."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import logging.handlers
|
|
from collections.abc import Sequence
|
|
from functools import partial
|
|
|
|
import PIL
|
|
|
|
from ocrmypdf._concurrent import Executor
|
|
from ocrmypdf._graft import OcrGrafter
|
|
from ocrmypdf._jobcontext import PageContext, PdfContext
|
|
from ocrmypdf._options import OcrOptions
|
|
from ocrmypdf._pipeline import copy_final
|
|
from ocrmypdf._pipelines._common import (
|
|
HOCRResult,
|
|
do_get_pdfinfo,
|
|
manage_work_folder,
|
|
postprocess,
|
|
report_output_pdf,
|
|
set_thread_pageno,
|
|
setup_pipeline,
|
|
worker_init,
|
|
)
|
|
from ocrmypdf._plugin_manager import OcrmypdfPluginManager
|
|
from ocrmypdf._progressbar import ProgressBar
|
|
from ocrmypdf.exceptions import ExitCode
|
|
from ocrmypdf.helpers import available_cpu_count
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
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.
|
|
return HOCRResult(pageno=page_context.pageno)
|
|
hocr_result = HOCRResult.from_json(hocr_json.read_text())
|
|
# hOCR path is passed directly to the grafting phase where fpdf2 renders it
|
|
hocr_result.textpdf = page_context.get_path('ocr_hocr.hocr')
|
|
return hocr_result
|
|
|
|
|
|
def exec_hocr_to_ocr_pdf(context: PdfContext, executor: Executor) -> Sequence[str]:
|
|
"""Convert hOCR files to OCR PDF."""
|
|
# Run exec_page_sync on every page
|
|
options = context.options
|
|
jobs = options.jobs or available_cpu_count()
|
|
max_workers = min(len(context.pdfinfo), jobs)
|
|
if max_workers > 1:
|
|
log.info("Continue processing %d pages concurrently", max_workers)
|
|
|
|
ocrgraft = OcrGrafter(context)
|
|
|
|
def graft_page(result: HOCRResult, pbar: ProgressBar):
|
|
"""Graft text only PDF on to main PDF's page."""
|
|
try:
|
|
set_thread_pageno(result.pageno + 1)
|
|
pbar.update()
|
|
ocrgraft.graft_page(
|
|
pageno=result.pageno,
|
|
image=result.pdf_page_from_image,
|
|
ocr_output=result.textpdf,
|
|
ocr_tree=result.ocr_tree,
|
|
autorotate_correction=result.orientation_correction,
|
|
)
|
|
pbar.update()
|
|
finally:
|
|
set_thread_pageno(None)
|
|
|
|
executor(
|
|
use_threads=options.use_threads,
|
|
max_workers=max_workers,
|
|
progress_kwargs=dict(
|
|
total=(2 * len(context.pdfinfo)),
|
|
desc='Grafting hOCR to PDF',
|
|
unit='page',
|
|
unit_scale=0.5,
|
|
disable=not options.progress_bar,
|
|
),
|
|
worker_initializer=partial(worker_init, PIL.Image.MAX_IMAGE_PIXELS),
|
|
task=_exec_hocrtransform_sync,
|
|
task_arguments=context.get_page_context_args(),
|
|
task_finished=graft_page,
|
|
)
|
|
|
|
pdf = ocrgraft.finalize()
|
|
messages: Sequence[str] = []
|
|
if options.output_type != 'none':
|
|
# PDF/A and metadata
|
|
log.info("Postprocessing...")
|
|
pdf, messages = postprocess(pdf, context, executor)
|
|
|
|
# Copy PDF file to destination
|
|
copy_final(pdf, options.output_file)
|
|
return messages
|
|
|
|
|
|
def run_hocr_to_ocr_pdf_pipeline(
|
|
options: OcrOptions,
|
|
*,
|
|
plugin_manager: OcrmypdfPluginManager,
|
|
) -> ExitCode:
|
|
"""Run pipeline to convert hOCR to final output PDF."""
|
|
# The _hocr_to_ocr_pdf() API requires work_folder: Path and stores it on
|
|
# options before this pipeline runs, so it is always set at this point.
|
|
assert options.work_folder is not None
|
|
with manage_work_folder(
|
|
work_folder=options.work_folder, retain=True, print_location=False
|
|
) as work_folder:
|
|
executor = setup_pipeline(options, plugin_manager)
|
|
origin_pdf = work_folder / 'origin.pdf'
|
|
|
|
# Gather pdfinfo and create context
|
|
pdfinfo = do_get_pdfinfo(origin_pdf, executor, options)
|
|
context = PdfContext(options, work_folder, origin_pdf, pdfinfo, plugin_manager)
|
|
plugin_manager.check_options(options=options)
|
|
optimize_messages = exec_hocr_to_ocr_pdf(context, executor)
|
|
|
|
return report_output_pdf(options, origin_pdf, optimize_messages)
|