forked from eth-cscs/stackinator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.py
More file actions
54 lines (41 loc) · 1.65 KB
/
schema.py
File metadata and controls
54 lines (41 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import json
import pathlib
import jsonschema
import yaml
prefix = pathlib.Path(__file__).parent.resolve()
# create a validator that will insert optional fields with their default values
# if they have not been provided.
def extend_with_default(validator_class):
validate_properties = validator_class.VALIDATORS["properties"]
def set_defaults(validator, properties, instance, schema):
# if instance is none, it's not possible to set any default for any sub-property
if instance is not None:
for property, subschema in properties.items():
if "default" in subschema:
instance.setdefault(property, subschema["default"])
for error in validate_properties(
validator,
properties,
instance,
schema,
):
yield error
return jsonschema.validators.extend(
validator_class,
{"properties": set_defaults},
)
def py2yaml(data, indent):
dump = yaml.dump(data)
lines = [ln for ln in dump.split("\n") if ln != ""]
res = ("\n" + " " * indent).join(lines)
return res
validator = extend_with_default(jsonschema.Draft7Validator)
# load recipe yaml schema
config_schema = json.load(open(prefix / "schema/config.json"))
config_validator = validator(config_schema)
compilers_schema = json.load(open(prefix / "schema/compilers.json"))
compilers_validator = validator(compilers_schema)
environments_schema = json.load(open(prefix / "schema/environments.json"))
environments_validator = validator(environments_schema)
cache_schema = json.load(open(prefix / "schema/cache.json"))
cache_validator = validator(cache_schema)