"Fossies" - the Fresh Open Source Software Archive

Member "OCRmyPDF-8.3.0/src/ocrmypdf/exec/qpdf.py" (13 May 2019, 1607 Bytes) of package /linux/privat/OCRmyPDF-8.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "qpdf.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 7.4.0_vs_8.0.0.

    1 # © 2017 James R. Barlow: github.com/jbarlow83
    2 #
    3 # This file is part of OCRmyPDF.
    4 #
    5 # OCRmyPDF is free software: you can redistribute it and/or modify
    6 # it under the terms of the GNU General Public License as published by
    7 # the Free Software Foundation, either version 3 of the License, or
    8 # (at your option) any later version.
    9 #
   10 # OCRmyPDF is distributed in the hope that it will be useful,
   11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 # GNU General Public License for more details.
   14 #
   15 # You should have received a copy of the GNU General Public License
   16 # along with OCRmyPDF.  If not, see <http://www.gnu.org/licenses/>.
   17 
   18 from functools import lru_cache
   19 from os import fspath
   20 from subprocess import PIPE, STDOUT, CalledProcessError, run
   21 
   22 from . import get_version
   23 
   24 
   25 @lru_cache(maxsize=1)
   26 def version():
   27     return get_version('qpdf', regex=r'qpdf version (.+)')
   28 
   29 
   30 def check(input_file, log=None):
   31     args_qpdf = ['qpdf', '--check', fspath(input_file)]
   32 
   33     if log is None:
   34         import logging as log
   35 
   36     try:
   37         run(args_qpdf, stderr=STDOUT, stdout=PIPE, universal_newlines=True, check=True)
   38     except CalledProcessError as e:
   39         if e.returncode == 2:
   40             log.error("%s: not a valid PDF, and could not repair it.", input_file)
   41             log.error("Details:")
   42             log.error(e.output)
   43         elif e.returncode == 3:
   44             log.info("qpdf --check returned warnings:")
   45             log.info(e.output)
   46         else:
   47             log.warning(e.output)
   48         return False
   49     return True