Skip to content

Commit

Permalink
ruff: further fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarlow83 committed Apr 14, 2023
1 parent a3c49b8 commit 9ce692a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 58 deletions.
14 changes: 9 additions & 5 deletions src/ocrmypdf/_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions src/ocrmypdf/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 "
Expand All @@ -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:
Expand Down
86 changes: 43 additions & 43 deletions src/ocrmypdf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions src/ocrmypdf/extra_plugins/semfree.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ def _execute(
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)
Expand Down
4 changes: 2 additions & 2 deletions src/ocrmypdf/pdfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/ocrmypdf/pdfinfo/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ def __init__(
*,
detailed_analysis: bool = False,
progbar: bool = False,
max_workers: int = None,
max_workers: int | None = None,
check_pages=None,
executor: Executor = DEFAULT_EXECUTOR,
):
Expand Down
3 changes: 2 additions & 1 deletion src/ocrmypdf/pluginspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
hookspec = pluggy.HookspecMarker('ocrmypdf')

# pylint: disable=unused-argument
# mypy:


@hookspec(firstresult=True)
Expand All @@ -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
Expand Down

0 comments on commit 9ce692a

Please sign in to comment.