Skip to content

Commit

Permalink
Merge pull request #503 from OCR-D/processors-wo-args
Browse files Browse the repository at this point in the history
bashlib/ocrd_wrap_cli_processor: show help if METS does not exist
  • Loading branch information
kba authored Jul 21, 2020
2 parents f9dd047 + db5ae64 commit be21dea
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ Changed:

* Refactoring of `ocrd_utils.__init__` into thematic submodules, #536
* validation of file groups downgraded to notice, allow PRE fileGrp/USE prefix, #541
* BaseProcessor: :fire: show help if no METS was specified, OCR-D/spec#156, #438, #503

Fixed:

* bashlib: Don't set `-x` in `ocrd__minversion`, #535
* bashlib: `ocrd__minversion` logic was broken

## [2.11.0] - 2020-07-13

Expand Down
33 changes: 17 additions & 16 deletions ocrd/bashlib/src/logging.bash
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@ ocrd__log () {
ocrd__minversion () {
local minversion="$1"
local version=$(ocrd --version|sed 's/ocrd, version //')
echo "$minversion < $version?"
if [[ $minversion == $version ]];then
return 0
fi
#echo "$minversion < $version?"
local IFS=.
version=($version)
minversion=($minversion)
# fill empty fields in version with zeros
for ((i=${#version[@]}; i<${#minversion[@]}; i++));do
version[i]=0
done
for ((i=0; i<${#version[@]}; i++));do
if [[ -z ${minversion[i]} ]];then
# fill empty fields in minversion with zeros
minversion[i]=0
fi
if ((10#${version[i]} < 10#${minversion[i]}));then
ocrd__raise "ocrd/core is too old (${version[*]} < ${minversion[*]}). Please update OCR-D/core"
# MAJOR > MAJOR
if (( ${version[0]} > ${minversion[0]} ));then
return
# MAJOR == MAJOR
elif (( ${version[0]} == ${minversion[0]} ));then
# MINOR > MINOR
if (( ${version[1]} > ${minversion[1]} ));then
return
# MINOR == MINOR
elif (( ${version[1]} == ${minversion[1]} ));then
# PATCH > PATCH
if (( ${version[2]} >= ${minversion[2]} ));then
return
fi
fi
done
fi
ocrd__raise "ocrd/core is too old (${version[*]} < ${minversion[*]}). Please update OCR-D/core"
}
3 changes: 2 additions & 1 deletion ocrd/bashlib/src/parse_argv.bash
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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
11 changes: 7 additions & 4 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 @@ -53,15 +54,17 @@ def ocrd_cli_wrap_processor(
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 not mets or (is_local_filename(mets) and not isfile(get_local_filename(mets))):
processorClass(workspace=None, show_help=True)
sys.exit(1)
# LOG.info('kwargs=%s' % kwargs)
# Merge parameter overrides and parameters
if 'parameter_override' in kwargs:
Expand Down
36 changes: 19 additions & 17 deletions ocrd/ocrd/lib.bash
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,27 @@ ocrd__log () {
ocrd__minversion () {
local minversion="$1"
local version=$(ocrd --version|sed 's/ocrd, version //')
echo "$minversion < $version?"
if [[ $minversion == $version ]];then
return 0
fi
#echo "$minversion < $version?"
local IFS=.
version=($version)
minversion=($minversion)
# fill empty fields in version with zeros
for ((i=${#version[@]}; i<${#minversion[@]}; i++));do
version[i]=0
done
for ((i=0; i<${#version[@]}; i++));do
if [[ -z ${minversion[i]} ]];then
# fill empty fields in minversion with zeros
minversion[i]=0
fi
if ((10#${version[i]} < 10#${minversion[i]}));then
ocrd__raise "ocrd/core is too old (${version[*]} < ${minversion[*]}). Please update OCR-D/core"
# MAJOR > MAJOR
if (( ${version[0]} > ${minversion[0]} ));then
return
# MAJOR == MAJOR
elif (( ${version[0]} == ${minversion[0]} ));then
# MINOR > MINOR
if (( ${version[1]} > ${minversion[1]} ));then
return
# MINOR == MINOR
elif (( ${version[1]} == ${minversion[1]} ));then
# PATCH > PATCH
if (( ${version[2]} >= ${minversion[2]} ));then
return
fi
fi
done
fi
ocrd__raise "ocrd/core is too old (${version[*]} < ${minversion[*]}). Please update OCR-D/core"
}

# END-INCLUDE
Expand Down Expand Up @@ -130,7 +131,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
2 changes: 1 addition & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def invoke_cli(self, cli, args):
cli.main(args=args)
except SystemExit as e:
out, err = self.capture_out_err()
return e.code, out, err
return e.code or 0, out, err

def capture_out_err(self):
return self.capfd.readouterr()
Expand Down
4 changes: 3 additions & 1 deletion tests/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

DUMMY_TOOL = {
'executable': 'ocrd-test',
'description': 'dolor sit',
'steps': ['recognition/post-correction'],
'parameters': {
'baz': {
'type': 'string',
'default': 'bla'
'default': 'bla',
'description': 'lorem ipsum'
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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):
exit_code, out, err = self.invoke_cli(cli_dummy_processor, ['--dump-json'])
print("exit_code=%s\nout=%s\nerr=%s" % (exit_code, out, err))
Expand All @@ -83,9 +92,8 @@ 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', '{"baz": "forty-two"}', '--mets', 'mets.xml', *DEFAULT_IN_OUT])
print(result)
self.assertEqual(result.exit_code, 0)
exit_code, out, err = self.invoke_cli(cli_dummy_processor, ['-p', '{"baz": "forty-two"}', '--mets', 'mets.xml', *DEFAULT_IN_OUT])
self.assertEqual(exit_code, 0)

def test_param_merging(self):
json1 = '{"foo": 23, "bar": 100}'
Expand Down

0 comments on commit be21dea

Please sign in to comment.