@autorest/python 6.38.1 → 6.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/README.md +2 -27
  2. package/autorest/codegen.py +0 -1
  3. package/autorest/jsonrpc/server.py +0 -3
  4. package/generator/build/lib/pygen/__init__.py +14 -23
  5. package/generator/build/lib/pygen/codegen/__init__.py +4 -4
  6. package/generator/build/lib/pygen/codegen/models/__init__.py +2 -2
  7. package/generator/build/lib/pygen/codegen/models/base.py +9 -12
  8. package/generator/build/lib/pygen/codegen/models/base_builder.py +4 -6
  9. package/generator/build/lib/pygen/codegen/models/client.py +61 -102
  10. package/generator/build/lib/pygen/codegen/models/code_model.py +29 -29
  11. package/generator/build/lib/pygen/codegen/models/combined_type.py +7 -7
  12. package/generator/build/lib/pygen/codegen/models/constant_type.py +4 -11
  13. package/generator/build/lib/pygen/codegen/models/credential_types.py +9 -11
  14. package/generator/build/lib/pygen/codegen/models/dictionary_type.py +7 -8
  15. package/generator/build/lib/pygen/codegen/models/enum_type.py +7 -7
  16. package/generator/build/lib/pygen/codegen/models/imports.py +24 -29
  17. package/generator/build/lib/pygen/codegen/models/list_type.py +15 -14
  18. package/generator/build/lib/pygen/codegen/models/lro_operation.py +6 -6
  19. package/generator/build/lib/pygen/codegen/models/lro_paging_operation.py +2 -2
  20. package/generator/build/lib/pygen/codegen/models/model_type.py +11 -11
  21. package/generator/build/lib/pygen/codegen/models/operation.py +26 -56
  22. package/generator/build/lib/pygen/codegen/models/operation_group.py +11 -22
  23. package/generator/build/lib/pygen/codegen/models/paging_operation.py +15 -19
  24. package/generator/build/lib/pygen/codegen/models/parameter.py +12 -21
  25. package/generator/build/lib/pygen/codegen/models/parameter_list.py +37 -39
  26. package/generator/build/lib/pygen/codegen/models/primitive_types.py +24 -18
  27. package/generator/build/lib/pygen/codegen/models/property.py +10 -10
  28. package/generator/build/lib/pygen/codegen/models/request_builder.py +7 -8
  29. package/generator/build/lib/pygen/codegen/models/request_builder_parameter.py +3 -3
  30. package/generator/build/lib/pygen/codegen/models/response.py +15 -40
  31. package/generator/build/lib/pygen/codegen/models/utils.py +2 -2
  32. package/generator/build/lib/pygen/codegen/serializers/__init__.py +26 -42
  33. package/generator/build/lib/pygen/codegen/serializers/builder_serializer.py +103 -100
  34. package/generator/build/lib/pygen/codegen/serializers/client_serializer.py +22 -25
  35. package/generator/build/lib/pygen/codegen/serializers/enum_serializer.py +2 -2
  36. package/generator/build/lib/pygen/codegen/serializers/general_serializer.py +49 -61
  37. package/generator/build/lib/pygen/codegen/serializers/import_serializer.py +6 -7
  38. package/generator/build/lib/pygen/codegen/serializers/model_init_serializer.py +1 -2
  39. package/generator/build/lib/pygen/codegen/serializers/model_serializer.py +15 -17
  40. package/generator/build/lib/pygen/codegen/serializers/operation_groups_serializer.py +3 -3
  41. package/generator/build/lib/pygen/codegen/serializers/operations_init_serializer.py +5 -6
  42. package/generator/build/lib/pygen/codegen/serializers/parameter_serializer.py +28 -18
  43. package/generator/build/lib/pygen/codegen/serializers/patch_serializer.py +1 -2
  44. package/generator/build/lib/pygen/codegen/serializers/request_builders_serializer.py +1 -2
  45. package/generator/build/lib/pygen/codegen/serializers/sample_serializer.py +9 -14
  46. package/generator/build/lib/pygen/codegen/serializers/test_serializer.py +7 -7
  47. package/generator/build/lib/pygen/codegen/serializers/utils.py +2 -2
  48. package/generator/build/lib/pygen/codegen/templates/model_base.py.jinja2 +30 -24
  49. package/generator/build/lib/pygen/codegen/templates/model_dpg.py.jinja2 +1 -1
  50. package/generator/build/lib/pygen/codegen/templates/operation_group.py.jinja2 +1 -11
  51. package/generator/build/lib/pygen/codegen/templates/operations_folder_init.py.jinja2 +1 -1
  52. package/generator/build/lib/pygen/codegen/templates/packaging_templates/CHANGELOG.md.jinja2 +2 -1
  53. package/generator/build/lib/pygen/codegen/templates/packaging_templates/dev_requirements.txt.jinja2 +1 -1
  54. package/generator/build/lib/pygen/codegen/templates/packaging_templates/pyproject.toml.jinja2 +13 -2
  55. package/generator/build/lib/pygen/codegen/templates/patch.py.jinja2 +1 -1
  56. package/generator/build/lib/pygen/codegen/templates/serialization.py.jinja2 +11 -13
  57. package/generator/build/lib/pygen/codegen/templates/utils.py.jinja2 +6 -6
  58. package/generator/build/lib/pygen/preprocess/__init__.py +47 -30
  59. package/generator/build/lib/pygen/preprocess/helpers.py +2 -2
  60. package/generator/build/lib/pygen/utils.py +6 -6
  61. package/generator/dist/pygen-0.1.0-py3-none-any.whl +0 -0
  62. package/generator/pygen/__init__.py +14 -23
  63. package/generator/pygen/codegen/__init__.py +4 -4
  64. package/generator/pygen/codegen/models/__init__.py +2 -2
  65. package/generator/pygen/codegen/models/base.py +9 -12
  66. package/generator/pygen/codegen/models/base_builder.py +4 -6
  67. package/generator/pygen/codegen/models/client.py +61 -102
  68. package/generator/pygen/codegen/models/code_model.py +29 -29
  69. package/generator/pygen/codegen/models/combined_type.py +7 -7
  70. package/generator/pygen/codegen/models/constant_type.py +4 -11
  71. package/generator/pygen/codegen/models/credential_types.py +9 -11
  72. package/generator/pygen/codegen/models/dictionary_type.py +7 -8
  73. package/generator/pygen/codegen/models/enum_type.py +7 -7
  74. package/generator/pygen/codegen/models/imports.py +24 -29
  75. package/generator/pygen/codegen/models/list_type.py +15 -14
  76. package/generator/pygen/codegen/models/lro_operation.py +6 -6
  77. package/generator/pygen/codegen/models/lro_paging_operation.py +2 -2
  78. package/generator/pygen/codegen/models/model_type.py +11 -11
  79. package/generator/pygen/codegen/models/operation.py +26 -56
  80. package/generator/pygen/codegen/models/operation_group.py +11 -22
  81. package/generator/pygen/codegen/models/paging_operation.py +15 -19
  82. package/generator/pygen/codegen/models/parameter.py +12 -21
  83. package/generator/pygen/codegen/models/parameter_list.py +37 -39
  84. package/generator/pygen/codegen/models/primitive_types.py +24 -18
  85. package/generator/pygen/codegen/models/property.py +10 -10
  86. package/generator/pygen/codegen/models/request_builder.py +7 -8
  87. package/generator/pygen/codegen/models/request_builder_parameter.py +3 -3
  88. package/generator/pygen/codegen/models/response.py +15 -40
  89. package/generator/pygen/codegen/models/utils.py +2 -2
  90. package/generator/pygen/codegen/serializers/__init__.py +26 -42
  91. package/generator/pygen/codegen/serializers/builder_serializer.py +103 -100
  92. package/generator/pygen/codegen/serializers/client_serializer.py +22 -25
  93. package/generator/pygen/codegen/serializers/enum_serializer.py +2 -2
  94. package/generator/pygen/codegen/serializers/general_serializer.py +49 -61
  95. package/generator/pygen/codegen/serializers/import_serializer.py +6 -7
  96. package/generator/pygen/codegen/serializers/model_init_serializer.py +1 -2
  97. package/generator/pygen/codegen/serializers/model_serializer.py +15 -17
  98. package/generator/pygen/codegen/serializers/operation_groups_serializer.py +3 -3
  99. package/generator/pygen/codegen/serializers/operations_init_serializer.py +5 -6
  100. package/generator/pygen/codegen/serializers/parameter_serializer.py +28 -18
  101. package/generator/pygen/codegen/serializers/patch_serializer.py +1 -2
  102. package/generator/pygen/codegen/serializers/request_builders_serializer.py +1 -2
  103. package/generator/pygen/codegen/serializers/sample_serializer.py +9 -14
  104. package/generator/pygen/codegen/serializers/test_serializer.py +7 -7
  105. package/generator/pygen/codegen/serializers/utils.py +2 -2
  106. package/generator/pygen/codegen/templates/model_base.py.jinja2 +30 -24
  107. package/generator/pygen/codegen/templates/model_dpg.py.jinja2 +1 -1
  108. package/generator/pygen/codegen/templates/operation_group.py.jinja2 +1 -11
  109. package/generator/pygen/codegen/templates/operations_folder_init.py.jinja2 +1 -1
  110. package/generator/pygen/codegen/templates/packaging_templates/CHANGELOG.md.jinja2 +2 -1
  111. package/generator/pygen/codegen/templates/packaging_templates/dev_requirements.txt.jinja2 +1 -1
  112. package/generator/pygen/codegen/templates/packaging_templates/pyproject.toml.jinja2 +13 -2
  113. package/generator/pygen/codegen/templates/patch.py.jinja2 +1 -1
  114. package/generator/pygen/codegen/templates/serialization.py.jinja2 +11 -13
  115. package/generator/pygen/codegen/templates/utils.py.jinja2 +6 -6
  116. package/generator/pygen/preprocess/__init__.py +47 -30
  117. package/generator/pygen/preprocess/helpers.py +2 -2
  118. package/generator/pygen/utils.py +6 -6
  119. package/generator/pygen.egg-info/SOURCES.txt +0 -2
  120. package/package.json +2 -2
  121. package/scripts/__pycache__/venvtools.cpython-310.pyc +0 -0
  122. package/autorest/multiapi/__init__.py +0 -185
  123. package/autorest/multiapi/models/__init__.py +0 -16
  124. package/autorest/multiapi/models/client.py +0 -68
  125. package/autorest/multiapi/models/code_model.py +0 -142
  126. package/autorest/multiapi/models/config.py +0 -24
  127. package/autorest/multiapi/models/constant_global_parameter.py +0 -11
  128. package/autorest/multiapi/models/global_parameter.py +0 -34
  129. package/autorest/multiapi/models/global_parameters.py +0 -53
  130. package/autorest/multiapi/models/imports.py +0 -181
  131. package/autorest/multiapi/models/mixin_operation.py +0 -38
  132. package/autorest/multiapi/models/operation_group.py +0 -29
  133. package/autorest/multiapi/models/operation_mixin_group.py +0 -89
  134. package/autorest/multiapi/serializers/__init__.py +0 -145
  135. package/autorest/multiapi/serializers/import_serializer.py +0 -181
  136. package/autorest/multiapi/templates/multiapi_config.py.jinja2 +0 -89
  137. package/autorest/multiapi/templates/multiapi_init.py.jinja2 +0 -22
  138. package/autorest/multiapi/templates/multiapi_models.py.jinja2 +0 -9
  139. package/autorest/multiapi/templates/multiapi_operations_mixin.py.jinja2 +0 -39
  140. package/autorest/multiapi/templates/multiapi_service_client.py.jinja2 +0 -163
  141. package/autorest/multiapi/templates/multiapi_version.py.jinja2 +0 -8
  142. package/autorest/multiapi/utils.py +0 -51
  143. package/generator/build/lib/pygen/codegen/serializers/metadata_serializer.py +0 -216
  144. package/generator/build/lib/pygen/codegen/templates/metadata.json.jinja2 +0 -167
  145. package/generator/pygen/codegen/serializers/metadata_serializer.py +0 -216
  146. package/generator/pygen/codegen/templates/metadata.json.jinja2 +0 -167
@@ -1,51 +0,0 @@
1
- # -------------------------------------------------------------------------
2
- # Copyright (c) Microsoft Corporation. All rights reserved.
3
- # Licensed under the MIT License. See License.txt in the project root for
4
- # license information.
5
- # --------------------------------------------------------------------------
6
- import logging
7
- from typing import Dict, List, Optional
8
-
9
- _LOGGER = logging.getLogger(__name__)
10
-
11
-
12
- def _sync_or_async(async_mode: bool) -> str:
13
- return "async" if async_mode else "sync"
14
-
15
-
16
- def _get_default_api_version_from_list(
17
- mod_to_api_version: Dict[str, str],
18
- api_versions_list: List[str],
19
- preview_mode: bool,
20
- user_specified_default_api: Optional[str],
21
- ) -> str:
22
- """Get the floating latest, from a random list of API versions."""
23
-
24
- # I need user_specified_default_api to be v2019_06_07_preview shaped if it exists, let's be smart
25
- # and change it automatically so I can take both syntax as input
26
- if user_specified_default_api and not user_specified_default_api.startswith("v"):
27
- default_api_version = [
28
- mod_api for mod_api, real_api in mod_to_api_version.items() if real_api == user_specified_default_api
29
- ][0]
30
- _LOGGER.info("Default API version will be: %s", default_api_version)
31
- return default_api_version
32
-
33
- absolute_latest = sorted(api_versions_list)[-1]
34
- not_preview_versions = [version for version in api_versions_list if "preview" not in version]
35
-
36
- # If there is no preview, easy: the absolute latest is the only latest
37
- if not not_preview_versions:
38
- return absolute_latest
39
-
40
- # If preview mode, let's use the absolute latest, I don't care preview or stable
41
- if preview_mode:
42
- return absolute_latest
43
-
44
- # If not preview mode, and there is preview, take the latest known stable
45
- return sorted(not_preview_versions)[-1]
46
-
47
-
48
- def convert_list_to_tuple(l):
49
- if not isinstance(l, list):
50
- return l
51
- return tuple(convert_list_to_tuple(x) for x in l) if isinstance(l, list) else l
@@ -1,216 +0,0 @@
1
- # -------------------------------------------------------------------------
2
- # Copyright (c) Microsoft Corporation. All rights reserved.
3
- # Licensed under the MIT License. See License.txt in the project root for
4
- # license information.
5
- # --------------------------------------------------------------------------
6
- import functools
7
- import json
8
- from typing import List, Optional, Set, Tuple, Dict, Union, Any
9
- from jinja2 import Environment
10
- from ..models import (
11
- OperationGroup,
12
- LROOperation,
13
- PagingOperation,
14
- TypingSection,
15
- ImportType,
16
- CodeModel,
17
- )
18
- from .builder_serializer import get_operation_serializer
19
- from .base_serializer import BaseSerializer
20
- from .import_serializer import FileImportSerializer
21
-
22
-
23
- def _to_string(data: Union[Tuple[Any], List[Any], str]) -> str:
24
- if isinstance(data, (list, tuple)):
25
- return "".join([_to_string(item) for item in data])
26
- return str(data)
27
-
28
-
29
- def _json_serialize_imports(
30
- imports: Dict[
31
- TypingSection,
32
- Dict[
33
- ImportType,
34
- Dict[
35
- str,
36
- Set[
37
- Optional[
38
- Union[
39
- str,
40
- Tuple[str, str],
41
- Tuple[
42
- str,
43
- Optional[str],
44
- Tuple[Tuple[Tuple[int, int], str, Optional[str]]],
45
- ],
46
- ]
47
- ]
48
- ],
49
- ],
50
- ],
51
- ],
52
- ) -> str:
53
- if not imports:
54
- return ""
55
-
56
- json_serialize_imports = {}
57
- # need to make name_import set -> list to make the dictionary json serializable
58
- # not using an OrderedDict since we're iterating through a set and the order there varies
59
- # going to sort the list instead
60
-
61
- for typing_section_key, typing_section_value in imports.items():
62
- json_import_type_dictionary = {}
63
- for import_type_key, import_type_value in typing_section_value.items():
64
- json_package_name_dictionary = {}
65
- for package_name, name_imports in import_type_value.items():
66
- name_import_ordered_list = []
67
- if name_imports:
68
- name_import_ordered_list = list(name_imports)
69
- name_import_ordered_list.sort(
70
- key=lambda e: (
71
- _to_string(e) # type: ignore
72
- if isinstance(e, (list, tuple))
73
- else e if isinstance(e, str) else ""
74
- )
75
- )
76
- json_package_name_dictionary[package_name] = name_import_ordered_list
77
- json_import_type_dictionary[import_type_key] = json_package_name_dictionary
78
- json_serialize_imports[typing_section_key] = json_import_type_dictionary
79
- return json.dumps(json_serialize_imports)
80
-
81
-
82
- def _mixin_imports(
83
- mixin_operation_group: Optional[OperationGroup],
84
- ) -> Tuple[Optional[str], Optional[str]]:
85
- if not mixin_operation_group:
86
- return None, None
87
-
88
- sync_mixin_imports = mixin_operation_group.imports_for_multiapi(async_mode=False)
89
- async_mixin_imports = mixin_operation_group.imports_for_multiapi(async_mode=True)
90
-
91
- return _json_serialize_imports(sync_mixin_imports.to_dict()), _json_serialize_imports(async_mixin_imports.to_dict())
92
-
93
-
94
- def _mixin_typing_definitions(
95
- mixin_operation_group: Optional[OperationGroup],
96
- ) -> Tuple[Optional[str], Optional[str]]:
97
- if not mixin_operation_group:
98
- return None, None
99
-
100
- sync_mixin_imports = mixin_operation_group.imports_for_multiapi(async_mode=False)
101
- async_mixin_imports = mixin_operation_group.imports_for_multiapi(async_mode=True)
102
- sync_mixin_typing_definitions = FileImportSerializer(sync_mixin_imports, False).get_typing_definitions()
103
- async_mixin_typing_definitions = FileImportSerializer(async_mixin_imports, True).get_typing_definitions()
104
-
105
- return sync_mixin_typing_definitions, async_mixin_typing_definitions
106
-
107
-
108
- class MetadataSerializer(BaseSerializer):
109
- def __init__(self, code_model: CodeModel, env: Environment, *, client_namespace: Optional[str] = None):
110
- super().__init__(code_model, env, client_namespace=client_namespace)
111
- self.client = self.code_model.clients[0] # we only do one client for multiapi
112
-
113
- def _choose_api_version(self) -> Tuple[str, List[str]]:
114
- chosen_version = ""
115
- total_api_version_set: Set[str] = set()
116
- for client in self.code_model.clients:
117
- for operation_group in client.operation_groups:
118
- total_api_version_set.update(operation_group.api_versions)
119
-
120
- total_api_version_list = list(total_api_version_set)
121
- total_api_version_list.sort()
122
-
123
- # switching ' to " so json can decode the dict we end up writing to file
124
- total_api_version_list = [str(api_version).replace("'", '"') for api_version in total_api_version_list]
125
- if len(total_api_version_list) == 1:
126
- chosen_version = total_api_version_list[0]
127
- elif len(total_api_version_list) > 1:
128
- module_version = self.code_model.namespace.split(".")[-1]
129
- for api_version in total_api_version_list:
130
- if "v{}".format(api_version.replace("-", "_")) == module_version:
131
- chosen_version = api_version
132
-
133
- return chosen_version, total_api_version_list
134
-
135
- def serialize(self) -> str:
136
- def _is_lro(operation):
137
- return isinstance(operation, LROOperation)
138
-
139
- def _is_paging(operation):
140
- return isinstance(operation, PagingOperation)
141
-
142
- mixin_operation_group: Optional[OperationGroup] = next(
143
- (
144
- operation_group
145
- for client in self.code_model.clients
146
- for operation_group in client.operation_groups
147
- if operation_group.is_mixin
148
- ),
149
- None,
150
- )
151
- mixin_operations = mixin_operation_group.operations if mixin_operation_group else []
152
- sync_mixin_imports, async_mixin_imports = _mixin_imports(mixin_operation_group)
153
- (
154
- sync_mixin_typing_definitions,
155
- async_mixin_typing_definitions,
156
- ) = _mixin_typing_definitions(mixin_operation_group)
157
-
158
- chosen_version, total_api_version_list = self._choose_api_version()
159
-
160
- # setting to true, because for multiapi we always generate with a version file with version 0.1.0
161
- self.code_model.options["package-version"] = "0.1.0"
162
- template = self.env.get_template("metadata.json.jinja2")
163
-
164
- client_serialize_namespace = self.code_model.get_serialize_namespace(self.client_namespace, async_mode=False)
165
- client_serialize_namespace_async = self.code_model.get_serialize_namespace(
166
- self.client_namespace, async_mode=True
167
- )
168
- return template.render(
169
- code_model=self.code_model,
170
- chosen_version=chosen_version,
171
- total_api_version_list=total_api_version_list,
172
- client=self.client,
173
- global_parameters=self.client.parameters,
174
- mixin_operations=mixin_operations,
175
- any=any,
176
- is_lro=_is_lro,
177
- is_paging=_is_paging,
178
- str=str,
179
- sync_mixin_imports=sync_mixin_imports,
180
- async_mixin_imports=async_mixin_imports,
181
- sync_mixin_typing_definitions=sync_mixin_typing_definitions,
182
- async_mixin_typing_definitions=async_mixin_typing_definitions,
183
- sync_client_imports=_json_serialize_imports(
184
- self.client.imports_for_multiapi(
185
- async_mode=False, serialize_namespace=client_serialize_namespace
186
- ).to_dict()
187
- ),
188
- async_client_imports=_json_serialize_imports(
189
- self.client.imports_for_multiapi(
190
- async_mode=True, serialize_namespace=client_serialize_namespace_async
191
- ).to_dict()
192
- ),
193
- sync_config_imports=_json_serialize_imports(
194
- self.client.config.imports_for_multiapi(
195
- async_mode=False, serialize_namespace=client_serialize_namespace
196
- ).to_dict()
197
- ),
198
- async_config_imports=_json_serialize_imports(
199
- self.client.config.imports_for_multiapi(
200
- async_mode=True, serialize_namespace=client_serialize_namespace_async
201
- ).to_dict()
202
- ),
203
- get_async_operation_serializer=functools.partial(
204
- get_operation_serializer,
205
- code_model=self.client.code_model,
206
- async_mode=True,
207
- client_namespace=self.client_namespace,
208
- ),
209
- get_sync_operation_serializer=functools.partial(
210
- get_operation_serializer,
211
- code_model=self.client.code_model,
212
- async_mode=False,
213
- client_namespace=self.client_namespace,
214
- ),
215
- has_credential=bool(self.client.credential),
216
- )
@@ -1,167 +0,0 @@
1
- {% import 'operation_tools.jinja2' as op_tools %}
2
- {% import 'keywords.jinja2' as keywords %}
3
- {
4
- "chosen_version": {{ chosen_version | tojson }},
5
- "total_api_version_list": {{ total_api_version_list | tojson }},
6
- "client": {
7
- "name": {{ client.name | tojson }},
8
- "filename": {{ ("_" + client.legacy_filename) | tojson }},
9
- "description": {{ client.description | tojson }},
10
- "host_value": {{ (client.parameters.host.client_default_value_declaration if not client.has_parameterized_host else None) | tojson }},
11
- "parameterized_host_template": {{ (keywords.escape_str(client.url) if client.has_parameterized_host else None) | tojson }},
12
- "azure_arm": {{ client.code_model.options["azure-arm"] | tojson }},
13
- "has_public_lro_operations": {{ client.has_public_lro_operations | tojson }},
14
- "client-side-validation": {{ client.code_model.options["client-side-validation"] | tojson }},
15
- "sync_imports": {{ sync_client_imports | tojson }},
16
- "async_imports": {{ async_client_imports | tojson }}
17
- },
18
- "global_parameters": {
19
- "sync": {
20
- {% for gp in global_parameters.method | rejectattr("client_name", "equalto", "api_version") | rejectattr("is_host") %}
21
- {{ gp.client_name | tojson }}: {
22
- "signature": {{ gp.method_signature(async_mode=False) | tojson }},
23
- "description": {{ gp.description | tojson }},
24
- "docstring_type": {{ gp.docstring_type(async_mode=False) | tojson }},
25
- "required": {{ (not gp.optional) | tojson }},
26
- "method_location": {{ gp.method_location | tojson }}
27
- }{{ "," if not loop.last else "" }}
28
- {% endfor %}
29
- },
30
- "async": {
31
- {% for gp in global_parameters.method | rejectattr("client_name", "equalto", "api_version") | rejectattr("is_host") %}
32
- {{ gp.client_name | tojson }}: {
33
- "signature": {{ (gp.method_signature(async_mode=True)) | tojson }},
34
- "description": {{ gp.description | tojson }},
35
- "docstring_type": {{ gp.docstring_type(async_mode=True) | tojson }},
36
- "required": {{ (not gp.optional) | tojson }}
37
- }{{ "," if not loop.last else "" }}
38
- {% endfor %}
39
- },
40
- "constant": {
41
- {% for gp in client.parameters.constant | rejectattr("client_name", "equalto", "api_version") %}
42
- {{ gp.client_name | tojson }}: {{ gp.constant_declaration | tojson }}{{ "," if not loop.last else "" }}
43
- {% endfor %}
44
- },
45
- "call": {{ client.parameters.method | rejectattr("client_name", "equalto", "api_version") | rejectattr("is_host") | map(attribute="client_name") | join(', ') | tojson }},
46
- "service_client_specific": {
47
- "sync": {
48
- "api_version": {
49
- "signature": "api_version: Optional[str]=None,",
50
- "description": "API version to use if no profile is provided, or if missing in profile.",
51
- "docstring_type": "str",
52
- "required": false,
53
- "method_location": "positional"
54
- },
55
- {% if not client.has_parameterized_host %}
56
- "base_url": {
57
- "signature": {{ client.parameters.host.method_signature(async_mode=False) | tojson }},
58
- "description": "Service URL",
59
- "docstring_type": "str",
60
- "required": false,
61
- "method_location": "positional"
62
- },
63
- {% endif %}
64
- "profile": {
65
- "signature": "profile: KnownProfiles=KnownProfiles.default,",
66
- "description": "A profile definition, from KnownProfiles to dict.",
67
- "docstring_type": "azure.profiles.KnownProfiles",
68
- "required": false,
69
- "method_location": "positional"
70
- }
71
- },
72
- "async": {
73
- "api_version": {
74
- "signature": "api_version: Optional[str] = None,",
75
- "description": "API version to use if no profile is provided, or if missing in profile.",
76
- "docstring_type": "str",
77
- "required": false,
78
- "method_location": "positional"
79
- },
80
- {% if not client.has_parameterized_host %}
81
- "base_url": {
82
- "signature": {{ client.parameters.host.method_signature(async_mode=True) | tojson }},
83
- "description": "Service URL",
84
- "docstring_type": "str",
85
- "required": false,
86
- "method_location": "positional"
87
- },
88
- {% endif %}
89
- "profile": {
90
- "signature": "profile: KnownProfiles = KnownProfiles.default,",
91
- "description": "A profile definition, from KnownProfiles to dict.",
92
- "docstring_type": "azure.profiles.KnownProfiles",
93
- "required": false,
94
- "method_location": "positional"
95
- }
96
- }
97
- }
98
- },
99
- "config": {
100
- "credential": {{ has_credential | tojson }},
101
- "credential_scopes": {{ (client.credential.type.policy.credential_scopes if has_credential and client.credential.type.policy.credential_scopes is defined else None)| tojson}},
102
- "credential_call_sync": {{ (client.credential.type.policy.call(async_mode=False) if has_credential else None) | tojson }},
103
- "credential_call_async": {{ (client.credential.type.policy.call(async_mode=True) if has_credential else None) | tojson }},
104
- "sync_imports": {{ sync_config_imports | tojson }},
105
- "async_imports": {{ async_config_imports | tojson }}
106
- },
107
- "operation_groups": {
108
- {% for operation_group in client.operation_groups | rejectattr('is_mixin') %}
109
- {{ operation_group.property_name | tojson }}: {{ operation_group.class_name | tojson }}{{ "," if not loop.last else "" }}
110
- {% endfor %}
111
- }{{ "," if mixin_operations }}
112
- {% if mixin_operations %}
113
- "operation_mixins": {
114
- "sync_imports": {{ str(sync_mixin_imports) | tojson }},
115
- "async_imports": {{ str(async_mixin_imports) | tojson }},
116
- "sync_mixin_typing_definitions": {{ str(sync_mixin_typing_definitions) | tojson }},
117
- "async_mixin_typing_definitions": {{ str(async_mixin_typing_definitions) | tojson }},
118
- "operations": {
119
- {% for operation in mixin_operations %}
120
- {{ operation.name | tojson }} : {
121
- {% set request_builder = operation.request_builder %}
122
- "sync": {
123
- {% set operation_serializer = get_sync_operation_serializer(operation) %}
124
- {% if is_lro(operation) and is_paging(operation) %}
125
- {% from "lro_paging_operation.py.jinja2" import operation_docstring with context %}
126
- {% set sync_return_type_wrapper = [operation.get_poller(async_mode=False), operation.get_pager(async_mode=False)] %}
127
- {% elif is_lro(operation) %}
128
- {% from "lro_operation.py.jinja2" import operation_docstring with context %}
129
- {% set sync_return_type_wrapper = [operation.get_poller(async_mode=False)] %}
130
- {% elif is_paging(operation) %}
131
- {% from "paging_operation.py.jinja2" import operation_docstring with context %}
132
- {% set sync_return_type_wrapper = [operation.get_pager(async_mode=False)] %}
133
- {% else %}
134
- {% from "operation.py.jinja2" import operation_docstring with context %}
135
- {% set sync_return_type_wrapper = "" %}
136
- {% endif %}
137
- "signature": {{ (operation_serializer.method_signature_and_response_type_annotation(operation, want_decorators=False) + "\n") | tojson }},
138
- "doc": {{ op_tools.description(operation, operation_serializer).rstrip("\n") | tojson }},
139
- "call": {{ operation.parameters.call | join(', ') | tojson }}
140
- },
141
- "async": {
142
- {% set coroutine = False if (is_paging(operation) and not is_lro(operation)) else True %}
143
- {% set operation_serializer = get_async_operation_serializer(operation) %}
144
- "coroutine": {{ coroutine | tojson }},
145
- {% if is_lro(operation) and is_paging(operation) %}
146
- {% from "lro_paging_operation.py.jinja2" import operation_docstring with context %}
147
- {% set async_return_type_wrapper = [operation.get_poller(async_mode=True), operation.get_pager(async_mode=True)] %}
148
- {% elif is_lro(operation) %}
149
- {% from "lro_operation.py.jinja2" import operation_docstring with context %}
150
- {% set async_return_type_wrapper = [operation.get_poller(async_mode=True)] %}
151
- {% elif is_paging(operation) %}
152
- {% from "paging_operation.py.jinja2" import operation_docstring with context %}
153
- {% set async_return_type_wrapper = [operation.get_pager(async_mode=True)] %}
154
- {% else %}
155
- {% from "operation.py.jinja2" import operation_docstring with context %}
156
- {% set async_return_type_wrapper = "" %}
157
- {% endif %}
158
- "signature": {{ (operation_serializer.method_signature_and_response_type_annotation(operation, want_decorators=False) + "\n") | tojson }},
159
- "doc": {{ op_tools.description(operation, operation_serializer).rstrip("\n") | tojson }},
160
- "call": {{ operation.parameters.call | join(', ') | tojson }}
161
- }
162
- }{{ "," if not loop.last else "" }}
163
- {% endfor %}
164
- }
165
- }
166
- {% endif %}
167
- }
@@ -1,216 +0,0 @@
1
- # -------------------------------------------------------------------------
2
- # Copyright (c) Microsoft Corporation. All rights reserved.
3
- # Licensed under the MIT License. See License.txt in the project root for
4
- # license information.
5
- # --------------------------------------------------------------------------
6
- import functools
7
- import json
8
- from typing import List, Optional, Set, Tuple, Dict, Union, Any
9
- from jinja2 import Environment
10
- from ..models import (
11
- OperationGroup,
12
- LROOperation,
13
- PagingOperation,
14
- TypingSection,
15
- ImportType,
16
- CodeModel,
17
- )
18
- from .builder_serializer import get_operation_serializer
19
- from .base_serializer import BaseSerializer
20
- from .import_serializer import FileImportSerializer
21
-
22
-
23
- def _to_string(data: Union[Tuple[Any], List[Any], str]) -> str:
24
- if isinstance(data, (list, tuple)):
25
- return "".join([_to_string(item) for item in data])
26
- return str(data)
27
-
28
-
29
- def _json_serialize_imports(
30
- imports: Dict[
31
- TypingSection,
32
- Dict[
33
- ImportType,
34
- Dict[
35
- str,
36
- Set[
37
- Optional[
38
- Union[
39
- str,
40
- Tuple[str, str],
41
- Tuple[
42
- str,
43
- Optional[str],
44
- Tuple[Tuple[Tuple[int, int], str, Optional[str]]],
45
- ],
46
- ]
47
- ]
48
- ],
49
- ],
50
- ],
51
- ],
52
- ) -> str:
53
- if not imports:
54
- return ""
55
-
56
- json_serialize_imports = {}
57
- # need to make name_import set -> list to make the dictionary json serializable
58
- # not using an OrderedDict since we're iterating through a set and the order there varies
59
- # going to sort the list instead
60
-
61
- for typing_section_key, typing_section_value in imports.items():
62
- json_import_type_dictionary = {}
63
- for import_type_key, import_type_value in typing_section_value.items():
64
- json_package_name_dictionary = {}
65
- for package_name, name_imports in import_type_value.items():
66
- name_import_ordered_list = []
67
- if name_imports:
68
- name_import_ordered_list = list(name_imports)
69
- name_import_ordered_list.sort(
70
- key=lambda e: (
71
- _to_string(e) # type: ignore
72
- if isinstance(e, (list, tuple))
73
- else e if isinstance(e, str) else ""
74
- )
75
- )
76
- json_package_name_dictionary[package_name] = name_import_ordered_list
77
- json_import_type_dictionary[import_type_key] = json_package_name_dictionary
78
- json_serialize_imports[typing_section_key] = json_import_type_dictionary
79
- return json.dumps(json_serialize_imports)
80
-
81
-
82
- def _mixin_imports(
83
- mixin_operation_group: Optional[OperationGroup],
84
- ) -> Tuple[Optional[str], Optional[str]]:
85
- if not mixin_operation_group:
86
- return None, None
87
-
88
- sync_mixin_imports = mixin_operation_group.imports_for_multiapi(async_mode=False)
89
- async_mixin_imports = mixin_operation_group.imports_for_multiapi(async_mode=True)
90
-
91
- return _json_serialize_imports(sync_mixin_imports.to_dict()), _json_serialize_imports(async_mixin_imports.to_dict())
92
-
93
-
94
- def _mixin_typing_definitions(
95
- mixin_operation_group: Optional[OperationGroup],
96
- ) -> Tuple[Optional[str], Optional[str]]:
97
- if not mixin_operation_group:
98
- return None, None
99
-
100
- sync_mixin_imports = mixin_operation_group.imports_for_multiapi(async_mode=False)
101
- async_mixin_imports = mixin_operation_group.imports_for_multiapi(async_mode=True)
102
- sync_mixin_typing_definitions = FileImportSerializer(sync_mixin_imports, False).get_typing_definitions()
103
- async_mixin_typing_definitions = FileImportSerializer(async_mixin_imports, True).get_typing_definitions()
104
-
105
- return sync_mixin_typing_definitions, async_mixin_typing_definitions
106
-
107
-
108
- class MetadataSerializer(BaseSerializer):
109
- def __init__(self, code_model: CodeModel, env: Environment, *, client_namespace: Optional[str] = None):
110
- super().__init__(code_model, env, client_namespace=client_namespace)
111
- self.client = self.code_model.clients[0] # we only do one client for multiapi
112
-
113
- def _choose_api_version(self) -> Tuple[str, List[str]]:
114
- chosen_version = ""
115
- total_api_version_set: Set[str] = set()
116
- for client in self.code_model.clients:
117
- for operation_group in client.operation_groups:
118
- total_api_version_set.update(operation_group.api_versions)
119
-
120
- total_api_version_list = list(total_api_version_set)
121
- total_api_version_list.sort()
122
-
123
- # switching ' to " so json can decode the dict we end up writing to file
124
- total_api_version_list = [str(api_version).replace("'", '"') for api_version in total_api_version_list]
125
- if len(total_api_version_list) == 1:
126
- chosen_version = total_api_version_list[0]
127
- elif len(total_api_version_list) > 1:
128
- module_version = self.code_model.namespace.split(".")[-1]
129
- for api_version in total_api_version_list:
130
- if "v{}".format(api_version.replace("-", "_")) == module_version:
131
- chosen_version = api_version
132
-
133
- return chosen_version, total_api_version_list
134
-
135
- def serialize(self) -> str:
136
- def _is_lro(operation):
137
- return isinstance(operation, LROOperation)
138
-
139
- def _is_paging(operation):
140
- return isinstance(operation, PagingOperation)
141
-
142
- mixin_operation_group: Optional[OperationGroup] = next(
143
- (
144
- operation_group
145
- for client in self.code_model.clients
146
- for operation_group in client.operation_groups
147
- if operation_group.is_mixin
148
- ),
149
- None,
150
- )
151
- mixin_operations = mixin_operation_group.operations if mixin_operation_group else []
152
- sync_mixin_imports, async_mixin_imports = _mixin_imports(mixin_operation_group)
153
- (
154
- sync_mixin_typing_definitions,
155
- async_mixin_typing_definitions,
156
- ) = _mixin_typing_definitions(mixin_operation_group)
157
-
158
- chosen_version, total_api_version_list = self._choose_api_version()
159
-
160
- # setting to true, because for multiapi we always generate with a version file with version 0.1.0
161
- self.code_model.options["package-version"] = "0.1.0"
162
- template = self.env.get_template("metadata.json.jinja2")
163
-
164
- client_serialize_namespace = self.code_model.get_serialize_namespace(self.client_namespace, async_mode=False)
165
- client_serialize_namespace_async = self.code_model.get_serialize_namespace(
166
- self.client_namespace, async_mode=True
167
- )
168
- return template.render(
169
- code_model=self.code_model,
170
- chosen_version=chosen_version,
171
- total_api_version_list=total_api_version_list,
172
- client=self.client,
173
- global_parameters=self.client.parameters,
174
- mixin_operations=mixin_operations,
175
- any=any,
176
- is_lro=_is_lro,
177
- is_paging=_is_paging,
178
- str=str,
179
- sync_mixin_imports=sync_mixin_imports,
180
- async_mixin_imports=async_mixin_imports,
181
- sync_mixin_typing_definitions=sync_mixin_typing_definitions,
182
- async_mixin_typing_definitions=async_mixin_typing_definitions,
183
- sync_client_imports=_json_serialize_imports(
184
- self.client.imports_for_multiapi(
185
- async_mode=False, serialize_namespace=client_serialize_namespace
186
- ).to_dict()
187
- ),
188
- async_client_imports=_json_serialize_imports(
189
- self.client.imports_for_multiapi(
190
- async_mode=True, serialize_namespace=client_serialize_namespace_async
191
- ).to_dict()
192
- ),
193
- sync_config_imports=_json_serialize_imports(
194
- self.client.config.imports_for_multiapi(
195
- async_mode=False, serialize_namespace=client_serialize_namespace
196
- ).to_dict()
197
- ),
198
- async_config_imports=_json_serialize_imports(
199
- self.client.config.imports_for_multiapi(
200
- async_mode=True, serialize_namespace=client_serialize_namespace_async
201
- ).to_dict()
202
- ),
203
- get_async_operation_serializer=functools.partial(
204
- get_operation_serializer,
205
- code_model=self.client.code_model,
206
- async_mode=True,
207
- client_namespace=self.client_namespace,
208
- ),
209
- get_sync_operation_serializer=functools.partial(
210
- get_operation_serializer,
211
- code_model=self.client.code_model,
212
- async_mode=False,
213
- client_namespace=self.client_namespace,
214
- ),
215
- has_credential=bool(self.client.credential),
216
- )