in src/sagemaker_core/tools/resources_codegen.py [0:0]
def generate_method(self, method: Method, resource_attributes: list):
# TODO: Use special templates for some methods with different formats like list and wait
if method.method_name.startswith("get_all"):
return self.generate_additional_get_all_method(method, resource_attributes)
operation_metadata = self.operations[method.operation_name]
operation_input_shape_name = operation_metadata["input"]["shape"]
if method.method_type == MethodType.CLASS.value:
decorator = "@classmethod"
method_args = add_indent("cls,\n", 4)
method_args += self._generate_method_args(operation_input_shape_name)
operation_input_args = self._generate_operation_input_args_updated(
operation_metadata, True, resource_attributes
)
exclude_resource_attrs = None
elif method.method_type == MethodType.STATIC.value:
decorator = "@staticmethod"
method_args = self._generate_method_args(operation_input_shape_name)
operation_input_args = self._generate_operation_input_args_updated(
operation_metadata, True
)
exclude_resource_attrs = None
else:
decorator = ""
method_args = add_indent("self,\n", 4)
method_args += (
self._generate_method_args(operation_input_shape_name, resource_attributes) + "\n"
)
operation_input_args = self._generate_operation_input_args_updated(
operation_metadata, False, resource_attributes
)
exclude_resource_attrs = resource_attributes
method_args += add_indent("session: Optional[Session] = None,\n", 4)
method_args += add_indent("region: Optional[str] = None,", 4)
initialize_client = INITIALIZE_CLIENT_TEMPLATE.format(service_name=method.service_name)
if len(self.shapes[operation_input_shape_name]["members"]) != 0:
# the method has input arguments
serialize_operation_input = SERIALIZE_INPUT_TEMPLATE.format(
operation_input_args=operation_input_args
)
call_operation_api = CALL_OPERATION_API_TEMPLATE.format(
operation=convert_to_snake_case(method.operation_name)
)
else:
# the method has no input arguments
serialize_operation_input = ""
call_operation_api = CALL_OPERATION_API_NO_ARG_TEMPLATE.format(
operation=convert_to_snake_case(method.operation_name)
)
if method.return_type == "None":
return_type = "None"
deserialize_response = ""
return_string = None
elif method.return_type in BASIC_RETURN_TYPES:
return_type = f"Optional[{method.return_type}]"
deserialize_response = DESERIALIZE_RESPONSE_TO_BASIC_TYPE_TEMPLATE
return_string = f"Returns:\n" f" {method.return_type}\n"
else:
if method.return_type == "cls":
return_type = f'Optional["{method.resource_name}"]'
return_type_conversion = "cls"
return_string = f"Returns:\n" f" {method.resource_name}\n"
else:
return_type_conversion = method.return_type
if method.resource_name != method.return_type and method.return_type in self.shapes:
return_type_conversion = f"shapes.{method.return_type}"
return_type = f"Optional[{return_type_conversion}]"
return_string = f"Returns:\n" f" {return_type_conversion}\n"
operation_output_shape = operation_metadata["output"]["shape"]
deserialize_response = DESERIALIZE_RESPONSE_TEMPLATE.format(
operation_output_shape=operation_output_shape,
return_type_conversion=return_type_conversion,
)
initialize_client = INITIALIZE_CLIENT_TEMPLATE.format(service_name=method.service_name)
if len(self.shapes[operation_input_shape_name]["members"]) != 0:
# the method has input arguments
serialize_operation_input = SERIALIZE_INPUT_TEMPLATE.format(
operation_input_args=operation_input_args
)
call_operation_api = CALL_OPERATION_API_TEMPLATE.format(
operation=convert_to_snake_case(method.operation_name)
)
else:
# the method has no input arguments
serialize_operation_input = ""
call_operation_api = CALL_OPERATION_API_NO_ARG_TEMPLATE.format(
operation=convert_to_snake_case(method.operation_name)
)
# generate docstring
docstring = self._generate_docstring(
title=method.docstring_title,
operation_name=method.operation_name,
resource_name=method.resource_name,
operation_input_shape_name=operation_input_shape_name,
include_session_region=True,
return_string=return_string,
exclude_resource_attrs=exclude_resource_attrs,
)
formatted_method = GENERIC_METHOD_TEMPLATE.format(
docstring=docstring,
decorator=decorator,
method_name=method.method_name,
method_args=method_args,
return_type=return_type,
serialize_operation_input=serialize_operation_input,
initialize_client=initialize_client,
call_operation_api=call_operation_api,
deserialize_response=deserialize_response,
)
return formatted_method