Passed
Push — master ( 5944d9...b0dff0 )
by Konstantin
02:14
created

ocrd.cli.validate.validate_parameters()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nop 3
1
import sys
2
3
import click
4
from json import loads
5
import codecs
6
7
from ocrd import Resolver, Workspace
8
from ocrd.task_sequence import ProcessorTask, validate_tasks
9
10
from ocrd_utils import (
11
    parse_json_string_or_file
12
)
13
from ocrd_validators import (
14
    OcrdToolValidator,
15
    OcrdZipValidator,
16
    PageValidator,
17
    ParameterValidator,
18
    WorkspaceValidator,
19
)
20
21
def _inform_of_result(report):
22
    if not report.is_valid:
23
        print(report.to_xml())
24
        sys.exit(1)
25
26
27
@click.group("validate")
28
def validate_cli():
29
    """
30
    All the validation in one CLI
31
    """
32
33
@validate_cli.command('tool-json')
34
@click.argument('ocrd_tool', required=False, nargs=1)
35
def validate_ocrd_tool(ocrd_tool):
36
    '''
37
    Validate OCRD_TOOL as an ocrd-tool.json file.
38
    '''
39
    if not ocrd_tool:
40
        ocrd_tool = 'ocrd-tool.json'
41
    with codecs.open(ocrd_tool, encoding='utf-8') as f:
42
        ocrd_tool = loads(f.read())
43
    _inform_of_result(OcrdToolValidator.validate(ocrd_tool))
44
45
@validate_cli.command('parameters')
46
@click.argument('ocrd_tool')
47
@click.argument('executable')
48
@click.argument('param_json')
49
def validate_parameters(ocrd_tool, executable, param_json):
50
    '''
51
    Validate PARAM_JSON against parameter definition of EXECUTABLE in OCRD_TOOL
52
    '''
53
    with codecs.open(ocrd_tool, encoding='utf-8') as f:
54
        ocrd_tool = loads(f.read())
55
    _inform_of_result(ParameterValidator(ocrd_tool['tools'][executable]).validate(parse_json_string_or_file(param_json)))
56
57
@validate_cli.command('page')
58
@click.argument('page', required=True, nargs=1)
59
@click.option('--page-textequiv-consistency', help="How strict to check PAGE multi-level textequiv consistency", type=click.Choice(['strict', 'lax', 'fix', 'off']), default='strict')
60
@click.option('--page-textequiv-strategy', help="Strategy to determine the correct textequiv", type=click.Choice(['index1']), default='index1')
61
@click.option('--check-baseline', help="Whether Baseline must be fully within TextLine/Coords", is_flag=True, default=False)
62
@click.option('--check-coords', help="Whether *Region/TextLine/Word/Glyph must each be fully contained within Border/*Region/TextLine/Word, resp.", is_flag=True, default=False)
63
def validate_page(page, **kwargs):
64
    '''
65
    Validate PAGE against OCR-D conventions
66
    '''
67
    _inform_of_result(PageValidator.validate(filename=page, **kwargs))
68
69
#  @validate_cli.command('zip')
70
#  @click.argument('src', type=click.Path(dir_okay=True, readable=True, resolve_path=True), required=True)
71
#  @click.option('-Z', '--skip-unzip', help="Treat SRC as a directory not a ZIP", is_flag=True, default=False)
72
#  @click.option('-B', '--skip-bag', help="Whether to skip all checks of manifests and files", is_flag=True, default=False)
73
#  @click.option('-C', '--skip-checksums', help="Whether to omit checksum checks but still check basic BagIt conformance", is_flag=True, default=False)
74
#  @click.option('-D', '--skip-delete', help="Whether to skip deleting the unpacked OCRD-ZIP dir after valdiation", is_flag=True, default=False)
75
#  @click.option('-j', '--processes', help="Number of parallel processes", type=int, default=1)
76
#  def validate(src, **kwargs):
77
#      """
78
#      Validate OCRD-ZIP
79
80
#      SRC must exist an be an OCRD-ZIP, either a ZIP file or a directory.
81
#      """
82
#      _inform_of_result(OcrdZipValidator(Resolver(), src).validate(**kwargs))
83
84
#  @validate_cli.command('workspace')
85
#  @click.option('-a', '--download', is_flag=True, help="Download all files")
86
#  @click.option('-s', '--skip', help="Tests to skip", default=[], multiple=True, type=click.Choice(['imagefilename', 'dimension', 'mets_unique_identifier', 'mets_file_group_names', 'mets_files', 'pixel_density', 'page', 'url']))
87
#  @click.option('--page-textequiv-consistency', '--page-strictness', help="How strict to check PAGE multi-level textequiv consistency", type=click.Choice(['strict', 'lax', 'fix', 'off']), default='strict')
88
#  @click.option('--page-coordinate-consistency', help="How fierce to check PAGE multi-level coordinate consistency", type=click.Choice(['poly', 'baseline', 'both', 'off']), default='poly')
89
#  @click.argument('mets_url')
90
#  def validate_workspace(mets_url, **kwargs):
91
#      '''
92
#          Validate a workspace
93
#      '''
94
#      _inform_of_result(WorkspaceValidator.validate(Resolver(), mets_url, **kwargs))
95
96
@validate_cli.command('tasks')
97
@click.option('--workspace', nargs=1, required=False, help='Workspace these tasks are to be run. If omitted, only validate syntax')
98
@click.argument('tasks', nargs=-1, required=True)
99
def validate_process(tasks, workspace):
100
    '''
101
    Validate a sequence of tasks passable to 'ocrd process'
102
    '''
103
    if workspace:
104
        _inform_of_result(validate_tasks([ProcessorTask.parse(t) for t in tasks], Workspace(Resolver(), directory=workspace)))
105
    else:
106
        for t in [ProcessorTask.parse(t) for t in tasks]:
107
            _inform_of_result(t.validate())
108