From 56067b590b3ee4dfa8346d79b736af6d907bd9fa Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Fri, 17 May 2019 01:59:36 -0700 Subject: [PATCH] Make re_symlink() not require a log object --- src/ocrmypdf/_pipeline.py | 2 +- src/ocrmypdf/_validation.py | 4 ++-- src/ocrmypdf/helpers.py | 31 +++++++++++++++++-------------- src/ocrmypdf/optimize.py | 18 ++++++++++++------ 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/ocrmypdf/_pipeline.py b/src/ocrmypdf/_pipeline.py index 5de49e33..d2881407 100644 --- a/src/ocrmypdf/_pipeline.py +++ b/src/ocrmypdf/_pipeline.py @@ -132,7 +132,7 @@ def triage(input_file, output_file, options, log): "input file is a PDF, not an image." ) # Origin file is a pdf create a symlink with pdf extension - re_symlink(input_file, output_file, log) + re_symlink(input_file, output_file) return output_file except EnvironmentError as e: log.error(e) diff --git a/src/ocrmypdf/_validation.py b/src/ocrmypdf/_validation.py index af6cf552..fb1be0d1 100644 --- a/src/ocrmypdf/_validation.py +++ b/src/ocrmypdf/_validation.py @@ -355,7 +355,7 @@ def create_input_file(options, work_folder): else: try: target = os.path.join(work_folder, 'origin') - re_symlink(options.input_file, target, log) + re_symlink(options.input_file, target) return target except FileNotFoundError: log.error("File not found - %s", options.input_file) @@ -372,7 +372,7 @@ def check_input_file(options, start_input_file): copyfileobj(sys.stdin.buffer, stream_buffer) else: try: - re_symlink(options.input_file, start_input_file, log) + re_symlink(options.input_file, start_input_file) except FileNotFoundError: log.error("File not found - %s", options.input_file) raise InputFileError() diff --git a/src/ocrmypdf/helpers.py b/src/ocrmypdf/helpers.py index 3d56a1f5..1e9a2cb7 100644 --- a/src/ocrmypdf/helpers.py +++ b/src/ocrmypdf/helpers.py @@ -15,32 +15,35 @@ # You should have received a copy of the GNU General Public License # along with OCRmyPDF. If not, see . +import logging import multiprocessing import os -import sys import warnings from collections.abc import Iterable from contextlib import suppress -from functools import partial, wraps +from functools import wraps from pathlib import Path +log = logging.getLogger(__name__) -def re_symlink(input_file, soft_link_name, log=None): + +def re_symlink(input_file, soft_link_name, *args, **kwargs): """ Helper function: relinks soft symbolic link if necessary """ + if len(args) == 1 and isinstance(args[0], logging.Logger): + log.warning("Deprecated: re_symlink(,log)") + if 'log' in kwargs: + log.warning('Deprecated: re_symlink(...log=)') + input_file = os.fspath(input_file) soft_link_name = os.fspath(soft_link_name) - if log is None: - prdebug = partial(print, file=sys.stderr) - else: - prdebug = log.debug # Guard against soft linking to oneself if input_file == soft_link_name: - prdebug( - "Warning: No symbolic link made. You are using " - + "the original data directory as the working directory." + log.warning( + "No symbolic link made. You are using " + "the original data directory as the working directory." ) return @@ -48,16 +51,16 @@ def re_symlink(input_file, soft_link_name, log=None): if os.path.lexists(soft_link_name): # do not delete or overwrite real (non-soft link) file if not os.path.islink(soft_link_name): - raise FileExistsError("%s exists and is not a link" % soft_link_name) + raise FileExistsError(f"{soft_link_name} exists and is not a link") try: os.unlink(soft_link_name) except OSError: - prdebug("Can't unlink %s" % (soft_link_name)) + log.debug("Can't unlink %s", soft_link_name) if not os.path.exists(input_file): - raise FileNotFoundError("trying to create a broken symlink to %s" % input_file) + raise FileNotFoundError(f"trying to create a broken symlink to {input_file}") - prdebug("os.symlink(%s, %s)" % (input_file, soft_link_name)) + log.debug("os.symlink(%s, %s)", input_file, soft_link_name) # Create symbolic link using absolute path os.symlink(os.path.abspath(input_file), soft_link_name) diff --git a/src/ocrmypdf/optimize.py b/src/ocrmypdf/optimize.py index b722228e..87ce0212 100644 --- a/src/ocrmypdf/optimize.py +++ b/src/ocrmypdf/optimize.py @@ -81,7 +81,9 @@ def extract_image_jbig2(*, pike, root, log, image, xref, options): pim, filtdp = result if ( - pim.bits_per_component == 1 and filtdp != Name.JBIG2Decode and jbig2enc.available() + pim.bits_per_component == 1 + and filtdp != Name.JBIG2Decode + and jbig2enc.available() ): try: imgname = Path(root / f'{xref:08d}') @@ -126,7 +128,9 @@ def extract_image_generic(*, pike, root, log, image, xref, options): return None return xref, ext elif ( - pim.indexed and pim.colorspace in pim.SIMPLE_COLORSPACES and options.optimize >= 3 + pim.indexed + and pim.colorspace in pim.SIMPLE_COLORSPACES + and options.optimize >= 3 ): # Try to improve on indexed images - these are far from low hanging # fruit in most cases @@ -426,7 +430,7 @@ def optimize(input_file, output_file, context): log = context.log options = context.options if options.optimize == 0: - re_symlink(input_file, output_file, log) + re_symlink(input_file, output_file) return if options.jpeg_quality == 0: @@ -467,9 +471,9 @@ def optimize(input_file, output_file, context): if savings < 0: log.info("Optimize did not improve the file - discarded") - re_symlink(input_file, output_file, log) + re_symlink(input_file, output_file) else: - re_symlink(target_file, output_file, log) + re_symlink(target_file, output_file) def main(infile, outfile, level, jobs=1): @@ -479,7 +483,9 @@ def main(infile, outfile, level, jobs=1): class OptimizeOptions: """Emulate ocrmypdf's options""" - def __init__(self, input_file, jobs, optimize, jpeg_quality, png_quality, jb2lossy): + def __init__( + self, input_file, jobs, optimize, jpeg_quality, png_quality, jb2lossy + ): self.input_file = input_file self.jobs = jobs self.optimize = optimize