Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/yaml.py @ 284:4aaf74858d07
Some links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws module
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 07 Dec 2020 01:59:11 +0100 |
| parents | ff964825a75a |
| children | edf5cc1ffd26 |
| 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 -*- |
|
208
bbe8513ea649
Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents:
207
diff
changeset
|
2 # :- |
|
237
13711ba8e81e
Adjust copyright year to 2020
Franz Glasner <fzglas.hg@dom66.de>
parents:
208
diff
changeset
|
3 # :Copyright: (c) 2015-2020, 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. |
|
208
bbe8513ea649
Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents:
207
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 |
|
250
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
13 |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
14 __all__ = ["safe_load", "safe_load_all", "load", "load_all"] |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
15 |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
16 |
|
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
|
17 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
|
18 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
|
19 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
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 |
|
21
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
27 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
|
28 |
|
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
|
29 |
|
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
|
30 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
|
31 |
|
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
|
32 |
|
1
e4c63b4f1568
Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
33 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
|
34 |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
35 """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
|
36 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
|
37 |
|
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
|
38 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
|
39 "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
|
40 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
|
41 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
|
42 |
|
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
|
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 |
|
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 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
|
46 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
|
47 |
|
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
|
48 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
|
49 |
|
bedc4f95b9e9
Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
1
diff
changeset
|
50 # |
|
bedc4f95b9e9
Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
1
diff
changeset
|
51 # 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
|
52 # (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
|
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 |
|
bedc4f95b9e9
Use a 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 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 |
|
bedc4f95b9e9
Use a 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 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
|
62 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
|
63 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
|
64 else: |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
65 raise yaml.constructor.ConstructorError( |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
66 None, |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
67 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
|
68 '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
|
69 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
|
70 |
|
bedc4f95b9e9
Use a 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 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 '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
|
79 'found unacceptable key (%s)' % (err, |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
80 key_node.start_mark) |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
81 ) |
|
3
bedc4f95b9e9
Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
1
diff
changeset
|
82 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
|
83 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
|
84 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
|
85 |
|
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
|
86 |
|
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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 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
|
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 |
|
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 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
|
100 |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
101 """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
|
102 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
|
103 |
|
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
|
104 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
|
105 "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
|
106 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
|
107 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
|
108 |
|
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
|
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 |
|
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 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
|
112 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
|
113 |
|
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
|
114 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
|
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 # |
|
bedc4f95b9e9
Use a 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 # 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
|
118 # (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
|
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 |
|
bedc4f95b9e9
Use a 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 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 |
|
bedc4f95b9e9
Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
1
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 else: |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
131 raise yaml.constructor.ConstructorError( |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
132 None, |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
133 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
|
134 '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
|
135 node.start_mark) |
|
bedc4f95b9e9
Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
1
diff
changeset
|
136 |
|
bedc4f95b9e9
Use a YAML constructor that automatically creates OrderedDict objects when an OrderedDict implementation is available
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
1
diff
changeset
|
137 mapping = 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 '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
|
145 'found unacceptable key (%s)' % (err, |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
146 key_node.start_mark) |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
147 ) |
|
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
|
148 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
|
149 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
|
150 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
|
151 |
|
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
|
152 |
|
e4c63b4f1568
Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 |
|
e4c63b4f1568
Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
164 |
|
e4c63b4f1568
Provide a yaml wrapper that import with all-unicode strings on Python2 but does not path the Loader globally
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
165 def load(stream, Loader=ConfigLoader): |
|
135
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
166 """Parse the given `stream` and return a Python object constructed |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
167 from for the first document in the stream. |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
168 |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
169 """ |
|
134
2f2e819e8d17
Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents:
90
diff
changeset
|
170 data = 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
|
171 # 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
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 |
|
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
|
179 def load_all(stream, Loader=ConfigLoader): |
|
135
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
180 """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
|
181 corresponding to the documents in the `stream`. |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
182 |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
183 """ |
|
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
|
184 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
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
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 |
|
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
|
196 def safe_load(stream): |
|
135
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
197 """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
|
198 from for the first document in the stream. |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
199 |
|
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
|
200 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
|
201 arbitrary Python object. |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
202 |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
203 """ |
|
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
|
204 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
|
205 # 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
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 |
|
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
|
213 def safe_load_all(stream): |
|
135
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
214 """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
|
215 |
|
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
|
216 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
|
217 arbitrary Python object. |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
218 |
|
b7b0cea8ec6e
Document "configmix.yaml.loadXXX()" functions
Franz Glasner <hg@dom66.de>
parents:
134
diff
changeset
|
219 """ |
|
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 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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 return data_all |
