comparison env/lib/python3.7/site-packages/gxformat2/_yaml.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 """YAML loading utilities for gxformat2."""
2 from collections import OrderedDict
3
4 try:
5 from galaxy.model.custom_types import MutationDict
6 except ImportError:
7 MutationDict = None
8 import yaml
9
10
11 def ordered_load(stream, Loader=yaml.SafeLoader, **kwds):
12 """Safe and ordered load of YAML from stream."""
13 class OrderedLoader(Loader):
14 pass
15
16 def construct_mapping(loader, node):
17 loader.flatten_mapping(node)
18 return OrderedDict(loader.construct_pairs(node))
19
20 OrderedLoader.add_constructor(
21 yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
22 construct_mapping)
23
24 return yaml.load(stream, OrderedLoader, **kwds)
25
26
27 def ordered_dump(data, stream=None, Dumper=yaml.SafeDumper, **kwds):
28 """Safe and ordered dump of YAML to stream."""
29 class OrderedDumper(Dumper):
30 pass
31
32 def _dict_representer(dumper, data):
33 return dumper.represent_mapping(
34 yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
35 list(data.items()))
36 OrderedDumper.add_representer(OrderedDict, _dict_representer)
37 if MutationDict is not None:
38 OrderedDumper.add_representer(MutationDict, _dict_representer)
39
40 return yaml.dump(data, stream, OrderedDumper, **kwds)
41
42
43 __all__ = (
44 'ordered_dump',
45 'ordered_load',
46 )