-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_yaml.py
40 lines (30 loc) · 1018 Bytes
/
to_yaml.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
from kamidana import (
as_filter,
)
from collections import OrderedDict
from jinja2 import pass_context
import ruamel.yaml
def represent_odict(dumper, instance):
return dumper.represent_mapping('tag:yaml.org,2002:map', instance.items())
ruamel.yaml.add_representer(OrderedDict, represent_odict)
def construct_odict(loader, node):
return OrderedDict(loader.construct_pairs(node))
ruamel.yaml.add_constructor('tag:yaml.org,2002:map', construct_odict)
@as_filter
@pass_context
def to_yaml(ctx, a, *args, **kw):
yaml = ruamel.yaml.YAML(typ=['rt', 'string'])
yaml.default_flow_style = kw.pop('default_flow_style', None)
yaml.allow_unicode=True
yaml.indent=kw.pop('indent', 2)
transformed = yaml.dump_to_string(a)
return transformed
@as_filter
@pass_context
def to_nice_yaml(ctx, a, indent=2, *args, **kw):
yaml = ruamel.yaml.YAML(typ=['rt', 'string'])
yaml.default_flow_style=False
yaml.allow_unicode=True
yaml.indent=indent
transformed = yaml.dump_to_string(a)
return transformed