From 276fe498679070b4ca22d213c06c6b859c6c7fdd Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Fri, 4 Dec 2015 03:07:53 -0800 Subject: [PATCH] Better error messages for input file not found or invalid Not as good finding a general way to deal with ruffus exceptions, but better than nil. --- ocrmypdf/main.py | 17 ++++++++++++++++- tests/test_main.py | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/ocrmypdf/main.py b/ocrmypdf/main.py index 97ca7e0c..d2ce1187 100755 --- a/ocrmypdf/main.py +++ b/ocrmypdf/main.py @@ -880,10 +880,16 @@ def available_cpu_count(): return 1 +def cleanup_ruffus_error_message(msg): + msg = re.sub(r'\s+', r' ', msg, re.MULTILINE) + msg = re.sub(r"\((.+?)\)", r'\1', msg, re.MULTILINE) + msg = msg.strip() + return msg + + def run_pipeline(): if not options.jobs or options.jobs == 1: options.jobs = available_cpu_count() - try: cmdline.run(options) except ruffus_exceptions.RethrownJobError as e: @@ -898,6 +904,15 @@ def run_pipeline(): return eval( exc_value, {'ExitCode': ExitCode}, {'exc_value': exc_value}) + elif exc_name == 'ruffus.ruffus_exceptions.MissingInputFileError': + print(cleanup_ruffus_error_message(exc_value)) + return ExitCode.input_file + elif exc_name == 'builtins.TypeError': + if task_name == 'split_pages': + print("Input file '{0}' is not a valid PDF".format( + options.input_file)) + return ExitCode.input_file + return ExitCode.other_error if not validate_pdfa(options.output_file, _log): diff --git a/tests/test_main.py b/tests/test_main.py index 14dee87c..cfa51ed2 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -337,3 +337,20 @@ def test_uppercase_extension(): os.unlink(_make_input("UPPERCASE.PDF")) +def test_input_file_not_found(): + input_file = "does not exist.pdf" + sh, out, err = run_ocrmypdf_sh( + _make_input(input_file), + _make_output("will not happen.pdf")) + assert sh.returncode == ExitCode.input_file + assert (input_file in out or input_file in err) + + +def test_input_file_not_a_pdf(): + input_file = __file__ # Try to OCR this file + sh, out, err = run_ocrmypdf_sh( + _make_input(input_file), + _make_output("will not happen.pdf")) + assert sh.returncode == ExitCode.input_file + assert (input_file in out or input_file in err) +