1# © 2016 James R. Barlow: github.com/jbarlow83 2# 3# This Source Code Form is subject to the terms of the Mozilla Public 4# License, v. 2.0. If a copy of the MPL was not distributed with this 5# file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 7 8from enum import IntEnum 9from textwrap import dedent 10 11 12class ExitCode(IntEnum): 13 ok = 0 14 bad_args = 1 15 input_file = 2 16 missing_dependency = 3 17 invalid_output_pdf = 4 18 file_access_error = 5 19 already_done_ocr = 6 20 child_process_error = 7 21 encrypted_pdf = 8 22 invalid_config = 9 23 pdfa_conversion_failed = 10 24 other_error = 15 25 ctrl_c = 130 26 27 28class ExitCodeException(Exception): 29 exit_code = ExitCode.other_error 30 message = "" 31 32 def __str__(self): 33 super_msg = super().__str__() # Don't do str(super()) 34 if self.message: 35 return self.message.format(super_msg) 36 return super_msg 37 38 39class BadArgsError(ExitCodeException): 40 exit_code = ExitCode.bad_args 41 42 43class PdfMergeFailedError(ExitCodeException): 44 exit_code = ExitCode.input_file 45 message = dedent( 46 '''\ 47 Failed to merge PDF image layer with OCR layer 48 49 Usually this happens because the input PDF file is malformed and 50 ocrmypdf cannot automatically correct the problem on its own. 51 52 Try using 53 ocrmypdf --pdf-renderer sandwich [..other args..] 54 ''' 55 ) 56 57 58class MissingDependencyError(ExitCodeException): 59 exit_code = ExitCode.missing_dependency 60 61 62class UnsupportedImageFormatError(ExitCodeException): 63 exit_code = ExitCode.input_file 64 65 66class DpiError(ExitCodeException): 67 exit_code = ExitCode.input_file 68 69 70class OutputFileAccessError(ExitCodeException): 71 exit_code = ExitCode.file_access_error 72 73 74class PriorOcrFoundError(ExitCodeException): 75 exit_code = ExitCode.already_done_ocr 76 77 78class InputFileError(ExitCodeException): 79 exit_code = ExitCode.input_file 80 81 82class SubprocessOutputError(ExitCodeException): 83 exit_code = ExitCode.child_process_error 84 85 86class EncryptedPdfError(ExitCodeException): 87 exit_code = ExitCode.encrypted_pdf 88 message = dedent( 89 '''\ 90 Input PDF is encrypted. The encryption must be removed to 91 perform OCR. 92 93 For information about this PDF's security use 94 qpdf --show-encryption infilename 95 96 You can remove the encryption using 97 qpdf --decrypt [--password=[password]] infilename 98 ''' 99 ) 100 101 102class TesseractConfigError(ExitCodeException): 103 exit_code = ExitCode.invalid_config 104 message = "Error occurred while parsing a Tesseract configuration file" 105