Skip to content

Commit

Permalink
bashlib/ocrd_wrap_cli_processor: show help if METS does not exist, fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Jun 7, 2020
1 parent 305b69a commit 33b2f13
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
3 changes: 2 additions & 1 deletion ocrd/bashlib/src/parse_argv.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions ocrd/ocrd/decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os.path import isfile
import sys

import click

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion ocrd/ocrd/lib.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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__)

0 comments on commit 33b2f13

Please sign in to comment.