From 5b10ec9d39c984d360ba12b5ceae2f039e1e1c3c Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Mon, 22 Jun 2020 00:34:58 -0700 Subject: [PATCH] jobcontext.PdfContext: remove dead code, add annotations --- src/ocrmypdf/_jobcontext.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/ocrmypdf/_jobcontext.py b/src/ocrmypdf/_jobcontext.py index 3c91e856..37e541ce 100644 --- a/src/ocrmypdf/_jobcontext.py +++ b/src/ocrmypdf/_jobcontext.py @@ -18,33 +18,38 @@ import os import shutil import sys +from argparse import Namespace from pathlib import Path +from typing import Iterator from ocrmypdf._plugin_manager import get_plugin_manager +from ocrmypdf.pdfinfo import PdfInfo class PdfContext: """Holds our context for a particular run of the pipeline""" def __init__( - self, options, work_folder: Path, origin: Path, pdfinfo, plugin_manager + self, + options: Namespace, + work_folder: Path, + origin: Path, + pdfinfo: PdfInfo, + plugin_manager, ): self.options = options - self.work_folder = Path(work_folder) - self.origin = Path(origin) + self.work_folder = work_folder + self.origin = origin self.pdfinfo = pdfinfo self.plugin_manager = plugin_manager - if options: - self.name = os.path.basename(options.input_file) - else: - self.name = 'origin.pdf' + self.name = os.path.basename(options.input_file) if self.name == '-': self.name = 'stdin' def get_path(self, name: str) -> Path: return self.work_folder / name - def get_page_contexts(self): + def get_page_contexts(self) -> Iterator[PageContext]: npages = len(self.pdfinfo) for n in range(npages): yield PageContext(self, n) @@ -53,7 +58,8 @@ class PdfContext: class PageContext: """Holds our context for a page - Must be pickable, so only store intrinsic/simple data elements + Must be pickable, so stores only intrinsic/simple data elements or those + capable of their serializing themselves via __getstate__. """ def __init__(self, pdf_context: PdfContext, pageno): @@ -70,8 +76,7 @@ class PageContext: def __getstate__(self): state = self.__dict__.copy() - if state['plugin_manager'] is not None: - state['plugin_manager'] = None + state['plugin_manager'] = None # We cannot serialize the plugin manager... return state def __setstate__(self, state): @@ -79,7 +84,7 @@ class PageContext: self.plugin_manager = get_plugin_manager(self.options.plugins) -def cleanup_working_files(work_folder, options): +def cleanup_working_files(work_folder: Path, options: Namespace): if options.keep_temporary_files: print(f"Temporary working files retained at:\n{work_folder}", file=sys.stderr) else: