| 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. |
| 13 |
import inspect |
| 14 |
|
| 15 |
import jmespath |
| 16 |
|
| 17 |
|
| 18 |
def get_resource_ignore_params(params): |
| 19 |
"""Helper method to determine which parameters to ignore for actions |
| 20 |
|
| 21 |
:returns: A list of the parameter names that does not need to be |
| 22 |
included in a resource's method call for documentation purposes. |
| 23 |
""" |
| 24 |
ignore_params = [] |
| 25 |
for param in params: |
| 26 |
result = jmespath.compile(param.target) |
| 27 |
current = result.parsed |
| 28 |
# Use JMESPath to find the left most element in the target expression |
| 29 |
# which will be the parameter to ignore in the action call. |
| 30 |
while current['children']: |
| 31 |
current = current['children'][0] |
| 32 |
# Make sure the parameter we are about to ignore is a field. |
| 33 |
# If it is not, we should ignore the result to avoid false positives. |
| 34 |
if current['type'] == 'field': |
| 35 |
ignore_params.append(current['value']) |
| 36 |
return ignore_params |
| 37 |
|
| 38 |
|
| 39 |
def is_resource_action(action_handle): |
| 40 |
return inspect.isfunction(action_handle) |
| 41 |
|
| 42 |
|
| 43 |
def get_resource_public_actions(resource_class): |
| 44 |
resource_class_members = inspect.getmembers(resource_class) |
| 45 |
resource_methods = {} |
| 46 |
for name, member in resource_class_members: |
| 47 |
if not name.startswith('_'): |
| 48 |
if not name[0].isupper(): |
| 49 |
if not name.startswith('wait_until'): |
| 50 |
if is_resource_action(member): |
| 51 |
resource_methods[name] = member |
| 52 |
return resource_methods |
| 53 |
|
| 54 |
|
| 55 |
def get_identifier_values_for_example(identifier_names): |
| 56 |
return ','.join([f'\'{identifier}\'' for identifier in identifier_names]) |
| 57 |
|
| 58 |
|
| 59 |
def get_identifier_args_for_signature(identifier_names): |
| 60 |
return ','.join(identifier_names) |
| 61 |
|
| 62 |
|
| 63 |
def get_identifier_description(resource_name, identifier_name): |
| 64 |
return ( |
| 65 |
f"The {resource_name}'s {identifier_name} identifier. " |
| 66 |
f"This **must** be set." |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
def add_resource_type_overview( |
| 71 |
section, resource_type, description, intro_link=None |
| 72 |
): |
| 73 |
section.style.new_line() |
| 74 |
section.style.h3(resource_type) |
| 75 |
section.style.new_line() |
| 76 |
section.style.new_line() |
| 77 |
section.write(description) |
| 78 |
section.style.new_line() |
| 79 |
if intro_link is not None: |
| 80 |
section.write( |
| 81 |
f'For more information about {resource_type.lower()} refer to the ' |
| 82 |
f':ref:`Resources Introduction Guide<{intro_link}>`.' |
| 83 |
) |
| 84 |
section.style.new_line() |
| 85 |
|
| 86 |
|
| 87 |
class DocumentModifiedShape: |
| 88 |
def __init__( |
| 89 |
self, shape_name, new_type, new_description, new_example_value |
| 90 |
): |
| 91 |
self._shape_name = shape_name |
| 92 |
self._new_type = new_type |
| 93 |
self._new_description = new_description |
| 94 |
self._new_example_value = new_example_value |
| 95 |
|
| 96 |
def replace_documentation_for_matching_shape( |
| 97 |
self, event_name, section, **kwargs |
| 98 |
): |
| 99 |
if self._shape_name == section.context.get('shape'): |
| 100 |
self._replace_documentation(event_name, section) |
| 101 |
for section_name in section.available_sections: |
| 102 |
sub_section = section.get_section(section_name) |
| 103 |
if self._shape_name == sub_section.context.get('shape'): |
| 104 |
self._replace_documentation(event_name, sub_section) |
| 105 |
else: |
| 106 |
self.replace_documentation_for_matching_shape( |
| 107 |
event_name, sub_section |
| 108 |
) |
| 109 |
|
| 110 |
def _replace_documentation(self, event_name, section): |
| 111 |
if event_name.startswith( |
| 112 |
'docs.request-example' |
| 113 |
) or event_name.startswith('docs.response-example'): |
| 114 |
section.remove_all_sections() |
| 115 |
section.clear_text() |
| 116 |
section.write(self._new_example_value) |
| 117 |
|
| 118 |
if event_name.startswith( |
| 119 |
'docs.request-params' |
| 120 |
) or event_name.startswith('docs.response-params'): |
| 121 |
allowed_sections = ( |
| 122 |
'param-name', |
| 123 |
'param-documentation', |
| 124 |
'end-structure', |
| 125 |
'param-type', |
| 126 |
'end-param', |
| 127 |
) |
| 128 |
for section_name in section.available_sections: |
| 129 |
# Delete any extra members as a new shape is being |
| 130 |
# used. |
| 131 |
if section_name not in allowed_sections: |
| 132 |
section.delete_section(section_name) |
| 133 |
|
| 134 |
# Update the documentation |
| 135 |
description_section = section.get_section('param-documentation') |
| 136 |
description_section.clear_text() |
| 137 |
description_section.write(self._new_description) |
| 138 |
|
| 139 |
# Update the param type |
| 140 |
type_section = section.get_section('param-type') |
| 141 |
if type_section.getvalue().decode('utf-8').startswith(':type'): |
| 142 |
type_section.clear_text() |
| 143 |
type_section.write(f':type {section.name}: {self._new_type}') |
| 144 |
else: |
| 145 |
type_section.clear_text() |
| 146 |
type_section.style.italics(f'({self._new_type}) -- ') |