-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyaml_wrapper.py
50 lines (33 loc) · 1.43 KB
/
yaml_wrapper.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
42
43
44
45
46
47
48
49
50
import collections
import ruamel.yaml
def dict_representer(dumper, data):
return dumper.represent_dict(data.iteritems())
def dict_constructor(loader, node):
return collections.OrderedDict(loader.construct_pairs(node))
def literal_str_representer(dumper, data):
if '\n' in data:
return dumper.represent_scalar(yaml.resolver.BaseResolver.DEFAULT_SCALAR_TAG, data, style='|')
else:
return dumper.represent_scalar(yaml.resolver.BaseResolver.DEFAULT_SCALAR_TAG, data)
class CarryOverComposer(ruamel.yaml.composer.Composer):
def __init__(self, loader=None):
super().__init__(loader=loader)
def compose_document(self):
# Drop the DOCUMENT-START event.
self.parser.get_event()
# Compose the root node.
node = self.compose_node(None, None)
# Drop the DOCUMENT-END event.
self.parser.get_event()
if not (hasattr(self, 'anchors') and getattr(self, 'anchors')):
# noinspection PyAttributeOutsideInit
self.anchors = {}
return node
def _yaml():
result = ruamel.yaml.YAML(typ='rt')
result.constructor.add_constructor(ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, dict_constructor)
result.representer.add_representer(collections.OrderedDict, dict_representer)
result.representer.add_representer(str, literal_str_representer)
result.Composer = CarryOverComposer
return result
yaml = _yaml()