Modernize type annotations

This commit is contained in:
James R. Barlow
2022-07-23 00:39:24 -07:00
parent 9c8ddd853d
commit dc6f1a266a
85 changed files with 306 additions and 187 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ repos:
rev: 5.10.1
hooks:
- id: isort
args: ["--profile", "black"]
args: ["--profile", "black", "-a", "from __future__ import annotations"]
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
+2 -1
View File
@@ -19,8 +19,9 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# This script must be edited to meet your needs.
from __future__ import annotations
# This script must be edited to meet your needs.
import logging
import os
import sys
+2
View File
@@ -37,6 +37,8 @@ To use this as an API:
)
"""
from __future__ import annotations
import logging
from PIL import Image
+2 -1
View File
@@ -19,8 +19,9 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# This script must be edited to meet your needs.
from __future__ import annotations
# This script must be edited to meet your needs.
import logging
import os
import shutil
+2
View File
@@ -20,6 +20,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import annotations
import json
import logging
import os
+2
View File
@@ -24,6 +24,8 @@ to emphasize that SaaS deployments should make sure they comply with
Ghostscript's license as well as OCRmyPDF's.
"""
from __future__ import annotations
import os
import shlex
from subprocess import PIPE, run
+2
View File
@@ -6,6 +6,8 @@
"""setup.py to support older setuptools and pip."""
from __future__ import annotations
from setuptools import setup
setup()
+2
View File
@@ -6,6 +6,8 @@
"""Adds OCR layer to PDFs."""
from __future__ import annotations
from pluggy import HookimplMarker as _HookimplMarker
from ocrmypdf import helpers, hocrtransform, pdfa, pdfinfo
+2
View File
@@ -7,6 +7,8 @@
"""ocrmypdf command line entrypoint."""
from __future__ import annotations
import logging
import os
import signal
+7 -5
View File
@@ -6,9 +6,11 @@
"""OCRmyPDF concurrency abstractions."""
from __future__ import annotations
import threading
from abc import ABC, abstractmethod
from typing import Callable, Iterable, Optional
from typing import Callable, Iterable
def _task_noop(*_args, **_kwargs):
@@ -47,10 +49,10 @@ class Executor(ABC):
use_threads: bool,
max_workers: int,
tqdm_kwargs: dict,
worker_initializer: Optional[Callable] = None,
task: Optional[Callable] = None,
task_arguments: Optional[Iterable] = None,
task_finished: Optional[Callable] = None,
worker_initializer: Callable | None = None,
task: Callable | None = None,
task_arguments: Iterable | None = None,
task_finished: Callable | None = None,
) -> None:
"""
Set up parallel execution and progress reporting.
+2
View File
@@ -6,3 +6,5 @@
"""Manage third party executables"""
from __future__ import annotations
+4 -3
View File
@@ -7,6 +7,8 @@
"""Interface to Ghostscript executable"""
from __future__ import annotations
import logging
import os
import re
@@ -15,7 +17,6 @@ from io import BytesIO
from os import fspath
from pathlib import Path
from subprocess import PIPE, CalledProcessError
from typing import Optional
from PIL import Image, UnidentifiedImageError
@@ -77,8 +78,8 @@ def rasterize_pdf(
raster_device: str,
raster_dpi: Resolution,
pageno: int = 1,
page_dpi: Optional[Resolution] = None,
rotation: Optional[int] = None,
page_dpi: Resolution | None = None,
rotation: int | None = None,
filter_vector: bool = False,
):
"""Rasterize one page of a PDF at resolution raster_dpi in canvas units."""
+2
View File
@@ -7,6 +7,8 @@
"""Interface to jbig2 executable"""
from __future__ import annotations
from subprocess import PIPE
from ocrmypdf.exceptions import MissingDependencyError
+2
View File
@@ -7,6 +7,8 @@
"""Interface to pngquant executable"""
from __future__ import annotations
from contextlib import contextmanager
from io import BytesIO
from pathlib import Path
+11 -10
View File
@@ -7,13 +7,14 @@
"""Interface to Tesseract executable"""
from __future__ import annotations
import logging
import re
from math import pi
from os import fspath
from pathlib import Path
from subprocess import PIPE, STDOUT, CalledProcessError, TimeoutExpired
from typing import Dict, List, Optional
from packaging.version import Version
from PIL import Image
@@ -46,7 +47,7 @@ HOCR_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
</html>
"""
TESSERACT_THRESHOLDING_METHODS: Dict[str, int] = {
TESSERACT_THRESHOLDING_METHODS: dict[str, int] = {
'auto': 0,
'otsu': 0,
'adaptive-otsu': 1,
@@ -162,7 +163,7 @@ def get_languages():
return {lang.strip() for lang in rest}
def tess_base_args(langs: List[str], engine_mode: Optional[int]) -> List[str]:
def tess_base_args(langs: list[str], engine_mode: int | None) -> list[str]:
args = ['tesseract']
if langs:
args.extend(['-l', '+'.join(langs)])
@@ -171,7 +172,7 @@ def tess_base_args(langs: List[str], engine_mode: Optional[int]) -> List[str]:
return args
def _parse_tesseract_output(binary_output: bytes) -> Dict[str, str]:
def _parse_tesseract_output(binary_output: bytes) -> dict[str, str]:
def gen():
for line in binary_output.decode().splitlines():
line = line.strip()
@@ -183,7 +184,7 @@ def _parse_tesseract_output(binary_output: bytes) -> Dict[str, str]:
def get_orientation(
input_file: Path, engine_mode: Optional[int], timeout: float
input_file: Path, engine_mode: int | None, timeout: float
) -> OrientationConfidence:
args_tesseract = tess_base_args(['osd'], engine_mode) + [
'--psm',
@@ -215,7 +216,7 @@ def get_orientation(
def get_deskew(
input_file: Path, languages: List[str], engine_mode: Optional[int], timeout: float
input_file: Path, languages: list[str], engine_mode: int | None, timeout: float
) -> float:
"""Gets angle to deskew this page, in degrees."""
args_tesseract = tess_base_args(languages, engine_mode) + [
@@ -306,9 +307,9 @@ def generate_hocr(
input_file: Path,
output_hocr: Path,
output_text: Path,
languages: List[str],
languages: list[str],
engine_mode: int,
tessconfig: List[str],
tessconfig: list[str],
timeout: float,
pagesegmode: int,
thresholding: int,
@@ -372,9 +373,9 @@ def generate_pdf(
input_file: Path,
output_pdf: Path,
output_text: Path,
languages: List[str],
languages: list[str],
engine_mode: int,
tessconfig: List[str],
tessconfig: list[str],
timeout: float,
pagesegmode: int,
thresholding: int,
+8 -7
View File
@@ -5,9 +5,10 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
# unpaper documentation:
# https://github.com/Flameeyes/unpaper/blob/master/doc/basic-concepts.md
"""Interface to unpaper executable"""
import logging
@@ -18,7 +19,7 @@ from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from subprocess import PIPE, STDOUT
from typing import Iterator, List, Optional, Tuple, Union
from typing import Iterator, Union
from PIL import Image
@@ -75,7 +76,7 @@ def version() -> str:
SUPPORTED_MODES = {'1', 'L', 'RGB'}
def _convert_image(im: Image.Image) -> Tuple[Image.Image, bool]:
def _convert_image(im: Image.Image) -> tuple[Image.Image, bool]:
im_modified = False
if im.mode not in SUPPORTED_MODES:
@@ -99,7 +100,7 @@ def _convert_image(im: Image.Image) -> Tuple[Image.Image, bool]:
@contextmanager
def _setup_unpaper_io(input_file: Path) -> Iterator[Tuple[Path, Path, Path]]:
def _setup_unpaper_io(input_file: Path) -> Iterator[tuple[Path, Path, Path]]:
with Image.open(input_file) as im:
if im.width * im.height >= UNPAPER_IMAGE_PIXEL_LIMIT:
raise UnpaperImageTooLargeError(w=im.width, h=im.height)
@@ -121,7 +122,7 @@ def _setup_unpaper_io(input_file: Path) -> Iterator[Tuple[Path, Path, Path]]:
def run_unpaper(
input_file: Path, output_file: Path, *, dpi: DecFloat, mode_args: List[str]
input_file: Path, output_file: Path, *, dpi: DecFloat, mode_args: list[str]
) -> None:
args_unpaper = ['unpaper', '-v', '--dpi', str(round(dpi, 6))] + mode_args
@@ -154,7 +155,7 @@ def run_unpaper(
) from e
def validate_custom_args(args: str) -> List[str]:
def validate_custom_args(args: str) -> list[str]:
unpaper_args = shlex.split(args)
if any(('/' in arg or arg == '.' or arg == '..') for arg in unpaper_args):
raise ValueError('No filenames allowed in --unpaper-args')
@@ -166,7 +167,7 @@ def clean(
output_file: Path,
*,
dpi: DecFloat,
unpaper_args: Optional[List[str]] = None,
unpaper_args: list[str] | None = None,
) -> Path:
default_args = [
'--layout',
+4 -3
View File
@@ -6,10 +6,11 @@
"""For grafting text-only PDF pages onto freeform PDF pages."""
from __future__ import annotations
import logging
from contextlib import suppress
from pathlib import Path
from typing import Optional
from pikepdf import (
Dictionary,
@@ -103,8 +104,8 @@ class OcrGrafter:
self,
*,
pageno: int,
image: Optional[Path],
textpdf: Optional[Path],
image: Path | None,
textpdf: Path | None,
autorotate_correction: int,
):
if textpdf and not self.font:
+3 -1
View File
@@ -6,6 +6,8 @@
"""Defines context objects that are passed to child processes/threads."""
from __future__ import annotations
import os
import shutil
import sys
@@ -50,7 +52,7 @@ class PdfContext:
"""
return self.work_folder / name
def get_page_contexts(self) -> Iterator['PageContext']:
def get_page_contexts(self) -> Iterator[PageContext]:
"""Get all ``PageContext`` for this PDF."""
npages = len(self.pdfinfo)
for n in range(npages):
+2
View File
@@ -6,6 +6,8 @@
"""Logging support classes."""
from __future__ import annotations
import logging
from contextlib import suppress
+5 -3
View File
@@ -6,6 +6,8 @@
"""OCRmyPDF page processing pipeline functions."""
from __future__ import annotations
import logging
import os
import re
@@ -14,7 +16,7 @@ from contextlib import suppress
from datetime import datetime, timezone
from pathlib import Path
from shutil import copyfileobj
from typing import Dict, Iterable, Optional
from typing import Iterable
import img2pdf
import pikepdf
@@ -665,7 +667,7 @@ def ocr_engine_textonly_pdf(input_image: Path, page_context: PageContext):
return (output_pdf, output_text)
def get_docinfo(base_pdf: pikepdf.Pdf, context: PdfContext) -> Dict[str, str]:
def get_docinfo(base_pdf: pikepdf.Pdf, context: PdfContext) -> dict[str, str]:
options = context.options
def from_document_info(key):
@@ -866,7 +868,7 @@ def enumerate_compress_ranges(iterable):
yield (skipped_from, index), None
def merge_sidecars(txt_files: Iterable[Optional[Path]], context: PdfContext):
def merge_sidecars(txt_files: Iterable[Path | None], context: PdfContext):
output_file = context.get_path('sidecar.txt')
with open(output_file, 'w', encoding="utf-8") as stream:
for (from_, to_), txt_file in enumerate_compress_ranges(txt_files):
+6 -4
View File
@@ -6,13 +6,15 @@
"""Plugin manager using pluggy."""
from __future__ import annotations
import argparse
import importlib
import importlib.util
import pkgutil
import sys
from pathlib import Path
from typing import List, Sequence, Tuple, Union
from typing import Sequence
import pluggy
@@ -34,7 +36,7 @@ class OcrmypdfPluginManager(pluggy.PluginManager):
def __init__(
self,
*args,
plugins: List[Union[str, Path]],
plugins: list[str | Path],
builtins: bool = True,
**kwargs,
):
@@ -101,7 +103,7 @@ class OcrmypdfPluginManager(pluggy.PluginManager):
self.register(module)
def get_plugin_manager(plugins: List[Union[str, Path]], builtins=True):
def get_plugin_manager(plugins: list[str | Path], builtins=True):
return OcrmypdfPluginManager(
project_name='ocrmypdf',
plugins=plugins,
@@ -111,7 +113,7 @@ def get_plugin_manager(plugins: List[Union[str, Path]], builtins=True):
def get_parser_options_plugins(
args: Sequence[str],
) -> Tuple[argparse.ArgumentParser, argparse.Namespace, pluggy.PluginManager]:
) -> tuple[argparse.ArgumentParser, argparse.Namespace, pluggy.PluginManager]:
pre_options, _unused = plugins_only_parser.parse_known_args(args=args)
plugin_manager = get_plugin_manager(pre_options.plugins)
+10 -8
View File
@@ -7,6 +7,8 @@
"""Implements the concurrent and page synchronous parts of the pipeline."""
from __future__ import annotations
import argparse
import logging
import logging.handlers
@@ -18,7 +20,7 @@ from concurrent.futures.thread import BrokenThreadPool
from functools import partial
from pathlib import Path
from tempfile import mkdtemp
from typing import List, NamedTuple, Optional, Sequence, Tuple, cast
from typing import NamedTuple, Sequence, cast
import PIL
@@ -74,9 +76,9 @@ class PageResult(NamedTuple):
"""Result when a page is finished processing."""
pageno: int
pdf_page_from_image: Optional[Path]
ocr: Optional[Path]
text: Optional[Path]
pdf_page_from_image: Path | None
ocr: Path | None
text: Path | None
orientation_correction: int
@@ -115,7 +117,7 @@ def preprocess(
def make_intermediate_images(
page_context: PageContext, orientation_correction: int
) -> Tuple[Path, Optional[Path]]:
) -> tuple[Path, Path | None]:
options = page_context.options
ocr_image = preprocess_out = None
@@ -232,7 +234,7 @@ def exec_page_sync(page_context: PageContext) -> PageResult:
def post_process(
pdf_file: Path, context: PdfContext, executor: Executor
) -> Tuple[Path, Sequence[str]]:
) -> tuple[Path, Sequence[str]]:
pdf_out = pdf_file
if context.options.output_type.startswith('pdfa'):
ps_stub_out = generate_postscript_stub(context)
@@ -259,7 +261,7 @@ def exec_concurrent(context: PdfContext, executor: Executor) -> Sequence[str]:
if max_workers > 1:
log.info("Start processing %d pages concurrently", max_workers)
sidecars: List[Optional[Path]] = [None] * len(context.pdfinfo)
sidecars: list[Path | None] = [None] * len(context.pdfinfo)
ocrgraft = OcrGrafter(context)
def update_page(result: PageResult, pbar):
@@ -337,7 +339,7 @@ def configure_debug_logging(
def run_pipeline(
options: argparse.Namespace,
*,
plugin_manager: Optional[OcrmypdfPluginManager],
plugin_manager: OcrmypdfPluginManager | None,
api: bool = False,
) -> ExitCode:
# Any changes to options will not take effect for options that are already
+7 -5
View File
@@ -7,6 +7,8 @@
"""Validate a work order from API or command line."""
from __future__ import annotations
import locale
import logging
import os
@@ -14,7 +16,7 @@ import sys
import unicodedata
from pathlib import Path
from shutil import copyfileobj
from typing import List, Optional, Sequence, Set, Tuple
from typing import Sequence
import pikepdf
import PIL
@@ -144,8 +146,8 @@ def check_options_preprocessing(options):
raise BadArgsError("--unpaper-args: " + str(e)) from e
def _pages_from_ranges(ranges: str) -> Set[int]:
pages: List[int] = []
def _pages_from_ranges(ranges: str) -> set[int]:
pages: list[int] = []
page_groups = ranges.replace(' ', '').split(',')
for group in page_groups:
if not group:
@@ -241,7 +243,7 @@ def check_options(options, plugin_manager):
_check_options(options, plugin_manager, ocr_engine_languages)
def create_input_file(options, work_folder: Path) -> Tuple[Path, str]:
def create_input_file(options, work_folder: Path) -> tuple[Path, str]:
if options.input_file == '-':
# stdin
log.info('reading file from standard input')
@@ -297,7 +299,7 @@ def report_output_file_size(
options,
input_file: Path,
output_file: Path,
optimize_messages: Optional[Sequence[str]] = None,
optimize_messages: Sequence[str] | None = None,
file_overhead: int = 4000,
page_overhead: int = 3000,
):
+2
View File
@@ -9,6 +9,8 @@
OCRmyPDF uses setuptools_scm to derive version from git tags.
"""
from __future__ import annotations
try:
from importlib.metadata import version as _package_version
except ImportError:
+4 -2
View File
@@ -6,6 +6,8 @@
"""Functions for using ocrmypdf as an API."""
from __future__ import annotations
import logging
import os
import sys
@@ -13,7 +15,7 @@ import threading
from enum import IntEnum
from io import IOBase
from pathlib import Path
from typing import AnyStr, BinaryIO, Iterable, Optional, Union
from typing import AnyStr, BinaryIO, Iterable, Union
from warnings import warn
from ocrmypdf._logging import PageNumberFilter, TqdmConsole
@@ -217,7 +219,7 @@ def ocr( # pylint: disable=unused-argument
language: Iterable[str] = None,
image_dpi: int = None,
output_type=None,
sidecar: Optional[StrPath] = None,
sidecar: StrPath | None = None,
jobs: int = None,
use_threads: bool = None,
title: str = None,
+2
View File
@@ -4,6 +4,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
# This file exists only mark builtin_plugins as a package.
# The plugin manager will not load it, so anything defined here may not be
# processed as a module.
+2 -1
View File
@@ -4,13 +4,14 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
# © 2020 James R. Barlow: github.com/jbarlow83
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""OCRmyPDF's multiprocessing/multithreading abstraction layer."""
import logging
@@ -6,6 +6,8 @@
"""OCRmyPDF automatically installs these filters as plugins."""
from __future__ import annotations
from ocrmypdf import hookimpl
@@ -7,6 +7,8 @@
"""Built-in plugin to implement PDF page rasterization and PDF/A production."""
from __future__ import annotations
import logging
from ocrmypdf import hookimpl
+4 -2
View File
@@ -7,10 +7,12 @@
"""Built-in plugin to implement PDF page optimization."""
from __future__ import annotations
import argparse
import logging
from pathlib import Path
from typing import Sequence, Tuple
from typing import Sequence
from ocrmypdf import Executor, PdfContext, hookimpl
from ocrmypdf._exec import jbig2enc, pngquant
@@ -130,7 +132,7 @@ def optimize_pdf(
context: PdfContext,
executor: Executor,
linearize: bool,
) -> Tuple[Path, Sequence[str]]:
) -> tuple[Path, Sequence[str]]:
save_settings = dict(
linearize=linearize,
**get_pdf_save_settings(context.options.output_type),
@@ -7,6 +7,8 @@
"""Built-in plugin to implement OCR using Tesseract."""
from __future__ import annotations
import logging
import os
+4 -4
View File
@@ -6,8 +6,10 @@
"""Command line interface customization and validation."""
from __future__ import annotations
import argparse
from typing import Any, Callable, Mapping, Optional, TypeVar
from typing import Any, Callable, Mapping, TypeVar
from ocrmypdf._version import PROGRAM_NAME as _PROGRAM_NAME
from ocrmypdf._version import __version__ as _VERSION
@@ -15,9 +17,7 @@ from ocrmypdf._version import __version__ as _VERSION
T = TypeVar('T', int, float)
def numeric(
basetype: Callable[[Any], T], min_: Optional[T] = None, max_: Optional[T] = None
):
def numeric(basetype: Callable[[Any], T], min_: T | None = None, max_: T | None = None):
"""Validator for numeric params"""
min_ = basetype(min_) if min_ is not None else None
max_ = basetype(max_) if max_ is not None else None
+2
View File
@@ -6,3 +6,5 @@
"""Data files used to generate certain PDFs."""
from __future__ import annotations
+2
View File
@@ -6,6 +6,8 @@
"""OCRmyPDF's exceptions."""
from __future__ import annotations
from enum import IntEnum
from textwrap import dedent
+5 -3
View File
@@ -20,6 +20,8 @@ be guaranteed, some workers may end up with too much work while others are idle.
It is less efficient than the standard implementation, so not th edefault.
"""
from __future__ import annotations
import logging
import logging.handlers
import signal
@@ -28,7 +30,7 @@ from enum import Enum, auto
from itertools import islice, repeat, takewhile, zip_longest
from multiprocessing import Pipe, Process
from multiprocessing.connection import Connection, wait
from typing import Callable, Iterable, Iterator, List
from typing import Callable, Iterable, Iterator
from ocrmypdf import Executor, hookimpl
from ocrmypdf._concurrent import NullProgressBar
@@ -134,8 +136,8 @@ class LambdaExecutor(Executor):
if not grouped_args:
return
processes: List[Process] = []
connections: List[Connection] = []
processes: list[Process] = []
connections: list[Connection] = []
for chunk in grouped_args:
parent_conn, child_conn = Pipe()
+2
View File
@@ -6,6 +6,8 @@
"""Support functions."""
from __future__ import annotations
import logging
import multiprocessing
import os
+7 -5
View File
@@ -30,6 +30,8 @@
"""Transform .hocr and page image to text PDF."""
from __future__ import annotations
import argparse
import os
import re
@@ -139,7 +141,7 @@ class HocrTransform:
{'': 'ff', '': 'ffi', '': 'ffl', '': 'fi', '': 'fl'}
)
def __init__(self, *, hocr_filename: Union[str, Path], dpi: float):
def __init__(self, *, hocr_filename: str | Path, dpi: float):
self.dpi = dpi
self.hocr = ElementTree.parse(os.fspath(hocr_filename))
@@ -203,7 +205,7 @@ class HocrTransform:
return out
@classmethod
def baseline(cls, element: Element) -> Tuple[float, float]:
def baseline(cls, element: Element) -> tuple[float, float]:
"""
Returns a tuple containing the baseline slope and intercept.
"""
@@ -219,7 +221,7 @@ class HocrTransform:
"""
return Rect._make((c / self.dpi * inch) for c in pxl)
def _child_xpath(self, html_tag: str, html_class: Optional[str] = None) -> str:
def _child_xpath(self, html_tag: str, html_class: str | None = None) -> str:
xpath = f".//{self.xmlns}{html_tag}"
if html_class:
xpath += f"[@class='{html_class}']"
@@ -246,7 +248,7 @@ class HocrTransform:
self,
*,
out_filename: Path,
image_filename: Optional[Path] = None,
image_filename: Path | None = None,
show_bounding_boxes: bool = False,
fontname: str = "Helvetica",
invisible_text: bool = False,
@@ -349,7 +351,7 @@ class HocrTransform:
def _do_line(
self,
pdf: Canvas,
line: Optional[Element],
line: Element | None,
elemclass: str,
fontname: str,
invisible_text: bool,
+20 -29
View File
@@ -7,6 +7,8 @@
"""Post-processing image optimization of OCR PDFs."""
from __future__ import annotations
import logging
import sys
import tempfile
@@ -14,18 +16,7 @@ import threading
from collections import defaultdict
from os import fspath
from pathlib import Path
from typing import (
Callable,
Dict,
Iterator,
List,
MutableSet,
NamedTuple,
NewType,
Optional,
Sequence,
Tuple,
)
from typing import Callable, Iterator, MutableSet, NamedTuple, NewType, Sequence
from zlib import compress
import img2pdf
@@ -78,7 +69,7 @@ def jpg_name(root: Path, xref: Xref) -> Path:
def extract_image_filter(
pike: Pdf, root: Path, image: Stream, xref: Xref
) -> Optional[Tuple[PdfImage, Tuple[Name, Object]]]:
) -> tuple[PdfImage, tuple[Name, Object]] | None:
del pike # unused args
del root
@@ -135,7 +126,7 @@ def extract_image_filter(
def extract_image_jbig2(
*, pike: Pdf, root: Path, image: Stream, xref: Xref, options
) -> Optional[XrefExt]:
) -> XrefExt | None:
del options # unused arg
result = extract_image_filter(pike, root, image, xref)
@@ -176,7 +167,7 @@ def extract_image_jbig2(
def extract_image_generic(
*, pike: Pdf, root: Path, image: Stream, xref: Xref, options
) -> Optional[XrefExt]:
) -> XrefExt | None:
result = extract_image_filter(pike, root, image, xref)
if result is None:
return None
@@ -240,8 +231,8 @@ def extract_images(
pike: Pdf,
root: Path,
options,
extract_fn: Callable[..., Optional[XrefExt]],
) -> Iterator[Tuple[int, XrefExt]]:
extract_fn: Callable[..., XrefExt | None],
) -> Iterator[tuple[int, XrefExt]]:
"""Extract image using extract_fn
Enumerate images on each page, lookup their xref/ID number in the PDF.
@@ -300,7 +291,7 @@ def extract_images(
def extract_images_generic(
pike: Pdf, root: Path, options
) -> Tuple[List[Xref], List[Xref]]:
) -> tuple[list[Xref], list[Xref]]:
"""Extract any >=2bpp image we think we can improve"""
jpegs = []
@@ -315,7 +306,7 @@ def extract_images_generic(
return jpegs, pngs
def extract_images_jbig2(pike: Pdf, root: Path, options) -> Dict[int, List[XrefExt]]:
def extract_images_jbig2(pike: Pdf, root: Path, options) -> dict[int, list[XrefExt]]:
"""Extract any bitonal image that we think we can improve as JBIG2"""
jbig2_groups = defaultdict(list)
@@ -328,11 +319,11 @@ def extract_images_jbig2(pike: Pdf, root: Path, options) -> Dict[int, List[XrefE
def _produce_jbig2_images(
jbig2_groups: Dict[int, List[XrefExt]], root: Path, options, executor: Executor
jbig2_groups: dict[int, list[XrefExt]], root: Path, options, executor: Executor
) -> None:
"""Produce JBIG2 images from their groups"""
def jbig2_group_args(root: Path, groups: Dict[int, List[XrefExt]]):
def jbig2_group_args(root: Path, groups: dict[int, list[XrefExt]]):
for group, xref_exts in groups.items():
prefix = f'group{group:08d}'
yield (
@@ -341,7 +332,7 @@ def _produce_jbig2_images(
prefix, # =out_prefix
)
def jbig2_single_args(root, groups: Dict[int, List[XrefExt]]):
def jbig2_single_args(root, groups: dict[int, list[XrefExt]]):
for group, xref_exts in groups.items():
prefix = f'group{group:08d}'
# Second loop is to ensure multiple images per page are unpacked
@@ -376,7 +367,7 @@ def _produce_jbig2_images(
def convert_to_jbig2(
pike: Pdf,
jbig2_groups: Dict[int, List[XrefExt]],
jbig2_groups: dict[int, list[XrefExt]],
root: Path,
options,
executor: Executor,
@@ -393,7 +384,7 @@ def convert_to_jbig2(
When the JBIG2 symbolic coder is not used, each JBIG2 stands on its own
and needs no dictionary. Currently this must be lossless JBIG2.
"""
jbig2_globals_dict: Optional[Dictionary]
jbig2_globals_dict: Dictionary | None
_produce_jbig2_images(jbig2_groups, root, options, executor)
@@ -419,7 +410,7 @@ def convert_to_jbig2(
)
def _optimize_jpeg(args: Tuple[Xref, Path, Path, int]) -> Tuple[Xref, Optional[Path]]:
def _optimize_jpeg(args: tuple[Xref, Path, Path, int]) -> tuple[Xref, Path | None]:
xref, in_jpg, opt_jpg, jpeg_quality = args
with Image.open(in_jpg) as im:
@@ -435,13 +426,13 @@ def _optimize_jpeg(args: Tuple[Xref, Path, Path, int]) -> Tuple[Xref, Optional[P
def transcode_jpegs(
pike: Pdf, jpegs: Sequence[Xref], root: Path, options, executor: Executor
) -> None:
def jpeg_args() -> Iterator[Tuple[Xref, Path, Path, int]]:
def jpeg_args() -> Iterator[tuple[Xref, Path, Path, int]]:
for xref in jpegs:
in_jpg = jpg_name(root, xref)
opt_jpg = in_jpg.with_suffix('.opt.jpg')
yield xref, in_jpg, opt_jpg, options.jpeg_quality
def finish_jpeg(result: Tuple[Xref, Optional[Path]], pbar):
def finish_jpeg(result: tuple[Xref, Path | None], pbar):
xref, opt_jpg = result
if opt_jpg:
compdata = opt_jpg.read_bytes() # JPEG can inserted into PDF as is
@@ -466,7 +457,7 @@ def transcode_jpegs(
def _find_deflatable_jpeg(
*, pike: Pdf, root: Path, image: Stream, xref: Xref, options
) -> Optional[XrefExt]:
) -> XrefExt | None:
result = extract_image_filter(pike, root, image, xref)
if result is None:
return None
@@ -478,7 +469,7 @@ def _find_deflatable_jpeg(
return None
def _deflate_jpeg(args: Tuple[Pdf, threading.Lock, Xref, int]) -> Tuple[Xref, bytes]:
def _deflate_jpeg(args: tuple[Pdf, threading.Lock, Xref, int]) -> tuple[Xref, bytes]:
pike, lock, xref, complevel = args
with lock:
xobj = pike.get_object(xref, 0)
+5 -3
View File
@@ -9,9 +9,11 @@
Utilities for PDF/A production and confirmation with Ghostspcript.
"""
from __future__ import annotations
import base64
from pathlib import Path
from typing import Dict, Iterator, Union
from typing import Iterator
try:
from importlib.resources import files as package_files
@@ -25,7 +27,7 @@ SRGB_ICC_PROFILE_NAME = 'sRGB.icc'
def _postscript_objdef(
alias: str,
dictionary: Dict[str, str],
dictionary: dict[str, str],
*,
stream_name: str = None,
stream_data: bytes = None,
@@ -131,7 +133,7 @@ def file_claims_pdfa(filename: Path):
}
valid_part_conforms = {'1A', '1B', '2A', '2B', '2U', '3A', '3B', '3U'}
conformance = f'PDF/A-{pdfmeta.pdfa_status}'
pdfa_dict: Dict[str, Union[str, bool]] = {}
pdfa_dict: dict[str, str | bool] = {}
if pdfmeta.pdfa_status in valid_part_conforms:
pdfa_dict['pass'] = True
pdfa_dict['output'] = 'pdfa'
+2
View File
@@ -8,4 +8,6 @@
"""For extracting information about PDFs prior to OCR."""
from __future__ import annotations
from ocrmypdf.pdfinfo.info import Colorspace, Encoding, PdfInfo
+27 -30
View File
@@ -8,6 +8,8 @@
"""Extract information about the content of a PDF."""
from __future__ import annotations
import atexit
import logging
import re
@@ -21,16 +23,13 @@ from os import PathLike
from pathlib import Path
from typing import (
Container,
Dict,
Iterable,
Iterator,
List,
Mapping,
NamedTuple,
Optional,
Sequence,
Tuple,
Union,
)
from warnings import warn
@@ -85,7 +84,7 @@ class Encoding(Enum):
FloatRect = Tuple[float, float, float, float]
FRIENDLY_COLORSPACE: Dict[str, Colorspace] = {
FRIENDLY_COLORSPACE: dict[str, Colorspace] = {
'/DeviceGray': Colorspace.gray,
'/CalGray': Colorspace.gray,
'/DeviceRGB': Colorspace.rgb,
@@ -103,7 +102,7 @@ FRIENDLY_COLORSPACE: Dict[str, Colorspace] = {
'/I': Colorspace.index,
}
FRIENDLY_ENCODING: Dict[str, Encoding] = {
FRIENDLY_ENCODING: dict[str, Encoding] = {
'/CCITTFaxDecode': Encoding.ccitt,
'/DCTDecode': Encoding.jpeg,
'/JPXDecode': Encoding.jpeg2000,
@@ -117,7 +116,7 @@ FRIENDLY_ENCODING: Dict[str, Encoding] = {
'/RL': Encoding.runlength,
}
FRIENDLY_COMP: Dict[Colorspace, int] = {
FRIENDLY_COMP: dict[Colorspace, int] = {
Colorspace.gray: 1,
Colorspace.rgb: 3,
Colorspace.cmyk: 4,
@@ -139,7 +138,7 @@ class XobjectSettings(NamedTuple):
"""Info about an XObject found in a PDF."""
name: str
shorthand: Tuple[float, float, float, float, float, float]
shorthand: tuple[float, float, float, float, float, float]
stack_depth: int
@@ -147,24 +146,24 @@ class InlineSettings(NamedTuple):
"""Info about an inline image found in a PDF."""
iimage: PdfInlineImage
shorthand: Tuple[float, float, float, float, float, float]
shorthand: tuple[float, float, float, float, float, float]
stack_depth: int
class ContentsInfo(NamedTuple):
"""Info about various objects found in a PDF."""
xobject_settings: List[XobjectSettings]
inline_images: List[InlineSettings]
xobject_settings: list[XobjectSettings]
inline_images: list[InlineSettings]
found_vector: bool
found_text: bool
name_index: Mapping[str, List[XobjectSettings]]
name_index: Mapping[str, list[XobjectSettings]]
class TextboxInfo(NamedTuple):
"""Info about a text box found in a PDF."""
bbox: Tuple[float, float, float, float]
bbox: tuple[float, float, float, float]
is_visible: bool
is_corrupt: bool
@@ -217,8 +216,8 @@ def _interpret_contents(contentstream: Object, initial_shorthand=UNIT_SQUARE):
stack = []
ctm = PdfMatrix(initial_shorthand)
xobject_settings: List[XobjectSettings] = []
inline_images: List[InlineSettings] = []
xobject_settings: list[XobjectSettings] = []
inline_images: list[InlineSettings] = []
name_index = defaultdict(lambda: [])
found_vector = False
found_text = False
@@ -342,21 +341,21 @@ class ImageInfo:
DPI_PREC = Decimal('1.000')
_comp: Optional[int]
_comp: int | None
_name: str
def __init__(
self,
*,
name='',
pdfimage: Optional[Object] = None,
inline: Optional[PdfInlineImage] = None,
pdfimage: Object | None = None,
inline: PdfInlineImage | None = None,
shorthand=None,
):
self._name = str(name)
self._shorthand = shorthand
pim: Union[PdfInlineImage, PdfImage]
pim: PdfInlineImage | PdfImage
if inline is not None:
self._origin = 'inline'
@@ -473,7 +472,7 @@ def _find_inline_images(contentsinfo: ContentsInfo) -> Iterator[ImageInfo]:
)
def _image_xobjects(container) -> Iterator[Tuple[Object, str]]:
def _image_xobjects(container) -> Iterator[tuple[Object, str]]:
"""Search for all XObject-based images in the container
Usually the container is a page, but it could also be a Form XObject
@@ -561,7 +560,7 @@ def _find_form_xobject_images(pdf: Pdf, container: Object, contentsinfo: Content
def _process_content_streams(
*, pdf: Pdf, container: Object, shorthand=None
) -> Iterator[Union[VectorMarker, TextMarker, ImageInfo]]:
) -> Iterator[VectorMarker | TextMarker | ImageInfo]:
"""Find all individual instances of images drawn in the container
Usually the container is a page, but it may also be a Form XObject.
@@ -691,8 +690,8 @@ def _pdf_pageinfo_concurrent(
max_workers,
check_pages,
detailed_analysis=False,
) -> Sequence[Optional['PageInfo']]:
pages: Sequence[Optional['PageInfo']] = [None] * len(pdf.pages)
) -> Sequence[PageInfo | None]:
pages: Sequence[PageInfo | None] = [None] * len(pdf.pages)
def update_pageinfo(result, pbar):
page = result
@@ -744,9 +743,9 @@ def _pdf_pageinfo_concurrent(
class PageInfo:
"""Information about type of contents on each page in a PDF."""
_has_text: Optional[bool]
_has_vector: Optional[bool]
_images: List[ImageInfo]
_has_text: bool | None
_has_vector: bool | None
_images: list[ImageInfo]
def __init__(
self,
@@ -879,9 +878,7 @@ class PageInfo:
def images(self):
return self._images
def get_textareas(
self, visible: Optional[bool] = None, corrupt: Optional[bool] = None
):
def get_textareas(self, visible: bool | None = None, corrupt: bool | None = None):
def predicate(obj, want_visible, want_corrupt):
result = True
if want_visible is not None:
@@ -965,7 +962,7 @@ class PdfInfo:
self._has_acroform = True
@property
def pages(self) -> Sequence[Optional[PageInfo]]:
def pages(self) -> Sequence[PageInfo | None]:
return self._pages
@property
@@ -982,7 +979,7 @@ class PdfInfo:
return self._has_acroform
@property
def filename(self) -> Union[str, Path]:
def filename(self) -> str | Path:
if not isinstance(self._infile, (str, Path)):
raise NotImplementedError("can't get filename from stream")
return self._infile
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import re
from math import copysign
from pathlib import Path
+11 -19
View File
@@ -6,19 +6,13 @@
"""OCRmyPDF pluggy plugin specification."""
from __future__ import annotations
from abc import ABC, abstractmethod
from argparse import ArgumentParser, Namespace
from logging import Handler
from pathlib import Path
from typing import (
TYPE_CHECKING,
AbstractSet,
List,
NamedTuple,
Optional,
Sequence,
Tuple,
)
from typing import TYPE_CHECKING, AbstractSet, NamedTuple, Sequence
import pluggy
@@ -177,7 +171,7 @@ def get_progressbar_class():
@hookspec
def validate(pdfinfo: 'PdfInfo', options: Namespace) -> None:
def validate(pdfinfo: PdfInfo, options: Namespace) -> None:
"""Called to give a plugin an opportunity to review *options* and *pdfinfo*.
*options* contains the "work order" to process a particular file. *pdfinfo*
@@ -203,8 +197,8 @@ def rasterize_pdf_page(
raster_device: str,
raster_dpi: Resolution,
pageno: int,
page_dpi: Optional[Resolution],
rotation: Optional[int],
page_dpi: Resolution | None,
rotation: int | None,
filter_vector: bool,
) -> Path:
"""Rasterize one page of a PDF at resolution raster_dpi in canvas units.
@@ -233,7 +227,7 @@ def rasterize_pdf_page(
@hookspec(firstresult=True)
def filter_ocr_image(page: 'PageContext', image: 'Image.Image') -> 'Image.Image':
def filter_ocr_image(page: PageContext, image: Image.Image) -> Image.Image:
"""Called to filter the image before it is sent to OCR.
This is the image that OCR sees, not what the user sees when they view the
@@ -260,7 +254,7 @@ def filter_ocr_image(page: 'PageContext', image: 'Image.Image') -> 'Image.Image'
@hookspec(firstresult=True)
def filter_page_image(page: 'PageContext', image_filename: Path) -> Path:
def filter_page_image(page: PageContext, image_filename: Path) -> Path:
"""Called to filter the whole page before it is inserted into the PDF.
A whole page image is only produced when preprocessing command line arguments
@@ -300,9 +294,7 @@ def filter_page_image(page: 'PageContext', image_filename: Path) -> Path:
@hookspec(firstresult=True)
def filter_pdf_page(
page: 'PageContext', image_filename: Path, output_pdf: Path
) -> Path:
def filter_pdf_page(page: PageContext, image_filename: Path, output_pdf: Path) -> Path:
"""Called to convert a filtered whole page image into a PDF.
A whole page image is only produced when preprocessing command line arguments
@@ -445,7 +437,7 @@ def get_ocr_engine() -> OcrEngine:
@hookspec(firstresult=True)
def generate_pdfa(
pdf_pages: List[Path],
pdf_pages: list[Path],
pdfmark: Path,
output_file: Path,
compression: str,
@@ -501,7 +493,7 @@ def optimize_pdf(
context: PdfContext,
executor: Executor,
linearize: bool,
) -> Tuple[Path, Sequence[str]]:
) -> tuple[Path, Sequence[str]]:
"""Optimize a PDF after image, OCR and metadata processing.
If the input_pdf is a PDF/A, the plugin should modify input_pdf in a way
+2
View File
@@ -8,6 +8,8 @@
"""Utilities to measure OCR quality"""
from __future__ import annotations
import re
from typing import Iterable
+14 -14
View File
@@ -7,6 +7,8 @@
"""Wrappers to manage subprocess calls"""
from __future__ import annotations
import logging
import os
import re
@@ -16,7 +18,7 @@ from functools import lru_cache
from pathlib import Path
from subprocess import PIPE, STDOUT, CalledProcessError, CompletedProcess, Popen
from subprocess import run as subprocess_run
from typing import Callable, Mapping, Optional, Sequence, Tuple, Type, Union
from typing import Callable, Mapping, Sequence, Union
from packaging.version import Version
@@ -33,7 +35,7 @@ OsEnviron = os._Environ # pylint: disable=protected-access
def run(
args: Args,
*,
env: Optional[OsEnviron] = None,
env: OsEnviron | None = None,
logs_errors_to_stdout: bool = False,
check: bool = False,
**kwargs,
@@ -80,7 +82,7 @@ def run_polling_stderr(
*,
callback: Callable[[str], None],
check: bool = False,
env: Optional[OsEnviron] = None,
env: OsEnviron | None = None,
**kwargs,
) -> CompletedProcess:
"""Run a process like ``ocrmypdf.subprocess.run``, and poll stderr.
@@ -115,8 +117,8 @@ def run_polling_stderr(
def _fix_process_args(
args: Args, env: Optional[OsEnviron], kwargs
) -> Tuple[Args, OsEnviron, logging.Logger, bool]:
args: Args, env: OsEnviron | None, kwargs
) -> tuple[Args, OsEnviron, logging.Logger, bool]:
assert 'universal_newlines' not in kwargs, "Use text= instead of universal_newlines"
if not env:
@@ -125,7 +127,7 @@ def _fix_process_args(
# Search in spoof path if necessary
program = str(args[0])
if os.name == 'nt':
if sys.platform == 'win32':
# pylint: disable=import-outside-toplevel
from ocrmypdf.subprocess._windows import fix_windows_args
@@ -144,7 +146,7 @@ def get_version(
*,
version_arg: str = '--version',
regex=r'(\d+(\.\d+)*)',
env: Optional[OsEnviron] = None,
env: OsEnviron | None = None,
) -> str:
"""Get the version of the specified program
@@ -253,9 +255,7 @@ def _get_platform() -> str:
return sys.platform
def _error_trailer(
program: str, package: Union[str, Mapping[str, str]], **kwargs
) -> None:
def _error_trailer(program: str, package: str | Mapping[str, str], **kwargs) -> None:
del kwargs
if isinstance(package, Mapping):
package = package.get(_get_platform(), program)
@@ -269,7 +269,7 @@ def _error_trailer(
def _error_missing_program(
program: str, package: str, required_for: Optional[str], recommended: bool
program: str, package: str, required_for: str | None, recommended: bool
) -> None:
# pylint: disable=unused-argument
if recommended:
@@ -286,7 +286,7 @@ def _error_old_version(
package: str,
need_version: str,
found_version: str,
required_for: Optional[str],
required_for: str | None,
) -> None:
# pylint: disable=unused-argument
if required_for:
@@ -311,9 +311,9 @@ def check_external_program(
package: str,
version_checker: Callable[[], str],
need_version: str,
required_for: Optional[str] = None,
required_for: str | None = None,
recommended: bool = False,
version_parser: Type[Version] = Version,
version_parser: type[Version] = Version,
) -> None:
"""Check for required version of external program and raise exception if not.
+8 -18
View File
@@ -3,33 +3,23 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# type: ignore
# Non-Windows mypy now breaks when trying to typecheck winreg
"""Find Tesseract and Ghostscript binaries on Windows using the registry."""
import logging
from __future__ import annotations
import os
import shutil
import sys
from itertools import chain
from pathlib import Path
from typing import Any, Callable, Iterable, Iterator, Set, Tuple, TypeVar
from typing import Any, Callable, Iterable, Iterator
try:
import winreg
except ModuleNotFoundError as _notfound_ex:
from unittest import mock
assert sys.platform == 'win32', "Suppress type checking when not on Windows"
winreg = mock.MagicMock()
log = logging.getLogger(__name__)
T = TypeVar('T')
import winreg
def ghostscript_version_key(s: str) -> Tuple[int, int, int]:
def ghostscript_version_key(s: str) -> tuple[int, int, int]:
"""Compare Ghostscript version numbers."""
try:
release = [int(elem) for elem in s.split('.', maxsplit=3)]
@@ -59,7 +49,7 @@ def registry_subkeys(key: winreg.HKEYType) -> Iterator[str]:
return registry_enum(key, winreg.EnumKey)
def registry_values(key: winreg.HKEYType) -> Iterator[Tuple[str, Any, int]]:
def registry_values(key: winreg.HKEYType) -> Iterator[tuple[str, Any, int]]:
return registry_enum(key, winreg.EnumValue)
@@ -162,7 +152,7 @@ def unique_everseen(iterable: Iterable[T], key: Callable[[T], T]) -> Iterator[T]
"List unique elements, preserving order."
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
# unique_everseen('ABBCcAD', str.lower) --> A B C D
seen: Set[T] = set()
seen: set[T] = set()
seen_add = seen.add
for element in iterable:
k = key(element)
+2
View File
@@ -4,4 +4,6 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
# Empty __init__.py file
+3 -1
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import os
import platform
import sys
@@ -52,7 +54,7 @@ def resources() -> Path:
@pytest.fixture
def ocrmypdf_exec() -> List[str]:
def ocrmypdf_exec() -> list[str]:
return [sys.executable, '-m', 'ocrmypdf']
+2
View File
@@ -19,6 +19,8 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import annotations
from unittest.mock import patch
from ocrmypdf import hookimpl
+2
View File
@@ -19,6 +19,8 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import annotations
from unittest.mock import patch
from ocrmypdf import hookimpl
+2
View File
@@ -19,6 +19,8 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import annotations
from pathlib import Path
from subprocess import CalledProcessError
from unittest.mock import patch
+2
View File
@@ -19,6 +19,8 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import annotations
from subprocess import CalledProcessError
from unittest.mock import patch
+2
View File
@@ -26,6 +26,8 @@ that is not UTF-8 compatible, so we are forced to check that we can convert it
and present it to the user.
"""
from __future__ import annotations
from contextlib import contextmanager
from subprocess import CalledProcessError
from unittest.mock import patch
@@ -19,6 +19,8 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import annotations
from contextlib import contextmanager
from subprocess import CalledProcessError
from unittest.mock import patch
+2
View File
@@ -44,6 +44,8 @@ Assumes Tesseract 4.0.0-alpha or higher.
"""
from __future__ import annotations
import argparse
import json
import logging
+2
View File
@@ -19,6 +19,8 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import annotations
import signal
from contextlib import contextmanager
from subprocess import CalledProcessError
+2
View File
@@ -31,6 +31,8 @@ In 'pdf' mode, convert the image to PDF using another program.
In orientation check mode, report 0, 90, 180, 270... based on page number.
"""
from __future__ import annotations
import pikepdf
from PIL import Image
+2
View File
@@ -30,6 +30,8 @@ In 'pdf' mode, convert the image to PDF using another program.
In orientation check mode, report the orientation is upright.
"""
from __future__ import annotations
import pikepdf
from PIL import Image
@@ -19,8 +19,6 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# type: ignore
"""Tesseract no-op plugin that simulates the OOM killer on page 4.
OCRmyPDF can use a lot of memory, even that it might trigger the
@@ -30,14 +28,18 @@ ensure we fail with an error rather than deadlock in such cases.
Page 4 was chosen because of this number's association with bad luck
in many East Asian cultures.
"""
# type: ignore
from __future__ import annotations
import os
import signal
import sys
from pathlib import Path
from ocrmypdf import hookimpl
# type: ignore
# Ugly hack that let us use the NoopOcrEngine without setting up packaging for our
# tests.
# This hack also requires us to set type: ignore
@@ -47,7 +49,7 @@ exec(parent)
NoopOcrEngine = locals()['NoopOcrEngine']
class Page4Engine(NoopOcrEngine):
class Page4Engine(NoopOcrEngine): # type: ignore
def __str__(self):
return f"NO-OP Page 4 {NoopOcrEngine.version()}"
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import logging
import pytest
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import logging
from io import BytesIO, StringIO
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import pytest
from ocrmypdf.helpers import check_pdf
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import os
from subprocess import PIPE, run
+2
View File
@@ -4,6 +4,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import os
import pytest
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import logging
import subprocess
from decimal import Decimal
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
from unittest.mock import patch
import pikepdf
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import logging
import multiprocessing
import os
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import re
from io import StringIO
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
from unittest.mock import patch
import img2pdf
+2
View File
@@ -4,6 +4,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import logging
import pytest
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import os
import shutil
from math import isclose
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import datetime
import warnings
from datetime import timezone
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
from os import fspath
from pathlib import Path
from unittest.mock import patch
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import pytest
import ocrmypdf
+2
View File
@@ -4,6 +4,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import os
import pikepdf
+2
View File
@@ -4,6 +4,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import pickle
from io import BytesIO
from math import isclose
+2
View File
@@ -4,6 +4,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
from unittest.mock import Mock
import pytest
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
from math import isclose
import pytest
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import pytest
from ocrmypdf import quality as qual
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import operator
from io import BytesIO
from math import cos, pi, sin
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import os
import sys
from pathlib import Path
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import logging
import os
import subprocess
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import logging
from os import fspath
from unittest.mock import patch
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
from math import isclose
import pytest
+2
View File
@@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations
import logging
import os
from unittest.mock import patch