1# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"). You
4# may not use this file except in compliance with the License. A copy of
5# the License is located at
6#
7# https://aws.amazon.com/apache2.0/
8#
9# or in the "license" file accompanying this file. This file is
10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11# ANY KIND, either express or implied. See the License for the specific
12# language governing permissions and limitations under the License.
13from botocore.docs.method import document_model_driven_method
14
15
16def document_model_driven_resource_method(
17        section, method_name, operation_model, event_emitter,
18        method_description=None, example_prefix=None, include_input=None,
19        include_output=None, exclude_input=None, exclude_output=None,
20        document_output=True, resource_action_model=None,
21        include_signature=True):
22
23    document_model_driven_method(
24        section=section, method_name=method_name,
25        operation_model=operation_model,
26        event_emitter=event_emitter,
27        method_description=method_description,
28        example_prefix=example_prefix,
29        include_input=include_input,
30        include_output=include_output,
31        exclude_input=exclude_input,
32        exclude_output=exclude_output,
33        document_output=document_output,
34        include_signature=include_signature
35    )
36
37    # If this action returns a resource modify the return example to
38    # appropriately reflect that.
39    if resource_action_model.resource:
40        if 'return' in section.available_sections:
41            section.delete_section('return')
42        resource_type = resource_action_model.resource.type
43
44        new_return_section = section.add_new_section('return')
45        return_resource_type = '%s.%s' % (
46            operation_model.service_model.service_name,
47            resource_type)
48
49        return_type = ':py:class:`%s`' % return_resource_type
50        return_description = '%s resource' % (resource_type)
51
52        if _method_returns_resource_list(resource_action_model.resource):
53            return_type = 'list(%s)' % return_type
54            return_description = 'A list of %s resources' % (
55                resource_type)
56
57        new_return_section.style.new_line()
58        new_return_section.write(
59            ':rtype: %s' % return_type)
60        new_return_section.style.new_line()
61        new_return_section.write(
62            ':returns: %s' % return_description)
63        new_return_section.style.new_line()
64
65
66def _method_returns_resource_list(resource):
67    for identifier in resource.identifiers:
68        if identifier.path and '[]' in identifier.path:
69            return True
70
71    return False
72