azure-ai-evaluation 1.5.0__py3-none-any.whl → 1.6.0__py3-none-any.whl

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.

Potentially problematic release.


This version of azure-ai-evaluation might be problematic. Click here for more details.

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