From 86875997b8eda4aab064bef6f9b9bc397b4b2274 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Mon, 29 Jun 2020 02:17:14 -0700 Subject: [PATCH] Fix more mypy errors --- src/ocrmypdf/_concurrent.py | 2 +- src/ocrmypdf/_exec/ghostscript.py | 18 +++++++++++------- src/ocrmypdf/_exec/tesseract.py | 12 ++++++------ src/ocrmypdf/api.py | 6 +++--- src/ocrmypdf/helpers.py | 1 + 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/ocrmypdf/_concurrent.py b/src/ocrmypdf/_concurrent.py index 6d608eb6..f954d6d8 100644 --- a/src/ocrmypdf/_concurrent.py +++ b/src/ocrmypdf/_concurrent.py @@ -84,7 +84,7 @@ def exec_progress_pool( task_arguments: Optional[Iterable] = None, task_finished: Optional[Callable] = None, ): - log_queue = multiprocessing.Queue(-1) + log_queue: multiprocessing.Queue = multiprocessing.Queue(-1) listener = threading.Thread(target=log_listener, args=(log_queue,)) if use_threads: diff --git a/src/ocrmypdf/_exec/ghostscript.py b/src/ocrmypdf/_exec/ghostscript.py index 0fb65b1b..44347a42 100644 --- a/src/ocrmypdf/_exec/ghostscript.py +++ b/src/ocrmypdf/_exec/ghostscript.py @@ -25,6 +25,7 @@ from os import fspath from pathlib import Path from shutil import which from subprocess import PIPE, CalledProcessError +from typing import Optional, cast from PIL import Image @@ -34,12 +35,12 @@ from ocrmypdf.subprocess import get_version, run log = logging.getLogger(__name__) -GS = 'gs' +_gswin = None if os.name == 'nt': - GS = which('gswin64c') - if not GS: - GS = which('gswin32c') - if not GS: + _gswin = which('gswin64c') + if not _gswin: + _gswin = which('gswin32c') + if not _gswin: raise MissingDependencyError( """ --------------------------------------------------------------------- @@ -52,7 +53,10 @@ if os.name == 'nt': --------------------------------------------------------------------- """ ) - GS = Path(GS).stem + _gswin = Path(_gswin).stem + +GS = _gswin if _gswin else 'gs' +del _gswin def version(): @@ -77,7 +81,7 @@ def jpeg_passthrough_available() -> bool: def _gs_error_reported(stream) -> bool: - return re.search(r'error', stream, flags=re.IGNORECASE) + return True if re.search(r'error', stream, flags=re.IGNORECASE) else False def rasterize_pdf( diff --git a/src/ocrmypdf/_exec/tesseract.py b/src/ocrmypdf/_exec/tesseract.py index bfa6305d..85dc5040 100644 --- a/src/ocrmypdf/_exec/tesseract.py +++ b/src/ocrmypdf/_exec/tesseract.py @@ -123,7 +123,7 @@ def get_languages(): return set(lang.strip() for lang in rest) -def tess_base_args(langs: List[str], engine_mode) -> List[str]: +def tess_base_args(langs: List[str], engine_mode: int) -> List[str]: args = ['tesseract'] if langs: args.extend(['-l', '+'.join(langs)]) @@ -132,7 +132,7 @@ def tess_base_args(langs: List[str], engine_mode) -> List[str]: return args -def get_orientation(input_file: Path, engine_mode, timeout: float): +def get_orientation(input_file: Path, engine_mode: int, timeout: float): args_tesseract = tess_base_args(['osd'], engine_mode) + [ '--psm', '0', @@ -229,9 +229,9 @@ def generate_hocr( input_file: Path, output_hocr: Path, output_text: Path, - languages: list, - engine_mode, - tessconfig: list, + languages: List[str], + engine_mode: int, + tessconfig: List[str], timeout: float, pagesegmode: int, user_words, @@ -290,7 +290,7 @@ def generate_pdf( output_pdf: Path, output_text: Path, languages: List[str], - engine_mode, + engine_mode: int, tessconfig: List[str], timeout: float, pagesegmode: int, diff --git a/src/ocrmypdf/api.py b/src/ocrmypdf/api.py index 4fac0159..f9db6a6f 100644 --- a/src/ocrmypdf/api.py +++ b/src/ocrmypdf/api.py @@ -18,7 +18,6 @@ import logging import os import sys -from argparse import ArgumentParser from enum import IntEnum from pathlib import Path from typing import BinaryIO, Iterable, Union @@ -27,7 +26,8 @@ from ocrmypdf._logging import PageNumberFilter, TqdmConsole from ocrmypdf._plugin_manager import get_plugin_manager from ocrmypdf._sync import run_pipeline from ocrmypdf._validation import check_options -from ocrmypdf.cli import get_parser +from ocrmypdf.cli import ArgumentParser, get_parser +from ocrmypdf.helpers import is_iterable_notstr try: import coloredlogs @@ -153,7 +153,7 @@ def create_options( cmdline.append(f"--{cmd_style_arg}") continue - if isinstance(val, Iterable) and not isinstance(val, str): + if is_iterable_notstr(val): for elem in val: cmdline.append(f"--{cmd_style_arg}") cmdline.append(elem) diff --git a/src/ocrmypdf/helpers.py b/src/ocrmypdf/helpers.py index aca40b6e..c457ab8b 100644 --- a/src/ocrmypdf/helpers.py +++ b/src/ocrmypdf/helpers.py @@ -113,6 +113,7 @@ def samefile(f1: os.PathLike, f2: os.PathLike): def is_iterable_notstr(thing: Any) -> bool: + """Is this is an iterable type, other than a string?""" return isinstance(thing, Iterable) and not isinstance(thing, str)