pre-commit: pyupgrade modernizing

This commit is contained in:
James R. Barlow
2021-08-26 18:04:38 -07:00
parent 067e61e03a
commit 4eca0a165b
12 changed files with 24 additions and 20 deletions
+5
View File
@@ -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"]
+1 -1
View File
@@ -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):
+1 -1
View File
@@ -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]:
+1 -1
View File
@@ -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
+3 -3
View File
@@ -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
+1 -1
View File
@@ -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)
+3 -3
View File
@@ -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 == '-':
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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,
)
+3 -4
View File
@@ -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 '<leptonica.Box x={0} y={1} w={2} h={3}>'.format(
return '<leptonica.Box x={} y={} w={} h={}>'.format(
self.x, self.y, self.w, self.h
)
return '<leptonica.Box NULL>'
@@ -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")
+1 -1
View File
@@ -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),
+2 -2
View File
@@ -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