Make re_symlink() not require a log object

This commit is contained in:
James R. Barlow
2019-05-17 01:59:36 -07:00
parent 4340ad9f12
commit 56067b590b
4 changed files with 32 additions and 23 deletions
+1 -1
View File
@@ -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)
+2 -2
View File
@@ -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()
+17 -14
View File
@@ -15,32 +15,35 @@
# You should have received a copy of the GNU General Public License
# along with OCRmyPDF. If not, see <http://www.gnu.org/licenses/>.
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)
+12 -6
View File
@@ -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