ruff: further fixes

This commit is contained in:
James R. Barlow
2023-04-14 02:39:36 -07:00
parent a3c49b8f31
commit 9ce692a6f1
7 changed files with 62 additions and 58 deletions
+9 -5
View File
@@ -14,7 +14,7 @@ from contextlib import suppress
from datetime import datetime, timezone
from pathlib import Path
from shutil import copyfileobj
from typing import Any, Iterable, Sequence
from typing import Any, BinaryIO, Iterable, Sequence, cast
import img2pdf
import pikepdf
@@ -575,7 +575,9 @@ def ocr_engine_hocr(input_file: Path, page_context: PageContext) -> tuple[Path,
def should_visible_page_image_use_jpg(pageinfo: PageInfo) -> bool:
# If all images were JPEGs originally, produce a JPEG as output
return pageinfo.images and all(im.enc == Encoding.jpeg for im in pageinfo.images)
return bool(pageinfo.images) and all(
im.enc == Encoding.jpeg for im in pageinfo.images
)
def create_visible_page_jpg(image: Path, page_context: PageContext) -> Path:
@@ -892,14 +894,16 @@ def merge_sidecars(txt_files: Iterable[Path | None], context: PdfContext) -> Pat
return output_file
def copy_final(input_file, output_file, _context: PdfContext) -> None:
def copy_final(
input_file: Path, output_file: str | Path | BinaryIO, _context: PdfContext
) -> None:
log.debug('%s -> %s', input_file, output_file)
with open(input_file, 'rb') as input_stream:
with input_file.open('rb') as input_stream:
if output_file == '-':
copyfileobj(input_stream, sys.stdout.buffer)
sys.stdout.flush()
elif hasattr(output_file, 'writable'):
output_stream = output_file
output_stream = cast(BinaryIO, output_file)
copyfileobj(input_stream, output_stream)
with suppress(AttributeError):
output_stream.flush()
+4 -4
View File
@@ -418,13 +418,13 @@ def run_pipeline(
options, start_input_file, options.output_file, optimize_messages
)
except (KeyboardInterrupt if not api else NeverRaise):
except KeyboardInterrupt if not api else NeverRaise:
if options.verbose >= 1:
log.exception("KeyboardInterrupt")
else:
log.error("KeyboardInterrupt")
return ExitCode.ctrl_c
except (ExitCodeException if not api else NeverRaise) as e:
except ExitCodeException if not api else NeverRaise as e:
e = cast(ExitCodeException, e)
if options.verbose >= 1:
log.exception("ExitCodeException")
@@ -433,7 +433,7 @@ def run_pipeline(
else:
log.error(type(e).__name__)
return e.exit_code
except (PIL.Image.DecompressionBombError if not api else NeverRaise):
except PIL.Image.DecompressionBombError if not api else NeverRaise:
log.exception(
"A decompression bomb error was encountered while executing the "
"pipeline. Use the argument --max-image-mpixels to raise the maximum "
@@ -451,7 +451,7 @@ def run_pipeline(
"argument."
)
return ExitCode.child_process_error
except (Exception if not api else NeverRaise): # pylint: disable=broad-except
except Exception if not api else NeverRaise: # pylint: disable=broad-except
log.exception("An exception occurred while executing the pipeline")
return ExitCode.other_error
finally:
+43 -43
View File
@@ -207,51 +207,51 @@ def ocr( # noqa: ruff: disable=D417
input_file: PathOrIO,
output_file: PathOrIO,
*,
language: Iterable[str] = None,
image_dpi: int = None,
output_type=None,
language: Iterable[str] | None = None,
image_dpi: int | None = None,
output_type: str | None = None,
sidecar: StrPath | None = None,
jobs: int = None,
use_threads: bool = None,
title: str = None,
author: str = None,
subject: str = None,
keywords: str = None,
rotate_pages: bool = None,
remove_background: bool = None,
deskew: bool = None,
clean: bool = None,
clean_final: bool = None,
unpaper_args: str = None,
oversample: int = None,
remove_vectors: bool = None,
force_ocr: bool = None,
skip_text: bool = None,
redo_ocr: bool = None,
skip_big: float = None,
optimize: int = None,
jpg_quality: int = None,
png_quality: int = None,
jbig2_lossy: bool = None,
jbig2_page_group_size: int = None,
pages: str = None,
max_image_mpixels: float = None,
tesseract_config: Iterable[str] = None,
tesseract_pagesegmode: int = None,
tesseract_oem: int = None,
tesseract_thresholding: int = None,
pdf_renderer=None,
tesseract_timeout: float = None,
tesseract_non_ocr_timeout: float = None,
rotate_pages_threshold: float = None,
pdfa_image_compression=None,
user_words: os.PathLike = None,
user_patterns: os.PathLike = None,
fast_web_view: float = None,
plugins: Iterable[StrPath] = None,
jobs: int | None = None,
use_threads: bool | None = None,
title: str | None = None,
author: str | None = None,
subject: str | None = None,
keywords: str | None = None,
rotate_pages: bool | None = None,
remove_background: bool | None = None,
deskew: bool | None = None,
clean: bool | None = None,
clean_final: bool | None = None,
unpaper_args: str | None = None,
oversample: int | None = None,
remove_vectors: bool | None = None,
force_ocr: bool | None = None,
skip_text: bool | None = None,
redo_ocr: bool | None = None,
skip_big: float | None = None,
optimize: int | None = None,
jpg_quality: int | None = None,
png_quality: int | None = None,
jbig2_lossy: bool | None = None,
jbig2_page_group_size: int | None = None,
pages: str | None = None,
max_image_mpixels: float | None = None,
tesseract_config: Iterable[str] | None = None,
tesseract_pagesegmode: int | None = None,
tesseract_oem: int | None = None,
tesseract_thresholding: int | None = None,
pdf_renderer: str | None = None,
tesseract_timeout: float | None = None,
tesseract_non_ocr_timeout: float | None = None,
rotate_pages_threshold: float | None = None,
pdfa_image_compression: str | None = None,
user_words: os.PathLike | None = None,
user_patterns: os.PathLike | None = None,
fast_web_view: float | None = None,
plugins: Iterable[StrPath] | None = None,
plugin_manager=None,
keep_temporary_files: bool = None,
progress_bar: bool = None,
keep_temporary_files: bool | None = None,
progress_bar: bool | None = None,
**kwargs,
):
"""Run OCRmyPDF on one PDF or image.
+1 -2
View File
@@ -168,8 +168,7 @@ class LambdaExecutor(Executor):
continue
if msg_type == MessageType.result:
if task_finished:
task_finished(msg, pbar)
task_finished(msg, pbar)
elif msg_type == 'log':
record = msg
logger = logging.getLogger(record.name)
+2 -2
View File
@@ -23,8 +23,8 @@ def _postscript_objdef(
alias: str,
dictionary: dict[str, str],
*,
stream_name: str = None,
stream_data: bytes = None,
stream_name: str | None = None,
stream_data: bytes | None = None,
) -> Iterator[str]:
assert (stream_name is None) == (stream_data is None)
+1 -1
View File
@@ -959,7 +959,7 @@ class PdfInfo:
*,
detailed_analysis: bool = False,
progbar: bool = False,
max_workers: int = None,
max_workers: int | None = None,
check_pages=None,
executor: Executor = DEFAULT_EXECUTOR,
):
+2 -1
View File
@@ -28,6 +28,7 @@ if TYPE_CHECKING:
hookspec = pluggy.HookspecMarker('ocrmypdf')
# pylint: disable=unused-argument
# mypy:
@hookspec(firstresult=True)
@@ -43,7 +44,7 @@ def get_logging_console() -> Handler:
@hookspec
def initialize(plugin_manager: pluggy.PluginManager):
def initialize(plugin_manager: pluggy.PluginManager) -> None:
"""Called when this plugin is first loaded into OCRmyPDF.
The primary intended use of this is for plugins to check compatibility with other