@azure-tools/typespec-python 0.24.2 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/dist/src/code-model.d.ts.map +1 -1
  2. package/dist/src/code-model.js +1 -4
  3. package/dist/src/code-model.js.map +1 -1
  4. package/dist/src/emitter.d.ts.map +1 -1
  5. package/dist/src/emitter.js +5 -4
  6. package/dist/src/emitter.js.map +1 -1
  7. package/dist/src/external-process.d.ts +0 -9
  8. package/dist/src/external-process.d.ts.map +1 -1
  9. package/dist/src/external-process.js +1 -25
  10. package/dist/src/external-process.js.map +1 -1
  11. package/dist/src/types.d.ts.map +1 -1
  12. package/dist/src/types.js +2 -0
  13. package/dist/src/types.js.map +1 -1
  14. package/generator/LICENSE +21 -0
  15. package/generator/README.md +1 -0
  16. package/generator/dev_requirements.txt +5 -0
  17. package/generator/pygen/__init__.py +107 -0
  18. package/generator/pygen/_version.py +7 -0
  19. package/generator/pygen/black.py +71 -0
  20. package/generator/pygen/codegen/__init__.py +334 -0
  21. package/generator/pygen/codegen/_utils.py +16 -0
  22. package/generator/pygen/codegen/models/__init__.py +202 -0
  23. package/generator/pygen/codegen/models/base.py +186 -0
  24. package/generator/pygen/codegen/models/base_builder.py +119 -0
  25. package/generator/pygen/codegen/models/client.py +429 -0
  26. package/generator/pygen/codegen/models/code_model.py +239 -0
  27. package/generator/pygen/codegen/models/combined_type.py +149 -0
  28. package/generator/pygen/codegen/models/constant_type.py +129 -0
  29. package/generator/pygen/codegen/models/credential_types.py +221 -0
  30. package/generator/pygen/codegen/models/dictionary_type.py +127 -0
  31. package/generator/pygen/codegen/models/enum_type.py +238 -0
  32. package/generator/pygen/codegen/models/imports.py +291 -0
  33. package/generator/pygen/codegen/models/list_type.py +143 -0
  34. package/generator/pygen/codegen/models/lro_operation.py +143 -0
  35. package/generator/pygen/codegen/models/lro_paging_operation.py +32 -0
  36. package/generator/pygen/codegen/models/model_type.py +361 -0
  37. package/generator/pygen/codegen/models/operation.py +518 -0
  38. package/generator/pygen/codegen/models/operation_group.py +184 -0
  39. package/generator/pygen/codegen/models/paging_operation.py +156 -0
  40. package/generator/pygen/codegen/models/parameter.py +402 -0
  41. package/generator/pygen/codegen/models/parameter_list.py +390 -0
  42. package/generator/pygen/codegen/models/primitive_types.py +626 -0
  43. package/generator/pygen/codegen/models/property.py +175 -0
  44. package/generator/pygen/codegen/models/request_builder.py +189 -0
  45. package/generator/pygen/codegen/models/request_builder_parameter.py +115 -0
  46. package/generator/pygen/codegen/models/response.py +348 -0
  47. package/generator/pygen/codegen/models/utils.py +23 -0
  48. package/generator/pygen/codegen/serializers/__init__.py +570 -0
  49. package/generator/pygen/codegen/serializers/base_serializer.py +21 -0
  50. package/generator/pygen/codegen/serializers/builder_serializer.py +1454 -0
  51. package/generator/pygen/codegen/serializers/client_serializer.py +295 -0
  52. package/generator/pygen/codegen/serializers/enum_serializer.py +15 -0
  53. package/generator/pygen/codegen/serializers/general_serializer.py +212 -0
  54. package/generator/pygen/codegen/serializers/import_serializer.py +127 -0
  55. package/generator/pygen/codegen/serializers/metadata_serializer.py +198 -0
  56. package/generator/pygen/codegen/serializers/model_init_serializer.py +33 -0
  57. package/generator/pygen/codegen/serializers/model_serializer.py +287 -0
  58. package/generator/pygen/codegen/serializers/operation_groups_serializer.py +89 -0
  59. package/generator/pygen/codegen/serializers/operations_init_serializer.py +44 -0
  60. package/generator/pygen/codegen/serializers/parameter_serializer.py +221 -0
  61. package/generator/pygen/codegen/serializers/patch_serializer.py +19 -0
  62. package/generator/pygen/codegen/serializers/request_builders_serializer.py +52 -0
  63. package/generator/pygen/codegen/serializers/sample_serializer.py +163 -0
  64. package/generator/pygen/codegen/serializers/test_serializer.py +287 -0
  65. package/generator/pygen/codegen/serializers/types_serializer.py +31 -0
  66. package/generator/pygen/codegen/serializers/utils.py +68 -0
  67. package/generator/pygen/codegen/templates/client.py.jinja2 +37 -0
  68. package/generator/pygen/codegen/templates/client_container.py.jinja2 +12 -0
  69. package/generator/pygen/codegen/templates/config.py.jinja2 +73 -0
  70. package/generator/pygen/codegen/templates/config_container.py.jinja2 +16 -0
  71. package/generator/pygen/codegen/templates/conftest.py.jinja2 +28 -0
  72. package/generator/pygen/codegen/templates/enum.py.jinja2 +13 -0
  73. package/generator/pygen/codegen/templates/enum_container.py.jinja2 +10 -0
  74. package/generator/pygen/codegen/templates/init.py.jinja2 +24 -0
  75. package/generator/pygen/codegen/templates/keywords.jinja2 +19 -0
  76. package/generator/pygen/codegen/templates/lro_operation.py.jinja2 +16 -0
  77. package/generator/pygen/codegen/templates/lro_paging_operation.py.jinja2 +18 -0
  78. package/generator/pygen/codegen/templates/macros.jinja2 +12 -0
  79. package/generator/pygen/codegen/templates/metadata.json.jinja2 +167 -0
  80. package/generator/pygen/codegen/templates/model_base.py.jinja2 +899 -0
  81. package/generator/pygen/codegen/templates/model_container.py.jinja2 +13 -0
  82. package/generator/pygen/codegen/templates/model_dpg.py.jinja2 +92 -0
  83. package/generator/pygen/codegen/templates/model_init.py.jinja2 +28 -0
  84. package/generator/pygen/codegen/templates/model_msrest.py.jinja2 +92 -0
  85. package/generator/pygen/codegen/templates/operation.py.jinja2 +21 -0
  86. package/generator/pygen/codegen/templates/operation_group.py.jinja2 +75 -0
  87. package/generator/pygen/codegen/templates/operation_groups_container.py.jinja2 +20 -0
  88. package/generator/pygen/codegen/templates/operation_tools.jinja2 +74 -0
  89. package/generator/pygen/codegen/templates/operations_folder_init.py.jinja2 +17 -0
  90. package/generator/pygen/codegen/templates/packaging_templates/CHANGELOG.md.jinja2 +6 -0
  91. package/generator/pygen/codegen/templates/packaging_templates/LICENSE.jinja2 +21 -0
  92. package/generator/pygen/codegen/templates/packaging_templates/MANIFEST.in.jinja2 +8 -0
  93. package/generator/pygen/codegen/templates/packaging_templates/README.md.jinja2 +107 -0
  94. package/generator/pygen/codegen/templates/packaging_templates/dev_requirements.txt.jinja2 +9 -0
  95. package/generator/pygen/codegen/templates/packaging_templates/setup.py.jinja2 +108 -0
  96. package/generator/pygen/codegen/templates/paging_operation.py.jinja2 +21 -0
  97. package/generator/pygen/codegen/templates/patch.py.jinja2 +19 -0
  98. package/generator/pygen/codegen/templates/pkgutil_init.py.jinja2 +1 -0
  99. package/generator/pygen/codegen/templates/request_builder.py.jinja2 +28 -0
  100. package/generator/pygen/codegen/templates/request_builders.py.jinja2 +10 -0
  101. package/generator/pygen/codegen/templates/rest_init.py.jinja2 +12 -0
  102. package/generator/pygen/codegen/templates/sample.py.jinja2 +44 -0
  103. package/generator/pygen/codegen/templates/serialization.py.jinja2 +2006 -0
  104. package/generator/pygen/codegen/templates/test.py.jinja2 +50 -0
  105. package/generator/pygen/codegen/templates/testpreparer.py.jinja2 +26 -0
  106. package/generator/pygen/codegen/templates/types.py.jinja2 +8 -0
  107. package/generator/pygen/codegen/templates/validation.py.jinja2 +38 -0
  108. package/generator/pygen/codegen/templates/vendor.py.jinja2 +98 -0
  109. package/generator/pygen/codegen/templates/version.py.jinja2 +4 -0
  110. package/generator/pygen/m2r.py +65 -0
  111. package/generator/pygen/postprocess/__init__.py +183 -0
  112. package/generator/pygen/postprocess/get_all.py +19 -0
  113. package/generator/pygen/postprocess/venvtools.py +77 -0
  114. package/generator/pygen/preprocess/__init__.py +503 -0
  115. package/generator/pygen/preprocess/helpers.py +27 -0
  116. package/generator/pygen/preprocess/python_mappings.py +222 -0
  117. package/generator/pygen/utils.py +149 -0
  118. package/generator/pygen.egg-info/PKG-INFO +25 -0
  119. package/generator/pygen.egg-info/SOURCES.txt +66 -0
  120. package/generator/pygen.egg-info/dependency_links.txt +1 -0
  121. package/generator/pygen.egg-info/requires.txt +4 -0
  122. package/generator/pygen.egg-info/top_level.txt +1 -0
  123. package/generator/requirements.txt +12 -0
  124. package/generator/setup.py +55 -0
  125. package/package.json +10 -7
  126. package/scripts/__pycache__/venvtools.cpython-38.pyc +0 -0
  127. package/scripts/install.py +49 -0
  128. package/scripts/prepare.py +38 -0
  129. package/scripts/run-python3.cjs +22 -0
  130. package/scripts/run_tsp.py +40 -0
  131. package/scripts/system-requirements.cjs +180 -0
  132. package/scripts/venvtools.py +81 -0
@@ -0,0 +1,2006 @@
1
+ # --------------------------------------------------------------------------
2
+ #
3
+ # Copyright (c) {{ code_model.options["company_name"] }} Corporation. All rights reserved.
4
+ #
5
+ # The MIT License (MIT)
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the ""Software""), to
9
+ # deal in the Software without restriction, including without limitation the
10
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
+ # sell copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
+ # IN THE SOFTWARE.
24
+ #
25
+ # --------------------------------------------------------------------------
26
+
27
+ # pylint: skip-file
28
+ # pyright: reportUnnecessaryTypeIgnoreComment=false
29
+
30
+ from base64 import b64decode, b64encode
31
+ import calendar
32
+ import datetime
33
+ import decimal
34
+ import email
35
+ from enum import Enum
36
+ import json
37
+ import logging
38
+ import re
39
+ import sys
40
+ import codecs
41
+ from typing import (
42
+ Dict,
43
+ Any,
44
+ cast,
45
+ Optional,
46
+ Union,
47
+ AnyStr,
48
+ IO,
49
+ Mapping,
50
+ Callable,
51
+ TypeVar,
52
+ MutableMapping,
53
+ Type,
54
+ List,
55
+ Mapping,
56
+ )
57
+
58
+ try:
59
+ from urllib import quote # type: ignore
60
+ except ImportError:
61
+ from urllib.parse import quote
62
+ import xml.etree.ElementTree as ET
63
+
64
+ import isodate # type: ignore
65
+
66
+ from {{ code_model.core_library }}.exceptions import DeserializationError, SerializationError
67
+ from {{ code_model.core_library }}.serialization import NULL as CoreNull
68
+
69
+ _BOM = codecs.BOM_UTF8.decode(encoding="utf-8")
70
+
71
+ ModelType = TypeVar("ModelType", bound="Model")
72
+ JSON = MutableMapping[str, Any]
73
+
74
+
75
+ class RawDeserializer:
76
+
77
+ # Accept "text" because we're open minded people...
78
+ JSON_REGEXP = re.compile(r"^(application|text)/([a-z+.]+\+)?json$")
79
+
80
+ # Name used in context
81
+ CONTEXT_NAME = "deserialized_data"
82
+
83
+ @classmethod
84
+ def deserialize_from_text(cls, data: Optional[Union[AnyStr, IO]], content_type: Optional[str] = None) -> Any:
85
+ """Decode data according to content-type.
86
+
87
+ Accept a stream of data as well, but will be load at once in memory for now.
88
+
89
+ If no content-type, will return the string version (not bytes, not stream)
90
+
91
+ :param data: Input, could be bytes or stream (will be decoded with UTF8) or text
92
+ :type data: str or bytes or IO
93
+ :param str content_type: The content type.
94
+ """
95
+ if hasattr(data, "read"):
96
+ # Assume a stream
97
+ data = cast(IO, data).read()
98
+
99
+ if isinstance(data, bytes):
100
+ data_as_str = data.decode(encoding="utf-8-sig")
101
+ else:
102
+ # Explain to mypy the correct type.
103
+ data_as_str = cast(str, data)
104
+
105
+ # Remove Byte Order Mark if present in string
106
+ data_as_str = data_as_str.lstrip(_BOM)
107
+
108
+ if content_type is None:
109
+ return data
110
+
111
+ if cls.JSON_REGEXP.match(content_type):
112
+ try:
113
+ return json.loads(data_as_str)
114
+ except ValueError as err:
115
+ raise DeserializationError("JSON is invalid: {}".format(err), err)
116
+ elif "xml" in (content_type or []):
117
+ try:
118
+
119
+ try:
120
+ if isinstance(data, unicode): # type: ignore
121
+ # If I'm Python 2.7 and unicode XML will scream if I try a "fromstring" on unicode string
122
+ data_as_str = data_as_str.encode(encoding="utf-8") # type: ignore
123
+ except NameError:
124
+ pass
125
+
126
+ return ET.fromstring(data_as_str) # nosec
127
+ except ET.ParseError as err:
128
+ # It might be because the server has an issue, and returned JSON with
129
+ # content-type XML....
130
+ # So let's try a JSON load, and if it's still broken
131
+ # let's flow the initial exception
132
+ def _json_attemp(data):
133
+ try:
134
+ return True, json.loads(data)
135
+ except ValueError:
136
+ return False, None # Don't care about this one
137
+
138
+ success, json_result = _json_attemp(data)
139
+ if success:
140
+ return json_result
141
+ # If i'm here, it's not JSON, it's not XML, let's scream
142
+ # and raise the last context in this block (the XML exception)
143
+ # The function hack is because Py2.7 messes up with exception
144
+ # context otherwise.
145
+ _LOGGER.critical("Wasn't XML not JSON, failing")
146
+ raise DeserializationError("XML is invalid") from err
147
+ elif content_type.startswith("text/"):
148
+ return data_as_str
149
+ raise DeserializationError("Cannot deserialize content-type: {}".format(content_type))
150
+
151
+ @classmethod
152
+ def deserialize_from_http_generics(cls, body_bytes: Optional[Union[AnyStr, IO]], headers: Mapping) -> Any:
153
+ """Deserialize from HTTP response.
154
+
155
+ Use bytes and headers to NOT use any requests/aiohttp or whatever
156
+ specific implementation.
157
+ Headers will tested for "content-type"
158
+ """
159
+ # Try to use content-type from headers if available
160
+ content_type = None
161
+ if "content-type" in headers:
162
+ content_type = headers["content-type"].split(";")[0].strip().lower()
163
+ # Ouch, this server did not declare what it sent...
164
+ # Let's guess it's JSON...
165
+ # Also, since Autorest was considering that an empty body was a valid JSON,
166
+ # need that test as well....
167
+ else:
168
+ content_type = "application/json"
169
+
170
+ if body_bytes:
171
+ return cls.deserialize_from_text(body_bytes, content_type)
172
+ return None
173
+
174
+
175
+ _LOGGER = logging.getLogger(__name__)
176
+
177
+ try:
178
+ _long_type = long # type: ignore
179
+ except NameError:
180
+ _long_type = int
181
+
182
+
183
+ class UTC(datetime.tzinfo):
184
+ """Time Zone info for handling UTC"""
185
+
186
+ def utcoffset(self, dt):
187
+ """UTF offset for UTC is 0."""
188
+ return datetime.timedelta(0)
189
+
190
+ def tzname(self, dt):
191
+ """Timestamp representation."""
192
+ return "Z"
193
+
194
+ def dst(self, dt):
195
+ """No daylight saving for UTC."""
196
+ return datetime.timedelta(hours=1)
197
+
198
+
199
+ try:
200
+ from datetime import timezone as _FixedOffset # type: ignore
201
+ except ImportError: # Python 2.7
202
+
203
+ class _FixedOffset(datetime.tzinfo): # type: ignore
204
+ """Fixed offset in minutes east from UTC.
205
+ Copy/pasted from Python doc
206
+ :param datetime.timedelta offset: offset in timedelta format
207
+ """
208
+
209
+ def __init__(self, offset):
210
+ self.__offset = offset
211
+
212
+ def utcoffset(self, dt):
213
+ return self.__offset
214
+
215
+ def tzname(self, dt):
216
+ return str(self.__offset.total_seconds() / 3600)
217
+
218
+ def __repr__(self):
219
+ return "<FixedOffset {}>".format(self.tzname(None))
220
+
221
+ def dst(self, dt):
222
+ return datetime.timedelta(0)
223
+
224
+ def __getinitargs__(self):
225
+ return (self.__offset,)
226
+
227
+
228
+ try:
229
+ from datetime import timezone
230
+
231
+ TZ_UTC = timezone.utc
232
+ except ImportError:
233
+ TZ_UTC = UTC() # type: ignore
234
+
235
+ _FLATTEN = re.compile(r"(?<!\\)\.")
236
+
237
+
238
+ def attribute_transformer(key, attr_desc, value):
239
+ """A key transformer that returns the Python attribute.
240
+
241
+ :param str key: The attribute name
242
+ :param dict attr_desc: The attribute metadata
243
+ :param object value: The value
244
+ :returns: A key using attribute name
245
+ """
246
+ return (key, value)
247
+
248
+
249
+ def full_restapi_key_transformer(key, attr_desc, value):
250
+ """A key transformer that returns the full RestAPI key path.
251
+
252
+ :param str _: The attribute name
253
+ :param dict attr_desc: The attribute metadata
254
+ :param object value: The value
255
+ :returns: A list of keys using RestAPI syntax.
256
+ """
257
+ keys = _FLATTEN.split(attr_desc["key"])
258
+ return ([_decode_attribute_map_key(k) for k in keys], value)
259
+
260
+
261
+ def last_restapi_key_transformer(key, attr_desc, value):
262
+ """A key transformer that returns the last RestAPI key.
263
+
264
+ :param str key: The attribute name
265
+ :param dict attr_desc: The attribute metadata
266
+ :param object value: The value
267
+ :returns: The last RestAPI key.
268
+ """
269
+ key, value = full_restapi_key_transformer(key, attr_desc, value)
270
+ return (key[-1], value)
271
+
272
+
273
+ def _create_xml_node(tag, prefix=None, ns=None):
274
+ """Create a XML node."""
275
+ if prefix and ns:
276
+ ET.register_namespace(prefix, ns)
277
+ if ns:
278
+ return ET.Element("{" + ns + "}" + tag)
279
+ else:
280
+ return ET.Element(tag)
281
+
282
+
283
+ class Model(object):
284
+ """Mixin for all client request body/response body models to support
285
+ serialization and deserialization.
286
+ """
287
+
288
+ _subtype_map: Dict[str, Dict[str, Any]] = {}
289
+ _attribute_map: Dict[str, Dict[str, Any]] = {}
290
+ _validation: Dict[str, Dict[str, Any]] = {}
291
+
292
+ def __init__(self, **kwargs: Any) -> None:
293
+ self.additional_properties: Optional[Dict[str, Any]] = {}
294
+ for k in kwargs:
295
+ if k not in self._attribute_map:
296
+ _LOGGER.warning("%s is not a known attribute of class %s and will be ignored", k, self.__class__)
297
+ elif k in self._validation and self._validation[k].get("readonly", False):
298
+ _LOGGER.warning("Readonly attribute %s will be ignored in class %s", k, self.__class__)
299
+ else:
300
+ setattr(self, k, kwargs[k])
301
+
302
+ def __eq__(self, other: Any) -> bool:
303
+ """Compare objects by comparing all attributes."""
304
+ if isinstance(other, self.__class__):
305
+ return self.__dict__ == other.__dict__
306
+ return False
307
+
308
+ def __ne__(self, other: Any) -> bool:
309
+ """Compare objects by comparing all attributes."""
310
+ return not self.__eq__(other)
311
+
312
+ def __str__(self) -> str:
313
+ return str(self.__dict__)
314
+
315
+ @classmethod
316
+ def enable_additional_properties_sending(cls) -> None:
317
+ cls._attribute_map["additional_properties"] = {"key": "", "type": "{object}"}
318
+
319
+ @classmethod
320
+ def is_xml_model(cls) -> bool:
321
+ try:
322
+ cls._xml_map # type: ignore
323
+ except AttributeError:
324
+ return False
325
+ return True
326
+
327
+ @classmethod
328
+ def _create_xml_node(cls):
329
+ """Create XML node."""
330
+ try:
331
+ xml_map = cls._xml_map # type: ignore
332
+ except AttributeError:
333
+ xml_map = {}
334
+
335
+ return _create_xml_node(xml_map.get("name", cls.__name__), xml_map.get("prefix", None), xml_map.get("ns", None))
336
+
337
+ def serialize(self, keep_readonly: bool = False, **kwargs: Any) -> JSON:
338
+ """Return the JSON that would be sent to server from this model.
339
+
340
+ This is an alias to `as_dict(full_restapi_key_transformer, keep_readonly=False)`.
341
+
342
+ If you want XML serialization, you can pass the kwargs is_xml=True.
343
+
344
+ :param bool keep_readonly: If you want to serialize the readonly attributes
345
+ :returns: A dict JSON compatible object
346
+ :rtype: dict
347
+ """
348
+ serializer = Serializer(self._infer_class_models())
349
+ return serializer._serialize(self, keep_readonly=keep_readonly, **kwargs) # type: ignore
350
+
351
+ def as_dict(
352
+ self,
353
+ keep_readonly: bool = True,
354
+ key_transformer: Callable[
355
+ [str, Dict[str, Any], Any], Any
356
+ ] = attribute_transformer,
357
+ **kwargs: Any
358
+ ) -> JSON:
359
+ """Return a dict that can be serialized using json.dump.
360
+
361
+ Advanced usage might optionally use a callback as parameter:
362
+
363
+ .. code::python
364
+
365
+ def my_key_transformer(key, attr_desc, value):
366
+ return key
367
+
368
+ Key is the attribute name used in Python. Attr_desc
369
+ is a dict of metadata. Currently contains 'type' with the
370
+ msrest type and 'key' with the RestAPI encoded key.
371
+ Value is the current value in this object.
372
+
373
+ The string returned will be used to serialize the key.
374
+ If the return type is a list, this is considered hierarchical
375
+ result dict.
376
+
377
+ See the three examples in this file:
378
+
379
+ - attribute_transformer
380
+ - full_restapi_key_transformer
381
+ - last_restapi_key_transformer
382
+
383
+ If you want XML serialization, you can pass the kwargs is_xml=True.
384
+
385
+ :param function key_transformer: A key transformer function.
386
+ :returns: A dict JSON compatible object
387
+ :rtype: dict
388
+ """
389
+ serializer = Serializer(self._infer_class_models())
390
+ return serializer._serialize(self, key_transformer=key_transformer, keep_readonly=keep_readonly, **kwargs) # type: ignore
391
+
392
+ @classmethod
393
+ def _infer_class_models(cls):
394
+ try:
395
+ str_models = cls.__module__.rsplit(".", 1)[0]
396
+ models = sys.modules[str_models]
397
+ client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)}
398
+ if cls.__name__ not in client_models:
399
+ raise ValueError("Not Autorest generated code")
400
+ except Exception:
401
+ # Assume it's not Autorest generated (tests?). Add ourselves as dependencies.
402
+ client_models = {cls.__name__: cls}
403
+ return client_models
404
+
405
+ @classmethod
406
+ def deserialize(cls: Type[ModelType], data: Any, content_type: Optional[str] = None) -> ModelType:
407
+ """Parse a str using the RestAPI syntax and return a model.
408
+
409
+ :param str data: A str using RestAPI structure. JSON by default.
410
+ :param str content_type: JSON by default, set application/xml if XML.
411
+ :returns: An instance of this model
412
+ :raises: DeserializationError if something went wrong
413
+ """
414
+ deserializer = Deserializer(cls._infer_class_models())
415
+ return deserializer(cls.__name__, data, content_type=content_type) # type: ignore
416
+
417
+ @classmethod
418
+ def from_dict(
419
+ cls: Type[ModelType],
420
+ data: Any,
421
+ key_extractors: Optional[Callable[[str, Dict[str, Any], Any], Any]] = None,
422
+ content_type: Optional[str] = None,
423
+ ) -> ModelType:
424
+ """Parse a dict using given key extractor return a model.
425
+
426
+ By default consider key
427
+ extractors (rest_key_case_insensitive_extractor, attribute_key_case_insensitive_extractor
428
+ and last_rest_key_case_insensitive_extractor)
429
+
430
+ :param dict data: A dict using RestAPI structure
431
+ :param str content_type: JSON by default, set application/xml if XML.
432
+ :returns: An instance of this model
433
+ :raises: DeserializationError if something went wrong
434
+ """
435
+ deserializer = Deserializer(cls._infer_class_models())
436
+ deserializer.key_extractors = ( # type: ignore
437
+ [ # type: ignore
438
+ attribute_key_case_insensitive_extractor,
439
+ rest_key_case_insensitive_extractor,
440
+ last_rest_key_case_insensitive_extractor,
441
+ ]
442
+ if key_extractors is None
443
+ else key_extractors
444
+ )
445
+ return deserializer(cls.__name__, data, content_type=content_type) # type: ignore
446
+
447
+ @classmethod
448
+ def _flatten_subtype(cls, key, objects):
449
+ if "_subtype_map" not in cls.__dict__:
450
+ return {}
451
+ result = dict(cls._subtype_map[key])
452
+ for valuetype in cls._subtype_map[key].values():
453
+ result.update(objects[valuetype]._flatten_subtype(key, objects))
454
+ return result
455
+
456
+ @classmethod
457
+ def _classify(cls, response, objects):
458
+ """Check the class _subtype_map for any child classes.
459
+ We want to ignore any inherited _subtype_maps.
460
+ Remove the polymorphic key from the initial data.
461
+ """
462
+ for subtype_key in cls.__dict__.get("_subtype_map", {}).keys():
463
+ subtype_value = None
464
+
465
+ if not isinstance(response, ET.Element):
466
+ rest_api_response_key = cls._get_rest_key_parts(subtype_key)[-1]
467
+ subtype_value = response.pop(rest_api_response_key, None) or response.pop(subtype_key, None)
468
+ else:
469
+ subtype_value = xml_key_extractor(subtype_key, cls._attribute_map[subtype_key], response)
470
+ if subtype_value:
471
+ # Try to match base class. Can be class name only
472
+ # (bug to fix in Autorest to support x-ms-discriminator-name)
473
+ if cls.__name__ == subtype_value:
474
+ return cls
475
+ flatten_mapping_type = cls._flatten_subtype(subtype_key, objects)
476
+ try:
477
+ return objects[flatten_mapping_type[subtype_value]] # type: ignore
478
+ except KeyError:
479
+ _LOGGER.warning(
480
+ "Subtype value %s has no mapping, use base class %s.",
481
+ subtype_value,
482
+ cls.__name__,
483
+ )
484
+ break
485
+ else:
486
+ _LOGGER.warning("Discriminator %s is absent or null, use base class %s.", subtype_key, cls.__name__)
487
+ break
488
+ return cls
489
+
490
+ @classmethod
491
+ def _get_rest_key_parts(cls, attr_key):
492
+ """Get the RestAPI key of this attr, split it and decode part
493
+ :param str attr_key: Attribute key must be in attribute_map.
494
+ :returns: A list of RestAPI part
495
+ :rtype: list
496
+ """
497
+ rest_split_key = _FLATTEN.split(cls._attribute_map[attr_key]["key"])
498
+ return [_decode_attribute_map_key(key_part) for key_part in rest_split_key]
499
+
500
+
501
+ def _decode_attribute_map_key(key):
502
+ """This decode a key in an _attribute_map to the actual key we want to look at
503
+ inside the received data.
504
+
505
+ :param str key: A key string from the generated code
506
+ """
507
+ return key.replace("\\.", ".")
508
+
509
+
510
+ class Serializer(object):
511
+ """Request object model serializer."""
512
+
513
+ basic_types = {str: "str", int: "int", bool: "bool", float: "float"}
514
+
515
+ _xml_basic_types_serializers = {"bool": lambda x: str(x).lower()}
516
+ days = {0: "Mon", 1: "Tue", 2: "Wed", 3: "Thu", 4: "Fri", 5: "Sat", 6: "Sun"}
517
+ months = {
518
+ 1: "Jan",
519
+ 2: "Feb",
520
+ 3: "Mar",
521
+ 4: "Apr",
522
+ 5: "May",
523
+ 6: "Jun",
524
+ 7: "Jul",
525
+ 8: "Aug",
526
+ 9: "Sep",
527
+ 10: "Oct",
528
+ 11: "Nov",
529
+ 12: "Dec",
530
+ }
531
+ validation = {
532
+ "min_length": lambda x, y: len(x) < y,
533
+ "max_length": lambda x, y: len(x) > y,
534
+ "minimum": lambda x, y: x < y,
535
+ "maximum": lambda x, y: x > y,
536
+ "minimum_ex": lambda x, y: x <= y,
537
+ "maximum_ex": lambda x, y: x >= y,
538
+ "min_items": lambda x, y: len(x) < y,
539
+ "max_items": lambda x, y: len(x) > y,
540
+ "pattern": lambda x, y: not re.match(y, x, re.UNICODE),
541
+ "unique": lambda x, y: len(x) != len(set(x)),
542
+ "multiple": lambda x, y: x % y != 0,
543
+ }
544
+
545
+ def __init__(self, classes: Optional[Mapping[str, type]]=None):
546
+ self.serialize_type = {
547
+ "iso-8601": Serializer.serialize_iso,
548
+ "rfc-1123": Serializer.serialize_rfc,
549
+ "unix-time": Serializer.serialize_unix,
550
+ "duration": Serializer.serialize_duration,
551
+ "date": Serializer.serialize_date,
552
+ "time": Serializer.serialize_time,
553
+ "decimal": Serializer.serialize_decimal,
554
+ "long": Serializer.serialize_long,
555
+ "bytearray": Serializer.serialize_bytearray,
556
+ "base64": Serializer.serialize_base64,
557
+ "object": self.serialize_object,
558
+ "[]": self.serialize_iter,
559
+ "{}": self.serialize_dict,
560
+ }
561
+ self.dependencies: Dict[str, type] = dict(classes) if classes else {}
562
+ self.key_transformer = full_restapi_key_transformer
563
+ self.client_side_validation = True
564
+
565
+ def _serialize(self, target_obj, data_type=None, **kwargs):
566
+ """Serialize data into a string according to type.
567
+
568
+ :param target_obj: The data to be serialized.
569
+ :param str data_type: The type to be serialized from.
570
+ :rtype: str, dict
571
+ :raises: SerializationError if serialization fails.
572
+ """
573
+ key_transformer = kwargs.get("key_transformer", self.key_transformer)
574
+ keep_readonly = kwargs.get("keep_readonly", False)
575
+ if target_obj is None:
576
+ return None
577
+
578
+ attr_name = None
579
+ class_name = target_obj.__class__.__name__
580
+
581
+ if data_type:
582
+ return self.serialize_data(target_obj, data_type, **kwargs)
583
+
584
+ if not hasattr(target_obj, "_attribute_map"):
585
+ data_type = type(target_obj).__name__
586
+ if data_type in self.basic_types.values():
587
+ return self.serialize_data(target_obj, data_type, **kwargs)
588
+
589
+ # Force "is_xml" kwargs if we detect a XML model
590
+ try:
591
+ is_xml_model_serialization = kwargs["is_xml"]
592
+ except KeyError:
593
+ is_xml_model_serialization = kwargs.setdefault("is_xml", target_obj.is_xml_model())
594
+
595
+ serialized = {}
596
+ if is_xml_model_serialization:
597
+ serialized = target_obj._create_xml_node()
598
+ try:
599
+ attributes = target_obj._attribute_map
600
+ for attr, attr_desc in attributes.items():
601
+ attr_name = attr
602
+ if not keep_readonly and target_obj._validation.get(attr_name, {}).get("readonly", False):
603
+ continue
604
+
605
+ if attr_name == "additional_properties" and attr_desc["key"] == "":
606
+ if target_obj.additional_properties is not None:
607
+ serialized.update(target_obj.additional_properties)
608
+ continue
609
+ try:
610
+
611
+ orig_attr = getattr(target_obj, attr)
612
+ if is_xml_model_serialization:
613
+ pass # Don't provide "transformer" for XML for now. Keep "orig_attr"
614
+ else: # JSON
615
+ keys, orig_attr = key_transformer(attr, attr_desc.copy(), orig_attr)
616
+ keys = keys if isinstance(keys, list) else [keys]
617
+
618
+ kwargs["serialization_ctxt"] = attr_desc
619
+ new_attr = self.serialize_data(orig_attr, attr_desc["type"], **kwargs)
620
+
621
+ if is_xml_model_serialization:
622
+ xml_desc = attr_desc.get("xml", {})
623
+ xml_name = xml_desc.get("name", attr_desc["key"])
624
+ xml_prefix = xml_desc.get("prefix", None)
625
+ xml_ns = xml_desc.get("ns", None)
626
+ if xml_desc.get("attr", False):
627
+ if xml_ns:
628
+ ET.register_namespace(xml_prefix, xml_ns)
629
+ xml_name = "{% raw %}{{{}}}{}{% endraw %}".format(xml_ns, xml_name)
630
+ serialized.set(xml_name, new_attr) # type: ignore
631
+ continue
632
+ if xml_desc.get("text", False):
633
+ serialized.text = new_attr # type: ignore
634
+ continue
635
+ if isinstance(new_attr, list):
636
+ serialized.extend(new_attr) # type: ignore
637
+ elif isinstance(new_attr, ET.Element):
638
+ # If the down XML has no XML/Name, we MUST replace the tag with the local tag. But keeping the namespaces.
639
+ if "name" not in getattr(orig_attr, "_xml_map", {}):
640
+ splitted_tag = new_attr.tag.split("}")
641
+ if len(splitted_tag) == 2: # Namespace
642
+ new_attr.tag = "}".join([splitted_tag[0], xml_name])
643
+ else:
644
+ new_attr.tag = xml_name
645
+ serialized.append(new_attr) # type: ignore
646
+ else: # That's a basic type
647
+ # Integrate namespace if necessary
648
+ local_node = _create_xml_node(xml_name, xml_prefix, xml_ns)
649
+ local_node.text = str(new_attr)
650
+ serialized.append(local_node) # type: ignore
651
+ else: # JSON
652
+ for k in reversed(keys): # type: ignore
653
+ new_attr = {k: new_attr}
654
+
655
+ _new_attr = new_attr
656
+ _serialized = serialized
657
+ for k in keys: # type: ignore
658
+ if k not in _serialized:
659
+ _serialized.update(_new_attr) # type: ignore
660
+ _new_attr = _new_attr[k] # type: ignore
661
+ _serialized = _serialized[k]
662
+ except ValueError as err:
663
+ if isinstance(err, SerializationError):
664
+ raise
665
+
666
+ except (AttributeError, KeyError, TypeError) as err:
667
+ msg = "Attribute {} in object {} cannot be serialized.\n{}".format(attr_name, class_name, str(target_obj))
668
+ raise SerializationError(msg) from err
669
+ else:
670
+ return serialized
671
+
672
+ def body(self, data, data_type, **kwargs):
673
+ """Serialize data intended for a request body.
674
+
675
+ :param data: The data to be serialized.
676
+ :param str data_type: The type to be serialized from.
677
+ :rtype: dict
678
+ :raises: SerializationError if serialization fails.
679
+ :raises: ValueError if data is None
680
+ """
681
+
682
+ # Just in case this is a dict
683
+ internal_data_type_str = data_type.strip("[]{}")
684
+ internal_data_type = self.dependencies.get(internal_data_type_str, None)
685
+ try:
686
+ is_xml_model_serialization = kwargs["is_xml"]
687
+ except KeyError:
688
+ if internal_data_type and issubclass(internal_data_type, Model):
689
+ is_xml_model_serialization = kwargs.setdefault("is_xml", internal_data_type.is_xml_model())
690
+ else:
691
+ is_xml_model_serialization = False
692
+ if internal_data_type and not isinstance(internal_data_type, Enum):
693
+ try:
694
+ deserializer = Deserializer(self.dependencies)
695
+ # Since it's on serialization, it's almost sure that format is not JSON REST
696
+ # We're not able to deal with additional properties for now.
697
+ deserializer.additional_properties_detection = False
698
+ if is_xml_model_serialization:
699
+ deserializer.key_extractors = [ # type: ignore
700
+ attribute_key_case_insensitive_extractor,
701
+ ]
702
+ else:
703
+ deserializer.key_extractors = [
704
+ rest_key_case_insensitive_extractor,
705
+ attribute_key_case_insensitive_extractor,
706
+ last_rest_key_case_insensitive_extractor,
707
+ ]
708
+ data = deserializer._deserialize(data_type, data)
709
+ except DeserializationError as err:
710
+ raise SerializationError("Unable to build a model: " + str(err)) from err
711
+
712
+ return self._serialize(data, data_type, **kwargs)
713
+
714
+ def url(self, name, data, data_type, **kwargs):
715
+ """Serialize data intended for a URL path.
716
+
717
+ :param data: The data to be serialized.
718
+ :param str data_type: The type to be serialized from.
719
+ :rtype: str
720
+ :raises: TypeError if serialization fails.
721
+ :raises: ValueError if data is None
722
+ """
723
+ try:
724
+ output = self.serialize_data(data, data_type, **kwargs)
725
+ if data_type == "bool":
726
+ output = json.dumps(output)
727
+
728
+ if kwargs.get("skip_quote") is True:
729
+ output = str(output)
730
+ output = output.replace("{", quote("{")).replace("}", quote("}"))
731
+ else:
732
+ output = quote(str(output), safe="")
733
+ except SerializationError:
734
+ raise TypeError("{} must be type {}.".format(name, data_type))
735
+ else:
736
+ return output
737
+
738
+ def query(self, name, data, data_type, **kwargs):
739
+ """Serialize data intended for a URL query.
740
+
741
+ :param data: The data to be serialized.
742
+ :param str data_type: The type to be serialized from.
743
+ :keyword bool skip_quote: Whether to skip quote the serialized result.
744
+ Defaults to False.
745
+ :rtype: str, list
746
+ :raises: TypeError if serialization fails.
747
+ :raises: ValueError if data is None
748
+ """
749
+ try:
750
+ # Treat the list aside, since we don't want to encode the div separator
751
+ if data_type.startswith("["):
752
+ internal_data_type = data_type[1:-1]
753
+ do_quote = not kwargs.get('skip_quote', False)
754
+ return self.serialize_iter(data, internal_data_type, do_quote=do_quote, **kwargs)
755
+
756
+ # Not a list, regular serialization
757
+ output = self.serialize_data(data, data_type, **kwargs)
758
+ if data_type == "bool":
759
+ output = json.dumps(output)
760
+ if kwargs.get("skip_quote") is True:
761
+ output = str(output)
762
+ else:
763
+ output = quote(str(output), safe="")
764
+ except SerializationError:
765
+ raise TypeError("{} must be type {}.".format(name, data_type))
766
+ else:
767
+ return str(output)
768
+
769
+ def header(self, name, data, data_type, **kwargs):
770
+ """Serialize data intended for a request header.
771
+
772
+ :param data: The data to be serialized.
773
+ :param str data_type: The type to be serialized from.
774
+ :rtype: str
775
+ :raises: TypeError if serialization fails.
776
+ :raises: ValueError if data is None
777
+ """
778
+ try:
779
+ if data_type in ["[str]"]:
780
+ data = ["" if d is None else d for d in data]
781
+
782
+ output = self.serialize_data(data, data_type, **kwargs)
783
+ if data_type == "bool":
784
+ output = json.dumps(output)
785
+ except SerializationError:
786
+ raise TypeError("{} must be type {}.".format(name, data_type))
787
+ else:
788
+ return str(output)
789
+
790
+ def serialize_data(self, data, data_type, **kwargs):
791
+ """Serialize generic data according to supplied data type.
792
+
793
+ :param data: The data to be serialized.
794
+ :param str data_type: The type to be serialized from.
795
+ :param bool required: Whether it's essential that the data not be
796
+ empty or None
797
+ :raises: AttributeError if required data is None.
798
+ :raises: ValueError if data is None
799
+ :raises: SerializationError if serialization fails.
800
+ """
801
+ if data is None:
802
+ raise ValueError("No value for given attribute")
803
+
804
+ try:
805
+ if data is CoreNull:
806
+ return None
807
+ if data_type in self.basic_types.values():
808
+ return self.serialize_basic(data, data_type, **kwargs)
809
+
810
+ elif data_type in self.serialize_type:
811
+ return self.serialize_type[data_type](data, **kwargs)
812
+
813
+ # If dependencies is empty, try with current data class
814
+ # It has to be a subclass of Enum anyway
815
+ enum_type = self.dependencies.get(data_type, data.__class__)
816
+ if issubclass(enum_type, Enum):
817
+ return Serializer.serialize_enum(data, enum_obj=enum_type)
818
+
819
+ iter_type = data_type[0] + data_type[-1]
820
+ if iter_type in self.serialize_type:
821
+ return self.serialize_type[iter_type](data, data_type[1:-1], **kwargs)
822
+
823
+ except (ValueError, TypeError) as err:
824
+ msg = "Unable to serialize value: {!r} as type: {!r}."
825
+ raise SerializationError(msg.format(data, data_type)) from err
826
+ else:
827
+ return self._serialize(data, **kwargs)
828
+
829
+ @classmethod
830
+ def _get_custom_serializers(cls, data_type, **kwargs):
831
+ custom_serializer = kwargs.get("basic_types_serializers", {}).get(data_type)
832
+ if custom_serializer:
833
+ return custom_serializer
834
+ if kwargs.get("is_xml", False):
835
+ return cls._xml_basic_types_serializers.get(data_type)
836
+
837
+ @classmethod
838
+ def serialize_basic(cls, data, data_type, **kwargs):
839
+ """Serialize basic builting data type.
840
+ Serializes objects to str, int, float or bool.
841
+
842
+ Possible kwargs:
843
+ - basic_types_serializers dict[str, callable] : If set, use the callable as serializer
844
+ - is_xml bool : If set, use xml_basic_types_serializers
845
+
846
+ :param data: Object to be serialized.
847
+ :param str data_type: Type of object in the iterable.
848
+ """
849
+ custom_serializer = cls._get_custom_serializers(data_type, **kwargs)
850
+ if custom_serializer:
851
+ return custom_serializer(data)
852
+ if data_type == "str":
853
+ return cls.serialize_unicode(data)
854
+ return eval(data_type)(data) # nosec
855
+
856
+ @classmethod
857
+ def serialize_unicode(cls, data):
858
+ """Special handling for serializing unicode strings in Py2.
859
+ Encode to UTF-8 if unicode, otherwise handle as a str.
860
+
861
+ :param data: Object to be serialized.
862
+ :rtype: str
863
+ """
864
+ try: # If I received an enum, return its value
865
+ return data.value
866
+ except AttributeError:
867
+ pass
868
+
869
+ try:
870
+ if isinstance(data, unicode): # type: ignore
871
+ # Don't change it, JSON and XML ElementTree are totally able
872
+ # to serialize correctly u'' strings
873
+ return data
874
+ except NameError:
875
+ return str(data)
876
+ else:
877
+ return str(data)
878
+
879
+ def serialize_iter(self, data, iter_type, div=None, **kwargs):
880
+ """Serialize iterable.
881
+
882
+ Supported kwargs:
883
+ - serialization_ctxt dict : The current entry of _attribute_map, or same format.
884
+ serialization_ctxt['type'] should be same as data_type.
885
+ - is_xml bool : If set, serialize as XML
886
+
887
+ :param list attr: Object to be serialized.
888
+ :param str iter_type: Type of object in the iterable.
889
+ :param bool required: Whether the objects in the iterable must
890
+ not be None or empty.
891
+ :param str div: If set, this str will be used to combine the elements
892
+ in the iterable into a combined string. Default is 'None'.
893
+ :keyword bool do_quote: Whether to quote the serialized result of each iterable element.
894
+ Defaults to False.
895
+ :rtype: list, str
896
+ """
897
+ if isinstance(data, str):
898
+ raise SerializationError("Refuse str type as a valid iter type.")
899
+
900
+ serialization_ctxt = kwargs.get("serialization_ctxt", {})
901
+ is_xml = kwargs.get("is_xml", False)
902
+
903
+ serialized = []
904
+ for d in data:
905
+ try:
906
+ serialized.append(self.serialize_data(d, iter_type, **kwargs))
907
+ except ValueError as err:
908
+ if isinstance(err, SerializationError):
909
+ raise
910
+ serialized.append(None)
911
+
912
+ if kwargs.get('do_quote', False):
913
+ serialized = [
914
+ '' if s is None else quote(str(s), safe='')
915
+ for s
916
+ in serialized
917
+ ]
918
+
919
+ if div:
920
+ serialized = ["" if s is None else str(s) for s in serialized]
921
+ serialized = div.join(serialized)
922
+
923
+ if "xml" in serialization_ctxt or is_xml:
924
+ # XML serialization is more complicated
925
+ xml_desc = serialization_ctxt.get("xml", {})
926
+ xml_name = xml_desc.get("name")
927
+ if not xml_name:
928
+ xml_name = serialization_ctxt["key"]
929
+
930
+ # Create a wrap node if necessary (use the fact that Element and list have "append")
931
+ is_wrapped = xml_desc.get("wrapped", False)
932
+ node_name = xml_desc.get("itemsName", xml_name)
933
+ if is_wrapped:
934
+ final_result = _create_xml_node(xml_name, xml_desc.get("prefix", None), xml_desc.get("ns", None))
935
+ else:
936
+ final_result = []
937
+ # All list elements to "local_node"
938
+ for el in serialized:
939
+ if isinstance(el, ET.Element):
940
+ el_node = el
941
+ else:
942
+ el_node = _create_xml_node(node_name, xml_desc.get("prefix", None), xml_desc.get("ns", None))
943
+ if el is not None: # Otherwise it writes "None" :-p
944
+ el_node.text = str(el)
945
+ final_result.append(el_node)
946
+ return final_result
947
+ return serialized
948
+
949
+ def serialize_dict(self, attr, dict_type, **kwargs):
950
+ """Serialize a dictionary of objects.
951
+
952
+ :param dict attr: Object to be serialized.
953
+ :param str dict_type: Type of object in the dictionary.
954
+ :param bool required: Whether the objects in the dictionary must
955
+ not be None or empty.
956
+ :rtype: dict
957
+ """
958
+ serialization_ctxt = kwargs.get("serialization_ctxt", {})
959
+ serialized = {}
960
+ for key, value in attr.items():
961
+ try:
962
+ serialized[self.serialize_unicode(key)] = self.serialize_data(value, dict_type, **kwargs)
963
+ except ValueError as err:
964
+ if isinstance(err, SerializationError):
965
+ raise
966
+ serialized[self.serialize_unicode(key)] = None
967
+
968
+ if "xml" in serialization_ctxt:
969
+ # XML serialization is more complicated
970
+ xml_desc = serialization_ctxt["xml"]
971
+ xml_name = xml_desc["name"]
972
+
973
+ final_result = _create_xml_node(xml_name, xml_desc.get("prefix", None), xml_desc.get("ns", None))
974
+ for key, value in serialized.items():
975
+ ET.SubElement(final_result, key).text = value
976
+ return final_result
977
+
978
+ return serialized
979
+
980
+ def serialize_object(self, attr, **kwargs):
981
+ """Serialize a generic object.
982
+ This will be handled as a dictionary. If object passed in is not
983
+ a basic type (str, int, float, dict, list) it will simply be
984
+ cast to str.
985
+
986
+ :param dict attr: Object to be serialized.
987
+ :rtype: dict or str
988
+ """
989
+ if attr is None:
990
+ return None
991
+ if isinstance(attr, ET.Element):
992
+ return attr
993
+ obj_type = type(attr)
994
+ if obj_type in self.basic_types:
995
+ return self.serialize_basic(attr, self.basic_types[obj_type], **kwargs)
996
+ if obj_type is _long_type:
997
+ return self.serialize_long(attr)
998
+ if obj_type is str:
999
+ return self.serialize_unicode(attr)
1000
+ if obj_type is datetime.datetime:
1001
+ return self.serialize_iso(attr)
1002
+ if obj_type is datetime.date:
1003
+ return self.serialize_date(attr)
1004
+ if obj_type is datetime.time:
1005
+ return self.serialize_time(attr)
1006
+ if obj_type is datetime.timedelta:
1007
+ return self.serialize_duration(attr)
1008
+ if obj_type is decimal.Decimal:
1009
+ return self.serialize_decimal(attr)
1010
+
1011
+ # If it's a model or I know this dependency, serialize as a Model
1012
+ elif obj_type in self.dependencies.values() or isinstance(attr, Model):
1013
+ return self._serialize(attr)
1014
+
1015
+ if obj_type == dict:
1016
+ serialized = {}
1017
+ for key, value in attr.items():
1018
+ try:
1019
+ serialized[self.serialize_unicode(key)] = self.serialize_object(value, **kwargs)
1020
+ except ValueError:
1021
+ serialized[self.serialize_unicode(key)] = None
1022
+ return serialized
1023
+
1024
+ if obj_type == list:
1025
+ serialized = []
1026
+ for obj in attr:
1027
+ try:
1028
+ serialized.append(self.serialize_object(obj, **kwargs))
1029
+ except ValueError:
1030
+ pass
1031
+ return serialized
1032
+ return str(attr)
1033
+
1034
+ @staticmethod
1035
+ def serialize_enum(attr, enum_obj=None):
1036
+ try:
1037
+ result = attr.value
1038
+ except AttributeError:
1039
+ result = attr
1040
+ try:
1041
+ enum_obj(result) # type: ignore
1042
+ return result
1043
+ except ValueError:
1044
+ for enum_value in enum_obj: # type: ignore
1045
+ if enum_value.value.lower() == str(attr).lower():
1046
+ return enum_value.value
1047
+ error = "{!r} is not valid value for enum {!r}"
1048
+ raise SerializationError(error.format(attr, enum_obj))
1049
+
1050
+ @staticmethod
1051
+ def serialize_bytearray(attr, **kwargs):
1052
+ """Serialize bytearray into base-64 string.
1053
+
1054
+ :param attr: Object to be serialized.
1055
+ :rtype: str
1056
+ """
1057
+ return b64encode(attr).decode()
1058
+
1059
+ @staticmethod
1060
+ def serialize_base64(attr, **kwargs):
1061
+ """Serialize str into base-64 string.
1062
+
1063
+ :param attr: Object to be serialized.
1064
+ :rtype: str
1065
+ """
1066
+ encoded = b64encode(attr).decode("ascii")
1067
+ return encoded.strip("=").replace("+", "-").replace("/", "_")
1068
+
1069
+ @staticmethod
1070
+ def serialize_decimal(attr, **kwargs):
1071
+ """Serialize Decimal object to float.
1072
+
1073
+ :param attr: Object to be serialized.
1074
+ :rtype: float
1075
+ """
1076
+ return float(attr)
1077
+
1078
+ @staticmethod
1079
+ def serialize_long(attr, **kwargs):
1080
+ """Serialize long (Py2) or int (Py3).
1081
+
1082
+ :param attr: Object to be serialized.
1083
+ :rtype: int/long
1084
+ """
1085
+ return _long_type(attr)
1086
+
1087
+ @staticmethod
1088
+ def serialize_date(attr, **kwargs):
1089
+ """Serialize Date object into ISO-8601 formatted string.
1090
+
1091
+ :param Date attr: Object to be serialized.
1092
+ :rtype: str
1093
+ """
1094
+ if isinstance(attr, str):
1095
+ attr = isodate.parse_date(attr)
1096
+ t = "{:04}-{:02}-{:02}".format(attr.year, attr.month, attr.day)
1097
+ return t
1098
+
1099
+ @staticmethod
1100
+ def serialize_time(attr, **kwargs):
1101
+ """Serialize Time object into ISO-8601 formatted string.
1102
+
1103
+ :param datetime.time attr: Object to be serialized.
1104
+ :rtype: str
1105
+ """
1106
+ if isinstance(attr, str):
1107
+ attr = isodate.parse_time(attr)
1108
+ t = "{:02}:{:02}:{:02}".format(attr.hour, attr.minute, attr.second)
1109
+ if attr.microsecond:
1110
+ t += ".{:02}".format(attr.microsecond)
1111
+ return t
1112
+
1113
+ @staticmethod
1114
+ def serialize_duration(attr, **kwargs):
1115
+ """Serialize TimeDelta object into ISO-8601 formatted string.
1116
+
1117
+ :param TimeDelta attr: Object to be serialized.
1118
+ :rtype: str
1119
+ """
1120
+ if isinstance(attr, str):
1121
+ attr = isodate.parse_duration(attr)
1122
+ return isodate.duration_isoformat(attr)
1123
+
1124
+ @staticmethod
1125
+ def serialize_rfc(attr, **kwargs):
1126
+ """Serialize Datetime object into RFC-1123 formatted string.
1127
+
1128
+ :param Datetime attr: Object to be serialized.
1129
+ :rtype: str
1130
+ :raises: TypeError if format invalid.
1131
+ """
1132
+ try:
1133
+ if not attr.tzinfo:
1134
+ _LOGGER.warning("Datetime with no tzinfo will be considered UTC.")
1135
+ utc = attr.utctimetuple()
1136
+ except AttributeError:
1137
+ raise TypeError("RFC1123 object must be valid Datetime object.")
1138
+
1139
+ return "{}, {:02} {} {:04} {:02}:{:02}:{:02} GMT".format(
1140
+ Serializer.days[utc.tm_wday],
1141
+ utc.tm_mday,
1142
+ Serializer.months[utc.tm_mon],
1143
+ utc.tm_year,
1144
+ utc.tm_hour,
1145
+ utc.tm_min,
1146
+ utc.tm_sec,
1147
+ )
1148
+
1149
+ @staticmethod
1150
+ def serialize_iso(attr, **kwargs):
1151
+ """Serialize Datetime object into ISO-8601 formatted string.
1152
+
1153
+ :param Datetime attr: Object to be serialized.
1154
+ :rtype: str
1155
+ :raises: SerializationError if format invalid.
1156
+ """
1157
+ if isinstance(attr, str):
1158
+ attr = isodate.parse_datetime(attr)
1159
+ try:
1160
+ if not attr.tzinfo:
1161
+ _LOGGER.warning("Datetime with no tzinfo will be considered UTC.")
1162
+ utc = attr.utctimetuple()
1163
+ if utc.tm_year > 9999 or utc.tm_year < 1:
1164
+ raise OverflowError("Hit max or min date")
1165
+
1166
+ microseconds = str(attr.microsecond).rjust(6, "0").rstrip("0").ljust(3, "0")
1167
+ if microseconds:
1168
+ microseconds = "." + microseconds
1169
+ date = "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}".format(
1170
+ utc.tm_year, utc.tm_mon, utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec
1171
+ )
1172
+ return date + microseconds + "Z"
1173
+ except (ValueError, OverflowError) as err:
1174
+ msg = "Unable to serialize datetime object."
1175
+ raise SerializationError(msg) from err
1176
+ except AttributeError as err:
1177
+ msg = "ISO-8601 object must be valid Datetime object."
1178
+ raise TypeError(msg) from err
1179
+
1180
+ @staticmethod
1181
+ def serialize_unix(attr, **kwargs):
1182
+ """Serialize Datetime object into IntTime format.
1183
+ This is represented as seconds.
1184
+
1185
+ :param Datetime attr: Object to be serialized.
1186
+ :rtype: int
1187
+ :raises: SerializationError if format invalid
1188
+ """
1189
+ if isinstance(attr, int):
1190
+ return attr
1191
+ try:
1192
+ if not attr.tzinfo:
1193
+ _LOGGER.warning("Datetime with no tzinfo will be considered UTC.")
1194
+ return int(calendar.timegm(attr.utctimetuple()))
1195
+ except AttributeError:
1196
+ raise TypeError("Unix time object must be valid Datetime object.")
1197
+
1198
+
1199
+ def rest_key_extractor(attr, attr_desc, data):
1200
+ key = attr_desc["key"]
1201
+ working_data = data
1202
+
1203
+ while "." in key:
1204
+ # Need the cast, as for some reasons "split" is typed as list[str | Any]
1205
+ dict_keys = cast(List[str], _FLATTEN.split(key))
1206
+ if len(dict_keys) == 1:
1207
+ key = _decode_attribute_map_key(dict_keys[0])
1208
+ break
1209
+ working_key = _decode_attribute_map_key(dict_keys[0])
1210
+ working_data = working_data.get(working_key, data)
1211
+ if working_data is None:
1212
+ # If at any point while following flatten JSON path see None, it means
1213
+ # that all properties under are None as well
1214
+ return None
1215
+ key = ".".join(dict_keys[1:])
1216
+
1217
+ return working_data.get(key)
1218
+
1219
+
1220
+ def rest_key_case_insensitive_extractor(attr, attr_desc, data):
1221
+ key = attr_desc["key"]
1222
+ working_data = data
1223
+
1224
+ while "." in key:
1225
+ dict_keys = _FLATTEN.split(key)
1226
+ if len(dict_keys) == 1:
1227
+ key = _decode_attribute_map_key(dict_keys[0])
1228
+ break
1229
+ working_key = _decode_attribute_map_key(dict_keys[0])
1230
+ working_data = attribute_key_case_insensitive_extractor(working_key, None, working_data)
1231
+ if working_data is None:
1232
+ # If at any point while following flatten JSON path see None, it means
1233
+ # that all properties under are None as well
1234
+ return None
1235
+ key = ".".join(dict_keys[1:])
1236
+
1237
+ if working_data:
1238
+ return attribute_key_case_insensitive_extractor(key, None, working_data)
1239
+
1240
+
1241
+ def last_rest_key_extractor(attr, attr_desc, data):
1242
+ """Extract the attribute in "data" based on the last part of the JSON path key."""
1243
+ key = attr_desc["key"]
1244
+ dict_keys = _FLATTEN.split(key)
1245
+ return attribute_key_extractor(dict_keys[-1], None, data)
1246
+
1247
+
1248
+ def last_rest_key_case_insensitive_extractor(attr, attr_desc, data):
1249
+ """Extract the attribute in "data" based on the last part of the JSON path key.
1250
+
1251
+ This is the case insensitive version of "last_rest_key_extractor"
1252
+ """
1253
+ key = attr_desc["key"]
1254
+ dict_keys = _FLATTEN.split(key)
1255
+ return attribute_key_case_insensitive_extractor(dict_keys[-1], None, data)
1256
+
1257
+
1258
+ def attribute_key_extractor(attr, _, data):
1259
+ return data.get(attr)
1260
+
1261
+
1262
+ def attribute_key_case_insensitive_extractor(attr, _, data):
1263
+ found_key = None
1264
+ lower_attr = attr.lower()
1265
+ for key in data:
1266
+ if lower_attr == key.lower():
1267
+ found_key = key
1268
+ break
1269
+
1270
+ return data.get(found_key)
1271
+
1272
+
1273
+ def _extract_name_from_internal_type(internal_type):
1274
+ """Given an internal type XML description, extract correct XML name with namespace.
1275
+
1276
+ :param dict internal_type: An model type
1277
+ :rtype: tuple
1278
+ :returns: A tuple XML name + namespace dict
1279
+ """
1280
+ internal_type_xml_map = getattr(internal_type, "_xml_map", {})
1281
+ xml_name = internal_type_xml_map.get("name", internal_type.__name__)
1282
+ xml_ns = internal_type_xml_map.get("ns", None)
1283
+ if xml_ns:
1284
+ xml_name = "{% raw %}{{{}}}{}{% endraw %}".format(xml_ns, xml_name)
1285
+ return xml_name
1286
+
1287
+
1288
+ def xml_key_extractor(attr, attr_desc, data):
1289
+ if isinstance(data, dict):
1290
+ return None
1291
+
1292
+ # Test if this model is XML ready first
1293
+ if not isinstance(data, ET.Element):
1294
+ return None
1295
+
1296
+ xml_desc = attr_desc.get("xml", {})
1297
+ xml_name = xml_desc.get("name", attr_desc["key"])
1298
+
1299
+ # Look for a children
1300
+ is_iter_type = attr_desc["type"].startswith("[")
1301
+ is_wrapped = xml_desc.get("wrapped", False)
1302
+ internal_type = attr_desc.get("internalType", None)
1303
+ internal_type_xml_map = getattr(internal_type, "_xml_map", {})
1304
+
1305
+ # Integrate namespace if necessary
1306
+ xml_ns = xml_desc.get("ns", internal_type_xml_map.get("ns", None))
1307
+ if xml_ns:
1308
+ xml_name = "{% raw %}{{{}}}{}{% endraw %}".format(xml_ns, xml_name)
1309
+
1310
+ # If it's an attribute, that's simple
1311
+ if xml_desc.get("attr", False):
1312
+ return data.get(xml_name)
1313
+
1314
+ # If it's x-ms-text, that's simple too
1315
+ if xml_desc.get("text", False):
1316
+ return data.text
1317
+
1318
+ # Scenario where I take the local name:
1319
+ # - Wrapped node
1320
+ # - Internal type is an enum (considered basic types)
1321
+ # - Internal type has no XML/Name node
1322
+ if is_wrapped or (internal_type and (issubclass(internal_type, Enum) or "name" not in internal_type_xml_map)):
1323
+ children = data.findall(xml_name)
1324
+ # If internal type has a local name and it's not a list, I use that name
1325
+ elif not is_iter_type and internal_type and "name" in internal_type_xml_map:
1326
+ xml_name = _extract_name_from_internal_type(internal_type)
1327
+ children = data.findall(xml_name)
1328
+ # That's an array
1329
+ else:
1330
+ if internal_type: # Complex type, ignore itemsName and use the complex type name
1331
+ items_name = _extract_name_from_internal_type(internal_type)
1332
+ else:
1333
+ items_name = xml_desc.get("itemsName", xml_name)
1334
+ children = data.findall(items_name)
1335
+
1336
+ if len(children) == 0:
1337
+ if is_iter_type:
1338
+ if is_wrapped:
1339
+ return None # is_wrapped no node, we want None
1340
+ else:
1341
+ return [] # not wrapped, assume empty list
1342
+ return None # Assume it's not there, maybe an optional node.
1343
+
1344
+ # If is_iter_type and not wrapped, return all found children
1345
+ if is_iter_type:
1346
+ if not is_wrapped:
1347
+ return children
1348
+ else: # Iter and wrapped, should have found one node only (the wrap one)
1349
+ if len(children) != 1:
1350
+ raise DeserializationError(
1351
+ "Tried to deserialize an array not wrapped, and found several nodes '{}'. Maybe you should declare this array as wrapped?".format(
1352
+ xml_name
1353
+ )
1354
+ )
1355
+ return list(children[0]) # Might be empty list and that's ok.
1356
+
1357
+ # Here it's not a itertype, we should have found one element only or empty
1358
+ if len(children) > 1:
1359
+ raise DeserializationError("Find several XML '{}' where it was not expected".format(xml_name))
1360
+ return children[0]
1361
+
1362
+
1363
+ class Deserializer(object):
1364
+ """Response object model deserializer.
1365
+
1366
+ :param dict classes: Class type dictionary for deserializing complex types.
1367
+ :ivar list key_extractors: Ordered list of extractors to be used by this deserializer.
1368
+ """
1369
+
1370
+ basic_types = {str: "str", int: "int", bool: "bool", float: "float"}
1371
+
1372
+ valid_date = re.compile(r"\d{4}[-]\d{2}[-]\d{2}T\d{2}:\d{2}:\d{2}" r"\.?\d*Z?[-+]?[\d{2}]?:?[\d{2}]?")
1373
+
1374
+ def __init__(self, classes: Optional[Mapping[str, type]]=None):
1375
+ self.deserialize_type = {
1376
+ "iso-8601": Deserializer.deserialize_iso,
1377
+ "rfc-1123": Deserializer.deserialize_rfc,
1378
+ "unix-time": Deserializer.deserialize_unix,
1379
+ "duration": Deserializer.deserialize_duration,
1380
+ "date": Deserializer.deserialize_date,
1381
+ "time": Deserializer.deserialize_time,
1382
+ "decimal": Deserializer.deserialize_decimal,
1383
+ "long": Deserializer.deserialize_long,
1384
+ "bytearray": Deserializer.deserialize_bytearray,
1385
+ "base64": Deserializer.deserialize_base64,
1386
+ "object": self.deserialize_object,
1387
+ "[]": self.deserialize_iter,
1388
+ "{}": self.deserialize_dict,
1389
+ }
1390
+ self.deserialize_expected_types = {
1391
+ "duration": (isodate.Duration, datetime.timedelta),
1392
+ "iso-8601": (datetime.datetime),
1393
+ }
1394
+ self.dependencies: Dict[str, type] = dict(classes) if classes else {}
1395
+ self.key_extractors = [rest_key_extractor, xml_key_extractor]
1396
+ # Additional properties only works if the "rest_key_extractor" is used to
1397
+ # extract the keys. Making it to work whatever the key extractor is too much
1398
+ # complicated, with no real scenario for now.
1399
+ # So adding a flag to disable additional properties detection. This flag should be
1400
+ # used if your expect the deserialization to NOT come from a JSON REST syntax.
1401
+ # Otherwise, result are unexpected
1402
+ self.additional_properties_detection = True
1403
+
1404
+ def __call__(self, target_obj, response_data, content_type=None):
1405
+ """Call the deserializer to process a REST response.
1406
+
1407
+ :param str target_obj: Target data type to deserialize to.
1408
+ :param requests.Response response_data: REST response object.
1409
+ :param str content_type: Swagger "produces" if available.
1410
+ :raises: DeserializationError if deserialization fails.
1411
+ :return: Deserialized object.
1412
+ """
1413
+ data = self._unpack_content(response_data, content_type)
1414
+ return self._deserialize(target_obj, data)
1415
+
1416
+ def _deserialize(self, target_obj, data):
1417
+ """Call the deserializer on a model.
1418
+
1419
+ Data needs to be already deserialized as JSON or XML ElementTree
1420
+
1421
+ :param str target_obj: Target data type to deserialize to.
1422
+ :param object data: Object to deserialize.
1423
+ :raises: DeserializationError if deserialization fails.
1424
+ :return: Deserialized object.
1425
+ """
1426
+ # This is already a model, go recursive just in case
1427
+ if hasattr(data, "_attribute_map"):
1428
+ constants = [name for name, config in getattr(data, "_validation", {}).items() if config.get("constant")]
1429
+ try:
1430
+ for attr, mapconfig in data._attribute_map.items():
1431
+ if attr in constants:
1432
+ continue
1433
+ value = getattr(data, attr)
1434
+ if value is None:
1435
+ continue
1436
+ local_type = mapconfig["type"]
1437
+ internal_data_type = local_type.strip("[]{}")
1438
+ if internal_data_type not in self.dependencies or isinstance(internal_data_type, Enum):
1439
+ continue
1440
+ setattr(data, attr, self._deserialize(local_type, value))
1441
+ return data
1442
+ except AttributeError:
1443
+ return
1444
+
1445
+ response, class_name = self._classify_target(target_obj, data)
1446
+
1447
+ if isinstance(response, str):
1448
+ return self.deserialize_data(data, response)
1449
+ elif isinstance(response, type) and issubclass(response, Enum):
1450
+ return self.deserialize_enum(data, response)
1451
+
1452
+ if data is None or data is CoreNull:
1453
+ return data
1454
+ try:
1455
+ attributes = response._attribute_map # type: ignore
1456
+ d_attrs = {}
1457
+ for attr, attr_desc in attributes.items():
1458
+ # Check empty string. If it's not empty, someone has a real "additionalProperties"...
1459
+ if attr == "additional_properties" and attr_desc["key"] == "":
1460
+ continue
1461
+ raw_value = None
1462
+ # Enhance attr_desc with some dynamic data
1463
+ attr_desc = attr_desc.copy() # Do a copy, do not change the real one
1464
+ internal_data_type = attr_desc["type"].strip("[]{}")
1465
+ if internal_data_type in self.dependencies:
1466
+ attr_desc["internalType"] = self.dependencies[internal_data_type]
1467
+
1468
+ for key_extractor in self.key_extractors:
1469
+ found_value = key_extractor(attr, attr_desc, data)
1470
+ if found_value is not None:
1471
+ if raw_value is not None and raw_value != found_value:
1472
+ msg = (
1473
+ "Ignoring extracted value '%s' from %s for key '%s'"
1474
+ " (duplicate extraction, follow extractors order)"
1475
+ )
1476
+ _LOGGER.warning(msg, found_value, key_extractor, attr)
1477
+ continue
1478
+ raw_value = found_value
1479
+
1480
+ value = self.deserialize_data(raw_value, attr_desc["type"])
1481
+ d_attrs[attr] = value
1482
+ except (AttributeError, TypeError, KeyError) as err:
1483
+ msg = "Unable to deserialize to object: " + class_name # type: ignore
1484
+ raise DeserializationError(msg) from err
1485
+ else:
1486
+ additional_properties = self._build_additional_properties(attributes, data)
1487
+ return self._instantiate_model(response, d_attrs, additional_properties)
1488
+
1489
+ def _build_additional_properties(self, attribute_map, data):
1490
+ if not self.additional_properties_detection:
1491
+ return None
1492
+ if "additional_properties" in attribute_map and attribute_map.get("additional_properties", {}).get("key") != "":
1493
+ # Check empty string. If it's not empty, someone has a real "additionalProperties"
1494
+ return None
1495
+ if isinstance(data, ET.Element):
1496
+ data = {el.tag: el.text for el in data}
1497
+
1498
+ known_keys = {
1499
+ _decode_attribute_map_key(_FLATTEN.split(desc["key"])[0])
1500
+ for desc in attribute_map.values()
1501
+ if desc["key"] != ""
1502
+ }
1503
+ present_keys = set(data.keys())
1504
+ missing_keys = present_keys - known_keys
1505
+ return {key: data[key] for key in missing_keys}
1506
+
1507
+ def _classify_target(self, target, data):
1508
+ """Check to see whether the deserialization target object can
1509
+ be classified into a subclass.
1510
+ Once classification has been determined, initialize object.
1511
+
1512
+ :param str target: The target object type to deserialize to.
1513
+ :param str/dict data: The response data to deserialize.
1514
+ """
1515
+ if target is None:
1516
+ return None, None
1517
+
1518
+ if isinstance(target, str):
1519
+ try:
1520
+ target = self.dependencies[target]
1521
+ except KeyError:
1522
+ return target, target
1523
+
1524
+ try:
1525
+ target = target._classify(data, self.dependencies) # type: ignore
1526
+ except AttributeError:
1527
+ pass # Target is not a Model, no classify
1528
+ return target, target.__class__.__name__ # type: ignore
1529
+
1530
+ def failsafe_deserialize(self, target_obj, data, content_type=None):
1531
+ """Ignores any errors encountered in deserialization,
1532
+ and falls back to not deserializing the object. Recommended
1533
+ for use in error deserialization, as we want to return the
1534
+ HttpResponseError to users, and not have them deal with
1535
+ a deserialization error.
1536
+
1537
+ :param str target_obj: The target object type to deserialize to.
1538
+ :param str/dict data: The response data to deserialize.
1539
+ :param str content_type: Swagger "produces" if available.
1540
+ """
1541
+ try:
1542
+ return self(target_obj, data, content_type=content_type)
1543
+ except:
1544
+ _LOGGER.debug(
1545
+ "Ran into a deserialization error. Ignoring since this is failsafe deserialization", exc_info=True
1546
+ )
1547
+ return None
1548
+
1549
+ @staticmethod
1550
+ def _unpack_content(raw_data, content_type=None):
1551
+ """Extract the correct structure for deserialization.
1552
+
1553
+ If raw_data is a PipelineResponse, try to extract the result of RawDeserializer.
1554
+ if we can't, raise. Your Pipeline should have a RawDeserializer.
1555
+
1556
+ If not a pipeline response and raw_data is bytes or string, use content-type
1557
+ to decode it. If no content-type, try JSON.
1558
+
1559
+ If raw_data is something else, bypass all logic and return it directly.
1560
+
1561
+ :param raw_data: Data to be processed.
1562
+ :param content_type: How to parse if raw_data is a string/bytes.
1563
+ :raises JSONDecodeError: If JSON is requested and parsing is impossible.
1564
+ :raises UnicodeDecodeError: If bytes is not UTF8
1565
+ """
1566
+ # Assume this is enough to detect a Pipeline Response without importing it
1567
+ context = getattr(raw_data, "context", {})
1568
+ if context:
1569
+ if RawDeserializer.CONTEXT_NAME in context:
1570
+ return context[RawDeserializer.CONTEXT_NAME]
1571
+ raise ValueError("This pipeline didn't have the RawDeserializer policy; can't deserialize")
1572
+
1573
+ # Assume this is enough to recognize universal_http.ClientResponse without importing it
1574
+ if hasattr(raw_data, "body"):
1575
+ return RawDeserializer.deserialize_from_http_generics(raw_data.text(), raw_data.headers)
1576
+
1577
+ # Assume this enough to recognize requests.Response without importing it.
1578
+ if hasattr(raw_data, "_content_consumed"):
1579
+ return RawDeserializer.deserialize_from_http_generics(raw_data.text, raw_data.headers)
1580
+
1581
+ if isinstance(raw_data, (str, bytes)) or hasattr(raw_data, "read"):
1582
+ return RawDeserializer.deserialize_from_text(raw_data, content_type) # type: ignore
1583
+ return raw_data
1584
+
1585
+ def _instantiate_model(self, response, attrs, additional_properties=None):
1586
+ """Instantiate a response model passing in deserialized args.
1587
+
1588
+ :param response: The response model class.
1589
+ :param d_attrs: The deserialized response attributes.
1590
+ """
1591
+ if callable(response):
1592
+ subtype = getattr(response, "_subtype_map", {})
1593
+ try:
1594
+ readonly = [k for k, v in response._validation.items() if v.get("readonly")]
1595
+ const = [k for k, v in response._validation.items() if v.get("constant")]
1596
+ kwargs = {k: v for k, v in attrs.items() if k not in subtype and k not in readonly + const}
1597
+ response_obj = response(**kwargs)
1598
+ for attr in readonly:
1599
+ setattr(response_obj, attr, attrs.get(attr))
1600
+ if additional_properties:
1601
+ response_obj.additional_properties = additional_properties
1602
+ return response_obj
1603
+ except TypeError as err:
1604
+ msg = "Unable to deserialize {} into model {}. ".format(kwargs, response) # type: ignore
1605
+ raise DeserializationError(msg + str(err))
1606
+ else:
1607
+ try:
1608
+ for attr, value in attrs.items():
1609
+ setattr(response, attr, value)
1610
+ return response
1611
+ except Exception as exp:
1612
+ msg = "Unable to populate response model. "
1613
+ msg += "Type: {}, Error: {}".format(type(response), exp)
1614
+ raise DeserializationError(msg)
1615
+
1616
+ def deserialize_data(self, data, data_type):
1617
+ """Process data for deserialization according to data type.
1618
+
1619
+ :param str data: The response string to be deserialized.
1620
+ :param str data_type: The type to deserialize to.
1621
+ :raises: DeserializationError if deserialization fails.
1622
+ :return: Deserialized object.
1623
+ """
1624
+ if data is None:
1625
+ return data
1626
+
1627
+ try:
1628
+ if not data_type:
1629
+ return data
1630
+ if data_type in self.basic_types.values():
1631
+ return self.deserialize_basic(data, data_type)
1632
+ if data_type in self.deserialize_type:
1633
+ if isinstance(data, self.deserialize_expected_types.get(data_type, tuple())):
1634
+ return data
1635
+
1636
+ is_a_text_parsing_type = lambda x: x not in ["object", "[]", r"{}"]
1637
+ if isinstance(data, ET.Element) and is_a_text_parsing_type(data_type) and not data.text:
1638
+ return None
1639
+ data_val = self.deserialize_type[data_type](data)
1640
+ return data_val
1641
+
1642
+ iter_type = data_type[0] + data_type[-1]
1643
+ if iter_type in self.deserialize_type:
1644
+ return self.deserialize_type[iter_type](data, data_type[1:-1])
1645
+
1646
+ obj_type = self.dependencies[data_type]
1647
+ if issubclass(obj_type, Enum):
1648
+ if isinstance(data, ET.Element):
1649
+ data = data.text
1650
+ return self.deserialize_enum(data, obj_type)
1651
+
1652
+ except (ValueError, TypeError, AttributeError) as err:
1653
+ msg = "Unable to deserialize response data."
1654
+ msg += " Data: {}, {}".format(data, data_type)
1655
+ raise DeserializationError(msg) from err
1656
+ else:
1657
+ return self._deserialize(obj_type, data)
1658
+
1659
+ def deserialize_iter(self, attr, iter_type):
1660
+ """Deserialize an iterable.
1661
+
1662
+ :param list attr: Iterable to be deserialized.
1663
+ :param str iter_type: The type of object in the iterable.
1664
+ :rtype: list
1665
+ """
1666
+ if attr is None:
1667
+ return None
1668
+ if isinstance(attr, ET.Element): # If I receive an element here, get the children
1669
+ attr = list(attr)
1670
+ if not isinstance(attr, (list, set)):
1671
+ raise DeserializationError("Cannot deserialize as [{}] an object of type {}".format(iter_type, type(attr)))
1672
+ return [self.deserialize_data(a, iter_type) for a in attr]
1673
+
1674
+ def deserialize_dict(self, attr, dict_type):
1675
+ """Deserialize a dictionary.
1676
+
1677
+ :param dict/list attr: Dictionary to be deserialized. Also accepts
1678
+ a list of key, value pairs.
1679
+ :param str dict_type: The object type of the items in the dictionary.
1680
+ :rtype: dict
1681
+ """
1682
+ if isinstance(attr, list):
1683
+ return {x["key"]: self.deserialize_data(x["value"], dict_type) for x in attr}
1684
+
1685
+ if isinstance(attr, ET.Element):
1686
+ # Transform <Key>value</Key> into {"Key": "value"}
1687
+ attr = {el.tag: el.text for el in attr}
1688
+ return {k: self.deserialize_data(v, dict_type) for k, v in attr.items()}
1689
+
1690
+ def deserialize_object(self, attr, **kwargs):
1691
+ """Deserialize a generic object.
1692
+ This will be handled as a dictionary.
1693
+
1694
+ :param dict attr: Dictionary to be deserialized.
1695
+ :rtype: dict
1696
+ :raises: TypeError if non-builtin datatype encountered.
1697
+ """
1698
+ if attr is None:
1699
+ return None
1700
+ if isinstance(attr, ET.Element):
1701
+ # Do no recurse on XML, just return the tree as-is
1702
+ return attr
1703
+ if isinstance(attr, str):
1704
+ return self.deserialize_basic(attr, "str")
1705
+ obj_type = type(attr)
1706
+ if obj_type in self.basic_types:
1707
+ return self.deserialize_basic(attr, self.basic_types[obj_type])
1708
+ if obj_type is _long_type:
1709
+ return self.deserialize_long(attr)
1710
+
1711
+ if obj_type == dict:
1712
+ deserialized = {}
1713
+ for key, value in attr.items():
1714
+ try:
1715
+ deserialized[key] = self.deserialize_object(value, **kwargs)
1716
+ except ValueError:
1717
+ deserialized[key] = None
1718
+ return deserialized
1719
+
1720
+ if obj_type == list:
1721
+ deserialized = []
1722
+ for obj in attr:
1723
+ try:
1724
+ deserialized.append(self.deserialize_object(obj, **kwargs))
1725
+ except ValueError:
1726
+ pass
1727
+ return deserialized
1728
+
1729
+ else:
1730
+ error = "Cannot deserialize generic object with type: "
1731
+ raise TypeError(error + str(obj_type))
1732
+
1733
+ def deserialize_basic(self, attr, data_type):
1734
+ """Deserialize basic builtin data type from string.
1735
+ Will attempt to convert to str, int, float and bool.
1736
+ This function will also accept '1', '0', 'true' and 'false' as
1737
+ valid bool values.
1738
+
1739
+ :param str attr: response string to be deserialized.
1740
+ :param str data_type: deserialization data type.
1741
+ :rtype: str, int, float or bool
1742
+ :raises: TypeError if string format is not valid.
1743
+ """
1744
+ # If we're here, data is supposed to be a basic type.
1745
+ # If it's still an XML node, take the text
1746
+ if isinstance(attr, ET.Element):
1747
+ attr = attr.text
1748
+ if not attr:
1749
+ if data_type == "str":
1750
+ # None or '', node <a/> is empty string.
1751
+ return ""
1752
+ else:
1753
+ # None or '', node <a/> with a strong type is None.
1754
+ # Don't try to model "empty bool" or "empty int"
1755
+ return None
1756
+
1757
+ if data_type == "bool":
1758
+ if attr in [True, False, 1, 0]:
1759
+ return bool(attr)
1760
+ elif isinstance(attr, str):
1761
+ if attr.lower() in ["true", "1"]:
1762
+ return True
1763
+ elif attr.lower() in ["false", "0"]:
1764
+ return False
1765
+ raise TypeError("Invalid boolean value: {}".format(attr))
1766
+
1767
+ if data_type == "str":
1768
+ return self.deserialize_unicode(attr)
1769
+ return eval(data_type)(attr) # nosec
1770
+
1771
+ @staticmethod
1772
+ def deserialize_unicode(data):
1773
+ """Preserve unicode objects in Python 2, otherwise return data
1774
+ as a string.
1775
+
1776
+ :param str data: response string to be deserialized.
1777
+ :rtype: str or unicode
1778
+ """
1779
+ # We might be here because we have an enum modeled as string,
1780
+ # and we try to deserialize a partial dict with enum inside
1781
+ if isinstance(data, Enum):
1782
+ return data
1783
+
1784
+ # Consider this is real string
1785
+ try:
1786
+ if isinstance(data, unicode): # type: ignore
1787
+ return data
1788
+ except NameError:
1789
+ return str(data)
1790
+ else:
1791
+ return str(data)
1792
+
1793
+ @staticmethod
1794
+ def deserialize_enum(data, enum_obj):
1795
+ """Deserialize string into enum object.
1796
+
1797
+ If the string is not a valid enum value it will be returned as-is
1798
+ and a warning will be logged.
1799
+
1800
+ :param str data: Response string to be deserialized. If this value is
1801
+ None or invalid it will be returned as-is.
1802
+ :param Enum enum_obj: Enum object to deserialize to.
1803
+ :rtype: Enum
1804
+ """
1805
+ if isinstance(data, enum_obj) or data is None:
1806
+ return data
1807
+ if isinstance(data, Enum):
1808
+ data = data.value
1809
+ if isinstance(data, int):
1810
+ # Workaround. We might consider remove it in the future.
1811
+ try:
1812
+ return list(enum_obj.__members__.values())[data]
1813
+ except IndexError:
1814
+ error = "{!r} is not a valid index for enum {!r}"
1815
+ raise DeserializationError(error.format(data, enum_obj))
1816
+ try:
1817
+ return enum_obj(str(data))
1818
+ except ValueError:
1819
+ for enum_value in enum_obj:
1820
+ if enum_value.value.lower() == str(data).lower():
1821
+ return enum_value
1822
+ # We don't fail anymore for unknown value, we deserialize as a string
1823
+ _LOGGER.warning("Deserializer is not able to find %s as valid enum in %s", data, enum_obj)
1824
+ return Deserializer.deserialize_unicode(data)
1825
+
1826
+ @staticmethod
1827
+ def deserialize_bytearray(attr):
1828
+ """Deserialize string into bytearray.
1829
+
1830
+ :param str attr: response string to be deserialized.
1831
+ :rtype: bytearray
1832
+ :raises: TypeError if string format invalid.
1833
+ """
1834
+ if isinstance(attr, ET.Element):
1835
+ attr = attr.text
1836
+ return bytearray(b64decode(attr)) # type: ignore
1837
+
1838
+ @staticmethod
1839
+ def deserialize_base64(attr):
1840
+ """Deserialize base64 encoded string into string.
1841
+
1842
+ :param str attr: response string to be deserialized.
1843
+ :rtype: bytearray
1844
+ :raises: TypeError if string format invalid.
1845
+ """
1846
+ if isinstance(attr, ET.Element):
1847
+ attr = attr.text
1848
+ padding = "=" * (3 - (len(attr) + 3) % 4) # type: ignore
1849
+ attr = attr + padding # type: ignore
1850
+ encoded = attr.replace("-", "+").replace("_", "/")
1851
+ return b64decode(encoded)
1852
+
1853
+ @staticmethod
1854
+ def deserialize_decimal(attr):
1855
+ """Deserialize string into Decimal object.
1856
+
1857
+ :param str attr: response string to be deserialized.
1858
+ :rtype: Decimal
1859
+ :raises: DeserializationError if string format invalid.
1860
+ """
1861
+ if isinstance(attr, ET.Element):
1862
+ attr = attr.text
1863
+ try:
1864
+ return decimal.Decimal(str(attr)) # type: ignore
1865
+ except decimal.DecimalException as err:
1866
+ msg = "Invalid decimal {}".format(attr)
1867
+ raise DeserializationError(msg) from err
1868
+
1869
+ @staticmethod
1870
+ def deserialize_long(attr):
1871
+ """Deserialize string into long (Py2) or int (Py3).
1872
+
1873
+ :param str attr: response string to be deserialized.
1874
+ :rtype: long or int
1875
+ :raises: ValueError if string format invalid.
1876
+ """
1877
+ if isinstance(attr, ET.Element):
1878
+ attr = attr.text
1879
+ return _long_type(attr) # type: ignore
1880
+
1881
+ @staticmethod
1882
+ def deserialize_duration(attr):
1883
+ """Deserialize ISO-8601 formatted string into TimeDelta object.
1884
+
1885
+ :param str attr: response string to be deserialized.
1886
+ :rtype: TimeDelta
1887
+ :raises: DeserializationError if string format invalid.
1888
+ """
1889
+ if isinstance(attr, ET.Element):
1890
+ attr = attr.text
1891
+ try:
1892
+ duration = isodate.parse_duration(attr)
1893
+ except (ValueError, OverflowError, AttributeError) as err:
1894
+ msg = "Cannot deserialize duration object."
1895
+ raise DeserializationError(msg) from err
1896
+ else:
1897
+ return duration
1898
+
1899
+ @staticmethod
1900
+ def deserialize_date(attr):
1901
+ """Deserialize ISO-8601 formatted string into Date object.
1902
+
1903
+ :param str attr: response string to be deserialized.
1904
+ :rtype: Date
1905
+ :raises: DeserializationError if string format invalid.
1906
+ """
1907
+ if isinstance(attr, ET.Element):
1908
+ attr = attr.text
1909
+ if re.search(r"[^\W\d_]", attr, re.I + re.U): # type: ignore
1910
+ raise DeserializationError("Date must have only digits and -. Received: %s" % attr)
1911
+ # This must NOT use defaultmonth/defaultday. Using None ensure this raises an exception.
1912
+ return isodate.parse_date(attr, defaultmonth=0, defaultday=0)
1913
+
1914
+ @staticmethod
1915
+ def deserialize_time(attr):
1916
+ """Deserialize ISO-8601 formatted string into time object.
1917
+
1918
+ :param str attr: response string to be deserialized.
1919
+ :rtype: datetime.time
1920
+ :raises: DeserializationError if string format invalid.
1921
+ """
1922
+ if isinstance(attr, ET.Element):
1923
+ attr = attr.text
1924
+ if re.search(r"[^\W\d_]", attr, re.I + re.U): # type: ignore
1925
+ raise DeserializationError("Date must have only digits and -. Received: %s" % attr)
1926
+ return isodate.parse_time(attr)
1927
+
1928
+ @staticmethod
1929
+ def deserialize_rfc(attr):
1930
+ """Deserialize RFC-1123 formatted string into Datetime object.
1931
+
1932
+ :param str attr: response string to be deserialized.
1933
+ :rtype: Datetime
1934
+ :raises: DeserializationError if string format invalid.
1935
+ """
1936
+ if isinstance(attr, ET.Element):
1937
+ attr = attr.text
1938
+ try:
1939
+ parsed_date = email.utils.parsedate_tz(attr) # type: ignore
1940
+ date_obj = datetime.datetime(
1941
+ *parsed_date[:6], tzinfo=_FixedOffset(datetime.timedelta(minutes=(parsed_date[9] or 0) / 60))
1942
+ )
1943
+ if not date_obj.tzinfo:
1944
+ date_obj = date_obj.astimezone(tz=TZ_UTC)
1945
+ except ValueError as err:
1946
+ msg = "Cannot deserialize to rfc datetime object."
1947
+ raise DeserializationError(msg) from err
1948
+ else:
1949
+ return date_obj
1950
+
1951
+ @staticmethod
1952
+ def deserialize_iso(attr):
1953
+ """Deserialize ISO-8601 formatted string into Datetime object.
1954
+
1955
+ :param str attr: response string to be deserialized.
1956
+ :rtype: Datetime
1957
+ :raises: DeserializationError if string format invalid.
1958
+ """
1959
+ if isinstance(attr, ET.Element):
1960
+ attr = attr.text
1961
+ try:
1962
+ attr = attr.upper() # type: ignore
1963
+ match = Deserializer.valid_date.match(attr)
1964
+ if not match:
1965
+ raise ValueError("Invalid datetime string: " + attr)
1966
+
1967
+ check_decimal = attr.split(".")
1968
+ if len(check_decimal) > 1:
1969
+ decimal_str = ""
1970
+ for digit in check_decimal[1]:
1971
+ if digit.isdigit():
1972
+ decimal_str += digit
1973
+ else:
1974
+ break
1975
+ if len(decimal_str) > 6:
1976
+ attr = attr.replace(decimal_str, decimal_str[0:6])
1977
+
1978
+ date_obj = isodate.parse_datetime(attr)
1979
+ test_utc = date_obj.utctimetuple()
1980
+ if test_utc.tm_year > 9999 or test_utc.tm_year < 1:
1981
+ raise OverflowError("Hit max or min date")
1982
+ except (ValueError, OverflowError, AttributeError) as err:
1983
+ msg = "Cannot deserialize datetime object."
1984
+ raise DeserializationError(msg) from err
1985
+ else:
1986
+ return date_obj
1987
+
1988
+ @staticmethod
1989
+ def deserialize_unix(attr):
1990
+ """Serialize Datetime object into IntTime format.
1991
+ This is represented as seconds.
1992
+
1993
+ :param int attr: Object to be serialized.
1994
+ :rtype: Datetime
1995
+ :raises: DeserializationError if format invalid
1996
+ """
1997
+ if isinstance(attr, ET.Element):
1998
+ attr = int(attr.text) # type: ignore
1999
+ try:
2000
+ attr = int(attr)
2001
+ date_obj = datetime.datetime.fromtimestamp(attr, TZ_UTC)
2002
+ except ValueError as err:
2003
+ msg = "Cannot deserialize to unix datetime object."
2004
+ raise DeserializationError(msg) from err
2005
+ else:
2006
+ return date_obj