forked from serviceprototypinglab/dcvalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyamlreader.py
41 lines (39 loc) · 1.31 KB
/
yamlreader.py
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
from ruamel.yaml import YAML
from ruamel.yaml.constructor import SafeConstructor
# def construct_yaml_map(self, node):
# # test if there are duplicate node keys
# data = []
# yield data
# for key_node, value_node in node.value:
# key = self.construct_object(key_node, deep=True)
# val = self.construct_object(value_node, deep=True)
# data.append((key, val))
def construct_yaml_map(self, node):
# test if there are duplicate node keys
keys = set()
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=True)
if key in keys:
break
keys.add(key)
else:
data = {} # type: Dict[Any, Any]
yield data
value = self.construct_mapping(node)
data.update(value)
return
data = []
yield data
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=True)
val = self.construct_object(value_node, deep=True)
data.append((key, val))
SafeConstructor.add_constructor(u'tag:yaml.org,2002:map', construct_yaml_map)
yaml_overwrited = YAML(typ='safe')
def reader(content):
try:
parsed = yaml_overwrited.load(content)
print("syntax is ok")
return parsed
except:
print("yaml syntax error")