Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/yaml.py @ 207:b3b5ed34d180
Handle most flake8 errors and warnings.
NOTE: E265 "block comment should start with '# ' ist not yet handled.
We would need to adjust our Python style.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sun, 05 May 2019 18:29:47 +0200 |
| parents | e2e8d21b4122 |
| children | bbe8513ea649 |
| 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: |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
64 raise yaml.constructor.ConstructorError( |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
65 None, |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
66 None, |
|
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
|
67 '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
|
68 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
|
69 |
|
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 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 'while constructing a mapping', node.start_mark, |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
78 'found unacceptable key (%s)' % (err, |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
79 key_node.start_mark) |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
80 ) |
|
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
|
81 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
|
82 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
|
83 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
|
84 |
|
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
|
85 |
|
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
|
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: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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 |
|
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 |
|
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 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
|
99 |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
100 """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
|
101 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
|
102 |
|
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
|
103 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
|
104 "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
|
105 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
|
106 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
|
107 |
|
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
|
108 """ |
|
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
|
109 |
|
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
|
110 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
|
111 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
|
112 |
|
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
|
113 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
|
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 # 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
|
117 # (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
|
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 |
|
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 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 |
|
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 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
|
127 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
|
128 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
|
129 else: |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
130 raise yaml.constructor.ConstructorError( |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
131 None, |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
132 None, |
|
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
|
133 '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
|
134 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 |
|
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 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
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 'while constructing a mapping', node.start_mark, |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
144 'found unacceptable key (%s)' % (err, |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
145 key_node.start_mark) |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
146 ) |
|
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 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
|
148 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
|
149 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
|
150 |
|
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 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 |
|
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 def load(stream, Loader=ConfigLoader): |
|
135
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
165 """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
|
166 from for the first document in the stream. |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
167 |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
168 """ |
|
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
|
169 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
|
170 # 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
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 load_all(stream, Loader=ConfigLoader): |
|
135
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
179 """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
|
180 corresponding to the documents 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 """ |
|
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
|
183 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
|
184 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
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 |
|
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
|
194 |
|
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
|
195 def safe_load(stream): |
|
135
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
196 """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
|
197 from for the first document in the stream. |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
198 |
|
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
|
199 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
|
200 arbitrary Python object. |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
201 |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
202 """ |
|
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
|
203 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
|
204 # 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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 |
|
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
|
211 |
|
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
|
212 def safe_load_all(stream): |
|
135
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
213 """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
|
214 |
|
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
|
215 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
|
216 arbitrary Python object. |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
217 |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
218 """ |
|
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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 return data_all |
