From fc789da9cf34d8f1f634e388c17d0c7f4a11cd16 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Fri, 3 Apr 2020 22:04:00 -0700 Subject: [PATCH] Add a few more type annotations to public APIs --- src/ocrmypdf/api.py | 8 ++++++-- src/ocrmypdf/helpers.py | 8 ++++---- src/ocrmypdf/pdfinfo/info.py | 6 +++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/ocrmypdf/api.py b/src/ocrmypdf/api.py index e48320b5..36be4dd4 100644 --- a/src/ocrmypdf/api.py +++ b/src/ocrmypdf/api.py @@ -67,7 +67,11 @@ class Verbosity(IntEnum): debug_all = 2 #: More detailed debugging from ocrmypdf and dependent modules -def configure_logging(verbosity, progress_bar_friendly=True, manage_root_logger=False): +def configure_logging( + verbosity: Verbosity, + progress_bar_friendly: bool = True, + manage_root_logger: bool = False, +): """Set up logging. Library users may wish to use this function if they want their log output to be @@ -128,7 +132,7 @@ def configure_logging(verbosity, progress_bar_friendly=True, manage_root_logger= return log -def create_options(*, input_file, output_file, **kwargs): +def create_options(*, input_file: os.PathLike, output_file: os.PathLike, **kwargs): cmdline = [] deferred = [] diff --git a/src/ocrmypdf/helpers.py b/src/ocrmypdf/helpers.py index 70e884d9..ae326331 100644 --- a/src/ocrmypdf/helpers.py +++ b/src/ocrmypdf/helpers.py @@ -28,7 +28,7 @@ from pathlib import Path log = logging.getLogger(__name__) -def safe_symlink(input_file, soft_link_name, *args, **kwargs): +def safe_symlink(input_file: os.PathLike, soft_link_name: os.PathLike, *args, **kwargs): """ Helper function: relinks soft symbolic link if necessary """ @@ -76,12 +76,12 @@ def is_iterable_notstr(thing): return isinstance(thing, Iterable) and not isinstance(thing, str) -def monotonic(L): +def monotonic(L: Iterable): """Does list increase monotonically?""" return all(b > a for a, b in zip(L, L[1:])) -def page_number(input_file): +def page_number(input_file: os.PathLike): """Get one-based page number implied by filename (000002.pdf -> 2)""" return int(os.path.basename(os.fspath(input_file))[0:6]) @@ -97,7 +97,7 @@ def available_cpu_count(): return 1 -def is_file_writable(test_file): +def is_file_writable(test_file: os.PathLike): """Intentionally racy test if target is writable. We intend to write to the output file if and only if we succeed and diff --git a/src/ocrmypdf/pdfinfo/info.py b/src/ocrmypdf/pdfinfo/info.py index 4251eb54..d304688b 100644 --- a/src/ocrmypdf/pdfinfo/info.py +++ b/src/ocrmypdf/pdfinfo/info.py @@ -22,7 +22,7 @@ from collections import defaultdict, namedtuple from decimal import Decimal from enum import Enum from math import hypot, isclose -from os import fspath +from os import PathLike, fspath from pathlib import Path from warnings import warn @@ -40,7 +40,7 @@ logger = logging.getLogger() Colorspace = Enum('Colorspace', 'gray rgb cmyk lab icc index sep devn pattern jpeg2000') Encoding = Enum( - 'Encoding', 'ccitt jpeg jpeg2000 jbig2 asciihex ascii85 lzw flate ' + 'runlength' + 'Encoding', 'ccitt jpeg jpeg2000 jbig2 asciihex ascii85 lzw flate runlength' ) FRIENDLY_COLORSPACE = { @@ -558,7 +558,7 @@ def simplify_textboxes(miner, textbox_getter): yield TextboxInfo(box.bbox, visible, corrupt) -def _pdf_get_pageinfo(pdf, pageno: int, infile, xmltext): +def _pdf_get_pageinfo(pdf, pageno: int, infile: PathLike, xmltext: str): pageinfo = {} pageinfo['pageno'] = pageno pageinfo['images'] = []