@autorest/python 5.10.0 → 5.12.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 (57) hide show
  1. package/ChangeLog.md +74 -0
  2. package/autorest/codegen/__init__.py +7 -7
  3. package/autorest/codegen/models/__init__.py +2 -1
  4. package/autorest/codegen/models/base_builder.py +16 -8
  5. package/autorest/codegen/models/client.py +5 -3
  6. package/autorest/codegen/models/code_model.py +19 -5
  7. package/autorest/codegen/models/imports.py +7 -0
  8. package/autorest/codegen/models/lro_operation.py +7 -3
  9. package/autorest/codegen/models/object_schema.py +3 -3
  10. package/autorest/codegen/models/operation.py +69 -38
  11. package/autorest/codegen/models/operation_group.py +13 -8
  12. package/autorest/codegen/models/paging_operation.py +5 -2
  13. package/autorest/codegen/models/parameter.py +46 -13
  14. package/autorest/codegen/models/parameter_list.py +62 -70
  15. package/autorest/codegen/models/primitive_schemas.py +11 -0
  16. package/autorest/codegen/models/request_builder.py +21 -8
  17. package/autorest/codegen/models/request_builder_parameter.py +9 -5
  18. package/autorest/codegen/models/request_builder_parameter_list.py +179 -77
  19. package/autorest/codegen/models/rest.py +3 -2
  20. package/autorest/codegen/models/schema_request.py +4 -17
  21. package/autorest/codegen/models/schema_response.py +4 -4
  22. package/autorest/codegen/serializers/__init__.py +57 -53
  23. package/autorest/codegen/serializers/builder_serializer.py +76 -46
  24. package/autorest/codegen/serializers/client_serializer.py +37 -9
  25. package/autorest/codegen/serializers/general_serializer.py +7 -5
  26. package/autorest/codegen/serializers/import_serializer.py +22 -7
  27. package/autorest/codegen/serializers/metadata_serializer.py +2 -2
  28. package/autorest/codegen/serializers/model_base_serializer.py +3 -3
  29. package/autorest/codegen/serializers/model_generic_serializer.py +1 -1
  30. package/autorest/codegen/serializers/model_python3_serializer.py +1 -1
  31. package/autorest/codegen/serializers/operation_groups_serializer.py +70 -0
  32. package/autorest/codegen/serializers/operations_init_serializer.py +34 -2
  33. package/autorest/codegen/serializers/patch_serializer.py +15 -0
  34. package/autorest/codegen/serializers/rest_serializer.py +9 -4
  35. package/autorest/codegen/serializers/utils.py +2 -2
  36. package/autorest/codegen/templates/config.py.jinja2 +1 -11
  37. package/autorest/codegen/templates/init.py.jinja2 +4 -7
  38. package/autorest/codegen/templates/metadata.json.jinja2 +2 -2
  39. package/autorest/codegen/templates/model_init.py.jinja2 +1 -1
  40. package/autorest/codegen/templates/{operations_class.py.jinja2 → operation_group.py.jinja2} +2 -0
  41. package/autorest/codegen/templates/operation_groups_container.py.jinja2 +26 -0
  42. package/autorest/codegen/templates/operation_tools.jinja2 +7 -0
  43. package/autorest/codegen/templates/operations_folder_init.py.jinja2 +13 -0
  44. package/autorest/codegen/templates/patch.py.jinja2 +31 -0
  45. package/autorest/codegen/templates/request_builder.py.jinja2 +7 -2
  46. package/autorest/codegen/templates/request_builders.py.jinja2 +3 -3
  47. package/autorest/codegen/templates/setup.py.jinja2 +1 -1
  48. package/autorest/multiapi/serializers/__init__.py +3 -3
  49. package/autorest/multiapi/serializers/import_serializer.py +5 -5
  50. package/autorest/namer/name_converter.py +48 -3
  51. package/package.json +2 -2
  52. package/setup.py +1 -0
  53. package/autorest/codegen/serializers/operation_group_serializer.py +0 -71
  54. package/autorest/codegen/templates/operations_class_mixin.py.jinja2 +0 -16
  55. package/autorest/codegen/templates/operations_container.py.jinja2 +0 -42
  56. package/autorest/codegen/templates/operations_container_init.py.jinja2 +0 -24
  57. package/autorest/codegen/templates/operations_container_mixin.py.jinja2 +0 -23
@@ -3,7 +3,10 @@
3
3
  # Licensed under the MIT License. See License.txt in the project root for
4
4
  # license information.
5
5
  # --------------------------------------------------------------------------
6
+ from typing import List
6
7
  from jinja2 import Environment
8
+
9
+ from autorest.codegen.models.operation_group import OperationGroup
7
10
  from ..models import CodeModel
8
11
 
9
12
 
@@ -13,9 +16,38 @@ class OperationsInitSerializer:
13
16
  self.env = env
14
17
  self.async_mode = async_mode
15
18
 
19
+ def _operation_group_imports_helper(self, filename_suffix: str = "") -> List[str]:
20
+ def _get_filename(operation_group: OperationGroup) -> str:
21
+ prefix = "_operations" if self.code_model.options["combine_operation_files"] else operation_group.filename
22
+ return prefix + filename_suffix
23
+ return [
24
+ f"from .{_get_filename(og)} import {og.class_name}"
25
+ for og in self.code_model.operation_groups
26
+ ]
27
+
28
+ def operation_group_imports(self) -> List[str]:
29
+ typed_py3_files = self.code_model.options["add_python3_operation_files"]
30
+ py3_only = self.code_model.options["python3_only"]
31
+ if typed_py3_files and not py3_only and not self.async_mode:
32
+ retval: List[str] = ["try:"]
33
+ retval.extend([
34
+ f" {line}"
35
+ for line in self._operation_group_imports_helper(filename_suffix="_py3")
36
+ ])
37
+ retval.append("except (SyntaxError, ImportError):")
38
+ retval.extend([
39
+ f" {line}"
40
+ for line in self._operation_group_imports_helper()
41
+ ])
42
+ return retval
43
+ return self._operation_group_imports_helper()
44
+
16
45
  def serialize(self) -> str:
17
- operation_group_init_template = self.env.get_template("operations_container_init.py.jinja2")
46
+ operation_group_init_template = self.env.get_template("operations_folder_init.py.jinja2")
18
47
 
19
48
  return operation_group_init_template.render(
20
- code_model=self.code_model, operation_groups=self.code_model.operation_groups, async_mode=self.async_mode
49
+ code_model=self.code_model,
50
+ operation_groups=self.code_model.operation_groups,
51
+ async_mode=self.async_mode,
52
+ operation_group_imports=self.operation_group_imports,
21
53
  )
@@ -0,0 +1,15 @@
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
+ from jinja2 import Environment
7
+
8
+
9
+ class PatchSerializer:
10
+ def __init__(self, env: Environment) -> None:
11
+ self.env = env
12
+
13
+ def serialize(self) -> str:
14
+ template = self.env.get_template("patch.py.jinja2")
15
+ return template.render()
@@ -20,6 +20,7 @@ class RestSerializer:
20
20
  self.code_model = code_model
21
21
  self.env = env
22
22
  self.request_builders = request_builders
23
+ self.builder_group_name = request_builders[0].builder_group_name
23
24
 
24
25
  def serialize_init(self) -> str:
25
26
  template = self.env.get_template("rest_init.py.jinja2")
@@ -33,8 +34,10 @@ class RestPython3Serializer(RestSerializer):
33
34
  return template.render(
34
35
  code_model=self.code_model,
35
36
  request_builders=self.request_builders,
36
- imports=FileImportSerializer(self.code_model.rest.imports(), is_python_3_file=True),
37
- is_python_3_file=True,
37
+ imports=FileImportSerializer(self.code_model.rest.imports(
38
+ self.builder_group_name
39
+ ), is_python3_file=True),
40
+ is_python3_file=True,
38
41
  request_builder_serializer=RequestBuilderPython3Serializer(self.code_model),
39
42
  )
40
43
 
@@ -46,7 +49,9 @@ class RestGenericSerializer(RestSerializer):
46
49
  return template.render(
47
50
  code_model=self.code_model,
48
51
  request_builders=self.request_builders,
49
- imports=FileImportSerializer(self.code_model.rest.imports(), is_python_3_file=False),
50
- is_python_3_file=False,
52
+ imports=FileImportSerializer(self.code_model.rest.imports(
53
+ self.builder_group_name
54
+ ), is_python3_file=False),
55
+ is_python3_file=False,
51
56
  request_builder_serializer=RequestBuilderGenericSerializer(self.code_model),
52
57
  )
@@ -90,11 +90,11 @@ def serialize_path(
90
90
 
91
91
  def method_signature_and_response_type_annotation_template(
92
92
  *,
93
- is_python_3_file: bool,
93
+ is_python3_file: bool,
94
94
  method_signature: str,
95
95
  response_type_annotation: str,
96
96
  ) -> str:
97
- if is_python_3_file:
97
+ if is_python3_file:
98
98
  return f"{method_signature} -> {response_type_annotation}:"
99
99
  return f"{method_signature}:\n # type: (...) -> {response_type_annotation}"
100
100
 
@@ -1,13 +1,6 @@
1
1
  {% import 'keywords.jinja2' as keywords with context %}
2
2
  {% import 'operation_tools.jinja2' as op_tools %}
3
3
  {% set version_import = ".._version" if async_mode else "._version" %}
4
- {% macro method_signature() %}
5
- def __init__(
6
- self,
7
- {% for param_signature in code_model.global_parameters.config_method_signature(async_mode) %}
8
- {{ param_signature }}
9
- {% endfor %}
10
- ){{" -> None" if async_mode else "" }}:{% endmacro %}
11
4
  {# actual template starts here #}
12
5
  # coding=utf-8
13
6
  {{ code_model.options['license_header'] }}
@@ -32,10 +25,7 @@ class {{ code_model.class_name }}Configuration(Configuration):
32
25
  {% endfor %}
33
26
  """
34
27
 
35
- {{ method_signature()|indent }}
36
- {% if not async_mode %}
37
- # type: (...) -> None
38
- {% endif %}
28
+ {{ serializer.init_signature_and_response_type_annotation(async_mode) | indent }}
39
29
  super({{ code_model.class_name }}Configuration, self).__init__(**kwargs)
40
30
  {% if code_model.service_client.parameters.config_kwargs_to_pop(async_mode) %}
41
31
  {{ op_tools.serialize(serializer.pop_kwargs_from_signature(async_mode)) | indent(8) }}
@@ -8,11 +8,8 @@ from ._version import VERSION
8
8
  __version__ = VERSION
9
9
  {% endif %}
10
10
  __all__ = ['{{ code_model.class_name }}']
11
- {% if not async_mode %}
12
11
 
13
- try:
14
- from ._patch import patch_sdk # type: ignore
15
- patch_sdk()
16
- except ImportError:
17
- pass
18
- {% endif %}
12
+ # `._patch.py` is used for handwritten extensions to the generated code
13
+ # Example: https://github.com/Azure/azure-sdk-for-python/blob/main/doc/dev/customize_code/how-to-patch-sdk-code.md
14
+ from ._patch import patch_sdk
15
+ patch_sdk()
@@ -20,7 +20,7 @@
20
20
  "sync": {
21
21
  {% for gp in sync_global_parameters.config_method %}
22
22
  {{ gp.serialized_name | tojson }}: {
23
- "signature": {{ gp.method_signature(is_python_3_file=False) | tojson }},
23
+ "signature": {{ gp.method_signature(is_python3_file=False) | tojson }},
24
24
  "description": {{ gp.description | tojson }},
25
25
  "docstring_type": {{ gp.docstring_type | tojson }},
26
26
  "required": {{ gp.required | tojson }}
@@ -30,7 +30,7 @@
30
30
  "async": {
31
31
  {% for gp in async_global_parameters.config_method %}
32
32
  {{ gp.serialized_name | tojson }}: {
33
- "signature": {{ (gp.method_signature(is_python_3_file=True)) | tojson }},
33
+ "signature": {{ (gp.method_signature(is_python3_file=True)) | tojson }},
34
34
  "description": {{ gp.description | tojson }},
35
35
  "docstring_type": {{ gp.docstring_type | tojson }},
36
36
  "required": {{ gp.required | tojson }}
@@ -7,7 +7,7 @@ from ._models_py3 import {{ schema }}
7
7
  {% endmacro %}
8
8
 
9
9
  {% if schemas %}
10
- {% if code_model.options["python_3_only"] %}
10
+ {% if code_model.options["python3_only"] %}
11
11
  {{ iterate_models_py3() }}
12
12
  {% else %}
13
13
  try:
@@ -1,4 +1,5 @@
1
1
  class {{ operation_group.class_name }}{{ object_base_class }}:
2
+ {% if not operation_group.is_empty_operation_group %}
2
3
  """{{ operation_group.class_name }} {{ operations_description }}.
3
4
 
4
5
  You should not instantiate this class directly. Instead, you should create a Client instance that
@@ -23,6 +24,7 @@ class {{ operation_group.class_name }}{{ object_base_class }}:
23
24
  self._serialize = serializer
24
25
  self._deserialize = deserializer
25
26
  self._config = config
27
+ {% endif %}
26
28
  {% for operation in operation_group.operations %}
27
29
 
28
30
  {% set request_builder = operation.request_builder %}
@@ -0,0 +1,26 @@
1
+ {% import 'operation_tools.jinja2' as op_tools %}
2
+ {% set object_base_class = "" if async_mode else "(object)" %}
3
+ {% set operations_description = "async operations" if async_mode else "operations" %}
4
+ {% set return_none_type_annotation = " -> None" if async_mode else "" %}
5
+ # coding=utf-8
6
+ {{ code_model.options['license_header'] }}
7
+ {{ imports }}
8
+
9
+ {% if code_model.options["builders_visibility"] == "embedded" and not async_mode %}
10
+ {{ op_tools.declare_serializer(code_model) }}
11
+ {%- if not is_python3_file %}
12
+ # fmt: off
13
+ {% endif %}
14
+ {% for operation_group in operation_groups %}
15
+ {% for request_builder in code_model.rest.request_builders | selectattr("operation_group_name", "equalto", operation_group.name) %}
16
+
17
+ {% include "request_builder.py.jinja2" %}
18
+ {% endfor %}
19
+ {% endfor %}
20
+ {% if not is_python3_file %}
21
+ # fmt: on
22
+ {% endif %}
23
+ {% endif %}
24
+ {% for operation_group in operation_groups %}
25
+ {% include "operation_group.py.jinja2" %}
26
+ {% endfor %}
@@ -48,3 +48,10 @@ Example:
48
48
 
49
49
  {% endif %}
50
50
  {% endfor %}{% endmacro %}
51
+
52
+ {% macro declare_serializer(code_model) %}
53
+ _SERIALIZER = Serializer()
54
+ {% if not code_model.options["client_side_validation"] %}
55
+ _SERIALIZER.client_side_validation = False
56
+ {% endif %}
57
+ {% endmacro %}
@@ -0,0 +1,13 @@
1
+ {% import 'operation_tools.jinja2' as op_tools %}
2
+ {# actual template starts here #}
3
+ # coding=utf-8
4
+ {{ code_model.options['license_header'] }}
5
+
6
+ {{ op_tools.serialize(operation_group_imports()) }}
7
+ {% if operation_groups %}
8
+ __all__ = [
9
+ {% for operation_group in operation_groups %}
10
+ '{{ operation_group.class_name }}',
11
+ {% endfor %}
12
+ ]
13
+ {% endif %}
@@ -0,0 +1,31 @@
1
+ # coding=utf-8
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
+ # This file is used for handwritten extensions to the generated code. Example:
29
+ # https://github.com/Azure/azure-sdk-for-python/blob/main/doc/dev/customize_code/how-to-patch-sdk-code.md
30
+ def patch_sdk():
31
+ pass
@@ -4,7 +4,7 @@
4
4
  {% if code_model.options["builders_visibility"] == "public" %}
5
5
  {{ op_tools.description(request_builder, request_builder_serializer) | indent }}
6
6
  {% endif %}
7
- {% if request_builder.parameters.kwargs_to_pop(is_python_3_file) %}
7
+ {% if request_builder.parameters.kwargs_to_pop(is_python3_file) %}
8
8
  {{ op_tools.serialize(request_builder_serializer.pop_kwargs_from_signature(request_builder)) | indent }}
9
9
  {% endif %}
10
10
  {% if request_builder.parameters.constant|selectattr("original_parameter", "equalto", None)|selectattr("in_method_code")|selectattr("in_method_signature", "equalto", False) %}
@@ -13,7 +13,12 @@
13
13
  {% endfor %}
14
14
  {% endif %}
15
15
  # Construct URL
16
- url = kwargs.pop("template_url", {{ keywords.escape_str(request_builder.url) }})
16
+ {% if code_model.options["version_tolerant"] or code_model.options["low_level_client"] %}
17
+ {% set url_value = keywords.escape_str(request_builder.url) %}
18
+ {% else %}
19
+ {% set url_value = 'kwargs.pop("template_url", ' + keywords.escape_str(request_builder.url) + ')' %}
20
+ {% endif %}
21
+ url = {{ url_value }}
17
22
  {% if request_builder.parameters.path %}
18
23
  {{ op_tools.serialize(request_builder_serializer.serialize_path(request_builder)) | indent }}
19
24
  url = _format_url_section(url, **path_format_arguments)
@@ -1,10 +1,10 @@
1
+ {% import 'operation_tools.jinja2' as op_tools %}
1
2
  # coding=utf-8
2
3
  {{ code_model.options['license_header'] }}
3
4
  {{ imports }}
4
5
 
5
- _SERIALIZER = Serializer()
6
-
7
- {% if not is_python_3_file %}
6
+ {{ op_tools.declare_serializer(code_model) }}
7
+ {% if not is_python3_file %}
8
8
  # fmt: off
9
9
  {% endif %}
10
10
  {% for request_builder in request_builders %}
@@ -16,7 +16,7 @@ VERSION = "{{ code_model.options.get('package_version', '0.0.0') }}"
16
16
  # prerequisite: setuptools
17
17
  # http://pypi.python.org/pypi/setuptools
18
18
 
19
- REQUIRES = ["msrest>=0.6.21", "azure-core<2.0.0,>=1.19.1"{{ azure_mgmt_core_import }}]
19
+ REQUIRES = ["msrest>=0.6.21", "azure-core<2.0.0,>=1.20.1"{{ azure_mgmt_core_import }}]
20
20
 
21
21
  setup(
22
22
  name=NAME,
@@ -55,7 +55,7 @@ class MultiAPISerializer(object):
55
55
  # serialize service client file
56
56
  imports = FileImportSerializer(
57
57
  code_model.service_client.imports(async_mode),
58
- is_python_3_file=async_mode
58
+ is_python3_file=async_mode
59
59
  )
60
60
  self._autorestapi.write_file(
61
61
  _get_file_path(code_model.service_client.filename, async_mode),
@@ -65,7 +65,7 @@ class MultiAPISerializer(object):
65
65
  # serialize config file
66
66
  imports = FileImportSerializer(
67
67
  code_model.config.imports(async_mode),
68
- is_python_3_file=async_mode
68
+ is_python3_file=async_mode
69
69
  )
70
70
  self._autorestapi.write_file(
71
71
  _get_file_path("_configuration", async_mode),
@@ -76,7 +76,7 @@ class MultiAPISerializer(object):
76
76
  if code_model.operation_mixin_group.mixin_operations:
77
77
  imports = FileImportSerializer(
78
78
  code_model.operation_mixin_group.imports(async_mode),
79
- is_python_3_file=async_mode
79
+ is_python3_file=async_mode
80
80
  )
81
81
  self._autorestapi.write_file(
82
82
  _get_file_path("_operations_mixin", async_mode),
@@ -36,9 +36,9 @@ def _get_import_clauses(imports: Dict[ImportType, Dict[str, Set[Optional[str]]]]
36
36
 
37
37
 
38
38
  class FileImportSerializer:
39
- def __init__(self, file_import: FileImport, is_python_3_file: bool) -> None:
39
+ def __init__(self, file_import: FileImport, is_python3_file: bool) -> None:
40
40
  self._file_import = file_import
41
- self.is_python_3_file = is_python_3_file
41
+ self.is_python3_file = is_python3_file
42
42
 
43
43
  def _switch_typing_section_key(self, new_key: TypingSection):
44
44
  switched_dictionary = {}
@@ -60,7 +60,7 @@ class FileImportSerializer:
60
60
  def _add_type_checking_import(self):
61
61
  if (
62
62
  self._file_import.imports.get(TypingSection.TYPING) or
63
- (not self.is_python_3_file and self._file_import.imports.get(TypingSection.CONDITIONAL))
63
+ (not self.is_python3_file and self._file_import.imports.get(TypingSection.CONDITIONAL))
64
64
  ):
65
65
  self._file_import.add_from_import("typing", "TYPE_CHECKING", ImportType.STDLIB)
66
66
 
@@ -68,7 +68,7 @@ class FileImportSerializer:
68
68
  self._add_type_checking_import()
69
69
  regular_imports = ""
70
70
  regular_imports_dict = self._get_imports_dict(
71
- baseline_typing_section=TypingSection.REGULAR, add_conditional_typing=self.is_python_3_file
71
+ baseline_typing_section=TypingSection.REGULAR, add_conditional_typing=self.is_python3_file
72
72
  )
73
73
 
74
74
  if regular_imports_dict:
@@ -78,7 +78,7 @@ class FileImportSerializer:
78
78
 
79
79
  typing_imports = ""
80
80
  typing_imports_dict = self._get_imports_dict(
81
- baseline_typing_section=TypingSection.TYPING, add_conditional_typing=not self.is_python_3_file
81
+ baseline_typing_section=TypingSection.TYPING, add_conditional_typing=not self.is_python3_file
82
82
  )
83
83
  if typing_imports_dict:
84
84
  typing_imports += "\n\nif TYPE_CHECKING:\n # pylint: disable=unused-import,ungrouped-imports\n "
@@ -8,6 +8,35 @@ import copy
8
8
  from typing import cast, Any, Dict, List, Match, Optional
9
9
  from .python_mappings import basic_latin_chars, reserved_words, PadType
10
10
 
11
+ def _get_all_values(all_headers: List[Dict[str, Any]]) -> List[str]:
12
+ content_types: List[str] = []
13
+ for h in all_headers:
14
+ if h['schema']['type'] == 'constant':
15
+ content_types.append(h['schema']['value']['value'])
16
+ elif any(
17
+ choice_type for choice_type in ['sealed-choice', 'choice']
18
+ if h['schema']['type'] == choice_type
19
+ ):
20
+ content_types.extend([
21
+ choice['value']
22
+ for choice in h['schema']['choices']
23
+ ])
24
+ return content_types
25
+
26
+ def _get_default_value(all_values: List[str]) -> str:
27
+ json_values = [v for v in all_values if "json" in v]
28
+ if json_values:
29
+ if "application/json" in json_values:
30
+ return "application/json"
31
+ return json_values[0]
32
+
33
+ xml_values = [v for v in all_values if "xml" in v]
34
+ if xml_values:
35
+ if "application/xml" in xml_values:
36
+ return "application/xml"
37
+ return xml_values[0]
38
+ return all_values[0]
39
+
11
40
  _M4_HEADER_PARAMETERS = ["content_type", "accept"]
12
41
  class NameConverter:
13
42
  @staticmethod
@@ -42,6 +71,7 @@ class NameConverter:
42
71
  operation_group_name = operation_group['language']['default']['name']
43
72
  if not operation_group_name:
44
73
  operation_group['language']['python']['className'] = code_model_title + "OperationsMixin"
74
+ operation_group['language']['python']['name'] = ""
45
75
  elif operation_group_name == 'Operations':
46
76
  operation_group['language']['python']['className'] = operation_group_name
47
77
  else:
@@ -105,6 +135,7 @@ class NameConverter:
105
135
  id(p) for p in params_of_header[1:]
106
136
  ])
107
137
  else:
138
+ all_values = _get_all_values(params_of_header)
108
139
  # if one of them is an enum schema, set the default value to constant
109
140
  param_with_constant_schema = next(p for p in params_of_header if p['schema']['type'] == 'constant')
110
141
  try:
@@ -116,9 +147,23 @@ class NameConverter:
116
147
  # this means there's no enum schema
117
148
  pass
118
149
  else:
119
- param_with_enum_schema['clientDefaultValue'] = (
120
- param_with_constant_schema['schema']['value']['value']
121
- )
150
+ param_with_enum_schema['clientDefaultValue'] = _get_default_value(all_values)
151
+ # add constant enum schema value into list of possible schema values
152
+ constant_schema = param_with_constant_schema['schema']
153
+ constant_choice = {
154
+ "language": {
155
+ "default": {
156
+ "description": constant_schema['language']['default']['description'],
157
+ "name": constant_schema['language']['default']['name'],
158
+ },
159
+ "python": {
160
+ "description": constant_schema['language']['python']['description'],
161
+ "name": constant_schema['language']['python']['name'].upper(),
162
+ }
163
+ },
164
+ "value": constant_schema['value']['value']
165
+ }
166
+ param_with_enum_schema['schema']['choices'].append(constant_choice)
122
167
  m4_header_params_to_remove.append(id(param_with_constant_schema))
123
168
 
124
169
  for request in requests:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autorest/python",
3
- "version": "5.10.0",
3
+ "version": "5.12.0",
4
4
  "description": "The Python extension for generators in AutoRest.",
5
5
  "scripts": {
6
6
  "prepare": "node run-python3.js prepare.py",
@@ -27,7 +27,7 @@
27
27
  "@azure-tools/extension": "~3.2.1"
28
28
  },
29
29
  "devDependencies": {
30
- "@microsoft.azure/autorest.testserver": "^3.1.5"
30
+ "@microsoft.azure/autorest.testserver": "^3.1.8"
31
31
  },
32
32
  "files": [
33
33
  "autorest/**/*.py",
package/setup.py CHANGED
@@ -48,6 +48,7 @@ setup(
48
48
  "json-rpc",
49
49
  "Jinja2 >= 2.11", # I need "include" and auto-context + blank line are not indented by default
50
50
  "pyyaml",
51
+ "mistune < 2.0.0",
51
52
  "m2r",
52
53
  "black",
53
54
  ],
@@ -1,71 +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
- from copy import copy
8
- from typing import List
9
- from jinja2 import Environment
10
-
11
- from .import_serializer import FileImportSerializer
12
- from ..models import LROOperation, PagingOperation, CodeModel, OperationGroup
13
- from .builder_serializer import get_operation_serializer, get_request_builder_serializer
14
-
15
-
16
- class OperationGroupSerializer:
17
- def __init__(
18
- self,
19
- code_model: CodeModel,
20
- env: Environment,
21
- operation_groups: List[OperationGroup],
22
- async_mode: bool,
23
- is_python_3_file: bool,
24
- ) -> None:
25
- self.code_model = code_model
26
- self.env = env
27
- self.operation_groups = operation_groups
28
- self.async_mode = async_mode
29
- self.is_python_3_file = is_python_3_file
30
-
31
- def serialize(self) -> str:
32
- def _is_lro(operation):
33
- return isinstance(operation, LROOperation)
34
-
35
- def _is_paging(operation):
36
- return isinstance(operation, PagingOperation)
37
-
38
- operation_group_template = self.env.get_template("operations_container.py.jinja2")
39
- if not self.code_model.options["combine_operation_files"] and self.operation_groups[0].is_empty_operation_group:
40
- operation_group_template = self.env.get_template("operations_container_mixin.py.jinja2")
41
-
42
- has_schemas = self.code_model.schemas or self.code_model.enums
43
-
44
- # extract all operations from operation_groups
45
- operaions_all = [operation for groups in self.operation_groups for operation in groups.operations]
46
- operation_group_temp = copy(self.operation_groups[0])
47
- operation_group_temp.operations = operaions_all
48
-
49
- return operation_group_template.render(
50
- code_model=self.code_model,
51
- operation_groups=self.operation_groups,
52
- imports=FileImportSerializer(
53
- operation_group_temp.imports(
54
- async_mode=self.async_mode,
55
- has_schemas=bool(has_schemas)
56
- ), is_python_3_file=self.is_python_3_file
57
- ),
58
- async_mode=self.async_mode,
59
- is_python_3_file=self.is_python_3_file,
60
- is_lro=_is_lro,
61
- is_paging=_is_paging,
62
- get_operation_serializer=functools.partial(
63
- get_operation_serializer,
64
- code_model=self.code_model,
65
- async_mode=self.async_mode,
66
- is_python_3_file=self.is_python_3_file,
67
- ),
68
- request_builder_serializer=get_request_builder_serializer(
69
- self.code_model, self.is_python_3_file,
70
- ),
71
- )
@@ -1,16 +0,0 @@
1
- class {{ operation_group.class_name }}{{ object_base_class }}:
2
- {% for operation in operation_group.operations %}
3
-
4
- {% set request_builder = operation.request_builder %}
5
- {% set operation_serializer = get_operation_serializer(operation) %}
6
- {% if is_lro(operation) and is_paging(operation) %}
7
- {%- macro someop() %}{% include "lro_paging_operation.py.jinja2" %}{% endmacro %}
8
- {% elif is_lro(operation) %}
9
- {%- macro someop() %}{% include "lro_operation.py.jinja2" %}{% endmacro %}
10
- {% elif is_paging(operation) %}
11
- {% macro someop() %}{% include "paging_operation.py.jinja2" %}{% endmacro %}
12
- {% else %}
13
- {% macro someop() %}{% include "operation.py.jinja2" %}{% endmacro %}
14
- {% endif %}
15
- {{ someop()|indent }}
16
- {% endfor %}
@@ -1,42 +0,0 @@
1
- {% set object_base_class = "" if async_mode else "(object)" %}
2
- {% set operations_description = "async operations" if async_mode else "operations" %}
3
- {% set return_none_type_annotation = " -> None" if async_mode else "" %}
4
- {% set operation_group = operation_groups[0] %}
5
- # coding=utf-8
6
- {{ code_model.options['license_header'] }}
7
- {{ imports }}
8
-
9
- {{ " " if not is_python_3_file }}T = TypeVar('T')
10
- {{ " " if not is_python_3_file }}ClsType = Optional[Callable[[PipelineResponse[HttpRequest, {{ "Async" if async_mode else "" }}HttpResponse], T, Dict[str, Any]], Any]]
11
-
12
- {% if code_model.options["builders_visibility"] == "embedded" and not async_mode %}
13
- _SERIALIZER = Serializer()
14
- {% if not is_python_3_file %}
15
- # fmt: off
16
- {% endif %}
17
- {% if not code_model.options['combine_operation_files'] %}
18
- {% for request_builder in code_model.rest.request_builders | selectattr("operation_group_name", "equalto", operation_group.name) %}
19
-
20
- {% include "request_builder.py.jinja2" %}
21
- {% endfor %}
22
- {% else %}
23
- {% for request_builder in code_model.rest.request_builders %}
24
-
25
- {% include "request_builder.py.jinja2" %}
26
- {% endfor %}
27
- {% endif %}
28
- {% if not is_python_3_file %}
29
- # fmt: on
30
- {% endif %}
31
- {% endif %}
32
- {% if not code_model.options['combine_operation_files'] %}
33
- {% include "operations_class.py.jinja2" %}
34
- {% else %}
35
- {% for operation_group in operation_groups %}
36
- {% if operation_group.is_empty_operation_group %}
37
- {% include "operations_class_mixin.py.jinja2" %}
38
- {% else %}
39
- {% include "operations_class.py.jinja2" %}
40
- {% endif %}
41
- {% endfor %}
42
- {% endif %}