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