comparison configmix/yaml.py @ 136:eee1dd1f99bf

Simplify the YAML return type check and map a "None" (empty document) result to an empty mapping
author Franz Glasner <hg@dom66.de>
date Thu, 05 Apr 2018 09:39:41 +0200
parents b7b0cea8ec6e
children e2e8d21b4122
comparison
equal deleted inserted replaced
135:b7b0cea8ec6e 136:eee1dd1f99bf
22 22
23 from .compat import u 23 from .compat import u
24 24
25 25
26 __all__ = ["safe_load", "safe_load_all", "load", "load_all"] 26 __all__ = ["safe_load", "safe_load_all", "load", "load_all"]
27
28
29 DictImpl = OrderedDict or dict
27 30
28 31
29 class ConfigLoader(yaml.Loader): 32 class ConfigLoader(yaml.Loader):
30 33
31 """A YAML loader, which makes all ``!!str`` strings to Unicode. 34 """A YAML loader, which makes all ``!!str`` strings to Unicode.
154 """Parse the given `stream` and return a Python object constructed 157 """Parse the given `stream` and return a Python object constructed
155 from for the first document in the stream. 158 from for the first document in the stream.
156 159
157 """ 160 """
158 data = yaml.load(stream, Loader) 161 data = yaml.load(stream, Loader)
159 if OrderedDict: 162 # Map an empty document to an empty dict
160 if not isinstance(data, OrderedDict): 163 if data is None:
161 raise TypeError("YAML root object must be a mapping") 164 return DictImpl()
165 if not isinstance(data, DictImpl):
166 raise TypeError("YAML root object must be a mapping")
162 return data 167 return data
163 168
164 169
165 def load_all(stream, Loader=ConfigLoader): 170 def load_all(stream, Loader=ConfigLoader):
166 """Parse the given `stream` and return a sequence of Python objects 171 """Parse the given `stream` and return a sequence of Python objects
167 corresponding to the documents in the `stream`. 172 corresponding to the documents in the `stream`.
168 173
169 """ 174 """
170 data_all = yaml.load_all(stream, Loader) 175 data_all = yaml.load_all(stream, Loader)
171 if OrderedDict: 176 rdata = []
172 for data in data_all: 177 for data in data_all:
173 if not isinstance(data, OrderedDict): 178 if data is None:
179 rdata.append(DictImpl())
180 else:
181 if not isinstance(data, DictImpl):
174 raise TypeError("YAML root object must be a mapping") 182 raise TypeError("YAML root object must be a mapping")
175 return data_all 183 rdata.append(data)
184 return rdata
176 185
177 186
178 def safe_load(stream): 187 def safe_load(stream):
179 """Parse the given `stream` and return a Python object constructed 188 """Parse the given `stream` and return a Python object constructed
180 from for the first document in the stream. 189 from for the first document in the stream.
181 190
182 Recognize only standard YAML tags and cannot construct an 191 Recognizes only standard YAML tags and cannot construct an
183 arbitrary Python object. 192 arbitrary Python object.
184 193
185 """ 194 """
186 data = yaml.load(stream, Loader=ConfigSafeLoader) 195 data = yaml.load(stream, Loader=ConfigSafeLoader)
187 if OrderedDict: 196 # Map an empty document to an empty dict
188 if not isinstance(data, OrderedDict): 197 if data is None:
189 raise TypeError("YAML root object must be a mapping") 198 return DictImpl()
199 if not isinstance(data, DictImpl):
200 raise TypeError("YAML root object must be a mapping")
190 return data 201 return data
191 202
192 203
193 def safe_load_all(stream): 204 def safe_load_all(stream):
194 """Return the list of all decoded YAML documents in the file `stream`. 205 """Return the list of all decoded YAML documents in the file `stream`.
195 206
196 Recognize only standard YAML tags and cannot construct an 207 Recognizes only standard YAML tags and cannot construct an
197 arbitrary Python object. 208 arbitrary Python object.
198 209
199 """ 210 """
200 data_all = yaml.load_all(stream, Loader=ConfigSafeLoader) 211 data_all = yaml.load_all(stream, Loader=ConfigSafeLoader)
201 if OrderedDict: 212 rdata = []
202 for data in data_all: 213 for data in data_all:
203 if not isinstance(data, OrderedDict): 214 if data is None:
215 rdata.append(DictImpl())
216 else:
217 if not isinstance(data, DictImpl):
204 raise TypeError("YAML root object must be a mapping") 218 raise TypeError("YAML root object must be a mapping")
219 rdata.append(data)
205 return data_all 220 return data_all