diff --git a/ocrmypdf/__init__.py b/ocrmypdf/__init__.py index 5d96e388..95a89a74 100644 --- a/ocrmypdf/__init__.py +++ b/ocrmypdf/__init__.py @@ -1,6 +1,5 @@ from enum import IntEnum import os -from collections.abc import Iterable import pkg_resources PROGRAM_NAME = 'ocrmypdf' @@ -31,6 +30,3 @@ def page_number(input_file): return int(os.path.basename(input_file)[0:6]) -def is_iterable_notstr(thing): - return isinstance(thing, Iterable) and not isinstance(thing, str) - diff --git a/ocrmypdf/__main__.py b/ocrmypdf/__main__.py index 2a02d54f..8d617567 100755 --- a/ocrmypdf/__main__.py +++ b/ocrmypdf/__main__.py @@ -25,17 +25,15 @@ import ruffus.proxy_logger as proxy_logger from .pipeline import JobContext, JobContextManager, re_symlink, \ cleanup_working_files, build_pipeline from .pdfa import file_claims_pdfa +from .helpers import is_iterable_notstr, re_symlink from . import tesseract from . import qpdf -from . import ExitCode, is_iterable_notstr, PROGRAM_NAME, VERSION +from . import ExitCode, PROGRAM_NAME, VERSION from collections.abc import Sequence warnings.simplefilter('ignore', pypdf.utils.PdfReadWarning) -BASEDIR = os.path.dirname(os.path.realpath(__file__)) - - # ------------- # External dependencies diff --git a/ocrmypdf/helpers.py b/ocrmypdf/helpers.py new file mode 100644 index 00000000..86696407 --- /dev/null +++ b/ocrmypdf/helpers.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# © 2016 James R. Barlow: github.com/jbarlow83 + +from functools import partial +from collections.abc import Iterable +import sys +import os + + +def re_symlink(input_file, soft_link_name, log=None): + """ + Helper function: relinks soft symbolic link if necessary + """ + + 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.") + return + + # Soft link already exists: delete for relink? + 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) + try: + os.unlink(soft_link_name) + except: + prdebug("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) + + prdebug("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 + ) + + +def is_iterable_notstr(thing): + return isinstance(thing, Iterable) and not isinstance(thing, str) + diff --git a/ocrmypdf/pipeline.py b/ocrmypdf/pipeline.py index e86574ac..8d35b844 100644 --- a/ocrmypdf/pipeline.py +++ b/ocrmypdf/pipeline.py @@ -24,12 +24,12 @@ from ruffus import formatter, regex, Pipeline, suffix from .hocrtransform import HocrTransform from .pageinfo import pdf_get_all_pageinfo from .pdfa import generate_pdfa_def, file_claims_pdfa +from .helpers import re_symlink, is_iterable_notstr from . import ghostscript from . import tesseract from . import qpdf from . import leptonica -from . import ExitCode, page_number, is_iterable_notstr, PROGRAM_NAME, \ - VERSION +from . import ExitCode, page_number, PROGRAM_NAME, VERSION VECTOR_PAGE_DPI = 400 @@ -78,43 +78,6 @@ class JobContextManager(BaseManager): pass - -def re_symlink(input_file, soft_link_name, log): - """ - Helper function: relinks soft symbolic link if necessary - """ - - # Guard against soft linking to oneself - if input_file == soft_link_name: - log.debug("Warning: No symbolic link made. You are using " + - "the original data directory as the working directory.") - return - - # Soft link already exists: delete for relink? - 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) - try: - os.unlink(soft_link_name) - except: - 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) - - 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 - ) - - - def cleanup_working_files(work_folder, options): if options.keep_temporary_files: print("Temporary working files saved at:\n{0}".format(work_folder), @@ -869,7 +832,7 @@ def build_pipeline(options, work_folder, log, context): output_dir=work_folder, extras=[log, context]) - # Split + # Split (kwargs for split seems to be broken, so pass plain args) task_split_pages = main_pipeline.split( split_pages, task_repair_pdf,