annotate configmix/yaml.py @ 135:b7b0cea8ec6e

Document "configmix.yaml.loadXXX()" functions
author Franz Glasner <hg@dom66.de>
date Thu, 05 Apr 2018 09:23:44 +0200
parents 2f2e819e8d17
children eee1dd1f99bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
79
a43749f751e0 Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents: 56
diff changeset
2 #-
a43749f751e0 Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents: 56
diff changeset
3 # :Copyright: (c) 2015-2018, Franz Glasner. All rights reserved.
a43749f751e0 Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents: 56
diff changeset
4 # :License: 3-clause BSD. See LICENSE.txt for details.
a43749f751e0 Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents: 56
diff changeset
5 #-
90
99e7b10c8aa8 Mark the yaml module with ":mod:"
Franz Glasner <hg@dom66.de>
parents: 82
diff changeset
6 """Simple wrapper for :mod:`yaml` to support all-unicode strings when
99e7b10c8aa8 Mark the yaml module with ":mod:"
Franz Glasner <hg@dom66.de>
parents: 82
diff changeset
7 loading configuration files.
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
8
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
9 """
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
10
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
11 from __future__ import division, print_function, absolute_import
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
12
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
13 try:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
14 from collections import OrderedDict
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
15 except ImportError:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
16 try:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
17 from ordereddict import OrderedDict
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
18 except ImportError:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
19 OrderedDict = None
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
20 import yaml
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
21 import yaml.constructor
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
22
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
23 from .compat import u
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
24
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
25
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
26 __all__ = ["safe_load", "safe_load_all", "load", "load_all"]
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
27
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
28
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
29 class ConfigLoader(yaml.Loader):
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
30
56
1f11672c4615 Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
31 """A YAML loader, which makes all ``!!str`` strings to Unicode.
1f11672c4615 Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
32 Standard PyYAML does this only in the non-ASCII case.
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
33
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
34 If an `OrderedDict` implementation is available then all "map" and
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
35 "omap" nodes are constructed as `OrderedDict`.
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
36 This is against YAML specs but within configuration files it seems
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
37 more natural.
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
38
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
39 """
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
40
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
41 def construct_yaml_str(self, node):
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
42 return self.construct_scalar(node)
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
43
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
44 if OrderedDict:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
45
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
46 #
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
47 # From https://pypi.python.org/pypi/yamlordereddictloader/0.1.1
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
48 # (MIT License)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
49 #
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
50
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
51 def construct_yaml_map(self, node):
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
52 data = OrderedDict()
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
53 yield data
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
54 value = self.construct_mapping(node)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
55 data.update(value)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
56
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
57 def construct_mapping(self, node, deep=False):
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
58 if isinstance(node, yaml.MappingNode):
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
59 self.flatten_mapping(node)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
60 else:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
61 raise yaml.constructor.ConstructorError(None, None,
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
62 'expected a mapping node, but found %s' % node.id,
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
63 node.start_mark)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
64
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
65 mapping = OrderedDict()
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
66 for key_node, value_node in node.value:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
67 key = self.construct_object(key_node, deep=deep)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
68 try:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
69 hash(key)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
70 except TypeError as err:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
71 raise yaml.constructor.ConstructorError(
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
72 'while constructing a mapping', node.start_mark,
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
73 'found unacceptable key (%s)' % err, key_node.start_mark)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
74 value = self.construct_object(value_node, deep=deep)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
75 mapping[key] = value
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
76 return mapping
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
77
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
78
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
79 ConfigLoader.add_constructor(
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
80 u("tag:yaml.org,2002:str"),
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
81 ConfigLoader.construct_yaml_str)
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
82 if OrderedDict:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
83 ConfigLoader.add_constructor(
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
84 u("tag:yaml.org,2002:map"),
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
85 ConfigLoader.construct_yaml_map)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
86 ConfigLoader.add_constructor(
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
87 u("tag:yaml.org,2002:omap"),
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
88 ConfigLoader.construct_yaml_map)
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
89
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
90
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
91 class ConfigSafeLoader(yaml.SafeLoader):
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
92
56
1f11672c4615 Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
93 """A safe YAML loader, which makes all ``!!str`` strings to Unicode.
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
94 Standard PyYAML does this only in the non-ASCII case.
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
95
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
96 If an `OrderedDict` implementation is available then all "map" and
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
97 "omap" nodes are constructed as `OrderedDict`.
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
98 This is against YAML specs but within configuration files it seems
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
99 more natural.
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
100
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
101 """
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
102
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
103 def construct_yaml_str(self, node):
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
104 return self.construct_scalar(node)
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
105
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
106 if OrderedDict:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
107
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
108 #
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
109 # From https://pypi.python.org/pypi/yamlordereddictloader/0.1.1
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
110 # (MIT License)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
111 #
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
112
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
113 def construct_yaml_map(self, node):
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
114 data = OrderedDict()
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
115 yield data
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
116 value = self.construct_mapping(node)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
117 data.update(value)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
118
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
119 def construct_mapping(self, node, deep=False):
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
120 if isinstance(node, yaml.MappingNode):
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
121 self.flatten_mapping(node)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
122 else:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
123 raise yaml.constructor.ConstructorError(None, None,
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
124 'expected a mapping node, but found %s' % node.id,
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
125 node.start_mark)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
126
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
127 mapping = OrderedDict()
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
128 for key_node, value_node in node.value:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
129 key = self.construct_object(key_node, deep=deep)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
130 try:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
131 hash(key)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
132 except TypeError as err:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
133 raise yaml.constructor.ConstructorError(
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
134 'while constructing a mapping', node.start_mark,
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
135 'found unacceptable key (%s)' % err, key_node.start_mark)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
136 value = self.construct_object(value_node, deep=deep)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
137 mapping[key] = value
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
138 return mapping
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
139
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
140
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
141 ConfigSafeLoader.add_constructor(
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
142 u("tag:yaml.org,2002:str"),
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
143 ConfigSafeLoader.construct_yaml_str)
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
144 if OrderedDict:
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
145 ConfigSafeLoader.add_constructor(
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
146 u("tag:yaml.org,2002:map"),
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
147 ConfigSafeLoader.construct_yaml_map)
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
148 ConfigSafeLoader.add_constructor(
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
149 u("tag:yaml.org,2002:omap"),
3
bedc4f95b9e9 Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 1
diff changeset
150 ConfigSafeLoader.construct_yaml_map)
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
151
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
152
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
153 def load(stream, Loader=ConfigLoader):
135
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
154 """Parse the given `stream` and return a Python object constructed
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
155 from for the first document in the stream.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
156
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
157 """
134
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
158 data = yaml.load(stream, Loader)
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
159 if OrderedDict:
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
160 if not isinstance(data, OrderedDict):
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
161 raise TypeError("YAML root object must be a mapping")
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
162 return data
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
163
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
164
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
165 def load_all(stream, Loader=ConfigLoader):
135
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
166 """Parse the given `stream` and return a sequence of Python objects
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
167 corresponding to the documents in the `stream`.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
168
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
169 """
134
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
170 data_all = yaml.load_all(stream, Loader)
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
171 if OrderedDict:
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
172 for data in data_all:
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
173 if not isinstance(data, OrderedDict):
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
174 raise TypeError("YAML root object must be a mapping")
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
175 return data_all
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
176
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
177
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
178 def safe_load(stream):
135
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
179 """Parse the given `stream` and return a Python object constructed
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
180 from for the first document in the stream.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
181
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
182 Recognize only standard YAML tags and cannot construct an
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
183 arbitrary Python object.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
184
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
185 """
134
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
186 data = yaml.load(stream, Loader=ConfigSafeLoader)
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
187 if OrderedDict:
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
188 if not isinstance(data, OrderedDict):
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
189 raise TypeError("YAML root object must be a mapping")
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
190 return data
1
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
191
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
192
e4c63b4f1568 Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
193 def safe_load_all(stream):
135
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
194 """Return the list of all decoded YAML documents in the file `stream`.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
195
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
196 Recognize only standard YAML tags and cannot construct an
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
197 arbitrary Python object.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
198
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
199 """
134
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
200 data_all = yaml.load_all(stream, Loader=ConfigSafeLoader)
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
201 if OrderedDict:
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
202 for data in data_all:
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
203 if not isinstance(data, OrderedDict):
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
204 raise TypeError("YAML root object must be a mapping")
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 90
diff changeset
205 return data_all