test_yaml.py
1.3 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
# -*- coding: utf-8 -*-
from pykwalify.core import Core
from pykwalify.errors import SchemaError
"""
Read yaml file :
import yaml
with open("C:/srv/develop/pyros/config/config.yml", 'r') as stream:
try:
a = yaml.safe_load(stream)
print(a)
except yaml.YAMLError as exc:
print(exc)
# Write YAML file
with open('C:/srv/develop/pyros/config/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 = 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("config.yml","schema.yaml"))