From 4f4ad0fb7602f4c10e9acf17ae67e2d0b778fe2c Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Tue, 2 Jun 2020 01:49:47 -0700 Subject: [PATCH] Convert tesseract_big_image_error to plugin --- tests/plugins/tesseract_big_image_error.py | 61 +++++++++++++++++ tests/spoof/tesseract_big_image_error.py | 77 ---------------------- tests/test_main.py | 12 +--- 3 files changed, 64 insertions(+), 86 deletions(-) create mode 100644 tests/plugins/tesseract_big_image_error.py delete mode 100755 tests/spoof/tesseract_big_image_error.py diff --git a/tests/plugins/tesseract_big_image_error.py b/tests/plugins/tesseract_big_image_error.py new file mode 100644 index 00000000..f040855c --- /dev/null +++ b/tests/plugins/tesseract_big_image_error.py @@ -0,0 +1,61 @@ +# © 2020 James R. Barlow: github.com/jbarlow83 +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +from subprocess import CalledProcessError +from unittest.mock import patch + +from ocrmypdf import hookimpl +from ocrmypdf.builtin_plugins.tesseract_ocr import TesseractOcrEngine + + +def raise_size_exception(*args, **kwargs): + raise CalledProcessError( + 1, + 'tesseract', + output=b"Image too large: (33830, 14959)\nError during processing.", + stderr=b"", + ) + + +class BigImageErrorOcrEngine(TesseractOcrEngine): + @staticmethod + def get_orientation(input_file, options): + with patch('ocrmypdf.exec.tesseract.run', new=raise_size_exception): + return TesseractOcrEngine.get_orientation(input_file, options) + + @staticmethod + def generate_hocr(input_file, output_hocr, output_text, options): + with patch('ocrmypdf.exec.tesseract.run', new=raise_size_exception): + TesseractOcrEngine.generate_hocr( + input_file, output_hocr, output_text, options + ) + + @staticmethod + def generate_pdf(input_file, output_pdf, output_text, options): + with patch('ocrmypdf.exec.tesseract.run', new=raise_size_exception): + TesseractOcrEngine.generate_pdf( + input_file, output_pdf, output_text, options + ) + + +@hookimpl +def get_ocr_engine(): + return BigImageErrorOcrEngine() diff --git a/tests/spoof/tesseract_big_image_error.py b/tests/spoof/tesseract_big_image_error.py deleted file mode 100755 index 8b710bee..00000000 --- a/tests/spoof/tesseract_big_image_error.py +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env python3 -# © 2016 James R. Barlow: github.com/jbarlow83 -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - - -import sys - -VERSION_STRING = '''tesseract 4.0.0 - leptonica-1.77.0 - libjpeg 9c : libpng 1.6.35 : libtiff 4.0.10 : zlib 1.2.11 : libopenjp2 2.3.0 - Found AVX2 - Found AVX - Found SSE -SPOOFED: return error claiming image too big -''' - -"""Simulates an error of Tesseract failing on attempts to process large images - -""" - - -def main(): - if sys.argv[1] == '--version': - print(VERSION_STRING, file=sys.stderr) - sys.exit(0) - elif sys.argv[1] == '--list-langs': - print('List of available languages (1):\neng\n', file=sys.stderr) - sys.exit(0) - elif sys.argv[-2] == '--print-parameters': - print('A parameter list would go here\ntextonly_pdf 0\n', file=sys.stderr) - sys.exit(0) - elif sys.argv[-2] == 'hocr': - print( - "Image too large: (33830, 14959)\n" "Error during processing.", - file=sys.stderr, - ) - sys.exit(1) - elif sys.argv[-2] == 'pdf': - print( - "Image too large: (33830, 14959)\n" "Error during processing.", - file=sys.stderr, - ) - sys.exit(1) - elif sys.argv[-1] == 'stdout': - print( - "Image too large: (33830, 14959)\n" "Error during processing.", - file=sys.stderr, - ) - sys.exit(1) - else: - print("Spoof doesn't understand arguments", file=sys.stderr) - print(sys.argv, file=sys.stderr) - sys.exit(1) - - sys.exit(0) - - -if __name__ == '__main__': - main() diff --git a/tests/test_main.py b/tests/test_main.py index e161730e..bf95bd76 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -50,11 +50,6 @@ def spoof_tesseract_crash(tmp_path_factory): return spoof(tmp_path_factory, tesseract='tesseract_crash.py') -@pytest.fixture -def spoof_tesseract_big_image_error(tmp_path_factory): - return spoof(tmp_path_factory, tesseract='tesseract_big_image_error.py') - - def test_quick(spoof_tesseract_cache, resources, outpdf): check_ocrmypdf(resources / 'ccitt.pdf', outpdf, env=spoof_tesseract_cache) @@ -337,9 +332,7 @@ def test_tesseract_crash_autorotate(spoof_tesseract_crash, resources, no_outpdf) @pytest.mark.parametrize('renderer', RENDERERS) @pytest.mark.slow -def test_tesseract_image_too_big( - renderer, spoof_tesseract_big_image_error, resources, outpdf -): +def test_tesseract_image_too_big(renderer, resources, outpdf): check_ocrmypdf( resources / 'hugemono.pdf', outpdf, @@ -348,7 +341,8 @@ def test_tesseract_image_too_big( renderer, '--max-image-mpixels', '0', - env=spoof_tesseract_big_image_error, + '--plugin', + 'tests/plugins/tesseract_big_image_error.py', )