From 4eca0a165b273fa5d953a133cb6f0b2b1cb23179 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 26 Aug 2021 18:04:38 -0700 Subject: [PATCH] pre-commit: pyupgrade modernizing --- .pre-commit-config.yaml | 5 +++++ misc/webservice.py | 2 +- src/ocrmypdf/_exec/tesseract.py | 2 +- src/ocrmypdf/_exec/unpaper.py | 2 +- src/ocrmypdf/_pipeline.py | 6 +++--- src/ocrmypdf/_sync.py | 2 +- src/ocrmypdf/_validation.py | 6 +++--- src/ocrmypdf/cli.py | 2 +- src/ocrmypdf/helpers.py | 4 ++-- src/ocrmypdf/leptonica.py | 7 +++---- src/ocrmypdf/pdfinfo/layout.py | 2 +- tests/test_main.py | 4 ++-- 12 files changed, 24 insertions(+), 20 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c3fb7fcb..ff10f765 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,3 +25,8 @@ repos: rev: v1.17.0 hooks: - id: setup-cfg-fmt + - repo: https://github.com/asottile/pyupgrade + rev: v2.24.0 + hooks: + - id: pyupgrade + args: ["--py36-plus"] diff --git a/misc/webservice.py b/misc/webservice.py index a8a87ad3..e005b7c2 100644 --- a/misc/webservice.py +++ b/misc/webservice.py @@ -37,7 +37,7 @@ app.secret_key = "secret" app.config['MAX_CONTENT_LENGTH'] = 50_000_000 app.config.from_envvar("OCRMYPDF_WEBSERVICE_SETTINGS", silent=True) -ALLOWED_EXTENSIONS = set(["pdf"]) +ALLOWED_EXTENSIONS = {"pdf"} def allowed_file(filename): diff --git a/src/ocrmypdf/_exec/tesseract.py b/src/ocrmypdf/_exec/tesseract.py index ac7b4fab..25f09bbd 100644 --- a/src/ocrmypdf/_exec/tesseract.py +++ b/src/ocrmypdf/_exec/tesseract.py @@ -117,7 +117,7 @@ def get_languages(): if line.startswith('Error'): raise MissingDependencyError(lang_error(output)) _header, *rest = output.splitlines() - return set(lang.strip() for lang in rest) + return {lang.strip() for lang in rest} def tess_base_args(langs: List[str], engine_mode: Optional[int]) -> List[str]: diff --git a/src/ocrmypdf/_exec/unpaper.py b/src/ocrmypdf/_exec/unpaper.py index 5226326d..3c3ae72c 100644 --- a/src/ocrmypdf/_exec/unpaper.py +++ b/src/ocrmypdf/_exec/unpaper.py @@ -45,7 +45,7 @@ def _setup_unpaper_io(tmpdir: Path, input_file: Path) -> Tuple[Path, Path]: im = im.convert(mode='1') else: im = im.convert(mode='RGB') - except IOError as e: + except OSError as e: raise MissingDependencyError( "Could not convert image with type " + im.mode ) from e diff --git a/src/ocrmypdf/_pipeline.py b/src/ocrmypdf/_pipeline.py index ce3b8d88..ebf69295 100644 --- a/src/ocrmypdf/_pipeline.py +++ b/src/ocrmypdf/_pipeline.py @@ -48,7 +48,7 @@ def triage_image_file(input_file, output_file, options): log.info("Input file is not a PDF, checking if it is an image...") try: im = Image.open(input_file) - except EnvironmentError as e: + except OSError as e: # Recover the original filename log.error(str(e).replace(str(input_file), str(options.input_file))) raise UnsupportedImageFormatError() from e @@ -135,7 +135,7 @@ def triage(original_filename, input_file, output_file, options): # Origin file is a pdf create a symlink with pdf extension safe_symlink(input_file, output_file) return output_file - except EnvironmentError as e: + except OSError as e: log.debug(f"Temporary file was at: {input_file}") msg = str(e).replace(str(input_file), original_filename) raise InputFileError(msg) from e @@ -856,7 +856,7 @@ def merge_sidecars(txt_files: Iterable[Optional[Path]], context: PdfContext): if frm != 1: stream.write('\f') # Form feed between pages if txt_file: - with open(txt_file, 'r', encoding="utf-8") as in_: + with open(txt_file, encoding="utf-8") as in_: txt = in_.read() # Some OCR engines (e.g. Tesseract v4 alpha) add form feeds # between pages, and some do not. For consistency, we ignore diff --git a/src/ocrmypdf/_sync.py b/src/ocrmypdf/_sync.py index 53e5c7cf..303b0cf6 100644 --- a/src/ocrmypdf/_sync.py +++ b/src/ocrmypdf/_sync.py @@ -421,7 +421,7 @@ def run_pipeline(options, *, plugin_manager, api=False): try: debug_log_handler.close() log.removeHandler(debug_log_handler) - except EnvironmentError as e: + except OSError as e: print(e, file=sys.stderr) cleanup_working_files(work_folder, options) diff --git a/src/ocrmypdf/_validation.py b/src/ocrmypdf/_validation.py index 4c348f5b..cb55c454 100644 --- a/src/ocrmypdf/_validation.py +++ b/src/ocrmypdf/_validation.py @@ -182,10 +182,10 @@ def _pages_from_ranges(ranges: str) -> Set[int]: def check_options_ocr_behavior(options): exclusive_options = sum( - [ + (1 if opt else 0) for opt in (options.force_ocr, options.skip_text, options.redo_ocr) - ] + ) if exclusive_options >= 2: raise BadArgsError("Choose only one of --force-ocr, --skip-text, --redo-ocr.") @@ -302,7 +302,7 @@ def check_closed_streams(options): # pragma: no cover if options.input_file == '-': log.error("Trying to read from stdin but stdin seems closed") return False - sys.stdin = open(os.devnull, 'r') + sys.stdin = open(os.devnull) if sys.stdout is None: if options.output_file == '-': diff --git a/src/ocrmypdf/cli.py b/src/ocrmypdf/cli.py index 204b7b31..43ea1de7 100644 --- a/src/ocrmypdf/cli.py +++ b/src/ocrmypdf/cli.py @@ -22,7 +22,7 @@ def numeric(basetype: Type[T], min_: Optional[T] = None, max_: Optional[T] = Non def _numeric(string): value = basetype(string) if (min_ is not None and value < min_) or (max_ is not None and value > max_): - msg = "%r not in valid range %r" % (string, (min_, max_)) + msg = f"{string!r} not in valid range {(min_, max_)!r}" raise argparse.ArgumentTypeError(msg) return value diff --git a/src/ocrmypdf/helpers.py b/src/ocrmypdf/helpers.py index 916fb42c..3f012ef1 100644 --- a/src/ocrmypdf/helpers.py +++ b/src/ocrmypdf/helpers.py @@ -189,7 +189,7 @@ def is_file_writable(test_file: os.PathLike) -> bool: with suppress(OSError): p.unlink() return True - except (EnvironmentError, RuntimeError) as e: + except (OSError, RuntimeError) as e: log.debug(e) log.error(str(e)) return False @@ -273,7 +273,7 @@ def deprecated(func): def new_func(*args, **kwargs): warnings.simplefilter('always', DeprecationWarning) # turn off filter warnings.warn( - "Call to deprecated function {}.".format(func.__name__), + f"Call to deprecated function {func.__name__}.", category=DeprecationWarning, stacklevel=2, ) diff --git a/src/ocrmypdf/leptonica.py b/src/ocrmypdf/leptonica.py index 807560ff..9f2554de 100644 --- a/src/ocrmypdf/leptonica.py +++ b/src/ocrmypdf/leptonica.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -# -*- coding: utf-8 -*- # # © 2013-16: jbarlow83 from Github (https://github.com/jbarlow83) # @@ -272,7 +271,7 @@ class LeptonicaObject: # Leptonica API uses double-pointers for its destroy APIs to prevent # dangling pointers. This means we need to put our single pointer, # cdata, in a temporary CDATA**. - pp = ffi.new('{} **'.format(cls.LEPTONICA_TYPENAME), cdata) + pp = ffi.new(f'{cls.LEPTONICA_TYPENAME} **', cdata) cls.cdata_destroy(pp) @@ -844,7 +843,7 @@ class Box(LeptonicaObject): def __repr__(self): if self._cdata: - return ''.format( + return ''.format( self.x, self.y, self.w, self.h ) return '' @@ -916,7 +915,7 @@ class Sel(LeptonicaObject): lines = [line.strip() for line in selstr.split('\n') if line.strip()] h = len(lines) w = len(lines[0]) - lengths = set(len(line) for line in lines) + lengths = {len(line) for line in lines} if len(lengths) != 1: raise ValueError("All lines in selstr must be same length") diff --git a/src/ocrmypdf/pdfinfo/layout.py b/src/ocrmypdf/pdfinfo/layout.py index 4159a1cb..bf8d7c30 100644 --- a/src/ocrmypdf/pdfinfo/layout.py +++ b/src/ocrmypdf/pdfinfo/layout.py @@ -135,7 +135,7 @@ class LTStateAwareChar(LTChar): return self._text def __repr__(self): - return '<%s %s matrix=%s rendermode=%r font=%r adv=%s text=%r>' % ( + return '<{} {} matrix={} rendermode={!r} font={!r} adv={} text={!r}>'.format( self.__class__.__name__, bbox2str(self.bbox), matrix2str(self.matrix), diff --git a/tests/test_main.py b/tests/test_main.py index 0d827fa3..0bd69ae9 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -701,7 +701,7 @@ def test_sidecar_pagecount(resources, outpdf): pdfinfo = PdfInfo(resources / '3small.pdf') num_pages = len(pdfinfo) - with open(sidecar, 'r', encoding='utf-8') as f: + with open(sidecar, encoding='utf-8') as f: ocr_text = f.read() # There should a formfeed between each pair of pages, so the count of @@ -722,7 +722,7 @@ def test_sidecar_nonempty(resources, outpdf): 'tests/plugins/tesseract_cache.py', ) - with open(sidecar, 'r', encoding='utf-8') as f: + with open(sidecar, encoding='utf-8') as f: ocr_text = f.read() assert 'the' in ocr_text