annotate configmix/yaml.py @ 166:b5ce9a8461bf

Use the filesystem encoding explicitely where appropriate.
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 14 Mar 2019 01:35:16 +0100
parents e2e8d21b4122
children b3b5ed34d180
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 #-
156
e2e8d21b4122 Adjust copyright to year 2019
Franz Glasner <fzglas.hg@dom66.de>
parents: 136
diff changeset
3 # :Copyright: (c) 2015-2019, Franz Glasner. All rights reserved.
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
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
136
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
29 DictImpl = OrderedDict or dict
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
30
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
31
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
32 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
33
56
1f11672c4615 Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
34 """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
35 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
36
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
37 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
38 "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
39 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
40 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
41
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
42 """
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
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
44 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
45 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
46
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
47 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
48
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 # 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
51 # (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
52 #
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
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 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
55 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
56 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
57 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
58 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
59
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 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
61 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
62 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
63 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
64 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
65 '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
66 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
67
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 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
69 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
70 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
71 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
72 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
73 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
74 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
75 '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
76 '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
77 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
78 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
79 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
80
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
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
82 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
83 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
84 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
85 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
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: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
88 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
89 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
90 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
91 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
92
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
93
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 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
95
56
1f11672c4615 Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
96 """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
97 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
98
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
99 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
100 "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
101 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
102 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
103
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
104 """
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
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
106 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
107 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
108
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
109 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
110
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 # 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
113 # (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
114 #
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
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 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
117 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
118 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
119 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
120 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
121
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 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
123 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
124 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
125 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
126 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
127 '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
128 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
129
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 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
131 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
132 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
133 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
134 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
135 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
136 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
137 '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
138 '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
139 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
140 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
141 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
142
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
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
144 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
145 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
146 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
147 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
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: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
150 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
151 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
152 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
153 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
154
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
155
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
156 def load(stream, Loader=ConfigLoader):
135
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
157 """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
158 from for the first document in the stream.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
159
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
160 """
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
161 data = yaml.load(stream, Loader)
136
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
162 # Map an empty document to an empty dict
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
163 if data is None:
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
164 return DictImpl()
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
165 if not isinstance(data, DictImpl):
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
166 raise TypeError("YAML root object must be a mapping")
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
167 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
168
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
169
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
170 def load_all(stream, Loader=ConfigLoader):
135
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
171 """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
172 corresponding to the documents in the `stream`.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
173
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
174 """
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
175 data_all = yaml.load_all(stream, Loader)
136
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
176 rdata = []
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
177 for data in data_all:
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
178 if data is None:
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
179 rdata.append(DictImpl())
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
180 else:
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
181 if not isinstance(data, DictImpl):
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
182 raise TypeError("YAML root object must be a mapping")
136
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
183 rdata.append(data)
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
184 return rdata
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
185
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
186
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
187 def safe_load(stream):
135
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
188 """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
189 from for the first document in the stream.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
190
136
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
191 Recognizes only standard YAML tags and cannot construct an
135
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
192 arbitrary Python object.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
193
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
194 """
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
195 data = yaml.load(stream, Loader=ConfigSafeLoader)
136
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
196 # Map an empty document to an empty dict
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
197 if data is None:
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
198 return DictImpl()
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
199 if not isinstance(data, DictImpl):
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
200 raise TypeError("YAML root object must be a mapping")
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
201 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
202
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
203
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
204 def safe_load_all(stream):
135
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
205 """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
206
136
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
207 Recognizes only standard YAML tags and cannot construct an
135
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
208 arbitrary Python object.
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
209
b7b0cea8ec6e Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
210 """
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
211 data_all = yaml.load_all(stream, Loader=ConfigSafeLoader)
136
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
212 rdata = []
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
213 for data in data_all:
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
214 if data is None:
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
215 rdata.append(DictImpl())
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
216 else:
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
217 if not isinstance(data, DictImpl):
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
218 raise TypeError("YAML root object must be a mapping")
136
eee1dd1f99bf Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
Franz Glasner <hg@dom66.de>
parents: 135
diff changeset
219 rdata.append(data)
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
220 return data_all