Support plugin invocation with API

This commit is contained in:
James R. Barlow
2020-05-02 03:34:31 -07:00
parent 8c9a8fc85c
commit e02f6c1e97
12 changed files with 472 additions and 429 deletions
+4 -4
View File
@@ -214,7 +214,7 @@ def no_outpdf(tmp_path):
def check_ocrmypdf(input_file, output_file, *args, env=None):
"""Run ocrmypdf and confirmed that a valid file was created"""
options = cli.parser.parse_args(
options = cli.get_parser().parse_args(
[str(input_file), str(output_file)]
+ [str(arg) for arg in args if arg is not None]
)
@@ -222,7 +222,7 @@ def check_ocrmypdf(input_file, output_file, *args, env=None):
if env:
options.tesseract_env = env
options.tesseract_env['_OCRMYPDF_TEST_INFILE'] = os.fspath(input_file)
result = api.run_pipeline(options, api=True)
result = api.run_pipeline(options, plugin_manager=None, api=True)
assert result == 0
assert os.path.exists(str(output_file)), "Output file not created"
@@ -238,7 +238,7 @@ def run_ocrmypdf_api(input_file, output_file, *args, env=None):
Does not currently have a way to manipulate the PATH except for Tesseract.
"""
options = cli.parser.parse_args(
options = cli.get_parser().parse_args(
[str(input_file), str(output_file)]
+ [str(arg) for arg in args if arg is not None]
)
@@ -253,7 +253,7 @@ def run_ocrmypdf_api(input_file, output_file, *args, env=None):
if options.tesseract_env:
assert all(isinstance(v, (str, bytes)) for v in options.tesseract_env.values())
return api.run_pipeline(options, api=False)
return api.run_pipeline(options, plugin_manager=None, api=False)
@pytest.helpers.register
+8 -9
View File
@@ -32,7 +32,7 @@ from pikepdf.models.metadata import decode_pdf_date
from ocrmypdf._jobcontext import PDFContext
from ocrmypdf._pipeline import convert_to_pdfa
from ocrmypdf.cli import parser
from ocrmypdf.cli import get_parser
from ocrmypdf.exceptions import ExitCode
from ocrmypdf.pdfa import SRGB_ICC_PROFILE, file_claims_pdfa, generate_pdfa_ps
from ocrmypdf.pdfinfo import PdfInfo
@@ -290,16 +290,15 @@ def test_kodak_toc(resources, outpdf, spoof_tesseract_noop):
def test_metadata_fixup_warning(resources, outdir, caplog):
from ocrmypdf.__main__ import parser
from ocrmypdf._pipeline import metadata_fixup
options = parser.parse_args(
options = get_parser().parse_args(
args=['--output-type', 'pdfa-2', 'graph.pdf', 'out.pdf']
)
copyfile(resources / 'graph.pdf', outdir / 'graph.pdf')
context = PDFContext(options, outdir, outdir / 'graph.pdf', None)
context = PDFContext(options, outdir, outdir / 'graph.pdf', None, None)
metadata_fixup(working_file=outdir / 'graph.pdf', context=context)
for record in caplog.records:
assert record.levelname != 'WARNING'
@@ -310,7 +309,7 @@ def test_metadata_fixup_warning(resources, outdir, caplog):
meta['prism2:publicationName'] = 'OCRmyPDF Test'
graph.save(outdir / 'graph_mod.pdf')
context = PDFContext(options, outdir, outdir / 'graph_mod.pdf', None)
context = PDFContext(options, outdir, outdir / 'graph_mod.pdf', None, None)
metadata_fixup(working_file=outdir / 'graph.pdf', context=context)
assert any(record.levelname == 'WARNING' for record in caplog.records)
@@ -326,11 +325,11 @@ def test_prevent_gs_invalid_xml(resources, outdir):
Title=b'String with trailing nul\x00'
)
options = parser.parse_args(
options = get_parser().parse_args(
args=['-j', '1', '--output-type', 'pdfa-2', 'a.pdf', 'b.pdf']
)
pdfinfo = PdfInfo(outdir / 'layers.rendered.pdf')
context = PDFContext(options, outdir, outdir / 'layers.rendered.pdf', pdfinfo)
context = PDFContext(options, outdir, outdir / 'layers.rendered.pdf', pdfinfo, None)
convert_to_pdfa(
str(outdir / 'layers.rendered.pdf'), str(outdir / 'pdfa.ps'), context
@@ -357,11 +356,11 @@ def test_malformed_docinfo(caplog, resources, outdir):
pike.trailer.Info = pikepdf.Stream(pike, b"<xml></xml>")
pike.save(outdir / 'layers.rendered.pdf', fix_metadata_version=False)
options = parser.parse_args(
options = get_parser().parse_args(
args=['-j', '1', '--output-type', 'pdfa-2', 'a.pdf', 'b.pdf']
)
pdfinfo = PdfInfo(outdir / 'layers.rendered.pdf')
context = PDFContext(options, outdir, outdir / 'layers.rendered.pdf', pdfinfo)
context = PDFContext(options, outdir, outdir / 'layers.rendered.pdf', pdfinfo, None)
convert_to_pdfa(
str(outdir / 'layers.rendered.pdf'), str(outdir / 'pdfa.ps'), context
+2 -2
View File
@@ -21,7 +21,7 @@ from unittest.mock import patch
import pytest
from ocrmypdf._validation import check_options
from ocrmypdf.cli import parser
from ocrmypdf.cli import get_parser
from ocrmypdf.exceptions import ExitCode, MissingDependencyError
from ocrmypdf.exec import unpaper
@@ -51,7 +51,7 @@ def spoof_unpaper_oldversion(tmp_path_factory):
def test_no_unpaper(resources, no_outpdf):
input_ = fspath(resources / "c02-22.pdf")
output = fspath(no_outpdf)
options = parser.parse_args(args=["--clean", input_, output])
options = get_parser().parse_args(args=["--clean", input_, output])
with patch("ocrmypdf.exec.unpaper.version") as mock_unpaper_version:
mock_unpaper_version.side_effect = FileNotFoundError("unpaper")
+4 -1
View File
@@ -23,6 +23,7 @@ import pytest
import ocrmypdf._validation as vd
from ocrmypdf.api import create_options
from ocrmypdf.cli import get_parser
from ocrmypdf.exceptions import BadArgsError, MissingDependencyError
from ocrmypdf.pdfinfo import PdfInfo
@@ -30,7 +31,9 @@ from ocrmypdf.pdfinfo import PdfInfo
def make_opts(input_file='a.pdf', output_file='b.pdf', language='eng', **kwargs):
if language is not None:
kwargs['language'] = language
return create_options(input_file=input_file, output_file=output_file, **kwargs)
return create_options(
input_file=input_file, output_file=output_file, parser=get_parser(), **kwargs
)
def test_hocr_notlatin_warning(caplog):