schema_observatory_verify.py
1.31 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
# -*- coding: utf-8 -*-
import pykwalify.core
from pykwalify.errors import SchemaError
"""
Read yaml file :
import yaml
filename = "observatory_guitalens.yml"
with open(filename, 'r') as stream:
try:
a = yaml.safe_load(stream)
print(a)
except yaml.YAMLError as exc:
print(exc)
# Write YAML file
with open('config2.yml', 'w', encoding='utf8') as outfile:
yaml.dump(a, outfile, default_flow_style=False, allow_unicode=True)
"""
"""
import yaml
with open("config.yml", 'r') as stream:
try:
a = yaml.safe_load(stream)
print(a)
except yaml.YAMLError as exc:
print(exc)
"""
def check_and_return_config(yaml_file:str,schema_file:str)->dict:
"""
Check if yaml_file is valid for the schema_file
Args:
yaml_file (str): Path to the config_file to be validated
schema_file (str): Path to the schema file
Returns:
dict: Dictionnary of the config file (with values)
"""
try:
c = pykwalify.core.Core(source_file=yaml_file, schema_files=[schema_file])
return c.validate(raise_exception=True)
except SchemaError as error:
#TODO : find a way to retrieve message with path to error
print(error.path)
print(check_and_return_config("observatory_v1.yml","schema_observatory-1.0.yml"))