From 33b2f1375d85ad5ee5fc129dc11f5223f09e1482 Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Sun, 7 Jun 2020 16:01:20 +0200 Subject: [PATCH] bashlib/ocrd_wrap_cli_processor: show help if METS does not exist, fix #438, OCR-D/spec#156 --- ocrd/bashlib/src/parse_argv.bash | 3 ++- ocrd/ocrd/decorators.py | 15 +++++++-------- ocrd/ocrd/lib.bash | 3 ++- tests/test_decorators.py | 20 ++++++++++++++++---- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/ocrd/bashlib/src/parse_argv.bash b/ocrd/bashlib/src/parse_argv.bash index 36cdbeb83..194ca0796 100644 --- a/ocrd/bashlib/src/parse_argv.bash +++ b/ocrd/bashlib/src/parse_argv.bash @@ -36,7 +36,8 @@ ocrd__parse_argv () { done if [[ ! -r "${ocrd__argv[mets_file]:=$PWD/mets.xml}" ]];then - ocrd__raise "METS '${ocrd__argv[mets_file]}' not readable. Use -m/--mets-file to set correctly" + ocrd__usage + exit 1 fi if [[ ! -d "${ocrd__argv[working_dir]:=$(dirname "${ocrd__argv[mets_file]}")}" ]];then diff --git a/ocrd/ocrd/decorators.py b/ocrd/ocrd/decorators.py index 424466bb5..ec09efd86 100644 --- a/ocrd/ocrd/decorators.py +++ b/ocrd/ocrd/decorators.py @@ -1,4 +1,5 @@ from os.path import isfile +import sys import click @@ -31,19 +32,17 @@ def ocrd_cli_wrap_processor(processorClass, ocrd_tool=None, mets=None, working_d LOG = getLogger('ocrd_cli_wrap_processor') if dump_json: processorClass(workspace=None, dump_json=True) + sys.exit() elif help: processorClass(workspace=None, show_help=True) + sys.exit() elif version: processorClass(workspace=None, show_version=True) - elif mets is None: - msg = 'Error: Missing option "-m" / "--mets".' - LOG.error(msg) - raise Exception(msg) + sys.exit() else: - if is_local_filename(mets) and not isfile(get_local_filename(mets)): - msg = "File does not exist: %s" % mets - LOG.error(msg) - raise Exception(msg) + if not mets or (is_local_filename(mets) and not isfile(get_local_filename(mets))): + processorClass(workspace=None, show_help=True) + sys.exit(1) resolver = Resolver() workspace = resolver.workspace_from_url(mets, working_dir) # TODO once we implement 'overwrite' CLI option and mechanism, disable the diff --git a/ocrd/ocrd/lib.bash b/ocrd/ocrd/lib.bash index 0564f7688..9096473f3 100644 --- a/ocrd/ocrd/lib.bash +++ b/ocrd/ocrd/lib.bash @@ -75,7 +75,8 @@ ocrd__parse_argv () { done if [[ ! -r "${ocrd__argv[mets_file]:=$PWD/mets.xml}" ]];then - ocrd__raise "METS '${ocrd__argv[mets_file]}' not readable. Use -m/--mets-file to set correctly" + ocrd__usage + exit 1 fi if [[ ! -d "${ocrd__argv[working_dir]:=$(dirname "${ocrd__argv[mets_file]}")}" ]];then diff --git a/tests/test_decorators.py b/tests/test_decorators.py index cf4386cec..07ecfe7a9 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -2,7 +2,7 @@ import click from click.testing import CliRunner -from tests.base import TestCase, assets, main, copy_of_directory # pylint: disable=import-error, no-name-in-module +from tests.base import CapturingTestCase as TestCase, assets, main, copy_of_directory # pylint: disable=import-error, no-name-in-module from ocrd import Processor from ocrd.decorators import ( @@ -26,8 +26,11 @@ def cli_with_ocrd_loglevel(*args, **kwargs): # pylint: disable=unused-ar DUMMY_TOOL = { 'executable': 'ocrd-test', 'steps': ['recognition/post-correction'], + 'description': 'A dummy processor for testing sigh', 'parameters': { 'foo': { + 'type': 'number', + 'description': 'dummy parameter for a dummy procesor', 'required': True } } @@ -73,6 +76,15 @@ def test_loglevel_override(self): self.assertEqual(logging.getLogger('PIL').getEffectiveLevel(), logging.DEBUG) initLogging() + def test_processor_no_mets(self): + """ + https://github.com/OCR-D/spec/pull/156 + """ + _, out_help, _ = self.invoke_cli(cli_dummy_processor, ['--help']) + exit_code, out_none, _ = self.invoke_cli(cli_dummy_processor, []) + self.assertEqual(exit_code, 1) + self.assertEqual(out_help, out_none) + def test_processor_dump_json(self): result = self.runner.invoke(cli_dummy_processor, ['--dump-json']) self.assertEqual(result.exit_code, 0) @@ -91,9 +103,9 @@ def test_processor_version(self): def test_processor_run(self): with copy_of_directory(assets.path_to('SBB0000F29300010000/data')) as tempdir: with pushd_popd(tempdir): - result = self.runner.invoke(cli_dummy_processor, ['-p', '{"foo": 42}', '--mets', 'mets.xml', '-I', 'OCR-D-IMG']) - self.assertEqual(result.exit_code, 0) + exit_code, out, err = self.invoke_cli(cli_dummy_processor, ['-p', '{"foo": 42}', '--mets', 'mets.xml', '-I', 'OCR-D-IMG']) + self.assertEqual(exit_code, 0) if __name__ == '__main__': - main() + main(__file__)