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