2 ##############################################################################
3 # Copyright (c) 2018 Enea AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 """This module validates a PDF file against the schema."""
15 PARSER = argparse.ArgumentParser()
16 PARSER.add_argument("--yaml", "-y", type=str, required=True)
17 PARSER.add_argument("--schema", "-s", type=str, required=True)
18 ARGS = PARSER.parse_args()
19 LOADER = yaml.CSafeLoader if yaml.__with_libyaml__ else yaml.SafeLoader
21 with open(ARGS.yaml) as _:
22 _DICT = yaml.load(_, Loader=LOADER)
24 with open(ARGS.schema) as _:
25 _SCHEMA = yaml.load(_, Loader=LOADER)
28 def schema_version_workaround(node):
29 """Traverse nested dictionaries and handle 'version' key where found."""
31 node['version_{0}'.format(node['version'])] = True
32 for item in node.items():
33 if type(item) is dict:
34 schema_version_workaround(item)
36 # Draft 4 (latest supported by py-jsonschema) does not support value-based
37 # decisions properly, see related github issue:
38 # https://github.com/json-schema-org/json-schema-spec/issues/64
39 # Workaround: build 'version_x.y: true' on the fly based on 'version: x.y'
40 schema_version_workaround(_DICT)
42 schema_version_workaround(_DICT['idf'])
44 _VALIDATOR = jsonschema.Draft4Validator(_SCHEMA)
45 for error in _VALIDATOR.iter_errors(_DICT):
46 raise RuntimeError(str(error))