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