comparison planemo/lib/python3.7/site-packages/oyaml.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 import platform
2 import sys
3 from collections import OrderedDict
4
5 import yaml as pyyaml
6
7
8 _items = "viewitems" if sys.version_info < (3,) else "items"
9 _std_dict_is_order_preserving = sys.version_info >= (3, 7) or (
10 sys.version_info >= (3, 6) and platform.python_implementation() == "CPython"
11 )
12
13
14 def map_representer(dumper, data):
15 return dumper.represent_dict(getattr(data, _items)())
16
17
18 def map_constructor(loader, node):
19 loader.flatten_mapping(node)
20 pairs = loader.construct_pairs(node)
21 try:
22 return OrderedDict(pairs)
23 except TypeError:
24 loader.construct_mapping(node) # trigger any contextual error
25 raise
26
27
28 if pyyaml.safe_dump is pyyaml.dump:
29 # PyYAML v4.x
30 SafeDumper = pyyaml.dumper.Dumper
31 DangerDumper = pyyaml.dumper.DangerDumper
32 else:
33 SafeDumper = pyyaml.dumper.SafeDumper
34 DangerDumper = pyyaml.dumper.Dumper
35
36 pyyaml.add_representer(dict, map_representer, Dumper=SafeDumper)
37 pyyaml.add_representer(OrderedDict, map_representer, Dumper=SafeDumper)
38 pyyaml.add_representer(dict, map_representer, Dumper=DangerDumper)
39 pyyaml.add_representer(OrderedDict, map_representer, Dumper=DangerDumper)
40
41
42 Loader = None
43 if not _std_dict_is_order_preserving:
44 for loader_name in pyyaml.loader.__all__:
45 Loader = getattr(pyyaml.loader, loader_name)
46 pyyaml.add_constructor("tag:yaml.org,2002:map", map_constructor, Loader=Loader)
47
48
49 # Merge PyYAML namespace into ours.
50 # This allows users a drop-in replacement:
51 # import oyaml as yaml
52 del map_constructor, map_representer, SafeDumper, DangerDumper, Loader
53 from yaml import *