Eliminate api= kwarg and implicit creation of pluginmanager

This commit is contained in:
James R. Barlow
2023-10-24 00:54:30 -07:00
parent 67be4d1904
commit e8ae370ceb
8 changed files with 51 additions and 40 deletions
+2 -2
View File
@@ -14,7 +14,7 @@ from contextlib import suppress
from ocrmypdf import __version__
from ocrmypdf._plugin_manager import get_parser_options_plugins
from ocrmypdf._sync import run_pipeline
from ocrmypdf._sync import run_pipeline_cli
from ocrmypdf._validation import check_options
from ocrmypdf.api import Verbosity, configure_logging
from ocrmypdf.exceptions import (
@@ -71,7 +71,7 @@ def run(args=None):
with suppress(AttributeError, OSError):
signal.signal(signal.SIGBUS, sigbus)
result = run_pipeline(options=options, plugin_manager=plugin_manager)
result = run_pipeline_cli(options=options, plugin_manager=plugin_manager)
return result
+7 -7
View File
@@ -203,10 +203,12 @@ def manage_work_folder(*, work_folder: Path, retain: bool, print_location: bool)
def cli_exception_handler(
fn: Callable[[Any], ExitCode], *args, options: argparse.Namespace
fn: Callable[[argparse.Namespace, OcrmypdfPluginManager], ExitCode],
options: argparse.Namespace,
plugin_manager: OcrmypdfPluginManager,
) -> ExitCode:
try:
return fn(*args)
return fn(options, plugin_manager)
except KeyboardInterrupt:
if options.verbose >= 1:
log.exception("KeyboardInterrupt")
@@ -247,19 +249,17 @@ def cli_exception_handler(
def setup_pipeline(
options: argparse.Namespace,
plugin_manager: OcrmypdfPluginManager | None,
) -> tuple[Executor, OcrmypdfPluginManager]:
plugin_manager: OcrmypdfPluginManager,
) -> Executor:
# Any changes to options will not take effect for options that are already
# bound to function parameters in the pipeline. (For example
# options.input_file, options.pdf_renderer are already bound.)
if not options.jobs:
options.jobs = available_cpu_count()
if not plugin_manager:
plugin_manager = get_plugin_manager(options.plugins)
pikepdf_enable_mmap()
executor = setup_executor(plugin_manager)
return executor, plugin_manager
return executor
def preprocess(
+2 -2
View File
@@ -111,12 +111,12 @@ def exec_hocr_to_ocr_pdf(context: PdfContext, executor: Executor) -> Sequence[st
def run_hocr_to_ocr_pdf_pipeline(
options: argparse.Namespace,
*,
plugin_manager: OcrmypdfPluginManager | None,
plugin_manager: OcrmypdfPluginManager,
) -> ExitCode:
with manage_work_folder(
work_folder=options.input_folder, retain=True, print_location=False
) as work_folder:
executor, plugin_manager = setup_pipeline(options, plugin_manager)
executor = setup_pipeline(options, plugin_manager)
origin_pdf = work_folder / 'origin.pdf'
shutil.copy2(options.input_file, origin_pdf)
+2 -2
View File
@@ -94,12 +94,12 @@ def exec_pdf_to_hocr(context: PdfContext, executor: Executor) -> None:
def run_hocr_pipeline(
options: argparse.Namespace,
*,
plugin_manager: OcrmypdfPluginManager | None,
plugin_manager: OcrmypdfPluginManager,
) -> None:
with manage_work_folder(
work_folder=options.output_folder, retain=True, print_location=False
) as work_folder:
executor, plugin_manager = setup_pipeline(options, plugin_manager)
executor = setup_pipeline(options, plugin_manager)
shutil.copy2(options.input_file, work_folder / 'origin.pdf')
# Gather pdfinfo and create context
+21 -16
View File
@@ -161,7 +161,7 @@ def exec_concurrent(context: PdfContext, executor: Executor) -> Sequence[str]:
def _run_pipeline(
options: argparse.Namespace,
plugin_manager: OcrmypdfPluginManager | None,
plugin_manager: OcrmypdfPluginManager,
) -> ExitCode:
with manage_work_folder(
work_folder=Path(mkdtemp(prefix="ocrmypdf.io.")),
@@ -170,7 +170,7 @@ def _run_pipeline(
) as work_folder, manage_debug_log_handler(
options=options, work_folder=work_folder
):
executor, plugin_manager = setup_pipeline(options, plugin_manager)
executor = setup_pipeline(options, plugin_manager)
check_requested_output_file(options)
start_input_file, original_filename = create_input_file(options, work_folder)
@@ -201,26 +201,31 @@ def _run_pipeline(
return exitcode
def run_pipeline(
def run_pipeline_cli(
options: argparse.Namespace,
*,
plugin_manager: OcrmypdfPluginManager | None,
api: bool = False,
plugin_manager: OcrmypdfPluginManager,
) -> ExitCode:
"""Run the OCR pipeline.
"""Run the OCR pipeline with command line exception handling.
Args:
options: The parsed command line options.
plugin_manager: The plugin manager to use. If not provided, one will be
created.
api: If ``True``, the pipeline is being run from the API. This is used
to manage exceptions in a way appropriate for API or CLI usage.
For CLI (``api=False``), exceptions are printed and described;
for API use, they are propagated to the caller.
"""
if api:
return _run_pipeline(options, plugin_manager)
else:
return cli_exception_handler(
_run_pipeline, options, plugin_manager, options=options
)
return cli_exception_handler(_run_pipeline, options, plugin_manager)
def run_pipeline(
options: argparse.Namespace,
*,
plugin_manager: OcrmypdfPluginManager,
) -> ExitCode:
"""Run the OCR pipeline without command line exception handling.
Args:
options: The parsed command line options.
plugin_manager: The plugin manager to use. If not provided, one will be
created.
"""
return _run_pipeline(options, plugin_manager)
+7 -2
View File
@@ -14,9 +14,14 @@ from ocrmypdf._pipelines.common import (
configure_debug_logging,
)
from ocrmypdf._pipelines.pdf_to_hocr import run_hocr_pipeline
from ocrmypdf._pipelines.standard import run_pipeline
from ocrmypdf._pipelines.standard import run_pipeline, run_pipeline_cli
log = logging.getLogger(__name__)
__all__ = ['run_pipeline', 'run_hocr_pipeline', 'configure_debug_logging']
__all__ = [
'run_pipeline',
'run_pipeline_cli',
'run_hocr_pipeline',
'configure_debug_logging',
]
+3 -2
View File
@@ -21,7 +21,7 @@ import pluggy
from ocrmypdf._logging import PageNumberFilter
from ocrmypdf._plugin_manager import get_plugin_manager
from ocrmypdf._sync import run_hocr_pipeline, run_pipeline
from ocrmypdf._sync import run_hocr_pipeline, run_pipeline, run_pipeline_cli
from ocrmypdf._validation import check_options
from ocrmypdf.cli import ArgumentParser, get_parser
from ocrmypdf.helpers import is_iterable_notstr
@@ -371,7 +371,7 @@ def ocr( # noqa: D417
**create_options_kwargs,
)
check_options(options, plugin_manager)
return run_pipeline(options=options, plugin_manager=plugin_manager, api=True)
return run_pipeline(options=options, plugin_manager=plugin_manager)
def pdf_to_hocr(
@@ -465,4 +465,5 @@ __all__ = [
'ocr',
'pdf_to_hocr',
'run_pipeline',
'run_pipeline_cli',
]