repo
stringlengths
7
55
path
stringlengths
4
223
func_name
stringlengths
1
134
original_string
stringlengths
75
104k
language
stringclasses
1 value
code
stringlengths
75
104k
code_tokens
listlengths
19
28.4k
docstring
stringlengths
1
46.9k
docstring_tokens
listlengths
1
1.97k
sha
stringlengths
40
40
url
stringlengths
87
315
partition
stringclasses
1 value
awslabs/serverless-application-model
samtranslator/model/intrinsics.py
make_combined_condition
def make_combined_condition(conditions_list, condition_name): """ Makes a combined condition using Fn::Or. Since Fn::Or only accepts up to 10 conditions, this method optionally creates multiple conditions. These conditions are named based on the condition_name parameter that is passed into the method. :param list conditions_list: list of conditions :param string condition_name: base name desired for new condition :return: dictionary of condition_name: condition_value """ if len(conditions_list) < 2: # Can't make a condition if <2 conditions provided. return None # Total number of conditions allows in an Fn::Or statement. See docs: # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-or max_conditions = 10 conditions = {} conditions_length = len(conditions_list) # Get number of conditions needed, then minus one to use them as 0-based indices zero_based_num_conditions = calculate_number_of_conditions(conditions_length, max_conditions) - 1 while len(conditions_list) > 1: new_condition_name = condition_name # If more than 1 new condition is needed, add a number to the end of the name if zero_based_num_conditions > 0: new_condition_name = '{}{}'.format(condition_name, zero_based_num_conditions) zero_based_num_conditions -= 1 new_condition_content = make_or_condition(conditions_list[:max_conditions]) conditions_list = conditions_list[max_conditions:] conditions_list.append(new_condition_name) conditions[new_condition_name] = new_condition_content return conditions
python
def make_combined_condition(conditions_list, condition_name): """ Makes a combined condition using Fn::Or. Since Fn::Or only accepts up to 10 conditions, this method optionally creates multiple conditions. These conditions are named based on the condition_name parameter that is passed into the method. :param list conditions_list: list of conditions :param string condition_name: base name desired for new condition :return: dictionary of condition_name: condition_value """ if len(conditions_list) < 2: # Can't make a condition if <2 conditions provided. return None # Total number of conditions allows in an Fn::Or statement. See docs: # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-or max_conditions = 10 conditions = {} conditions_length = len(conditions_list) # Get number of conditions needed, then minus one to use them as 0-based indices zero_based_num_conditions = calculate_number_of_conditions(conditions_length, max_conditions) - 1 while len(conditions_list) > 1: new_condition_name = condition_name # If more than 1 new condition is needed, add a number to the end of the name if zero_based_num_conditions > 0: new_condition_name = '{}{}'.format(condition_name, zero_based_num_conditions) zero_based_num_conditions -= 1 new_condition_content = make_or_condition(conditions_list[:max_conditions]) conditions_list = conditions_list[max_conditions:] conditions_list.append(new_condition_name) conditions[new_condition_name] = new_condition_content return conditions
[ "def", "make_combined_condition", "(", "conditions_list", ",", "condition_name", ")", ":", "if", "len", "(", "conditions_list", ")", "<", "2", ":", "# Can't make a condition if <2 conditions provided.", "return", "None", "# Total number of conditions allows in an Fn::Or statement. See docs:", "# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-or", "max_conditions", "=", "10", "conditions", "=", "{", "}", "conditions_length", "=", "len", "(", "conditions_list", ")", "# Get number of conditions needed, then minus one to use them as 0-based indices", "zero_based_num_conditions", "=", "calculate_number_of_conditions", "(", "conditions_length", ",", "max_conditions", ")", "-", "1", "while", "len", "(", "conditions_list", ")", ">", "1", ":", "new_condition_name", "=", "condition_name", "# If more than 1 new condition is needed, add a number to the end of the name", "if", "zero_based_num_conditions", ">", "0", ":", "new_condition_name", "=", "'{}{}'", ".", "format", "(", "condition_name", ",", "zero_based_num_conditions", ")", "zero_based_num_conditions", "-=", "1", "new_condition_content", "=", "make_or_condition", "(", "conditions_list", "[", ":", "max_conditions", "]", ")", "conditions_list", "=", "conditions_list", "[", "max_conditions", ":", "]", "conditions_list", ".", "append", "(", "new_condition_name", ")", "conditions", "[", "new_condition_name", "]", "=", "new_condition_content", "return", "conditions" ]
Makes a combined condition using Fn::Or. Since Fn::Or only accepts up to 10 conditions, this method optionally creates multiple conditions. These conditions are named based on the condition_name parameter that is passed into the method. :param list conditions_list: list of conditions :param string condition_name: base name desired for new condition :return: dictionary of condition_name: condition_value
[ "Makes", "a", "combined", "condition", "using", "Fn", "::", "Or", ".", "Since", "Fn", "::", "Or", "only", "accepts", "up", "to", "10", "conditions", "this", "method", "optionally", "creates", "multiple", "conditions", ".", "These", "conditions", "are", "named", "based", "on", "the", "condition_name", "parameter", "that", "is", "passed", "into", "the", "method", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/intrinsics.py#L66-L99
train
awslabs/serverless-application-model
samtranslator/model/intrinsics.py
is_instrinsic
def is_instrinsic(input): """ Checks if the given input is an intrinsic function dictionary. Intrinsic function is a dictionary with single key that is the name of the intrinsics. :param input: Input value to check if it is an intrinsic :return: True, if yes """ if input is not None \ and isinstance(input, dict) \ and len(input) == 1: key = list(input.keys())[0] return key == "Ref" or key == "Condition" or key.startswith("Fn::") return False
python
def is_instrinsic(input): """ Checks if the given input is an intrinsic function dictionary. Intrinsic function is a dictionary with single key that is the name of the intrinsics. :param input: Input value to check if it is an intrinsic :return: True, if yes """ if input is not None \ and isinstance(input, dict) \ and len(input) == 1: key = list(input.keys())[0] return key == "Ref" or key == "Condition" or key.startswith("Fn::") return False
[ "def", "is_instrinsic", "(", "input", ")", ":", "if", "input", "is", "not", "None", "and", "isinstance", "(", "input", ",", "dict", ")", "and", "len", "(", "input", ")", "==", "1", ":", "key", "=", "list", "(", "input", ".", "keys", "(", ")", ")", "[", "0", "]", "return", "key", "==", "\"Ref\"", "or", "key", "==", "\"Condition\"", "or", "key", ".", "startswith", "(", "\"Fn::\"", ")", "return", "False" ]
Checks if the given input is an intrinsic function dictionary. Intrinsic function is a dictionary with single key that is the name of the intrinsics. :param input: Input value to check if it is an intrinsic :return: True, if yes
[ "Checks", "if", "the", "given", "input", "is", "an", "intrinsic", "function", "dictionary", ".", "Intrinsic", "function", "is", "a", "dictionary", "with", "single", "key", "that", "is", "the", "name", "of", "the", "intrinsics", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/intrinsics.py#L124-L140
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
Action.can_handle
def can_handle(self, input_dict): """ Validates that the input dictionary contains only one key and is of the given intrinsic_name :param input_dict: Input dictionary representing the intrinsic function :return: True if it matches expected structure, False otherwise """ return input_dict is not None \ and isinstance(input_dict, dict) \ and len(input_dict) == 1 \ and self.intrinsic_name in input_dict
python
def can_handle(self, input_dict): """ Validates that the input dictionary contains only one key and is of the given intrinsic_name :param input_dict: Input dictionary representing the intrinsic function :return: True if it matches expected structure, False otherwise """ return input_dict is not None \ and isinstance(input_dict, dict) \ and len(input_dict) == 1 \ and self.intrinsic_name in input_dict
[ "def", "can_handle", "(", "self", ",", "input_dict", ")", ":", "return", "input_dict", "is", "not", "None", "and", "isinstance", "(", "input_dict", ",", "dict", ")", "and", "len", "(", "input_dict", ")", "==", "1", "and", "self", ".", "intrinsic_name", "in", "input_dict" ]
Validates that the input dictionary contains only one key and is of the given intrinsic_name :param input_dict: Input dictionary representing the intrinsic function :return: True if it matches expected structure, False otherwise
[ "Validates", "that", "the", "input", "dictionary", "contains", "only", "one", "key", "and", "is", "of", "the", "given", "intrinsic_name" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L41-L52
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
Action._parse_resource_reference
def _parse_resource_reference(cls, ref_value): """ Splits a resource reference of structure "LogicalId.Property" and returns the "LogicalId" and "Property" separately. :param string ref_value: Input reference value which *may* contain the structure "LogicalId.Property" :return string, string: Returns two values - logical_id, property. If the input does not contain the structure, then both `logical_id` and property will be None """ no_result = (None, None) if not isinstance(ref_value, string_types): return no_result splits = ref_value.split(cls._resource_ref_separator, 1) # Either there is no 'dot' (or) one of the values is empty string (Ex: when you split "LogicalId.") if len(splits) != 2 or not all(splits): return no_result return splits[0], splits[1]
python
def _parse_resource_reference(cls, ref_value): """ Splits a resource reference of structure "LogicalId.Property" and returns the "LogicalId" and "Property" separately. :param string ref_value: Input reference value which *may* contain the structure "LogicalId.Property" :return string, string: Returns two values - logical_id, property. If the input does not contain the structure, then both `logical_id` and property will be None """ no_result = (None, None) if not isinstance(ref_value, string_types): return no_result splits = ref_value.split(cls._resource_ref_separator, 1) # Either there is no 'dot' (or) one of the values is empty string (Ex: when you split "LogicalId.") if len(splits) != 2 or not all(splits): return no_result return splits[0], splits[1]
[ "def", "_parse_resource_reference", "(", "cls", ",", "ref_value", ")", ":", "no_result", "=", "(", "None", ",", "None", ")", "if", "not", "isinstance", "(", "ref_value", ",", "string_types", ")", ":", "return", "no_result", "splits", "=", "ref_value", ".", "split", "(", "cls", ".", "_resource_ref_separator", ",", "1", ")", "# Either there is no 'dot' (or) one of the values is empty string (Ex: when you split \"LogicalId.\")", "if", "len", "(", "splits", ")", "!=", "2", "or", "not", "all", "(", "splits", ")", ":", "return", "no_result", "return", "splits", "[", "0", "]", ",", "splits", "[", "1", "]" ]
Splits a resource reference of structure "LogicalId.Property" and returns the "LogicalId" and "Property" separately. :param string ref_value: Input reference value which *may* contain the structure "LogicalId.Property" :return string, string: Returns two values - logical_id, property. If the input does not contain the structure, then both `logical_id` and property will be None
[ "Splits", "a", "resource", "reference", "of", "structure", "LogicalId", ".", "Property", "and", "returns", "the", "LogicalId", "and", "Property", "separately", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L55-L76
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
RefAction.resolve_parameter_refs
def resolve_parameter_refs(self, input_dict, parameters): """ Resolves references that are present in the parameters and returns the value. If it is not in parameters, this method simply returns the input unchanged. :param input_dict: Dictionary representing the Ref function. Must contain only one key and it should be "Ref". Ex: {Ref: "foo"} :param parameters: Dictionary of parameter values for resolution :return: """ if not self.can_handle(input_dict): return input_dict param_name = input_dict[self.intrinsic_name] if not isinstance(param_name, string_types): return input_dict if param_name in parameters: return parameters[param_name] else: return input_dict
python
def resolve_parameter_refs(self, input_dict, parameters): """ Resolves references that are present in the parameters and returns the value. If it is not in parameters, this method simply returns the input unchanged. :param input_dict: Dictionary representing the Ref function. Must contain only one key and it should be "Ref". Ex: {Ref: "foo"} :param parameters: Dictionary of parameter values for resolution :return: """ if not self.can_handle(input_dict): return input_dict param_name = input_dict[self.intrinsic_name] if not isinstance(param_name, string_types): return input_dict if param_name in parameters: return parameters[param_name] else: return input_dict
[ "def", "resolve_parameter_refs", "(", "self", ",", "input_dict", ",", "parameters", ")", ":", "if", "not", "self", ".", "can_handle", "(", "input_dict", ")", ":", "return", "input_dict", "param_name", "=", "input_dict", "[", "self", ".", "intrinsic_name", "]", "if", "not", "isinstance", "(", "param_name", ",", "string_types", ")", ":", "return", "input_dict", "if", "param_name", "in", "parameters", ":", "return", "parameters", "[", "param_name", "]", "else", ":", "return", "input_dict" ]
Resolves references that are present in the parameters and returns the value. If it is not in parameters, this method simply returns the input unchanged. :param input_dict: Dictionary representing the Ref function. Must contain only one key and it should be "Ref". Ex: {Ref: "foo"} :param parameters: Dictionary of parameter values for resolution :return:
[ "Resolves", "references", "that", "are", "present", "in", "the", "parameters", "and", "returns", "the", "value", ".", "If", "it", "is", "not", "in", "parameters", "this", "method", "simply", "returns", "the", "input", "unchanged", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L82-L104
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
RefAction.resolve_resource_refs
def resolve_resource_refs(self, input_dict, supported_resource_refs): """ Resolves references to some property of a resource. These are runtime properties which can't be converted to a value here. Instead we output another reference that will more actually resolve to the value when executed via CloudFormation Example: {"Ref": "LogicalId.Property"} => {"Ref": "SomeOtherLogicalId"} :param dict input_dict: Dictionary representing the Ref function to be resolved. :param samtranslator.intrinsics.resource_refs.SupportedResourceReferences supported_resource_refs: Instance of an `SupportedResourceReferences` object that contain value of the property. :return dict: Dictionary with resource references resolved. """ if not self.can_handle(input_dict): return input_dict ref_value = input_dict[self.intrinsic_name] logical_id, property = self._parse_resource_reference(ref_value) # ref_value could not be parsed if not logical_id: return input_dict resolved_value = supported_resource_refs.get(logical_id, property) if not resolved_value: return input_dict return { self.intrinsic_name: resolved_value }
python
def resolve_resource_refs(self, input_dict, supported_resource_refs): """ Resolves references to some property of a resource. These are runtime properties which can't be converted to a value here. Instead we output another reference that will more actually resolve to the value when executed via CloudFormation Example: {"Ref": "LogicalId.Property"} => {"Ref": "SomeOtherLogicalId"} :param dict input_dict: Dictionary representing the Ref function to be resolved. :param samtranslator.intrinsics.resource_refs.SupportedResourceReferences supported_resource_refs: Instance of an `SupportedResourceReferences` object that contain value of the property. :return dict: Dictionary with resource references resolved. """ if not self.can_handle(input_dict): return input_dict ref_value = input_dict[self.intrinsic_name] logical_id, property = self._parse_resource_reference(ref_value) # ref_value could not be parsed if not logical_id: return input_dict resolved_value = supported_resource_refs.get(logical_id, property) if not resolved_value: return input_dict return { self.intrinsic_name: resolved_value }
[ "def", "resolve_resource_refs", "(", "self", ",", "input_dict", ",", "supported_resource_refs", ")", ":", "if", "not", "self", ".", "can_handle", "(", "input_dict", ")", ":", "return", "input_dict", "ref_value", "=", "input_dict", "[", "self", ".", "intrinsic_name", "]", "logical_id", ",", "property", "=", "self", ".", "_parse_resource_reference", "(", "ref_value", ")", "# ref_value could not be parsed", "if", "not", "logical_id", ":", "return", "input_dict", "resolved_value", "=", "supported_resource_refs", ".", "get", "(", "logical_id", ",", "property", ")", "if", "not", "resolved_value", ":", "return", "input_dict", "return", "{", "self", ".", "intrinsic_name", ":", "resolved_value", "}" ]
Resolves references to some property of a resource. These are runtime properties which can't be converted to a value here. Instead we output another reference that will more actually resolve to the value when executed via CloudFormation Example: {"Ref": "LogicalId.Property"} => {"Ref": "SomeOtherLogicalId"} :param dict input_dict: Dictionary representing the Ref function to be resolved. :param samtranslator.intrinsics.resource_refs.SupportedResourceReferences supported_resource_refs: Instance of an `SupportedResourceReferences` object that contain value of the property. :return dict: Dictionary with resource references resolved.
[ "Resolves", "references", "to", "some", "property", "of", "a", "resource", ".", "These", "are", "runtime", "properties", "which", "can", "t", "be", "converted", "to", "a", "value", "here", ".", "Instead", "we", "output", "another", "reference", "that", "will", "more", "actually", "resolve", "to", "the", "value", "when", "executed", "via", "CloudFormation" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L106-L137
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
RefAction.resolve_resource_id_refs
def resolve_resource_id_refs(self, input_dict, supported_resource_id_refs): """ Updates references to the old logical id of a resource to the new (generated) logical id. Example: {"Ref": "MyLayer"} => {"Ref": "MyLayerABC123"} :param dict input_dict: Dictionary representing the Ref function to be resolved. :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return dict: Dictionary with resource references resolved. """ if not self.can_handle(input_dict): return input_dict ref_value = input_dict[self.intrinsic_name] if not isinstance(ref_value, string_types) or self._resource_ref_separator in ref_value: return input_dict logical_id = ref_value resolved_value = supported_resource_id_refs.get(logical_id) if not resolved_value: return input_dict return { self.intrinsic_name: resolved_value }
python
def resolve_resource_id_refs(self, input_dict, supported_resource_id_refs): """ Updates references to the old logical id of a resource to the new (generated) logical id. Example: {"Ref": "MyLayer"} => {"Ref": "MyLayerABC123"} :param dict input_dict: Dictionary representing the Ref function to be resolved. :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return dict: Dictionary with resource references resolved. """ if not self.can_handle(input_dict): return input_dict ref_value = input_dict[self.intrinsic_name] if not isinstance(ref_value, string_types) or self._resource_ref_separator in ref_value: return input_dict logical_id = ref_value resolved_value = supported_resource_id_refs.get(logical_id) if not resolved_value: return input_dict return { self.intrinsic_name: resolved_value }
[ "def", "resolve_resource_id_refs", "(", "self", ",", "input_dict", ",", "supported_resource_id_refs", ")", ":", "if", "not", "self", ".", "can_handle", "(", "input_dict", ")", ":", "return", "input_dict", "ref_value", "=", "input_dict", "[", "self", ".", "intrinsic_name", "]", "if", "not", "isinstance", "(", "ref_value", ",", "string_types", ")", "or", "self", ".", "_resource_ref_separator", "in", "ref_value", ":", "return", "input_dict", "logical_id", "=", "ref_value", "resolved_value", "=", "supported_resource_id_refs", ".", "get", "(", "logical_id", ")", "if", "not", "resolved_value", ":", "return", "input_dict", "return", "{", "self", ".", "intrinsic_name", ":", "resolved_value", "}" ]
Updates references to the old logical id of a resource to the new (generated) logical id. Example: {"Ref": "MyLayer"} => {"Ref": "MyLayerABC123"} :param dict input_dict: Dictionary representing the Ref function to be resolved. :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return dict: Dictionary with resource references resolved.
[ "Updates", "references", "to", "the", "old", "logical", "id", "of", "a", "resource", "to", "the", "new", "(", "generated", ")", "logical", "id", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L139-L166
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
SubAction.resolve_parameter_refs
def resolve_parameter_refs(self, input_dict, parameters): """ Substitute references found within the string of `Fn::Sub` intrinsic function :param input_dict: Dictionary representing the Fn::Sub function. Must contain only one key and it should be `Fn::Sub`. Ex: {"Fn::Sub": ...} :param parameters: Dictionary of parameter values for substitution :return: Resolved """ def do_replacement(full_ref, prop_name): """ Replace parameter references with actual value. Return value of this method is directly replaces the reference structure :param full_ref: => ${logicalId.property} :param prop_name: => logicalId.property :return: Either the value it resolves to. If not the original reference """ return parameters.get(prop_name, full_ref) return self._handle_sub_action(input_dict, do_replacement)
python
def resolve_parameter_refs(self, input_dict, parameters): """ Substitute references found within the string of `Fn::Sub` intrinsic function :param input_dict: Dictionary representing the Fn::Sub function. Must contain only one key and it should be `Fn::Sub`. Ex: {"Fn::Sub": ...} :param parameters: Dictionary of parameter values for substitution :return: Resolved """ def do_replacement(full_ref, prop_name): """ Replace parameter references with actual value. Return value of this method is directly replaces the reference structure :param full_ref: => ${logicalId.property} :param prop_name: => logicalId.property :return: Either the value it resolves to. If not the original reference """ return parameters.get(prop_name, full_ref) return self._handle_sub_action(input_dict, do_replacement)
[ "def", "resolve_parameter_refs", "(", "self", ",", "input_dict", ",", "parameters", ")", ":", "def", "do_replacement", "(", "full_ref", ",", "prop_name", ")", ":", "\"\"\"\n Replace parameter references with actual value. Return value of this method is directly replaces the\n reference structure\n\n :param full_ref: => ${logicalId.property}\n :param prop_name: => logicalId.property\n :return: Either the value it resolves to. If not the original reference\n \"\"\"", "return", "parameters", ".", "get", "(", "prop_name", ",", "full_ref", ")", "return", "self", ".", "_handle_sub_action", "(", "input_dict", ",", "do_replacement", ")" ]
Substitute references found within the string of `Fn::Sub` intrinsic function :param input_dict: Dictionary representing the Fn::Sub function. Must contain only one key and it should be `Fn::Sub`. Ex: {"Fn::Sub": ...} :param parameters: Dictionary of parameter values for substitution :return: Resolved
[ "Substitute", "references", "found", "within", "the", "string", "of", "Fn", "::", "Sub", "intrinsic", "function" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L172-L194
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
SubAction.resolve_resource_refs
def resolve_resource_refs(self, input_dict, supported_resource_refs): """ Resolves reference to some property of a resource. Inside string to be substituted, there could be either a "Ref" or a "GetAtt" usage of this property. They have to be handled differently. Ref usages are directly converted to a Ref on the resolved value. GetAtt usages are split under the assumption that there can be only one property of resource referenced here. Everything else is an attribute reference. Example: Let's say `LogicalId.Property` will be resolved to `ResolvedValue` Ref usage: ${LogicalId.Property} => ${ResolvedValue} GetAtt usage: ${LogicalId.Property.Arn} => ${ResolvedValue.Arn} ${LogicalId.Property.Attr1.Attr2} => {ResolvedValue.Attr1.Attr2} :param input_dict: Dictionary to be resolved :param samtranslator.intrinsics.resource_refs.SupportedResourceReferences supported_resource_refs: Instance of an `SupportedResourceReferences` object that contain value of the property. :return: Resolved dictionary """ def do_replacement(full_ref, ref_value): """ Perform the appropriate replacement to handle ${LogicalId.Property} type references inside a Sub. This method is called to get the replacement string for each reference within Sub's value :param full_ref: Entire reference string such as "${LogicalId.Property}" :param ref_value: Just the value of the reference such as "LogicalId.Property" :return: Resolved reference of the structure "${SomeOtherLogicalId}". Result should always include the ${} structure since we are not resolving to final value, but just converting one reference to another """ # Split the value by separator, expecting to separate out LogicalId.Property splits = ref_value.split(self._resource_ref_separator) # If we don't find at least two parts, there is nothing to resolve if len(splits) < 2: return full_ref logical_id = splits[0] property = splits[1] resolved_value = supported_resource_refs.get(logical_id, property) if not resolved_value: # This ID/property combination is not in the supported references return full_ref # We found a LogicalId.Property combination that can be resolved. Construct the output by replacing # the part of the reference string and not constructing a new ref. This allows us to support GetAtt-like # syntax and retain other attributes. Ex: ${LogicalId.Property.Arn} => ${SomeOtherLogicalId.Arn} replacement = self._resource_ref_separator.join([logical_id, property]) return full_ref.replace(replacement, resolved_value) return self._handle_sub_action(input_dict, do_replacement)
python
def resolve_resource_refs(self, input_dict, supported_resource_refs): """ Resolves reference to some property of a resource. Inside string to be substituted, there could be either a "Ref" or a "GetAtt" usage of this property. They have to be handled differently. Ref usages are directly converted to a Ref on the resolved value. GetAtt usages are split under the assumption that there can be only one property of resource referenced here. Everything else is an attribute reference. Example: Let's say `LogicalId.Property` will be resolved to `ResolvedValue` Ref usage: ${LogicalId.Property} => ${ResolvedValue} GetAtt usage: ${LogicalId.Property.Arn} => ${ResolvedValue.Arn} ${LogicalId.Property.Attr1.Attr2} => {ResolvedValue.Attr1.Attr2} :param input_dict: Dictionary to be resolved :param samtranslator.intrinsics.resource_refs.SupportedResourceReferences supported_resource_refs: Instance of an `SupportedResourceReferences` object that contain value of the property. :return: Resolved dictionary """ def do_replacement(full_ref, ref_value): """ Perform the appropriate replacement to handle ${LogicalId.Property} type references inside a Sub. This method is called to get the replacement string for each reference within Sub's value :param full_ref: Entire reference string such as "${LogicalId.Property}" :param ref_value: Just the value of the reference such as "LogicalId.Property" :return: Resolved reference of the structure "${SomeOtherLogicalId}". Result should always include the ${} structure since we are not resolving to final value, but just converting one reference to another """ # Split the value by separator, expecting to separate out LogicalId.Property splits = ref_value.split(self._resource_ref_separator) # If we don't find at least two parts, there is nothing to resolve if len(splits) < 2: return full_ref logical_id = splits[0] property = splits[1] resolved_value = supported_resource_refs.get(logical_id, property) if not resolved_value: # This ID/property combination is not in the supported references return full_ref # We found a LogicalId.Property combination that can be resolved. Construct the output by replacing # the part of the reference string and not constructing a new ref. This allows us to support GetAtt-like # syntax and retain other attributes. Ex: ${LogicalId.Property.Arn} => ${SomeOtherLogicalId.Arn} replacement = self._resource_ref_separator.join([logical_id, property]) return full_ref.replace(replacement, resolved_value) return self._handle_sub_action(input_dict, do_replacement)
[ "def", "resolve_resource_refs", "(", "self", ",", "input_dict", ",", "supported_resource_refs", ")", ":", "def", "do_replacement", "(", "full_ref", ",", "ref_value", ")", ":", "\"\"\"\n Perform the appropriate replacement to handle ${LogicalId.Property} type references inside a Sub.\n This method is called to get the replacement string for each reference within Sub's value\n\n :param full_ref: Entire reference string such as \"${LogicalId.Property}\"\n :param ref_value: Just the value of the reference such as \"LogicalId.Property\"\n :return: Resolved reference of the structure \"${SomeOtherLogicalId}\". Result should always include the\n ${} structure since we are not resolving to final value, but just converting one reference to another\n \"\"\"", "# Split the value by separator, expecting to separate out LogicalId.Property", "splits", "=", "ref_value", ".", "split", "(", "self", ".", "_resource_ref_separator", ")", "# If we don't find at least two parts, there is nothing to resolve", "if", "len", "(", "splits", ")", "<", "2", ":", "return", "full_ref", "logical_id", "=", "splits", "[", "0", "]", "property", "=", "splits", "[", "1", "]", "resolved_value", "=", "supported_resource_refs", ".", "get", "(", "logical_id", ",", "property", ")", "if", "not", "resolved_value", ":", "# This ID/property combination is not in the supported references", "return", "full_ref", "# We found a LogicalId.Property combination that can be resolved. Construct the output by replacing", "# the part of the reference string and not constructing a new ref. This allows us to support GetAtt-like", "# syntax and retain other attributes. Ex: ${LogicalId.Property.Arn} => ${SomeOtherLogicalId.Arn}", "replacement", "=", "self", ".", "_resource_ref_separator", ".", "join", "(", "[", "logical_id", ",", "property", "]", ")", "return", "full_ref", ".", "replace", "(", "replacement", ",", "resolved_value", ")", "return", "self", ".", "_handle_sub_action", "(", "input_dict", ",", "do_replacement", ")" ]
Resolves reference to some property of a resource. Inside string to be substituted, there could be either a "Ref" or a "GetAtt" usage of this property. They have to be handled differently. Ref usages are directly converted to a Ref on the resolved value. GetAtt usages are split under the assumption that there can be only one property of resource referenced here. Everything else is an attribute reference. Example: Let's say `LogicalId.Property` will be resolved to `ResolvedValue` Ref usage: ${LogicalId.Property} => ${ResolvedValue} GetAtt usage: ${LogicalId.Property.Arn} => ${ResolvedValue.Arn} ${LogicalId.Property.Attr1.Attr2} => {ResolvedValue.Attr1.Attr2} :param input_dict: Dictionary to be resolved :param samtranslator.intrinsics.resource_refs.SupportedResourceReferences supported_resource_refs: Instance of an `SupportedResourceReferences` object that contain value of the property. :return: Resolved dictionary
[ "Resolves", "reference", "to", "some", "property", "of", "a", "resource", ".", "Inside", "string", "to", "be", "substituted", "there", "could", "be", "either", "a", "Ref", "or", "a", "GetAtt", "usage", "of", "this", "property", ".", "They", "have", "to", "be", "handled", "differently", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L196-L253
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
SubAction.resolve_resource_id_refs
def resolve_resource_id_refs(self, input_dict, supported_resource_id_refs): """ Resolves reference to some property of a resource. Inside string to be substituted, there could be either a "Ref" or a "GetAtt" usage of this property. They have to be handled differently. Ref usages are directly converted to a Ref on the resolved value. GetAtt usages are split under the assumption that there can be only one property of resource referenced here. Everything else is an attribute reference. Example: Let's say `LogicalId` will be resolved to `NewLogicalId` Ref usage: ${LogicalId} => ${NewLogicalId} GetAtt usage: ${LogicalId.Arn} => ${NewLogicalId.Arn} ${LogicalId.Attr1.Attr2} => {NewLogicalId.Attr1.Attr2} :param input_dict: Dictionary to be resolved :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return: Resolved dictionary """ def do_replacement(full_ref, ref_value): """ Perform the appropriate replacement to handle ${LogicalId} type references inside a Sub. This method is called to get the replacement string for each reference within Sub's value :param full_ref: Entire reference string such as "${LogicalId.Property}" :param ref_value: Just the value of the reference such as "LogicalId.Property" :return: Resolved reference of the structure "${SomeOtherLogicalId}". Result should always include the ${} structure since we are not resolving to final value, but just converting one reference to another """ # Split the value by separator, expecting to separate out LogicalId splits = ref_value.split(self._resource_ref_separator) # If we don't find at least one part, there is nothing to resolve if len(splits) < 1: return full_ref logical_id = splits[0] resolved_value = supported_resource_id_refs.get(logical_id) if not resolved_value: # This ID/property combination is not in the supported references return full_ref # We found a LogicalId.Property combination that can be resolved. Construct the output by replacing # the part of the reference string and not constructing a new ref. This allows us to support GetAtt-like # syntax and retain other attributes. Ex: ${LogicalId.Property.Arn} => ${SomeOtherLogicalId.Arn} return full_ref.replace(logical_id, resolved_value) return self._handle_sub_action(input_dict, do_replacement)
python
def resolve_resource_id_refs(self, input_dict, supported_resource_id_refs): """ Resolves reference to some property of a resource. Inside string to be substituted, there could be either a "Ref" or a "GetAtt" usage of this property. They have to be handled differently. Ref usages are directly converted to a Ref on the resolved value. GetAtt usages are split under the assumption that there can be only one property of resource referenced here. Everything else is an attribute reference. Example: Let's say `LogicalId` will be resolved to `NewLogicalId` Ref usage: ${LogicalId} => ${NewLogicalId} GetAtt usage: ${LogicalId.Arn} => ${NewLogicalId.Arn} ${LogicalId.Attr1.Attr2} => {NewLogicalId.Attr1.Attr2} :param input_dict: Dictionary to be resolved :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return: Resolved dictionary """ def do_replacement(full_ref, ref_value): """ Perform the appropriate replacement to handle ${LogicalId} type references inside a Sub. This method is called to get the replacement string for each reference within Sub's value :param full_ref: Entire reference string such as "${LogicalId.Property}" :param ref_value: Just the value of the reference such as "LogicalId.Property" :return: Resolved reference of the structure "${SomeOtherLogicalId}". Result should always include the ${} structure since we are not resolving to final value, but just converting one reference to another """ # Split the value by separator, expecting to separate out LogicalId splits = ref_value.split(self._resource_ref_separator) # If we don't find at least one part, there is nothing to resolve if len(splits) < 1: return full_ref logical_id = splits[0] resolved_value = supported_resource_id_refs.get(logical_id) if not resolved_value: # This ID/property combination is not in the supported references return full_ref # We found a LogicalId.Property combination that can be resolved. Construct the output by replacing # the part of the reference string and not constructing a new ref. This allows us to support GetAtt-like # syntax and retain other attributes. Ex: ${LogicalId.Property.Arn} => ${SomeOtherLogicalId.Arn} return full_ref.replace(logical_id, resolved_value) return self._handle_sub_action(input_dict, do_replacement)
[ "def", "resolve_resource_id_refs", "(", "self", ",", "input_dict", ",", "supported_resource_id_refs", ")", ":", "def", "do_replacement", "(", "full_ref", ",", "ref_value", ")", ":", "\"\"\"\n Perform the appropriate replacement to handle ${LogicalId} type references inside a Sub.\n This method is called to get the replacement string for each reference within Sub's value\n\n :param full_ref: Entire reference string such as \"${LogicalId.Property}\"\n :param ref_value: Just the value of the reference such as \"LogicalId.Property\"\n :return: Resolved reference of the structure \"${SomeOtherLogicalId}\". Result should always include the\n ${} structure since we are not resolving to final value, but just converting one reference to another\n \"\"\"", "# Split the value by separator, expecting to separate out LogicalId", "splits", "=", "ref_value", ".", "split", "(", "self", ".", "_resource_ref_separator", ")", "# If we don't find at least one part, there is nothing to resolve", "if", "len", "(", "splits", ")", "<", "1", ":", "return", "full_ref", "logical_id", "=", "splits", "[", "0", "]", "resolved_value", "=", "supported_resource_id_refs", ".", "get", "(", "logical_id", ")", "if", "not", "resolved_value", ":", "# This ID/property combination is not in the supported references", "return", "full_ref", "# We found a LogicalId.Property combination that can be resolved. Construct the output by replacing", "# the part of the reference string and not constructing a new ref. This allows us to support GetAtt-like", "# syntax and retain other attributes. Ex: ${LogicalId.Property.Arn} => ${SomeOtherLogicalId.Arn}", "return", "full_ref", ".", "replace", "(", "logical_id", ",", "resolved_value", ")", "return", "self", ".", "_handle_sub_action", "(", "input_dict", ",", "do_replacement", ")" ]
Resolves reference to some property of a resource. Inside string to be substituted, there could be either a "Ref" or a "GetAtt" usage of this property. They have to be handled differently. Ref usages are directly converted to a Ref on the resolved value. GetAtt usages are split under the assumption that there can be only one property of resource referenced here. Everything else is an attribute reference. Example: Let's say `LogicalId` will be resolved to `NewLogicalId` Ref usage: ${LogicalId} => ${NewLogicalId} GetAtt usage: ${LogicalId.Arn} => ${NewLogicalId.Arn} ${LogicalId.Attr1.Attr2} => {NewLogicalId.Attr1.Attr2} :param input_dict: Dictionary to be resolved :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return: Resolved dictionary
[ "Resolves", "reference", "to", "some", "property", "of", "a", "resource", ".", "Inside", "string", "to", "be", "substituted", "there", "could", "be", "either", "a", "Ref", "or", "a", "GetAtt", "usage", "of", "this", "property", ".", "They", "have", "to", "be", "handled", "differently", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L255-L309
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
SubAction._handle_sub_action
def _handle_sub_action(self, input_dict, handler): """ Handles resolving replacements in the Sub action based on the handler that is passed as an input. :param input_dict: Dictionary to be resolved :param supported_values: One of several different objects that contain the supported values that need to be changed. See each method above for specifics on these objects. :param handler: handler that is specific to each implementation. :return: Resolved value of the Sub dictionary """ if not self.can_handle(input_dict): return input_dict key = self.intrinsic_name sub_value = input_dict[key] input_dict[key] = self._handle_sub_value(sub_value, handler) return input_dict
python
def _handle_sub_action(self, input_dict, handler): """ Handles resolving replacements in the Sub action based on the handler that is passed as an input. :param input_dict: Dictionary to be resolved :param supported_values: One of several different objects that contain the supported values that need to be changed. See each method above for specifics on these objects. :param handler: handler that is specific to each implementation. :return: Resolved value of the Sub dictionary """ if not self.can_handle(input_dict): return input_dict key = self.intrinsic_name sub_value = input_dict[key] input_dict[key] = self._handle_sub_value(sub_value, handler) return input_dict
[ "def", "_handle_sub_action", "(", "self", ",", "input_dict", ",", "handler", ")", ":", "if", "not", "self", ".", "can_handle", "(", "input_dict", ")", ":", "return", "input_dict", "key", "=", "self", ".", "intrinsic_name", "sub_value", "=", "input_dict", "[", "key", "]", "input_dict", "[", "key", "]", "=", "self", ".", "_handle_sub_value", "(", "sub_value", ",", "handler", ")", "return", "input_dict" ]
Handles resolving replacements in the Sub action based on the handler that is passed as an input. :param input_dict: Dictionary to be resolved :param supported_values: One of several different objects that contain the supported values that need to be changed. See each method above for specifics on these objects. :param handler: handler that is specific to each implementation. :return: Resolved value of the Sub dictionary
[ "Handles", "resolving", "replacements", "in", "the", "Sub", "action", "based", "on", "the", "handler", "that", "is", "passed", "as", "an", "input", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L311-L329
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
SubAction._handle_sub_value
def _handle_sub_value(self, sub_value, handler_method): """ Generic method to handle value to Fn::Sub key. We are interested in parsing the ${} syntaxes inside the string portion of the value. :param sub_value: Value of the Sub function :param handler_method: Method to be called on every occurrence of `${LogicalId}` structure within the string. Implementation could resolve and replace this structure with whatever they seem fit :return: Resolved value of the Sub dictionary """ # Just handle known references within the string to be substituted and return the whole dictionary # because that's the best we can do here. if isinstance(sub_value, string_types): # Ex: {Fn::Sub: "some string"} sub_value = self._sub_all_refs(sub_value, handler_method) elif isinstance(sub_value, list) and len(sub_value) > 0 and isinstance(sub_value[0], string_types): # Ex: {Fn::Sub: ["some string", {a:b}] } sub_value[0] = self._sub_all_refs(sub_value[0], handler_method) return sub_value
python
def _handle_sub_value(self, sub_value, handler_method): """ Generic method to handle value to Fn::Sub key. We are interested in parsing the ${} syntaxes inside the string portion of the value. :param sub_value: Value of the Sub function :param handler_method: Method to be called on every occurrence of `${LogicalId}` structure within the string. Implementation could resolve and replace this structure with whatever they seem fit :return: Resolved value of the Sub dictionary """ # Just handle known references within the string to be substituted and return the whole dictionary # because that's the best we can do here. if isinstance(sub_value, string_types): # Ex: {Fn::Sub: "some string"} sub_value = self._sub_all_refs(sub_value, handler_method) elif isinstance(sub_value, list) and len(sub_value) > 0 and isinstance(sub_value[0], string_types): # Ex: {Fn::Sub: ["some string", {a:b}] } sub_value[0] = self._sub_all_refs(sub_value[0], handler_method) return sub_value
[ "def", "_handle_sub_value", "(", "self", ",", "sub_value", ",", "handler_method", ")", ":", "# Just handle known references within the string to be substituted and return the whole dictionary", "# because that's the best we can do here.", "if", "isinstance", "(", "sub_value", ",", "string_types", ")", ":", "# Ex: {Fn::Sub: \"some string\"}", "sub_value", "=", "self", ".", "_sub_all_refs", "(", "sub_value", ",", "handler_method", ")", "elif", "isinstance", "(", "sub_value", ",", "list", ")", "and", "len", "(", "sub_value", ")", ">", "0", "and", "isinstance", "(", "sub_value", "[", "0", "]", ",", "string_types", ")", ":", "# Ex: {Fn::Sub: [\"some string\", {a:b}] }", "sub_value", "[", "0", "]", "=", "self", ".", "_sub_all_refs", "(", "sub_value", "[", "0", "]", ",", "handler_method", ")", "return", "sub_value" ]
Generic method to handle value to Fn::Sub key. We are interested in parsing the ${} syntaxes inside the string portion of the value. :param sub_value: Value of the Sub function :param handler_method: Method to be called on every occurrence of `${LogicalId}` structure within the string. Implementation could resolve and replace this structure with whatever they seem fit :return: Resolved value of the Sub dictionary
[ "Generic", "method", "to", "handle", "value", "to", "Fn", "::", "Sub", "key", ".", "We", "are", "interested", "in", "parsing", "the", "$", "{}", "syntaxes", "inside", "the", "string", "portion", "of", "the", "value", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L331-L352
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
SubAction._sub_all_refs
def _sub_all_refs(self, text, handler_method): """ Substitute references within a string that is using ${key} syntax by calling the `handler_method` on every occurrence of this structure. The value returned by this method directly replaces the reference structure. Ex: text = "${key1}-hello-${key2} def handler_method(full_ref, ref_value): return "foo" _sub_all_refs(text, handler_method) will output "foo-hello-foo" :param string text: Input text :param handler_method: Method to be called to handle each occurrence of ${blah} reference structure. First parameter to this method is the full reference structure Ex: ${LogicalId.Property}. Second parameter is just the value of the reference such as "LogicalId.Property" :return string: Text with all reference structures replaced as necessary """ # RegExp to find pattern "${logicalId.property}" and return the word inside bracket logical_id_regex = '[A-Za-z0-9\.]+|AWS::[A-Z][A-Za-z]*' ref_pattern = re.compile(r'\$\{(' + logical_id_regex + ')\}') # Find all the pattern, and call the handler to decide how to substitute them. # Do the substitution and return the final text return re.sub(ref_pattern, # Pass the handler entire string ${logicalId.property} as first parameter and "logicalId.property" # as second parameter. Return value will be substituted lambda match: handler_method(match.group(0), match.group(1)), text)
python
def _sub_all_refs(self, text, handler_method): """ Substitute references within a string that is using ${key} syntax by calling the `handler_method` on every occurrence of this structure. The value returned by this method directly replaces the reference structure. Ex: text = "${key1}-hello-${key2} def handler_method(full_ref, ref_value): return "foo" _sub_all_refs(text, handler_method) will output "foo-hello-foo" :param string text: Input text :param handler_method: Method to be called to handle each occurrence of ${blah} reference structure. First parameter to this method is the full reference structure Ex: ${LogicalId.Property}. Second parameter is just the value of the reference such as "LogicalId.Property" :return string: Text with all reference structures replaced as necessary """ # RegExp to find pattern "${logicalId.property}" and return the word inside bracket logical_id_regex = '[A-Za-z0-9\.]+|AWS::[A-Z][A-Za-z]*' ref_pattern = re.compile(r'\$\{(' + logical_id_regex + ')\}') # Find all the pattern, and call the handler to decide how to substitute them. # Do the substitution and return the final text return re.sub(ref_pattern, # Pass the handler entire string ${logicalId.property} as first parameter and "logicalId.property" # as second parameter. Return value will be substituted lambda match: handler_method(match.group(0), match.group(1)), text)
[ "def", "_sub_all_refs", "(", "self", ",", "text", ",", "handler_method", ")", ":", "# RegExp to find pattern \"${logicalId.property}\" and return the word inside bracket", "logical_id_regex", "=", "'[A-Za-z0-9\\.]+|AWS::[A-Z][A-Za-z]*'", "ref_pattern", "=", "re", ".", "compile", "(", "r'\\$\\{('", "+", "logical_id_regex", "+", "')\\}'", ")", "# Find all the pattern, and call the handler to decide how to substitute them.", "# Do the substitution and return the final text", "return", "re", ".", "sub", "(", "ref_pattern", ",", "# Pass the handler entire string ${logicalId.property} as first parameter and \"logicalId.property\"", "# as second parameter. Return value will be substituted", "lambda", "match", ":", "handler_method", "(", "match", ".", "group", "(", "0", ")", ",", "match", ".", "group", "(", "1", ")", ")", ",", "text", ")" ]
Substitute references within a string that is using ${key} syntax by calling the `handler_method` on every occurrence of this structure. The value returned by this method directly replaces the reference structure. Ex: text = "${key1}-hello-${key2} def handler_method(full_ref, ref_value): return "foo" _sub_all_refs(text, handler_method) will output "foo-hello-foo" :param string text: Input text :param handler_method: Method to be called to handle each occurrence of ${blah} reference structure. First parameter to this method is the full reference structure Ex: ${LogicalId.Property}. Second parameter is just the value of the reference such as "LogicalId.Property" :return string: Text with all reference structures replaced as necessary
[ "Substitute", "references", "within", "a", "string", "that", "is", "using", "$", "{", "key", "}", "syntax", "by", "calling", "the", "handler_method", "on", "every", "occurrence", "of", "this", "structure", ".", "The", "value", "returned", "by", "this", "method", "directly", "replaces", "the", "reference", "structure", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L354-L384
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
GetAttAction.resolve_resource_refs
def resolve_resource_refs(self, input_dict, supported_resource_refs): """ Resolve resource references within a GetAtt dict. Example: { "Fn::GetAtt": ["LogicalId.Property", "Arn"] } => {"Fn::GetAtt": ["ResolvedLogicalId", "Arn"]} Theoretically, only the first element of the array can contain reference to SAM resources. The second element is name of an attribute (like Arn) of the resource. However tools like AWS CLI apply the assumption that first element of the array is a LogicalId and cannot contain a 'dot'. So they break at the first dot to convert YAML tag to JSON map like this: `!GetAtt LogicalId.Property.Arn` => {"Fn::GetAtt": [ "LogicalId", "Property.Arn" ] } Therefore to resolve the reference, we join the array into a string, break it back up to check if it contains a known reference, and resolve it if we can. :param input_dict: Dictionary to be resolved :param samtransaltor.intrinsics.resource_refs.SupportedResourceReferences supported_resource_refs: Instance of an `SupportedResourceReferences` object that contain value of the property. :return: Resolved dictionary """ if not self.can_handle(input_dict): return input_dict key = self.intrinsic_name value = input_dict[key] # Value must be an array with *at least* two elements. If not, this is invalid GetAtt syntax. We just pass along # the input to CFN for it to do the "official" validation. if not isinstance(value, list) or len(value) < 2: return input_dict if (not all(isinstance(entry, string_types) for entry in value)): raise InvalidDocumentException( [InvalidTemplateException('Invalid GetAtt value {}. GetAtt expects an array with 2 strings.' .format(value))]) # Value of GetAtt is an array. It can contain any number of elements, with first being the LogicalId of # resource and rest being the attributes. In a SAM template, a reference to a resource can be used in the # first parameter. However tools like AWS CLI might break them down as well. So let's just concatenate # all elements, and break them into separate parts in a more standard way. # # Example: # { Fn::GetAtt: ["LogicalId.Property", "Arn"] } is equivalent to { Fn::GetAtt: ["LogicalId", "Property.Arn"] } # Former is the correct notation. However tools like AWS CLI can construct the later style. # Let's normalize the value into "LogicalId.Property.Arn" to handle both scenarios value_str = self._resource_ref_separator.join(value) splits = value_str.split(self._resource_ref_separator) logical_id = splits[0] property = splits[1] remaining = splits[2:] # if any resolved_value = supported_resource_refs.get(logical_id, property) return self._get_resolved_dictionary(input_dict, key, resolved_value, remaining)
python
def resolve_resource_refs(self, input_dict, supported_resource_refs): """ Resolve resource references within a GetAtt dict. Example: { "Fn::GetAtt": ["LogicalId.Property", "Arn"] } => {"Fn::GetAtt": ["ResolvedLogicalId", "Arn"]} Theoretically, only the first element of the array can contain reference to SAM resources. The second element is name of an attribute (like Arn) of the resource. However tools like AWS CLI apply the assumption that first element of the array is a LogicalId and cannot contain a 'dot'. So they break at the first dot to convert YAML tag to JSON map like this: `!GetAtt LogicalId.Property.Arn` => {"Fn::GetAtt": [ "LogicalId", "Property.Arn" ] } Therefore to resolve the reference, we join the array into a string, break it back up to check if it contains a known reference, and resolve it if we can. :param input_dict: Dictionary to be resolved :param samtransaltor.intrinsics.resource_refs.SupportedResourceReferences supported_resource_refs: Instance of an `SupportedResourceReferences` object that contain value of the property. :return: Resolved dictionary """ if not self.can_handle(input_dict): return input_dict key = self.intrinsic_name value = input_dict[key] # Value must be an array with *at least* two elements. If not, this is invalid GetAtt syntax. We just pass along # the input to CFN for it to do the "official" validation. if not isinstance(value, list) or len(value) < 2: return input_dict if (not all(isinstance(entry, string_types) for entry in value)): raise InvalidDocumentException( [InvalidTemplateException('Invalid GetAtt value {}. GetAtt expects an array with 2 strings.' .format(value))]) # Value of GetAtt is an array. It can contain any number of elements, with first being the LogicalId of # resource and rest being the attributes. In a SAM template, a reference to a resource can be used in the # first parameter. However tools like AWS CLI might break them down as well. So let's just concatenate # all elements, and break them into separate parts in a more standard way. # # Example: # { Fn::GetAtt: ["LogicalId.Property", "Arn"] } is equivalent to { Fn::GetAtt: ["LogicalId", "Property.Arn"] } # Former is the correct notation. However tools like AWS CLI can construct the later style. # Let's normalize the value into "LogicalId.Property.Arn" to handle both scenarios value_str = self._resource_ref_separator.join(value) splits = value_str.split(self._resource_ref_separator) logical_id = splits[0] property = splits[1] remaining = splits[2:] # if any resolved_value = supported_resource_refs.get(logical_id, property) return self._get_resolved_dictionary(input_dict, key, resolved_value, remaining)
[ "def", "resolve_resource_refs", "(", "self", ",", "input_dict", ",", "supported_resource_refs", ")", ":", "if", "not", "self", ".", "can_handle", "(", "input_dict", ")", ":", "return", "input_dict", "key", "=", "self", ".", "intrinsic_name", "value", "=", "input_dict", "[", "key", "]", "# Value must be an array with *at least* two elements. If not, this is invalid GetAtt syntax. We just pass along", "# the input to CFN for it to do the \"official\" validation.", "if", "not", "isinstance", "(", "value", ",", "list", ")", "or", "len", "(", "value", ")", "<", "2", ":", "return", "input_dict", "if", "(", "not", "all", "(", "isinstance", "(", "entry", ",", "string_types", ")", "for", "entry", "in", "value", ")", ")", ":", "raise", "InvalidDocumentException", "(", "[", "InvalidTemplateException", "(", "'Invalid GetAtt value {}. GetAtt expects an array with 2 strings.'", ".", "format", "(", "value", ")", ")", "]", ")", "# Value of GetAtt is an array. It can contain any number of elements, with first being the LogicalId of", "# resource and rest being the attributes. In a SAM template, a reference to a resource can be used in the", "# first parameter. However tools like AWS CLI might break them down as well. So let's just concatenate", "# all elements, and break them into separate parts in a more standard way.", "#", "# Example:", "# { Fn::GetAtt: [\"LogicalId.Property\", \"Arn\"] } is equivalent to { Fn::GetAtt: [\"LogicalId\", \"Property.Arn\"] }", "# Former is the correct notation. However tools like AWS CLI can construct the later style.", "# Let's normalize the value into \"LogicalId.Property.Arn\" to handle both scenarios", "value_str", "=", "self", ".", "_resource_ref_separator", ".", "join", "(", "value", ")", "splits", "=", "value_str", ".", "split", "(", "self", ".", "_resource_ref_separator", ")", "logical_id", "=", "splits", "[", "0", "]", "property", "=", "splits", "[", "1", "]", "remaining", "=", "splits", "[", "2", ":", "]", "# if any", "resolved_value", "=", "supported_resource_refs", ".", "get", "(", "logical_id", ",", "property", ")", "return", "self", ".", "_get_resolved_dictionary", "(", "input_dict", ",", "key", ",", "resolved_value", ",", "remaining", ")" ]
Resolve resource references within a GetAtt dict. Example: { "Fn::GetAtt": ["LogicalId.Property", "Arn"] } => {"Fn::GetAtt": ["ResolvedLogicalId", "Arn"]} Theoretically, only the first element of the array can contain reference to SAM resources. The second element is name of an attribute (like Arn) of the resource. However tools like AWS CLI apply the assumption that first element of the array is a LogicalId and cannot contain a 'dot'. So they break at the first dot to convert YAML tag to JSON map like this: `!GetAtt LogicalId.Property.Arn` => {"Fn::GetAtt": [ "LogicalId", "Property.Arn" ] } Therefore to resolve the reference, we join the array into a string, break it back up to check if it contains a known reference, and resolve it if we can. :param input_dict: Dictionary to be resolved :param samtransaltor.intrinsics.resource_refs.SupportedResourceReferences supported_resource_refs: Instance of an `SupportedResourceReferences` object that contain value of the property. :return: Resolved dictionary
[ "Resolve", "resource", "references", "within", "a", "GetAtt", "dict", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L394-L452
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
GetAttAction.resolve_resource_id_refs
def resolve_resource_id_refs(self, input_dict, supported_resource_id_refs): """ Resolve resource references within a GetAtt dict. Example: { "Fn::GetAtt": ["LogicalId", "Arn"] } => {"Fn::GetAtt": ["ResolvedLogicalId", "Arn"]} Theoretically, only the first element of the array can contain reference to SAM resources. The second element is name of an attribute (like Arn) of the resource. However tools like AWS CLI apply the assumption that first element of the array is a LogicalId and cannot contain a 'dot'. So they break at the first dot to convert YAML tag to JSON map like this: `!GetAtt LogicalId.Arn` => {"Fn::GetAtt": [ "LogicalId", "Arn" ] } Therefore to resolve the reference, we join the array into a string, break it back up to check if it contains a known reference, and resolve it if we can. :param input_dict: Dictionary to be resolved :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return: Resolved dictionary """ if not self.can_handle(input_dict): return input_dict key = self.intrinsic_name value = input_dict[key] # Value must be an array with *at least* two elements. If not, this is invalid GetAtt syntax. We just pass along # the input to CFN for it to do the "official" validation. if not isinstance(value, list) or len(value) < 2: return input_dict value_str = self._resource_ref_separator.join(value) splits = value_str.split(self._resource_ref_separator) logical_id = splits[0] remaining = splits[1:] # if any resolved_value = supported_resource_id_refs.get(logical_id) return self._get_resolved_dictionary(input_dict, key, resolved_value, remaining)
python
def resolve_resource_id_refs(self, input_dict, supported_resource_id_refs): """ Resolve resource references within a GetAtt dict. Example: { "Fn::GetAtt": ["LogicalId", "Arn"] } => {"Fn::GetAtt": ["ResolvedLogicalId", "Arn"]} Theoretically, only the first element of the array can contain reference to SAM resources. The second element is name of an attribute (like Arn) of the resource. However tools like AWS CLI apply the assumption that first element of the array is a LogicalId and cannot contain a 'dot'. So they break at the first dot to convert YAML tag to JSON map like this: `!GetAtt LogicalId.Arn` => {"Fn::GetAtt": [ "LogicalId", "Arn" ] } Therefore to resolve the reference, we join the array into a string, break it back up to check if it contains a known reference, and resolve it if we can. :param input_dict: Dictionary to be resolved :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return: Resolved dictionary """ if not self.can_handle(input_dict): return input_dict key = self.intrinsic_name value = input_dict[key] # Value must be an array with *at least* two elements. If not, this is invalid GetAtt syntax. We just pass along # the input to CFN for it to do the "official" validation. if not isinstance(value, list) or len(value) < 2: return input_dict value_str = self._resource_ref_separator.join(value) splits = value_str.split(self._resource_ref_separator) logical_id = splits[0] remaining = splits[1:] # if any resolved_value = supported_resource_id_refs.get(logical_id) return self._get_resolved_dictionary(input_dict, key, resolved_value, remaining)
[ "def", "resolve_resource_id_refs", "(", "self", ",", "input_dict", ",", "supported_resource_id_refs", ")", ":", "if", "not", "self", ".", "can_handle", "(", "input_dict", ")", ":", "return", "input_dict", "key", "=", "self", ".", "intrinsic_name", "value", "=", "input_dict", "[", "key", "]", "# Value must be an array with *at least* two elements. If not, this is invalid GetAtt syntax. We just pass along", "# the input to CFN for it to do the \"official\" validation.", "if", "not", "isinstance", "(", "value", ",", "list", ")", "or", "len", "(", "value", ")", "<", "2", ":", "return", "input_dict", "value_str", "=", "self", ".", "_resource_ref_separator", ".", "join", "(", "value", ")", "splits", "=", "value_str", ".", "split", "(", "self", ".", "_resource_ref_separator", ")", "logical_id", "=", "splits", "[", "0", "]", "remaining", "=", "splits", "[", "1", ":", "]", "# if any", "resolved_value", "=", "supported_resource_id_refs", ".", "get", "(", "logical_id", ")", "return", "self", ".", "_get_resolved_dictionary", "(", "input_dict", ",", "key", ",", "resolved_value", ",", "remaining", ")" ]
Resolve resource references within a GetAtt dict. Example: { "Fn::GetAtt": ["LogicalId", "Arn"] } => {"Fn::GetAtt": ["ResolvedLogicalId", "Arn"]} Theoretically, only the first element of the array can contain reference to SAM resources. The second element is name of an attribute (like Arn) of the resource. However tools like AWS CLI apply the assumption that first element of the array is a LogicalId and cannot contain a 'dot'. So they break at the first dot to convert YAML tag to JSON map like this: `!GetAtt LogicalId.Arn` => {"Fn::GetAtt": [ "LogicalId", "Arn" ] } Therefore to resolve the reference, we join the array into a string, break it back up to check if it contains a known reference, and resolve it if we can. :param input_dict: Dictionary to be resolved :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return: Resolved dictionary
[ "Resolve", "resource", "references", "within", "a", "GetAtt", "dict", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L454-L495
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
GetAttAction._get_resolved_dictionary
def _get_resolved_dictionary(self, input_dict, key, resolved_value, remaining): """ Resolves the function and returns the updated dictionary :param input_dict: Dictionary to be resolved :param key: Name of this intrinsic. :param resolved_value: Resolved or updated value for this action. :param remaining: Remaining sections for the GetAtt action. """ if resolved_value: # We resolved to a new resource logicalId. Use this as the first element and keep remaining elements intact # This is the new value of Fn::GetAtt input_dict[key] = [resolved_value] + remaining return input_dict
python
def _get_resolved_dictionary(self, input_dict, key, resolved_value, remaining): """ Resolves the function and returns the updated dictionary :param input_dict: Dictionary to be resolved :param key: Name of this intrinsic. :param resolved_value: Resolved or updated value for this action. :param remaining: Remaining sections for the GetAtt action. """ if resolved_value: # We resolved to a new resource logicalId. Use this as the first element and keep remaining elements intact # This is the new value of Fn::GetAtt input_dict[key] = [resolved_value] + remaining return input_dict
[ "def", "_get_resolved_dictionary", "(", "self", ",", "input_dict", ",", "key", ",", "resolved_value", ",", "remaining", ")", ":", "if", "resolved_value", ":", "# We resolved to a new resource logicalId. Use this as the first element and keep remaining elements intact", "# This is the new value of Fn::GetAtt", "input_dict", "[", "key", "]", "=", "[", "resolved_value", "]", "+", "remaining", "return", "input_dict" ]
Resolves the function and returns the updated dictionary :param input_dict: Dictionary to be resolved :param key: Name of this intrinsic. :param resolved_value: Resolved or updated value for this action. :param remaining: Remaining sections for the GetAtt action.
[ "Resolves", "the", "function", "and", "returns", "the", "updated", "dictionary" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L497-L511
train
awslabs/serverless-application-model
samtranslator/intrinsics/actions.py
FindInMapAction.resolve_parameter_refs
def resolve_parameter_refs(self, input_dict, parameters): """ Recursively resolves "Fn::FindInMap"references that are present in the mappings and returns the value. If it is not in mappings, this method simply returns the input unchanged. :param input_dict: Dictionary representing the FindInMap function. Must contain only one key and it should be "Fn::FindInMap". :param parameters: Dictionary of mappings from the SAM template """ if not self.can_handle(input_dict): return input_dict value = input_dict[self.intrinsic_name] # FindInMap expects an array with 3 values if not isinstance(value, list) or len(value) != 3: raise InvalidDocumentException( [InvalidTemplateException('Invalid FindInMap value {}. FindInMap expects an array with 3 values.' .format(value))]) map_name = self.resolve_parameter_refs(value[0], parameters) top_level_key = self.resolve_parameter_refs(value[1], parameters) second_level_key = self.resolve_parameter_refs(value[2], parameters) if not isinstance(map_name, string_types) or \ not isinstance(top_level_key, string_types) or \ not isinstance(second_level_key, string_types): return input_dict if map_name not in parameters or \ top_level_key not in parameters[map_name] or \ second_level_key not in parameters[map_name][top_level_key]: return input_dict return parameters[map_name][top_level_key][second_level_key]
python
def resolve_parameter_refs(self, input_dict, parameters): """ Recursively resolves "Fn::FindInMap"references that are present in the mappings and returns the value. If it is not in mappings, this method simply returns the input unchanged. :param input_dict: Dictionary representing the FindInMap function. Must contain only one key and it should be "Fn::FindInMap". :param parameters: Dictionary of mappings from the SAM template """ if not self.can_handle(input_dict): return input_dict value = input_dict[self.intrinsic_name] # FindInMap expects an array with 3 values if not isinstance(value, list) or len(value) != 3: raise InvalidDocumentException( [InvalidTemplateException('Invalid FindInMap value {}. FindInMap expects an array with 3 values.' .format(value))]) map_name = self.resolve_parameter_refs(value[0], parameters) top_level_key = self.resolve_parameter_refs(value[1], parameters) second_level_key = self.resolve_parameter_refs(value[2], parameters) if not isinstance(map_name, string_types) or \ not isinstance(top_level_key, string_types) or \ not isinstance(second_level_key, string_types): return input_dict if map_name not in parameters or \ top_level_key not in parameters[map_name] or \ second_level_key not in parameters[map_name][top_level_key]: return input_dict return parameters[map_name][top_level_key][second_level_key]
[ "def", "resolve_parameter_refs", "(", "self", ",", "input_dict", ",", "parameters", ")", ":", "if", "not", "self", ".", "can_handle", "(", "input_dict", ")", ":", "return", "input_dict", "value", "=", "input_dict", "[", "self", ".", "intrinsic_name", "]", "# FindInMap expects an array with 3 values", "if", "not", "isinstance", "(", "value", ",", "list", ")", "or", "len", "(", "value", ")", "!=", "3", ":", "raise", "InvalidDocumentException", "(", "[", "InvalidTemplateException", "(", "'Invalid FindInMap value {}. FindInMap expects an array with 3 values.'", ".", "format", "(", "value", ")", ")", "]", ")", "map_name", "=", "self", ".", "resolve_parameter_refs", "(", "value", "[", "0", "]", ",", "parameters", ")", "top_level_key", "=", "self", ".", "resolve_parameter_refs", "(", "value", "[", "1", "]", ",", "parameters", ")", "second_level_key", "=", "self", ".", "resolve_parameter_refs", "(", "value", "[", "2", "]", ",", "parameters", ")", "if", "not", "isinstance", "(", "map_name", ",", "string_types", ")", "or", "not", "isinstance", "(", "top_level_key", ",", "string_types", ")", "or", "not", "isinstance", "(", "second_level_key", ",", "string_types", ")", ":", "return", "input_dict", "if", "map_name", "not", "in", "parameters", "or", "top_level_key", "not", "in", "parameters", "[", "map_name", "]", "or", "second_level_key", "not", "in", "parameters", "[", "map_name", "]", "[", "top_level_key", "]", ":", "return", "input_dict", "return", "parameters", "[", "map_name", "]", "[", "top_level_key", "]", "[", "second_level_key", "]" ]
Recursively resolves "Fn::FindInMap"references that are present in the mappings and returns the value. If it is not in mappings, this method simply returns the input unchanged. :param input_dict: Dictionary representing the FindInMap function. Must contain only one key and it should be "Fn::FindInMap". :param parameters: Dictionary of mappings from the SAM template
[ "Recursively", "resolves", "Fn", "::", "FindInMap", "references", "that", "are", "present", "in", "the", "mappings", "and", "returns", "the", "value", ".", "If", "it", "is", "not", "in", "mappings", "this", "method", "simply", "returns", "the", "input", "unchanged", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/actions.py#L520-L555
train
awslabs/serverless-application-model
examples/apps/datadog-process-rds-metrics/lambda_function.py
lambda_handler
def lambda_handler(event, context): ''' Process a RDS enhenced monitoring DATA_MESSAGE, coming from CLOUDWATCH LOGS ''' # event is a dict containing a base64 string gzipped event = json.loads(gzip.GzipFile(fileobj=StringIO(event['awslogs']['data'].decode('base64'))).read()) account = event['owner'] region = context.invoked_function_arn.split(':', 4)[3] log_events = event['logEvents'] for log_event in log_events: message = json.loads(log_event['message']) ts = log_event['timestamp'] / 1000 _process_rds_enhanced_monitoring_message(ts, message, account, region) stats.flush() return {'Status': 'OK'}
python
def lambda_handler(event, context): ''' Process a RDS enhenced monitoring DATA_MESSAGE, coming from CLOUDWATCH LOGS ''' # event is a dict containing a base64 string gzipped event = json.loads(gzip.GzipFile(fileobj=StringIO(event['awslogs']['data'].decode('base64'))).read()) account = event['owner'] region = context.invoked_function_arn.split(':', 4)[3] log_events = event['logEvents'] for log_event in log_events: message = json.loads(log_event['message']) ts = log_event['timestamp'] / 1000 _process_rds_enhanced_monitoring_message(ts, message, account, region) stats.flush() return {'Status': 'OK'}
[ "def", "lambda_handler", "(", "event", ",", "context", ")", ":", "# event is a dict containing a base64 string gzipped", "event", "=", "json", ".", "loads", "(", "gzip", ".", "GzipFile", "(", "fileobj", "=", "StringIO", "(", "event", "[", "'awslogs'", "]", "[", "'data'", "]", ".", "decode", "(", "'base64'", ")", ")", ")", ".", "read", "(", ")", ")", "account", "=", "event", "[", "'owner'", "]", "region", "=", "context", ".", "invoked_function_arn", ".", "split", "(", "':'", ",", "4", ")", "[", "3", "]", "log_events", "=", "event", "[", "'logEvents'", "]", "for", "log_event", "in", "log_events", ":", "message", "=", "json", ".", "loads", "(", "log_event", "[", "'message'", "]", ")", "ts", "=", "log_event", "[", "'timestamp'", "]", "/", "1000", "_process_rds_enhanced_monitoring_message", "(", "ts", ",", "message", ",", "account", ",", "region", ")", "stats", ".", "flush", "(", ")", "return", "{", "'Status'", ":", "'OK'", "}" ]
Process a RDS enhenced monitoring DATA_MESSAGE, coming from CLOUDWATCH LOGS
[ "Process", "a", "RDS", "enhenced", "monitoring", "DATA_MESSAGE", "coming", "from", "CLOUDWATCH", "LOGS" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/examples/apps/datadog-process-rds-metrics/lambda_function.py#L104-L122
train
awslabs/serverless-application-model
samtranslator/model/api/api_generator.py
ApiGenerator._construct_rest_api
def _construct_rest_api(self): """Constructs and returns the ApiGateway RestApi. :returns: the RestApi to which this SAM Api corresponds :rtype: model.apigateway.ApiGatewayRestApi """ rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on, attributes=self.resource_attributes) rest_api.BinaryMediaTypes = self.binary_media rest_api.MinimumCompressionSize = self.minimum_compression_size if self.endpoint_configuration: self._set_endpoint_configuration(rest_api, self.endpoint_configuration) elif not RegionConfiguration.is_apigw_edge_configuration_supported(): # Since this region does not support EDGE configuration, we explicitly set the endpoint type # to Regional which is the only supported config. self._set_endpoint_configuration(rest_api, "REGIONAL") if self.definition_uri and self.definition_body: raise InvalidResourceException(self.logical_id, "Specify either 'DefinitionUri' or 'DefinitionBody' property and not both") self._add_cors() self._add_auth() self._add_gateway_responses() if self.definition_uri: rest_api.BodyS3Location = self._construct_body_s3_dict() elif self.definition_body: rest_api.Body = self.definition_body if self.name: rest_api.Name = self.name return rest_api
python
def _construct_rest_api(self): """Constructs and returns the ApiGateway RestApi. :returns: the RestApi to which this SAM Api corresponds :rtype: model.apigateway.ApiGatewayRestApi """ rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on, attributes=self.resource_attributes) rest_api.BinaryMediaTypes = self.binary_media rest_api.MinimumCompressionSize = self.minimum_compression_size if self.endpoint_configuration: self._set_endpoint_configuration(rest_api, self.endpoint_configuration) elif not RegionConfiguration.is_apigw_edge_configuration_supported(): # Since this region does not support EDGE configuration, we explicitly set the endpoint type # to Regional which is the only supported config. self._set_endpoint_configuration(rest_api, "REGIONAL") if self.definition_uri and self.definition_body: raise InvalidResourceException(self.logical_id, "Specify either 'DefinitionUri' or 'DefinitionBody' property and not both") self._add_cors() self._add_auth() self._add_gateway_responses() if self.definition_uri: rest_api.BodyS3Location = self._construct_body_s3_dict() elif self.definition_body: rest_api.Body = self.definition_body if self.name: rest_api.Name = self.name return rest_api
[ "def", "_construct_rest_api", "(", "self", ")", ":", "rest_api", "=", "ApiGatewayRestApi", "(", "self", ".", "logical_id", ",", "depends_on", "=", "self", ".", "depends_on", ",", "attributes", "=", "self", ".", "resource_attributes", ")", "rest_api", ".", "BinaryMediaTypes", "=", "self", ".", "binary_media", "rest_api", ".", "MinimumCompressionSize", "=", "self", ".", "minimum_compression_size", "if", "self", ".", "endpoint_configuration", ":", "self", ".", "_set_endpoint_configuration", "(", "rest_api", ",", "self", ".", "endpoint_configuration", ")", "elif", "not", "RegionConfiguration", ".", "is_apigw_edge_configuration_supported", "(", ")", ":", "# Since this region does not support EDGE configuration, we explicitly set the endpoint type", "# to Regional which is the only supported config.", "self", ".", "_set_endpoint_configuration", "(", "rest_api", ",", "\"REGIONAL\"", ")", "if", "self", ".", "definition_uri", "and", "self", ".", "definition_body", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Specify either 'DefinitionUri' or 'DefinitionBody' property and not both\"", ")", "self", ".", "_add_cors", "(", ")", "self", ".", "_add_auth", "(", ")", "self", ".", "_add_gateway_responses", "(", ")", "if", "self", ".", "definition_uri", ":", "rest_api", ".", "BodyS3Location", "=", "self", ".", "_construct_body_s3_dict", "(", ")", "elif", "self", ".", "definition_body", ":", "rest_api", ".", "Body", "=", "self", ".", "definition_body", "if", "self", ".", "name", ":", "rest_api", ".", "Name", "=", "self", ".", "name", "return", "rest_api" ]
Constructs and returns the ApiGateway RestApi. :returns: the RestApi to which this SAM Api corresponds :rtype: model.apigateway.ApiGatewayRestApi
[ "Constructs", "and", "returns", "the", "ApiGateway", "RestApi", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/api/api_generator.py#L74-L108
train
awslabs/serverless-application-model
samtranslator/model/api/api_generator.py
ApiGenerator._construct_body_s3_dict
def _construct_body_s3_dict(self): """Constructs the RestApi's `BodyS3Location property`_, from the SAM Api's DefinitionUri property. :returns: a BodyS3Location dict, containing the S3 Bucket, Key, and Version of the Swagger definition :rtype: dict """ if isinstance(self.definition_uri, dict): if not self.definition_uri.get("Bucket", None) or not self.definition_uri.get("Key", None): # DefinitionUri is a dictionary but does not contain Bucket or Key property raise InvalidResourceException(self.logical_id, "'DefinitionUri' requires Bucket and Key properties to be specified") s3_pointer = self.definition_uri else: # DefinitionUri is a string s3_pointer = parse_s3_uri(self.definition_uri) if s3_pointer is None: raise InvalidResourceException(self.logical_id, '\'DefinitionUri\' is not a valid S3 Uri of the form ' '"s3://bucket/key" with optional versionId query parameter.') body_s3 = { 'Bucket': s3_pointer['Bucket'], 'Key': s3_pointer['Key'] } if 'Version' in s3_pointer: body_s3['Version'] = s3_pointer['Version'] return body_s3
python
def _construct_body_s3_dict(self): """Constructs the RestApi's `BodyS3Location property`_, from the SAM Api's DefinitionUri property. :returns: a BodyS3Location dict, containing the S3 Bucket, Key, and Version of the Swagger definition :rtype: dict """ if isinstance(self.definition_uri, dict): if not self.definition_uri.get("Bucket", None) or not self.definition_uri.get("Key", None): # DefinitionUri is a dictionary but does not contain Bucket or Key property raise InvalidResourceException(self.logical_id, "'DefinitionUri' requires Bucket and Key properties to be specified") s3_pointer = self.definition_uri else: # DefinitionUri is a string s3_pointer = parse_s3_uri(self.definition_uri) if s3_pointer is None: raise InvalidResourceException(self.logical_id, '\'DefinitionUri\' is not a valid S3 Uri of the form ' '"s3://bucket/key" with optional versionId query parameter.') body_s3 = { 'Bucket': s3_pointer['Bucket'], 'Key': s3_pointer['Key'] } if 'Version' in s3_pointer: body_s3['Version'] = s3_pointer['Version'] return body_s3
[ "def", "_construct_body_s3_dict", "(", "self", ")", ":", "if", "isinstance", "(", "self", ".", "definition_uri", ",", "dict", ")", ":", "if", "not", "self", ".", "definition_uri", ".", "get", "(", "\"Bucket\"", ",", "None", ")", "or", "not", "self", ".", "definition_uri", ".", "get", "(", "\"Key\"", ",", "None", ")", ":", "# DefinitionUri is a dictionary but does not contain Bucket or Key property", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"'DefinitionUri' requires Bucket and Key properties to be specified\"", ")", "s3_pointer", "=", "self", ".", "definition_uri", "else", ":", "# DefinitionUri is a string", "s3_pointer", "=", "parse_s3_uri", "(", "self", ".", "definition_uri", ")", "if", "s3_pointer", "is", "None", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "'\\'DefinitionUri\\' is not a valid S3 Uri of the form '", "'\"s3://bucket/key\" with optional versionId query parameter.'", ")", "body_s3", "=", "{", "'Bucket'", ":", "s3_pointer", "[", "'Bucket'", "]", ",", "'Key'", ":", "s3_pointer", "[", "'Key'", "]", "}", "if", "'Version'", "in", "s3_pointer", ":", "body_s3", "[", "'Version'", "]", "=", "s3_pointer", "[", "'Version'", "]", "return", "body_s3" ]
Constructs the RestApi's `BodyS3Location property`_, from the SAM Api's DefinitionUri property. :returns: a BodyS3Location dict, containing the S3 Bucket, Key, and Version of the Swagger definition :rtype: dict
[ "Constructs", "the", "RestApi", "s", "BodyS3Location", "property", "_", "from", "the", "SAM", "Api", "s", "DefinitionUri", "property", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/api/api_generator.py#L110-L138
train
awslabs/serverless-application-model
samtranslator/model/api/api_generator.py
ApiGenerator._construct_deployment
def _construct_deployment(self, rest_api): """Constructs and returns the ApiGateway Deployment. :param model.apigateway.ApiGatewayRestApi rest_api: the RestApi for this Deployment :returns: the Deployment to which this SAM Api corresponds :rtype: model.apigateway.ApiGatewayDeployment """ deployment = ApiGatewayDeployment(self.logical_id + 'Deployment', attributes=self.passthrough_resource_attributes) deployment.RestApiId = rest_api.get_runtime_attr('rest_api_id') deployment.StageName = 'Stage' return deployment
python
def _construct_deployment(self, rest_api): """Constructs and returns the ApiGateway Deployment. :param model.apigateway.ApiGatewayRestApi rest_api: the RestApi for this Deployment :returns: the Deployment to which this SAM Api corresponds :rtype: model.apigateway.ApiGatewayDeployment """ deployment = ApiGatewayDeployment(self.logical_id + 'Deployment', attributes=self.passthrough_resource_attributes) deployment.RestApiId = rest_api.get_runtime_attr('rest_api_id') deployment.StageName = 'Stage' return deployment
[ "def", "_construct_deployment", "(", "self", ",", "rest_api", ")", ":", "deployment", "=", "ApiGatewayDeployment", "(", "self", ".", "logical_id", "+", "'Deployment'", ",", "attributes", "=", "self", ".", "passthrough_resource_attributes", ")", "deployment", ".", "RestApiId", "=", "rest_api", ".", "get_runtime_attr", "(", "'rest_api_id'", ")", "deployment", ".", "StageName", "=", "'Stage'", "return", "deployment" ]
Constructs and returns the ApiGateway Deployment. :param model.apigateway.ApiGatewayRestApi rest_api: the RestApi for this Deployment :returns: the Deployment to which this SAM Api corresponds :rtype: model.apigateway.ApiGatewayDeployment
[ "Constructs", "and", "returns", "the", "ApiGateway", "Deployment", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/api/api_generator.py#L140-L152
train
awslabs/serverless-application-model
samtranslator/model/api/api_generator.py
ApiGenerator._construct_stage
def _construct_stage(self, deployment, swagger): """Constructs and returns the ApiGateway Stage. :param model.apigateway.ApiGatewayDeployment deployment: the Deployment for this Stage :returns: the Stage to which this SAM Api corresponds :rtype: model.apigateway.ApiGatewayStage """ # If StageName is some intrinsic function, then don't prefix the Stage's logical ID # This will NOT create duplicates because we allow only ONE stage per API resource stage_name_prefix = self.stage_name if isinstance(self.stage_name, string_types) else "" stage = ApiGatewayStage(self.logical_id + stage_name_prefix + 'Stage', attributes=self.passthrough_resource_attributes) stage.RestApiId = ref(self.logical_id) stage.update_deployment_ref(deployment.logical_id) stage.StageName = self.stage_name stage.CacheClusterEnabled = self.cache_cluster_enabled stage.CacheClusterSize = self.cache_cluster_size stage.Variables = self.variables stage.MethodSettings = self.method_settings stage.AccessLogSetting = self.access_log_setting stage.CanarySetting = self.canary_setting stage.TracingEnabled = self.tracing_enabled if swagger is not None: deployment.make_auto_deployable(stage, swagger) return stage
python
def _construct_stage(self, deployment, swagger): """Constructs and returns the ApiGateway Stage. :param model.apigateway.ApiGatewayDeployment deployment: the Deployment for this Stage :returns: the Stage to which this SAM Api corresponds :rtype: model.apigateway.ApiGatewayStage """ # If StageName is some intrinsic function, then don't prefix the Stage's logical ID # This will NOT create duplicates because we allow only ONE stage per API resource stage_name_prefix = self.stage_name if isinstance(self.stage_name, string_types) else "" stage = ApiGatewayStage(self.logical_id + stage_name_prefix + 'Stage', attributes=self.passthrough_resource_attributes) stage.RestApiId = ref(self.logical_id) stage.update_deployment_ref(deployment.logical_id) stage.StageName = self.stage_name stage.CacheClusterEnabled = self.cache_cluster_enabled stage.CacheClusterSize = self.cache_cluster_size stage.Variables = self.variables stage.MethodSettings = self.method_settings stage.AccessLogSetting = self.access_log_setting stage.CanarySetting = self.canary_setting stage.TracingEnabled = self.tracing_enabled if swagger is not None: deployment.make_auto_deployable(stage, swagger) return stage
[ "def", "_construct_stage", "(", "self", ",", "deployment", ",", "swagger", ")", ":", "# If StageName is some intrinsic function, then don't prefix the Stage's logical ID", "# This will NOT create duplicates because we allow only ONE stage per API resource", "stage_name_prefix", "=", "self", ".", "stage_name", "if", "isinstance", "(", "self", ".", "stage_name", ",", "string_types", ")", "else", "\"\"", "stage", "=", "ApiGatewayStage", "(", "self", ".", "logical_id", "+", "stage_name_prefix", "+", "'Stage'", ",", "attributes", "=", "self", ".", "passthrough_resource_attributes", ")", "stage", ".", "RestApiId", "=", "ref", "(", "self", ".", "logical_id", ")", "stage", ".", "update_deployment_ref", "(", "deployment", ".", "logical_id", ")", "stage", ".", "StageName", "=", "self", ".", "stage_name", "stage", ".", "CacheClusterEnabled", "=", "self", ".", "cache_cluster_enabled", "stage", ".", "CacheClusterSize", "=", "self", ".", "cache_cluster_size", "stage", ".", "Variables", "=", "self", ".", "variables", "stage", ".", "MethodSettings", "=", "self", ".", "method_settings", "stage", ".", "AccessLogSetting", "=", "self", ".", "access_log_setting", "stage", ".", "CanarySetting", "=", "self", ".", "canary_setting", "stage", ".", "TracingEnabled", "=", "self", ".", "tracing_enabled", "if", "swagger", "is", "not", "None", ":", "deployment", ".", "make_auto_deployable", "(", "stage", ",", "swagger", ")", "return", "stage" ]
Constructs and returns the ApiGateway Stage. :param model.apigateway.ApiGatewayDeployment deployment: the Deployment for this Stage :returns: the Stage to which this SAM Api corresponds :rtype: model.apigateway.ApiGatewayStage
[ "Constructs", "and", "returns", "the", "ApiGateway", "Stage", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/api/api_generator.py#L154-L182
train
awslabs/serverless-application-model
samtranslator/model/api/api_generator.py
ApiGenerator.to_cloudformation
def to_cloudformation(self): """Generates CloudFormation resources from a SAM API resource :returns: a tuple containing the RestApi, Deployment, and Stage for an empty Api. :rtype: tuple """ rest_api = self._construct_rest_api() deployment = self._construct_deployment(rest_api) swagger = None if rest_api.Body is not None: swagger = rest_api.Body elif rest_api.BodyS3Location is not None: swagger = rest_api.BodyS3Location stage = self._construct_stage(deployment, swagger) permissions = self._construct_authorizer_lambda_permission() return rest_api, deployment, stage, permissions
python
def to_cloudformation(self): """Generates CloudFormation resources from a SAM API resource :returns: a tuple containing the RestApi, Deployment, and Stage for an empty Api. :rtype: tuple """ rest_api = self._construct_rest_api() deployment = self._construct_deployment(rest_api) swagger = None if rest_api.Body is not None: swagger = rest_api.Body elif rest_api.BodyS3Location is not None: swagger = rest_api.BodyS3Location stage = self._construct_stage(deployment, swagger) permissions = self._construct_authorizer_lambda_permission() return rest_api, deployment, stage, permissions
[ "def", "to_cloudformation", "(", "self", ")", ":", "rest_api", "=", "self", ".", "_construct_rest_api", "(", ")", "deployment", "=", "self", ".", "_construct_deployment", "(", "rest_api", ")", "swagger", "=", "None", "if", "rest_api", ".", "Body", "is", "not", "None", ":", "swagger", "=", "rest_api", ".", "Body", "elif", "rest_api", ".", "BodyS3Location", "is", "not", "None", ":", "swagger", "=", "rest_api", ".", "BodyS3Location", "stage", "=", "self", ".", "_construct_stage", "(", "deployment", ",", "swagger", ")", "permissions", "=", "self", ".", "_construct_authorizer_lambda_permission", "(", ")", "return", "rest_api", ",", "deployment", ",", "stage", ",", "permissions" ]
Generates CloudFormation resources from a SAM API resource :returns: a tuple containing the RestApi, Deployment, and Stage for an empty Api. :rtype: tuple
[ "Generates", "CloudFormation", "resources", "from", "a", "SAM", "API", "resource" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/api/api_generator.py#L184-L203
train
awslabs/serverless-application-model
samtranslator/model/api/api_generator.py
ApiGenerator._add_cors
def _add_cors(self): """ Add CORS configuration to the Swagger file, if necessary """ INVALID_ERROR = "Invalid value for 'Cors' property" if not self.cors: return if self.cors and not self.definition_body: raise InvalidResourceException(self.logical_id, "Cors works only with inline Swagger specified in " "'DefinitionBody' property") if isinstance(self.cors, string_types) or is_instrinsic(self.cors): # Just set Origin property. Others will be defaults properties = CorsProperties(AllowOrigin=self.cors) elif isinstance(self.cors, dict): # Make sure keys in the dict are recognized if not all(key in CorsProperties._fields for key in self.cors.keys()): raise InvalidResourceException(self.logical_id, INVALID_ERROR) properties = CorsProperties(**self.cors) else: raise InvalidResourceException(self.logical_id, INVALID_ERROR) if not SwaggerEditor.is_valid(self.definition_body): raise InvalidResourceException(self.logical_id, "Unable to add Cors configuration because " "'DefinitionBody' does not contain a valid Swagger") if properties.AllowCredentials is True and properties.AllowOrigin == _CORS_WILDCARD: raise InvalidResourceException(self.logical_id, "Unable to add Cors configuration because " "'AllowCredentials' can not be true when " "'AllowOrigin' is \"'*'\" or not set") editor = SwaggerEditor(self.definition_body) for path in editor.iter_on_path(): editor.add_cors(path, properties.AllowOrigin, properties.AllowHeaders, properties.AllowMethods, max_age=properties.MaxAge, allow_credentials=properties.AllowCredentials) # Assign the Swagger back to template self.definition_body = editor.swagger
python
def _add_cors(self): """ Add CORS configuration to the Swagger file, if necessary """ INVALID_ERROR = "Invalid value for 'Cors' property" if not self.cors: return if self.cors and not self.definition_body: raise InvalidResourceException(self.logical_id, "Cors works only with inline Swagger specified in " "'DefinitionBody' property") if isinstance(self.cors, string_types) or is_instrinsic(self.cors): # Just set Origin property. Others will be defaults properties = CorsProperties(AllowOrigin=self.cors) elif isinstance(self.cors, dict): # Make sure keys in the dict are recognized if not all(key in CorsProperties._fields for key in self.cors.keys()): raise InvalidResourceException(self.logical_id, INVALID_ERROR) properties = CorsProperties(**self.cors) else: raise InvalidResourceException(self.logical_id, INVALID_ERROR) if not SwaggerEditor.is_valid(self.definition_body): raise InvalidResourceException(self.logical_id, "Unable to add Cors configuration because " "'DefinitionBody' does not contain a valid Swagger") if properties.AllowCredentials is True and properties.AllowOrigin == _CORS_WILDCARD: raise InvalidResourceException(self.logical_id, "Unable to add Cors configuration because " "'AllowCredentials' can not be true when " "'AllowOrigin' is \"'*'\" or not set") editor = SwaggerEditor(self.definition_body) for path in editor.iter_on_path(): editor.add_cors(path, properties.AllowOrigin, properties.AllowHeaders, properties.AllowMethods, max_age=properties.MaxAge, allow_credentials=properties.AllowCredentials) # Assign the Swagger back to template self.definition_body = editor.swagger
[ "def", "_add_cors", "(", "self", ")", ":", "INVALID_ERROR", "=", "\"Invalid value for 'Cors' property\"", "if", "not", "self", ".", "cors", ":", "return", "if", "self", ".", "cors", "and", "not", "self", ".", "definition_body", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Cors works only with inline Swagger specified in \"", "\"'DefinitionBody' property\"", ")", "if", "isinstance", "(", "self", ".", "cors", ",", "string_types", ")", "or", "is_instrinsic", "(", "self", ".", "cors", ")", ":", "# Just set Origin property. Others will be defaults", "properties", "=", "CorsProperties", "(", "AllowOrigin", "=", "self", ".", "cors", ")", "elif", "isinstance", "(", "self", ".", "cors", ",", "dict", ")", ":", "# Make sure keys in the dict are recognized", "if", "not", "all", "(", "key", "in", "CorsProperties", ".", "_fields", "for", "key", "in", "self", ".", "cors", ".", "keys", "(", ")", ")", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "INVALID_ERROR", ")", "properties", "=", "CorsProperties", "(", "*", "*", "self", ".", "cors", ")", "else", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "INVALID_ERROR", ")", "if", "not", "SwaggerEditor", ".", "is_valid", "(", "self", ".", "definition_body", ")", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Unable to add Cors configuration because \"", "\"'DefinitionBody' does not contain a valid Swagger\"", ")", "if", "properties", ".", "AllowCredentials", "is", "True", "and", "properties", ".", "AllowOrigin", "==", "_CORS_WILDCARD", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Unable to add Cors configuration because \"", "\"'AllowCredentials' can not be true when \"", "\"'AllowOrigin' is \\\"'*'\\\" or not set\"", ")", "editor", "=", "SwaggerEditor", "(", "self", ".", "definition_body", ")", "for", "path", "in", "editor", ".", "iter_on_path", "(", ")", ":", "editor", ".", "add_cors", "(", "path", ",", "properties", ".", "AllowOrigin", ",", "properties", ".", "AllowHeaders", ",", "properties", ".", "AllowMethods", ",", "max_age", "=", "properties", ".", "MaxAge", ",", "allow_credentials", "=", "properties", ".", "AllowCredentials", ")", "# Assign the Swagger back to template", "self", ".", "definition_body", "=", "editor", ".", "swagger" ]
Add CORS configuration to the Swagger file, if necessary
[ "Add", "CORS", "configuration", "to", "the", "Swagger", "file", "if", "necessary" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/api/api_generator.py#L205-L249
train
awslabs/serverless-application-model
samtranslator/model/api/api_generator.py
ApiGenerator._add_auth
def _add_auth(self): """ Add Auth configuration to the Swagger file, if necessary """ if not self.auth: return if self.auth and not self.definition_body: raise InvalidResourceException(self.logical_id, "Auth works only with inline Swagger specified in " "'DefinitionBody' property") # Make sure keys in the dict are recognized if not all(key in AuthProperties._fields for key in self.auth.keys()): raise InvalidResourceException( self.logical_id, "Invalid value for 'Auth' property") if not SwaggerEditor.is_valid(self.definition_body): raise InvalidResourceException(self.logical_id, "Unable to add Auth configuration because " "'DefinitionBody' does not contain a valid Swagger") swagger_editor = SwaggerEditor(self.definition_body) auth_properties = AuthProperties(**self.auth) authorizers = self._get_authorizers(auth_properties.Authorizers, auth_properties.DefaultAuthorizer) if authorizers: swagger_editor.add_authorizers(authorizers) self._set_default_authorizer(swagger_editor, authorizers, auth_properties.DefaultAuthorizer) # Assign the Swagger back to template self.definition_body = swagger_editor.swagger
python
def _add_auth(self): """ Add Auth configuration to the Swagger file, if necessary """ if not self.auth: return if self.auth and not self.definition_body: raise InvalidResourceException(self.logical_id, "Auth works only with inline Swagger specified in " "'DefinitionBody' property") # Make sure keys in the dict are recognized if not all(key in AuthProperties._fields for key in self.auth.keys()): raise InvalidResourceException( self.logical_id, "Invalid value for 'Auth' property") if not SwaggerEditor.is_valid(self.definition_body): raise InvalidResourceException(self.logical_id, "Unable to add Auth configuration because " "'DefinitionBody' does not contain a valid Swagger") swagger_editor = SwaggerEditor(self.definition_body) auth_properties = AuthProperties(**self.auth) authorizers = self._get_authorizers(auth_properties.Authorizers, auth_properties.DefaultAuthorizer) if authorizers: swagger_editor.add_authorizers(authorizers) self._set_default_authorizer(swagger_editor, authorizers, auth_properties.DefaultAuthorizer) # Assign the Swagger back to template self.definition_body = swagger_editor.swagger
[ "def", "_add_auth", "(", "self", ")", ":", "if", "not", "self", ".", "auth", ":", "return", "if", "self", ".", "auth", "and", "not", "self", ".", "definition_body", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Auth works only with inline Swagger specified in \"", "\"'DefinitionBody' property\"", ")", "# Make sure keys in the dict are recognized", "if", "not", "all", "(", "key", "in", "AuthProperties", ".", "_fields", "for", "key", "in", "self", ".", "auth", ".", "keys", "(", ")", ")", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Invalid value for 'Auth' property\"", ")", "if", "not", "SwaggerEditor", ".", "is_valid", "(", "self", ".", "definition_body", ")", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Unable to add Auth configuration because \"", "\"'DefinitionBody' does not contain a valid Swagger\"", ")", "swagger_editor", "=", "SwaggerEditor", "(", "self", ".", "definition_body", ")", "auth_properties", "=", "AuthProperties", "(", "*", "*", "self", ".", "auth", ")", "authorizers", "=", "self", ".", "_get_authorizers", "(", "auth_properties", ".", "Authorizers", ",", "auth_properties", ".", "DefaultAuthorizer", ")", "if", "authorizers", ":", "swagger_editor", ".", "add_authorizers", "(", "authorizers", ")", "self", ".", "_set_default_authorizer", "(", "swagger_editor", ",", "authorizers", ",", "auth_properties", ".", "DefaultAuthorizer", ")", "# Assign the Swagger back to template", "self", ".", "definition_body", "=", "swagger_editor", ".", "swagger" ]
Add Auth configuration to the Swagger file, if necessary
[ "Add", "Auth", "configuration", "to", "the", "Swagger", "file", "if", "necessary" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/api/api_generator.py#L251-L281
train
awslabs/serverless-application-model
samtranslator/model/api/api_generator.py
ApiGenerator._add_gateway_responses
def _add_gateway_responses(self): """ Add Gateway Response configuration to the Swagger file, if necessary """ if not self.gateway_responses: return if self.gateway_responses and not self.definition_body: raise InvalidResourceException( self.logical_id, "GatewayResponses works only with inline Swagger specified in " "'DefinitionBody' property") # Make sure keys in the dict are recognized for responses_key, responses_value in self.gateway_responses.items(): for response_key in responses_value.keys(): if response_key not in GatewayResponseProperties: raise InvalidResourceException( self.logical_id, "Invalid property '{}' in 'GatewayResponses' property '{}'".format(response_key, responses_key)) if not SwaggerEditor.is_valid(self.definition_body): raise InvalidResourceException( self.logical_id, "Unable to add Auth configuration because " "'DefinitionBody' does not contain a valid Swagger") swagger_editor = SwaggerEditor(self.definition_body) gateway_responses = {} for response_type, response in self.gateway_responses.items(): gateway_responses[response_type] = ApiGatewayResponse( api_logical_id=self.logical_id, response_parameters=response.get('ResponseParameters', {}), response_templates=response.get('ResponseTemplates', {}), status_code=response.get('StatusCode', None) ) if gateway_responses: swagger_editor.add_gateway_responses(gateway_responses) # Assign the Swagger back to template self.definition_body = swagger_editor.swagger
python
def _add_gateway_responses(self): """ Add Gateway Response configuration to the Swagger file, if necessary """ if not self.gateway_responses: return if self.gateway_responses and not self.definition_body: raise InvalidResourceException( self.logical_id, "GatewayResponses works only with inline Swagger specified in " "'DefinitionBody' property") # Make sure keys in the dict are recognized for responses_key, responses_value in self.gateway_responses.items(): for response_key in responses_value.keys(): if response_key not in GatewayResponseProperties: raise InvalidResourceException( self.logical_id, "Invalid property '{}' in 'GatewayResponses' property '{}'".format(response_key, responses_key)) if not SwaggerEditor.is_valid(self.definition_body): raise InvalidResourceException( self.logical_id, "Unable to add Auth configuration because " "'DefinitionBody' does not contain a valid Swagger") swagger_editor = SwaggerEditor(self.definition_body) gateway_responses = {} for response_type, response in self.gateway_responses.items(): gateway_responses[response_type] = ApiGatewayResponse( api_logical_id=self.logical_id, response_parameters=response.get('ResponseParameters', {}), response_templates=response.get('ResponseTemplates', {}), status_code=response.get('StatusCode', None) ) if gateway_responses: swagger_editor.add_gateway_responses(gateway_responses) # Assign the Swagger back to template self.definition_body = swagger_editor.swagger
[ "def", "_add_gateway_responses", "(", "self", ")", ":", "if", "not", "self", ".", "gateway_responses", ":", "return", "if", "self", ".", "gateway_responses", "and", "not", "self", ".", "definition_body", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"GatewayResponses works only with inline Swagger specified in \"", "\"'DefinitionBody' property\"", ")", "# Make sure keys in the dict are recognized", "for", "responses_key", ",", "responses_value", "in", "self", ".", "gateway_responses", ".", "items", "(", ")", ":", "for", "response_key", "in", "responses_value", ".", "keys", "(", ")", ":", "if", "response_key", "not", "in", "GatewayResponseProperties", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Invalid property '{}' in 'GatewayResponses' property '{}'\"", ".", "format", "(", "response_key", ",", "responses_key", ")", ")", "if", "not", "SwaggerEditor", ".", "is_valid", "(", "self", ".", "definition_body", ")", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Unable to add Auth configuration because \"", "\"'DefinitionBody' does not contain a valid Swagger\"", ")", "swagger_editor", "=", "SwaggerEditor", "(", "self", ".", "definition_body", ")", "gateway_responses", "=", "{", "}", "for", "response_type", ",", "response", "in", "self", ".", "gateway_responses", ".", "items", "(", ")", ":", "gateway_responses", "[", "response_type", "]", "=", "ApiGatewayResponse", "(", "api_logical_id", "=", "self", ".", "logical_id", ",", "response_parameters", "=", "response", ".", "get", "(", "'ResponseParameters'", ",", "{", "}", ")", ",", "response_templates", "=", "response", ".", "get", "(", "'ResponseTemplates'", ",", "{", "}", ")", ",", "status_code", "=", "response", ".", "get", "(", "'StatusCode'", ",", "None", ")", ")", "if", "gateway_responses", ":", "swagger_editor", ".", "add_gateway_responses", "(", "gateway_responses", ")", "# Assign the Swagger back to template", "self", ".", "definition_body", "=", "swagger_editor", ".", "swagger" ]
Add Gateway Response configuration to the Swagger file, if necessary
[ "Add", "Gateway", "Response", "configuration", "to", "the", "Swagger", "file", "if", "necessary" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/api/api_generator.py#L283-L324
train
awslabs/serverless-application-model
samtranslator/model/api/api_generator.py
ApiGenerator._get_permission
def _get_permission(self, authorizer_name, authorizer_lambda_function_arn): """Constructs and returns the Lambda Permission resource allowing the Authorizer to invoke the function. :returns: the permission resource :rtype: model.lambda_.LambdaPermission """ rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on, attributes=self.resource_attributes) api_id = rest_api.get_runtime_attr('rest_api_id') partition = ArnGenerator.get_partition_name() resource = '${__ApiId__}/authorizers/*' source_arn = fnSub(ArnGenerator.generate_arn(partition=partition, service='execute-api', resource=resource), {"__ApiId__": api_id}) lambda_permission = LambdaPermission(self.logical_id + authorizer_name + 'AuthorizerPermission', attributes=self.passthrough_resource_attributes) lambda_permission.Action = 'lambda:invokeFunction' lambda_permission.FunctionName = authorizer_lambda_function_arn lambda_permission.Principal = 'apigateway.amazonaws.com' lambda_permission.SourceArn = source_arn return lambda_permission
python
def _get_permission(self, authorizer_name, authorizer_lambda_function_arn): """Constructs and returns the Lambda Permission resource allowing the Authorizer to invoke the function. :returns: the permission resource :rtype: model.lambda_.LambdaPermission """ rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on, attributes=self.resource_attributes) api_id = rest_api.get_runtime_attr('rest_api_id') partition = ArnGenerator.get_partition_name() resource = '${__ApiId__}/authorizers/*' source_arn = fnSub(ArnGenerator.generate_arn(partition=partition, service='execute-api', resource=resource), {"__ApiId__": api_id}) lambda_permission = LambdaPermission(self.logical_id + authorizer_name + 'AuthorizerPermission', attributes=self.passthrough_resource_attributes) lambda_permission.Action = 'lambda:invokeFunction' lambda_permission.FunctionName = authorizer_lambda_function_arn lambda_permission.Principal = 'apigateway.amazonaws.com' lambda_permission.SourceArn = source_arn return lambda_permission
[ "def", "_get_permission", "(", "self", ",", "authorizer_name", ",", "authorizer_lambda_function_arn", ")", ":", "rest_api", "=", "ApiGatewayRestApi", "(", "self", ".", "logical_id", ",", "depends_on", "=", "self", ".", "depends_on", ",", "attributes", "=", "self", ".", "resource_attributes", ")", "api_id", "=", "rest_api", ".", "get_runtime_attr", "(", "'rest_api_id'", ")", "partition", "=", "ArnGenerator", ".", "get_partition_name", "(", ")", "resource", "=", "'${__ApiId__}/authorizers/*'", "source_arn", "=", "fnSub", "(", "ArnGenerator", ".", "generate_arn", "(", "partition", "=", "partition", ",", "service", "=", "'execute-api'", ",", "resource", "=", "resource", ")", ",", "{", "\"__ApiId__\"", ":", "api_id", "}", ")", "lambda_permission", "=", "LambdaPermission", "(", "self", ".", "logical_id", "+", "authorizer_name", "+", "'AuthorizerPermission'", ",", "attributes", "=", "self", ".", "passthrough_resource_attributes", ")", "lambda_permission", ".", "Action", "=", "'lambda:invokeFunction'", "lambda_permission", ".", "FunctionName", "=", "authorizer_lambda_function_arn", "lambda_permission", ".", "Principal", "=", "'apigateway.amazonaws.com'", "lambda_permission", ".", "SourceArn", "=", "source_arn", "return", "lambda_permission" ]
Constructs and returns the Lambda Permission resource allowing the Authorizer to invoke the function. :returns: the permission resource :rtype: model.lambda_.LambdaPermission
[ "Constructs", "and", "returns", "the", "Lambda", "Permission", "resource", "allowing", "the", "Authorizer", "to", "invoke", "the", "function", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/api/api_generator.py#L360-L381
train
awslabs/serverless-application-model
samtranslator/model/api/api_generator.py
ApiGenerator._set_endpoint_configuration
def _set_endpoint_configuration(self, rest_api, value): """ Sets endpoint configuration property of AWS::ApiGateway::RestApi resource :param rest_api: RestApi resource :param string/dict value: Value to be set """ rest_api.EndpointConfiguration = {"Types": [value]} rest_api.Parameters = {"endpointConfigurationTypes": value}
python
def _set_endpoint_configuration(self, rest_api, value): """ Sets endpoint configuration property of AWS::ApiGateway::RestApi resource :param rest_api: RestApi resource :param string/dict value: Value to be set """ rest_api.EndpointConfiguration = {"Types": [value]} rest_api.Parameters = {"endpointConfigurationTypes": value}
[ "def", "_set_endpoint_configuration", "(", "self", ",", "rest_api", ",", "value", ")", ":", "rest_api", ".", "EndpointConfiguration", "=", "{", "\"Types\"", ":", "[", "value", "]", "}", "rest_api", ".", "Parameters", "=", "{", "\"endpointConfigurationTypes\"", ":", "value", "}" ]
Sets endpoint configuration property of AWS::ApiGateway::RestApi resource :param rest_api: RestApi resource :param string/dict value: Value to be set
[ "Sets", "endpoint", "configuration", "property", "of", "AWS", "::", "ApiGateway", "::", "RestApi", "resource", ":", "param", "rest_api", ":", "RestApi", "resource", ":", "param", "string", "/", "dict", "value", ":", "Value", "to", "be", "set" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/api/api_generator.py#L416-L424
train
awslabs/serverless-application-model
examples/apps/greengrass-hello-world/greengrass_ipc_python_sdk/utils/exponential_backoff.py
retry
def retry(time_unit, multiplier, backoff_coefficient, max_delay, max_attempts, expiration_duration, enable_jitter): """ The retry function will keep retrying `task_to_try` until either: (1) it returns None, then retry() finishes (2) `max_attempts` is reached, then retry() raises an exception. (3) if retrying one more time will cause total wait time to go above: `expiration_duration`, then retry() raises an exception Beware that any exception raised by task_to_try won't get surfaced until (2) or (3) is satisfied. At step n, it sleeps for [0, delay), where delay is defined as the following: `delay = min(max_delay, multiplier * (backoff_coefficient ** (n - 1))) * time_unit` seconds Additionally, if you enable jitter, for each retry, the function will instead sleep for: random.random() * sleep, that is [0, sleep) seconds. :param time_unit: This field represents a fraction of a second, which is used as a multiplier to compute the amount of time to sleep. :type time_unit: float :param multiplier: The initial wait duration for the first retry. :type multiplier: float :param backoff_coefficient: the base value for exponential retry. :type backoff_coefficient: float :param max_delay: The maximum amount of time to wait per try. :type max_delay: float :param max_attempts: This method will retry up to this value. :type max_attempts: int :param expiration_duration: the maximum amount of time retry can wait. :type expiration_duration: float :param enable_jitter: Setting this to true will add jitter. :type enable_jitter: bool """ def deco_retry(task_to_try): @wraps(task_to_try) def retry_impl(*args, **kwargs): total_wait_time = 0 have_tried = 0 retry_errors = [] while have_tried < max_attempts: try: task_to_try(*args, **kwargs) return except Exception as e: retry_errors.append(e) going_to_sleep_for = min(max_delay, multiplier * (backoff_coefficient ** have_tried)) if enable_jitter: going_to_sleep_for = random.random() * going_to_sleep_for duration = going_to_sleep_for * time_unit if total_wait_time + duration > expiration_duration: raise RetryTimeoutException(task_to_try.__name__, have_tried, max_attempts, total_wait_time, multiplier, backoff_coefficient, enable_jitter, retry_errors) runtime_logger.warn('Retrying [{0}], going to sleep for {1} seconds, exception stacktrace:\n{2}' .format(task_to_try.__name__, duration, traceback.format_exc())) time.sleep(duration) total_wait_time += duration have_tried += 1 raise RetryTimeoutException(task_to_try.__name__, have_tried, max_attempts, total_wait_time, multiplier, backoff_coefficient, enable_jitter, retry_errors) return retry_impl return deco_retry
python
def retry(time_unit, multiplier, backoff_coefficient, max_delay, max_attempts, expiration_duration, enable_jitter): """ The retry function will keep retrying `task_to_try` until either: (1) it returns None, then retry() finishes (2) `max_attempts` is reached, then retry() raises an exception. (3) if retrying one more time will cause total wait time to go above: `expiration_duration`, then retry() raises an exception Beware that any exception raised by task_to_try won't get surfaced until (2) or (3) is satisfied. At step n, it sleeps for [0, delay), where delay is defined as the following: `delay = min(max_delay, multiplier * (backoff_coefficient ** (n - 1))) * time_unit` seconds Additionally, if you enable jitter, for each retry, the function will instead sleep for: random.random() * sleep, that is [0, sleep) seconds. :param time_unit: This field represents a fraction of a second, which is used as a multiplier to compute the amount of time to sleep. :type time_unit: float :param multiplier: The initial wait duration for the first retry. :type multiplier: float :param backoff_coefficient: the base value for exponential retry. :type backoff_coefficient: float :param max_delay: The maximum amount of time to wait per try. :type max_delay: float :param max_attempts: This method will retry up to this value. :type max_attempts: int :param expiration_duration: the maximum amount of time retry can wait. :type expiration_duration: float :param enable_jitter: Setting this to true will add jitter. :type enable_jitter: bool """ def deco_retry(task_to_try): @wraps(task_to_try) def retry_impl(*args, **kwargs): total_wait_time = 0 have_tried = 0 retry_errors = [] while have_tried < max_attempts: try: task_to_try(*args, **kwargs) return except Exception as e: retry_errors.append(e) going_to_sleep_for = min(max_delay, multiplier * (backoff_coefficient ** have_tried)) if enable_jitter: going_to_sleep_for = random.random() * going_to_sleep_for duration = going_to_sleep_for * time_unit if total_wait_time + duration > expiration_duration: raise RetryTimeoutException(task_to_try.__name__, have_tried, max_attempts, total_wait_time, multiplier, backoff_coefficient, enable_jitter, retry_errors) runtime_logger.warn('Retrying [{0}], going to sleep for {1} seconds, exception stacktrace:\n{2}' .format(task_to_try.__name__, duration, traceback.format_exc())) time.sleep(duration) total_wait_time += duration have_tried += 1 raise RetryTimeoutException(task_to_try.__name__, have_tried, max_attempts, total_wait_time, multiplier, backoff_coefficient, enable_jitter, retry_errors) return retry_impl return deco_retry
[ "def", "retry", "(", "time_unit", ",", "multiplier", ",", "backoff_coefficient", ",", "max_delay", ",", "max_attempts", ",", "expiration_duration", ",", "enable_jitter", ")", ":", "def", "deco_retry", "(", "task_to_try", ")", ":", "@", "wraps", "(", "task_to_try", ")", "def", "retry_impl", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "total_wait_time", "=", "0", "have_tried", "=", "0", "retry_errors", "=", "[", "]", "while", "have_tried", "<", "max_attempts", ":", "try", ":", "task_to_try", "(", "*", "args", ",", "*", "*", "kwargs", ")", "return", "except", "Exception", "as", "e", ":", "retry_errors", ".", "append", "(", "e", ")", "going_to_sleep_for", "=", "min", "(", "max_delay", ",", "multiplier", "*", "(", "backoff_coefficient", "**", "have_tried", ")", ")", "if", "enable_jitter", ":", "going_to_sleep_for", "=", "random", ".", "random", "(", ")", "*", "going_to_sleep_for", "duration", "=", "going_to_sleep_for", "*", "time_unit", "if", "total_wait_time", "+", "duration", ">", "expiration_duration", ":", "raise", "RetryTimeoutException", "(", "task_to_try", ".", "__name__", ",", "have_tried", ",", "max_attempts", ",", "total_wait_time", ",", "multiplier", ",", "backoff_coefficient", ",", "enable_jitter", ",", "retry_errors", ")", "runtime_logger", ".", "warn", "(", "'Retrying [{0}], going to sleep for {1} seconds, exception stacktrace:\\n{2}'", ".", "format", "(", "task_to_try", ".", "__name__", ",", "duration", ",", "traceback", ".", "format_exc", "(", ")", ")", ")", "time", ".", "sleep", "(", "duration", ")", "total_wait_time", "+=", "duration", "have_tried", "+=", "1", "raise", "RetryTimeoutException", "(", "task_to_try", ".", "__name__", ",", "have_tried", ",", "max_attempts", ",", "total_wait_time", ",", "multiplier", ",", "backoff_coefficient", ",", "enable_jitter", ",", "retry_errors", ")", "return", "retry_impl", "return", "deco_retry" ]
The retry function will keep retrying `task_to_try` until either: (1) it returns None, then retry() finishes (2) `max_attempts` is reached, then retry() raises an exception. (3) if retrying one more time will cause total wait time to go above: `expiration_duration`, then retry() raises an exception Beware that any exception raised by task_to_try won't get surfaced until (2) or (3) is satisfied. At step n, it sleeps for [0, delay), where delay is defined as the following: `delay = min(max_delay, multiplier * (backoff_coefficient ** (n - 1))) * time_unit` seconds Additionally, if you enable jitter, for each retry, the function will instead sleep for: random.random() * sleep, that is [0, sleep) seconds. :param time_unit: This field represents a fraction of a second, which is used as a multiplier to compute the amount of time to sleep. :type time_unit: float :param multiplier: The initial wait duration for the first retry. :type multiplier: float :param backoff_coefficient: the base value for exponential retry. :type backoff_coefficient: float :param max_delay: The maximum amount of time to wait per try. :type max_delay: float :param max_attempts: This method will retry up to this value. :type max_attempts: int :param expiration_duration: the maximum amount of time retry can wait. :type expiration_duration: float :param enable_jitter: Setting this to true will add jitter. :type enable_jitter: bool
[ "The", "retry", "function", "will", "keep", "retrying", "task_to_try", "until", "either", ":", "(", "1", ")", "it", "returns", "None", "then", "retry", "()", "finishes", "(", "2", ")", "max_attempts", "is", "reached", "then", "retry", "()", "raises", "an", "exception", ".", "(", "3", ")", "if", "retrying", "one", "more", "time", "will", "cause", "total", "wait", "time", "to", "go", "above", ":", "expiration_duration", "then", "retry", "()", "raises", "an", "exception" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/examples/apps/greengrass-hello-world/greengrass_ipc_python_sdk/utils/exponential_backoff.py#L47-L116
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
to_cloudformation
def to_cloudformation(self, **kwargs): """Returns the Lambda function, role, and event resources to which this SAM Function corresponds. :param dict kwargs: already-converted resources that may need to be modified when converting this \ macro to pure CloudFormation :returns: a list of vanilla CloudFormation Resources, to which this Function expands :rtype: list """ resources = [] intrinsics_resolver = kwargs["intrinsics_resolver"] if self.DeadLetterQueue: self._validate_dlq() lambda_function = self._construct_lambda_function() resources.append(lambda_function) lambda_alias = None if self.AutoPublishAlias: alias_name = self._get_resolved_alias_name("AutoPublishAlias", self.AutoPublishAlias, intrinsics_resolver) lambda_version = self._construct_version(lambda_function, intrinsics_resolver=intrinsics_resolver) lambda_alias = self._construct_alias(alias_name, lambda_function, lambda_version) resources.append(lambda_version) resources.append(lambda_alias) if self.DeploymentPreference: self._validate_deployment_preference_and_add_update_policy(kwargs.get('deployment_preference_collection', None), lambda_alias, intrinsics_resolver) managed_policy_map = kwargs.get('managed_policy_map', {}) if not managed_policy_map: raise Exception('Managed policy map is empty, but should not be.') execution_role = None if lambda_function.Role is None: execution_role = self._construct_role(managed_policy_map) lambda_function.Role = execution_role.get_runtime_attr('arn') resources.append(execution_role) try: resources += self._generate_event_resources(lambda_function, execution_role, kwargs['event_resources'], lambda_alias=lambda_alias) except InvalidEventException as e: raise InvalidResourceException(self.logical_id, e.message) return resources
python
def to_cloudformation(self, **kwargs): """Returns the Lambda function, role, and event resources to which this SAM Function corresponds. :param dict kwargs: already-converted resources that may need to be modified when converting this \ macro to pure CloudFormation :returns: a list of vanilla CloudFormation Resources, to which this Function expands :rtype: list """ resources = [] intrinsics_resolver = kwargs["intrinsics_resolver"] if self.DeadLetterQueue: self._validate_dlq() lambda_function = self._construct_lambda_function() resources.append(lambda_function) lambda_alias = None if self.AutoPublishAlias: alias_name = self._get_resolved_alias_name("AutoPublishAlias", self.AutoPublishAlias, intrinsics_resolver) lambda_version = self._construct_version(lambda_function, intrinsics_resolver=intrinsics_resolver) lambda_alias = self._construct_alias(alias_name, lambda_function, lambda_version) resources.append(lambda_version) resources.append(lambda_alias) if self.DeploymentPreference: self._validate_deployment_preference_and_add_update_policy(kwargs.get('deployment_preference_collection', None), lambda_alias, intrinsics_resolver) managed_policy_map = kwargs.get('managed_policy_map', {}) if not managed_policy_map: raise Exception('Managed policy map is empty, but should not be.') execution_role = None if lambda_function.Role is None: execution_role = self._construct_role(managed_policy_map) lambda_function.Role = execution_role.get_runtime_attr('arn') resources.append(execution_role) try: resources += self._generate_event_resources(lambda_function, execution_role, kwargs['event_resources'], lambda_alias=lambda_alias) except InvalidEventException as e: raise InvalidResourceException(self.logical_id, e.message) return resources
[ "def", "to_cloudformation", "(", "self", ",", "*", "*", "kwargs", ")", ":", "resources", "=", "[", "]", "intrinsics_resolver", "=", "kwargs", "[", "\"intrinsics_resolver\"", "]", "if", "self", ".", "DeadLetterQueue", ":", "self", ".", "_validate_dlq", "(", ")", "lambda_function", "=", "self", ".", "_construct_lambda_function", "(", ")", "resources", ".", "append", "(", "lambda_function", ")", "lambda_alias", "=", "None", "if", "self", ".", "AutoPublishAlias", ":", "alias_name", "=", "self", ".", "_get_resolved_alias_name", "(", "\"AutoPublishAlias\"", ",", "self", ".", "AutoPublishAlias", ",", "intrinsics_resolver", ")", "lambda_version", "=", "self", ".", "_construct_version", "(", "lambda_function", ",", "intrinsics_resolver", "=", "intrinsics_resolver", ")", "lambda_alias", "=", "self", ".", "_construct_alias", "(", "alias_name", ",", "lambda_function", ",", "lambda_version", ")", "resources", ".", "append", "(", "lambda_version", ")", "resources", ".", "append", "(", "lambda_alias", ")", "if", "self", ".", "DeploymentPreference", ":", "self", ".", "_validate_deployment_preference_and_add_update_policy", "(", "kwargs", ".", "get", "(", "'deployment_preference_collection'", ",", "None", ")", ",", "lambda_alias", ",", "intrinsics_resolver", ")", "managed_policy_map", "=", "kwargs", ".", "get", "(", "'managed_policy_map'", ",", "{", "}", ")", "if", "not", "managed_policy_map", ":", "raise", "Exception", "(", "'Managed policy map is empty, but should not be.'", ")", "execution_role", "=", "None", "if", "lambda_function", ".", "Role", "is", "None", ":", "execution_role", "=", "self", ".", "_construct_role", "(", "managed_policy_map", ")", "lambda_function", ".", "Role", "=", "execution_role", ".", "get_runtime_attr", "(", "'arn'", ")", "resources", ".", "append", "(", "execution_role", ")", "try", ":", "resources", "+=", "self", ".", "_generate_event_resources", "(", "lambda_function", ",", "execution_role", ",", "kwargs", "[", "'event_resources'", "]", ",", "lambda_alias", "=", "lambda_alias", ")", "except", "InvalidEventException", "as", "e", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "e", ".", "message", ")", "return", "resources" ]
Returns the Lambda function, role, and event resources to which this SAM Function corresponds. :param dict kwargs: already-converted resources that may need to be modified when converting this \ macro to pure CloudFormation :returns: a list of vanilla CloudFormation Resources, to which this Function expands :rtype: list
[ "Returns", "the", "Lambda", "function", "role", "and", "event", "resources", "to", "which", "this", "SAM", "Function", "corresponds", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L80-L126
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
_get_resolved_alias_name
def _get_resolved_alias_name(self, property_name, original_alias_value, intrinsics_resolver): """ Alias names can be supplied as an intrinsic function. This method tries to extract alias name from a reference to a parameter. If it cannot completely resolve (ie. if a complex intrinsic function was used), then this method raises an exception. If alias name is just a plain string, it will return as is :param dict or string original_alias_value: Value of Alias property as provided by the customer :param samtranslator.intrinsics.resolver.IntrinsicsResolver intrinsics_resolver: Instance of the resolver that knows how to resolve parameter references :return string: Alias name :raises InvalidResourceException: If the value is a complex intrinsic function that cannot be resolved """ # Try to resolve. resolved_alias_name = intrinsics_resolver.resolve_parameter_refs(original_alias_value) if not isinstance(resolved_alias_name, string_types): # This is still a dictionary which means we are not able to completely resolve intrinsics raise InvalidResourceException(self.logical_id, "'{}' must be a string or a Ref to a template parameter" .format(property_name)) return resolved_alias_name
python
def _get_resolved_alias_name(self, property_name, original_alias_value, intrinsics_resolver): """ Alias names can be supplied as an intrinsic function. This method tries to extract alias name from a reference to a parameter. If it cannot completely resolve (ie. if a complex intrinsic function was used), then this method raises an exception. If alias name is just a plain string, it will return as is :param dict or string original_alias_value: Value of Alias property as provided by the customer :param samtranslator.intrinsics.resolver.IntrinsicsResolver intrinsics_resolver: Instance of the resolver that knows how to resolve parameter references :return string: Alias name :raises InvalidResourceException: If the value is a complex intrinsic function that cannot be resolved """ # Try to resolve. resolved_alias_name = intrinsics_resolver.resolve_parameter_refs(original_alias_value) if not isinstance(resolved_alias_name, string_types): # This is still a dictionary which means we are not able to completely resolve intrinsics raise InvalidResourceException(self.logical_id, "'{}' must be a string or a Ref to a template parameter" .format(property_name)) return resolved_alias_name
[ "def", "_get_resolved_alias_name", "(", "self", ",", "property_name", ",", "original_alias_value", ",", "intrinsics_resolver", ")", ":", "# Try to resolve.", "resolved_alias_name", "=", "intrinsics_resolver", ".", "resolve_parameter_refs", "(", "original_alias_value", ")", "if", "not", "isinstance", "(", "resolved_alias_name", ",", "string_types", ")", ":", "# This is still a dictionary which means we are not able to completely resolve intrinsics", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"'{}' must be a string or a Ref to a template parameter\"", ".", "format", "(", "property_name", ")", ")", "return", "resolved_alias_name" ]
Alias names can be supplied as an intrinsic function. This method tries to extract alias name from a reference to a parameter. If it cannot completely resolve (ie. if a complex intrinsic function was used), then this method raises an exception. If alias name is just a plain string, it will return as is :param dict or string original_alias_value: Value of Alias property as provided by the customer :param samtranslator.intrinsics.resolver.IntrinsicsResolver intrinsics_resolver: Instance of the resolver that knows how to resolve parameter references :return string: Alias name :raises InvalidResourceException: If the value is a complex intrinsic function that cannot be resolved
[ "Alias", "names", "can", "be", "supplied", "as", "an", "intrinsic", "function", ".", "This", "method", "tries", "to", "extract", "alias", "name", "from", "a", "reference", "to", "a", "parameter", ".", "If", "it", "cannot", "completely", "resolve", "(", "ie", ".", "if", "a", "complex", "intrinsic", "function", "was", "used", ")", "then", "this", "method", "raises", "an", "exception", ".", "If", "alias", "name", "is", "just", "a", "plain", "string", "it", "will", "return", "as", "is" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L128-L150
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
_construct_lambda_function
def _construct_lambda_function(self): """Constructs and returns the Lambda function. :returns: a list containing the Lambda function and execution role resources :rtype: list """ lambda_function = LambdaFunction(self.logical_id, depends_on=self.depends_on, attributes=self.resource_attributes) if self.FunctionName: lambda_function.FunctionName = self.FunctionName lambda_function.Handler = self.Handler lambda_function.Runtime = self.Runtime lambda_function.Description = self.Description lambda_function.MemorySize = self.MemorySize lambda_function.Timeout = self.Timeout lambda_function.VpcConfig = self.VpcConfig lambda_function.Role = self.Role lambda_function.Environment = self.Environment lambda_function.Code = self._construct_code_dict() lambda_function.KmsKeyArn = self.KmsKeyArn lambda_function.ReservedConcurrentExecutions = self.ReservedConcurrentExecutions lambda_function.Tags = self._construct_tag_list(self.Tags) lambda_function.Layers = self.Layers if self.Tracing: lambda_function.TracingConfig = {"Mode": self.Tracing} if self.DeadLetterQueue: lambda_function.DeadLetterConfig = {"TargetArn": self.DeadLetterQueue['TargetArn']} return lambda_function
python
def _construct_lambda_function(self): """Constructs and returns the Lambda function. :returns: a list containing the Lambda function and execution role resources :rtype: list """ lambda_function = LambdaFunction(self.logical_id, depends_on=self.depends_on, attributes=self.resource_attributes) if self.FunctionName: lambda_function.FunctionName = self.FunctionName lambda_function.Handler = self.Handler lambda_function.Runtime = self.Runtime lambda_function.Description = self.Description lambda_function.MemorySize = self.MemorySize lambda_function.Timeout = self.Timeout lambda_function.VpcConfig = self.VpcConfig lambda_function.Role = self.Role lambda_function.Environment = self.Environment lambda_function.Code = self._construct_code_dict() lambda_function.KmsKeyArn = self.KmsKeyArn lambda_function.ReservedConcurrentExecutions = self.ReservedConcurrentExecutions lambda_function.Tags = self._construct_tag_list(self.Tags) lambda_function.Layers = self.Layers if self.Tracing: lambda_function.TracingConfig = {"Mode": self.Tracing} if self.DeadLetterQueue: lambda_function.DeadLetterConfig = {"TargetArn": self.DeadLetterQueue['TargetArn']} return lambda_function
[ "def", "_construct_lambda_function", "(", "self", ")", ":", "lambda_function", "=", "LambdaFunction", "(", "self", ".", "logical_id", ",", "depends_on", "=", "self", ".", "depends_on", ",", "attributes", "=", "self", ".", "resource_attributes", ")", "if", "self", ".", "FunctionName", ":", "lambda_function", ".", "FunctionName", "=", "self", ".", "FunctionName", "lambda_function", ".", "Handler", "=", "self", ".", "Handler", "lambda_function", ".", "Runtime", "=", "self", ".", "Runtime", "lambda_function", ".", "Description", "=", "self", ".", "Description", "lambda_function", ".", "MemorySize", "=", "self", ".", "MemorySize", "lambda_function", ".", "Timeout", "=", "self", ".", "Timeout", "lambda_function", ".", "VpcConfig", "=", "self", ".", "VpcConfig", "lambda_function", ".", "Role", "=", "self", ".", "Role", "lambda_function", ".", "Environment", "=", "self", ".", "Environment", "lambda_function", ".", "Code", "=", "self", ".", "_construct_code_dict", "(", ")", "lambda_function", ".", "KmsKeyArn", "=", "self", ".", "KmsKeyArn", "lambda_function", ".", "ReservedConcurrentExecutions", "=", "self", ".", "ReservedConcurrentExecutions", "lambda_function", ".", "Tags", "=", "self", ".", "_construct_tag_list", "(", "self", ".", "Tags", ")", "lambda_function", ".", "Layers", "=", "self", ".", "Layers", "if", "self", ".", "Tracing", ":", "lambda_function", ".", "TracingConfig", "=", "{", "\"Mode\"", ":", "self", ".", "Tracing", "}", "if", "self", ".", "DeadLetterQueue", ":", "lambda_function", ".", "DeadLetterConfig", "=", "{", "\"TargetArn\"", ":", "self", ".", "DeadLetterQueue", "[", "'TargetArn'", "]", "}", "return", "lambda_function" ]
Constructs and returns the Lambda function. :returns: a list containing the Lambda function and execution role resources :rtype: list
[ "Constructs", "and", "returns", "the", "Lambda", "function", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L152-L184
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
_construct_role
def _construct_role(self, managed_policy_map): """Constructs a Lambda execution role based on this SAM function's Policies property. :returns: the generated IAM Role :rtype: model.iam.IAMRole """ execution_role = IAMRole(self.logical_id + 'Role', attributes=self.get_passthrough_resource_attributes()) execution_role.AssumeRolePolicyDocument = IAMRolePolicies.lambda_assume_role_policy() managed_policy_arns = [ArnGenerator.generate_aws_managed_policy_arn('service-role/AWSLambdaBasicExecutionRole')] if self.Tracing: managed_policy_arns.append(ArnGenerator.generate_aws_managed_policy_arn('AWSXrayWriteOnlyAccess')) function_policies = FunctionPolicies({"Policies": self.Policies}, # No support for policy templates in the "core" policy_template_processor=None) policy_documents = [] if self.DeadLetterQueue: policy_documents.append(IAMRolePolicies.dead_letter_queue_policy( self.dead_letter_queue_policy_actions[self.DeadLetterQueue['Type']], self.DeadLetterQueue['TargetArn'])) for index, policy_entry in enumerate(function_policies.get()): if policy_entry.type is PolicyTypes.POLICY_STATEMENT: policy_documents.append({ 'PolicyName': execution_role.logical_id + 'Policy' + str(index), 'PolicyDocument': policy_entry.data }) elif policy_entry.type is PolicyTypes.MANAGED_POLICY: # There are three options: # Managed Policy Name (string): Try to convert to Managed Policy ARN # Managed Policy Arn (string): Insert it directly into the list # Intrinsic Function (dict): Insert it directly into the list # # When you insert into managed_policy_arns list, de-dupe to prevent same ARN from showing up twice # policy_arn = policy_entry.data if isinstance(policy_entry.data, string_types) and policy_entry.data in managed_policy_map: policy_arn = managed_policy_map[policy_entry.data] # De-Duplicate managed policy arns before inserting. Mainly useful # when customer specifies a managed policy which is already inserted # by SAM, such as AWSLambdaBasicExecutionRole if policy_arn not in managed_policy_arns: managed_policy_arns.append(policy_arn) else: # Policy Templates are not supported here in the "core" raise InvalidResourceException( self.logical_id, "Policy at index {} in the 'Policies' property is not valid".format(index)) execution_role.ManagedPolicyArns = list(managed_policy_arns) execution_role.Policies = policy_documents or None execution_role.PermissionsBoundary = self.PermissionsBoundary return execution_role
python
def _construct_role(self, managed_policy_map): """Constructs a Lambda execution role based on this SAM function's Policies property. :returns: the generated IAM Role :rtype: model.iam.IAMRole """ execution_role = IAMRole(self.logical_id + 'Role', attributes=self.get_passthrough_resource_attributes()) execution_role.AssumeRolePolicyDocument = IAMRolePolicies.lambda_assume_role_policy() managed_policy_arns = [ArnGenerator.generate_aws_managed_policy_arn('service-role/AWSLambdaBasicExecutionRole')] if self.Tracing: managed_policy_arns.append(ArnGenerator.generate_aws_managed_policy_arn('AWSXrayWriteOnlyAccess')) function_policies = FunctionPolicies({"Policies": self.Policies}, # No support for policy templates in the "core" policy_template_processor=None) policy_documents = [] if self.DeadLetterQueue: policy_documents.append(IAMRolePolicies.dead_letter_queue_policy( self.dead_letter_queue_policy_actions[self.DeadLetterQueue['Type']], self.DeadLetterQueue['TargetArn'])) for index, policy_entry in enumerate(function_policies.get()): if policy_entry.type is PolicyTypes.POLICY_STATEMENT: policy_documents.append({ 'PolicyName': execution_role.logical_id + 'Policy' + str(index), 'PolicyDocument': policy_entry.data }) elif policy_entry.type is PolicyTypes.MANAGED_POLICY: # There are three options: # Managed Policy Name (string): Try to convert to Managed Policy ARN # Managed Policy Arn (string): Insert it directly into the list # Intrinsic Function (dict): Insert it directly into the list # # When you insert into managed_policy_arns list, de-dupe to prevent same ARN from showing up twice # policy_arn = policy_entry.data if isinstance(policy_entry.data, string_types) and policy_entry.data in managed_policy_map: policy_arn = managed_policy_map[policy_entry.data] # De-Duplicate managed policy arns before inserting. Mainly useful # when customer specifies a managed policy which is already inserted # by SAM, such as AWSLambdaBasicExecutionRole if policy_arn not in managed_policy_arns: managed_policy_arns.append(policy_arn) else: # Policy Templates are not supported here in the "core" raise InvalidResourceException( self.logical_id, "Policy at index {} in the 'Policies' property is not valid".format(index)) execution_role.ManagedPolicyArns = list(managed_policy_arns) execution_role.Policies = policy_documents or None execution_role.PermissionsBoundary = self.PermissionsBoundary return execution_role
[ "def", "_construct_role", "(", "self", ",", "managed_policy_map", ")", ":", "execution_role", "=", "IAMRole", "(", "self", ".", "logical_id", "+", "'Role'", ",", "attributes", "=", "self", ".", "get_passthrough_resource_attributes", "(", ")", ")", "execution_role", ".", "AssumeRolePolicyDocument", "=", "IAMRolePolicies", ".", "lambda_assume_role_policy", "(", ")", "managed_policy_arns", "=", "[", "ArnGenerator", ".", "generate_aws_managed_policy_arn", "(", "'service-role/AWSLambdaBasicExecutionRole'", ")", "]", "if", "self", ".", "Tracing", ":", "managed_policy_arns", ".", "append", "(", "ArnGenerator", ".", "generate_aws_managed_policy_arn", "(", "'AWSXrayWriteOnlyAccess'", ")", ")", "function_policies", "=", "FunctionPolicies", "(", "{", "\"Policies\"", ":", "self", ".", "Policies", "}", ",", "# No support for policy templates in the \"core\"", "policy_template_processor", "=", "None", ")", "policy_documents", "=", "[", "]", "if", "self", ".", "DeadLetterQueue", ":", "policy_documents", ".", "append", "(", "IAMRolePolicies", ".", "dead_letter_queue_policy", "(", "self", ".", "dead_letter_queue_policy_actions", "[", "self", ".", "DeadLetterQueue", "[", "'Type'", "]", "]", ",", "self", ".", "DeadLetterQueue", "[", "'TargetArn'", "]", ")", ")", "for", "index", ",", "policy_entry", "in", "enumerate", "(", "function_policies", ".", "get", "(", ")", ")", ":", "if", "policy_entry", ".", "type", "is", "PolicyTypes", ".", "POLICY_STATEMENT", ":", "policy_documents", ".", "append", "(", "{", "'PolicyName'", ":", "execution_role", ".", "logical_id", "+", "'Policy'", "+", "str", "(", "index", ")", ",", "'PolicyDocument'", ":", "policy_entry", ".", "data", "}", ")", "elif", "policy_entry", ".", "type", "is", "PolicyTypes", ".", "MANAGED_POLICY", ":", "# There are three options:", "# Managed Policy Name (string): Try to convert to Managed Policy ARN", "# Managed Policy Arn (string): Insert it directly into the list", "# Intrinsic Function (dict): Insert it directly into the list", "#", "# When you insert into managed_policy_arns list, de-dupe to prevent same ARN from showing up twice", "#", "policy_arn", "=", "policy_entry", ".", "data", "if", "isinstance", "(", "policy_entry", ".", "data", ",", "string_types", ")", "and", "policy_entry", ".", "data", "in", "managed_policy_map", ":", "policy_arn", "=", "managed_policy_map", "[", "policy_entry", ".", "data", "]", "# De-Duplicate managed policy arns before inserting. Mainly useful", "# when customer specifies a managed policy which is already inserted", "# by SAM, such as AWSLambdaBasicExecutionRole", "if", "policy_arn", "not", "in", "managed_policy_arns", ":", "managed_policy_arns", ".", "append", "(", "policy_arn", ")", "else", ":", "# Policy Templates are not supported here in the \"core\"", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Policy at index {} in the 'Policies' property is not valid\"", ".", "format", "(", "index", ")", ")", "execution_role", ".", "ManagedPolicyArns", "=", "list", "(", "managed_policy_arns", ")", "execution_role", ".", "Policies", "=", "policy_documents", "or", "None", "execution_role", ".", "PermissionsBoundary", "=", "self", ".", "PermissionsBoundary", "return", "execution_role" ]
Constructs a Lambda execution role based on this SAM function's Policies property. :returns: the generated IAM Role :rtype: model.iam.IAMRole
[ "Constructs", "a", "Lambda", "execution", "role", "based", "on", "this", "SAM", "function", "s", "Policies", "property", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L186-L246
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
_validate_dlq
def _validate_dlq(self): """Validates whether the DeadLetterQueue LogicalId is validation :raise: InvalidResourceException """ # Validate required logical ids valid_dlq_types = str(list(self.dead_letter_queue_policy_actions.keys())) if not self.DeadLetterQueue.get('Type') or not self.DeadLetterQueue.get('TargetArn'): raise InvalidResourceException(self.logical_id, "'DeadLetterQueue' requires Type and TargetArn properties to be specified" .format(valid_dlq_types)) # Validate required Types if not self.DeadLetterQueue['Type'] in self.dead_letter_queue_policy_actions: raise InvalidResourceException(self.logical_id, "'DeadLetterQueue' requires Type of {}".format(valid_dlq_types))
python
def _validate_dlq(self): """Validates whether the DeadLetterQueue LogicalId is validation :raise: InvalidResourceException """ # Validate required logical ids valid_dlq_types = str(list(self.dead_letter_queue_policy_actions.keys())) if not self.DeadLetterQueue.get('Type') or not self.DeadLetterQueue.get('TargetArn'): raise InvalidResourceException(self.logical_id, "'DeadLetterQueue' requires Type and TargetArn properties to be specified" .format(valid_dlq_types)) # Validate required Types if not self.DeadLetterQueue['Type'] in self.dead_letter_queue_policy_actions: raise InvalidResourceException(self.logical_id, "'DeadLetterQueue' requires Type of {}".format(valid_dlq_types))
[ "def", "_validate_dlq", "(", "self", ")", ":", "# Validate required logical ids", "valid_dlq_types", "=", "str", "(", "list", "(", "self", ".", "dead_letter_queue_policy_actions", ".", "keys", "(", ")", ")", ")", "if", "not", "self", ".", "DeadLetterQueue", ".", "get", "(", "'Type'", ")", "or", "not", "self", ".", "DeadLetterQueue", ".", "get", "(", "'TargetArn'", ")", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"'DeadLetterQueue' requires Type and TargetArn properties to be specified\"", ".", "format", "(", "valid_dlq_types", ")", ")", "# Validate required Types", "if", "not", "self", ".", "DeadLetterQueue", "[", "'Type'", "]", "in", "self", ".", "dead_letter_queue_policy_actions", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"'DeadLetterQueue' requires Type of {}\"", ".", "format", "(", "valid_dlq_types", ")", ")" ]
Validates whether the DeadLetterQueue LogicalId is validation :raise: InvalidResourceException
[ "Validates", "whether", "the", "DeadLetterQueue", "LogicalId", "is", "validation", ":", "raise", ":", "InvalidResourceException" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L248-L262
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
_generate_event_resources
def _generate_event_resources(self, lambda_function, execution_role, event_resources, lambda_alias=None): """Generates and returns the resources associated with this function's events. :param model.lambda_.LambdaFunction lambda_function: generated Lambda function :param iam.IAMRole execution_role: generated Lambda execution role :param implicit_api: Global Implicit API resource where the implicit APIs get attached to, if necessary :param implicit_api_stage: Global implicit API stage resource where implicit APIs get attached to, if necessary :param event_resources: All the event sources associated with this Lambda function :param model.lambda_.LambdaAlias lambda_alias: Optional Lambda Alias resource if we want to connect the event sources to this alias :returns: a list containing the function's event resources :rtype: list """ resources = [] if self.Events: for logical_id, event_dict in self.Events.items(): try: eventsource = self.event_resolver.resolve_resource_type(event_dict).from_dict( lambda_function.logical_id + logical_id, event_dict, logical_id) except TypeError as e: raise InvalidEventException(logical_id, "{}".format(e)) kwargs = { # When Alias is provided, connect all event sources to the alias and *not* the function 'function': lambda_alias or lambda_function, 'role': execution_role, } for name, resource in event_resources[logical_id].items(): kwargs[name] = resource resources += eventsource.to_cloudformation(**kwargs) return resources
python
def _generate_event_resources(self, lambda_function, execution_role, event_resources, lambda_alias=None): """Generates and returns the resources associated with this function's events. :param model.lambda_.LambdaFunction lambda_function: generated Lambda function :param iam.IAMRole execution_role: generated Lambda execution role :param implicit_api: Global Implicit API resource where the implicit APIs get attached to, if necessary :param implicit_api_stage: Global implicit API stage resource where implicit APIs get attached to, if necessary :param event_resources: All the event sources associated with this Lambda function :param model.lambda_.LambdaAlias lambda_alias: Optional Lambda Alias resource if we want to connect the event sources to this alias :returns: a list containing the function's event resources :rtype: list """ resources = [] if self.Events: for logical_id, event_dict in self.Events.items(): try: eventsource = self.event_resolver.resolve_resource_type(event_dict).from_dict( lambda_function.logical_id + logical_id, event_dict, logical_id) except TypeError as e: raise InvalidEventException(logical_id, "{}".format(e)) kwargs = { # When Alias is provided, connect all event sources to the alias and *not* the function 'function': lambda_alias or lambda_function, 'role': execution_role, } for name, resource in event_resources[logical_id].items(): kwargs[name] = resource resources += eventsource.to_cloudformation(**kwargs) return resources
[ "def", "_generate_event_resources", "(", "self", ",", "lambda_function", ",", "execution_role", ",", "event_resources", ",", "lambda_alias", "=", "None", ")", ":", "resources", "=", "[", "]", "if", "self", ".", "Events", ":", "for", "logical_id", ",", "event_dict", "in", "self", ".", "Events", ".", "items", "(", ")", ":", "try", ":", "eventsource", "=", "self", ".", "event_resolver", ".", "resolve_resource_type", "(", "event_dict", ")", ".", "from_dict", "(", "lambda_function", ".", "logical_id", "+", "logical_id", ",", "event_dict", ",", "logical_id", ")", "except", "TypeError", "as", "e", ":", "raise", "InvalidEventException", "(", "logical_id", ",", "\"{}\"", ".", "format", "(", "e", ")", ")", "kwargs", "=", "{", "# When Alias is provided, connect all event sources to the alias and *not* the function", "'function'", ":", "lambda_alias", "or", "lambda_function", ",", "'role'", ":", "execution_role", ",", "}", "for", "name", ",", "resource", "in", "event_resources", "[", "logical_id", "]", ".", "items", "(", ")", ":", "kwargs", "[", "name", "]", "=", "resource", "resources", "+=", "eventsource", ".", "to_cloudformation", "(", "*", "*", "kwargs", ")", "return", "resources" ]
Generates and returns the resources associated with this function's events. :param model.lambda_.LambdaFunction lambda_function: generated Lambda function :param iam.IAMRole execution_role: generated Lambda execution role :param implicit_api: Global Implicit API resource where the implicit APIs get attached to, if necessary :param implicit_api_stage: Global implicit API stage resource where implicit APIs get attached to, if necessary :param event_resources: All the event sources associated with this Lambda function :param model.lambda_.LambdaAlias lambda_alias: Optional Lambda Alias resource if we want to connect the event sources to this alias :returns: a list containing the function's event resources :rtype: list
[ "Generates", "and", "returns", "the", "resources", "associated", "with", "this", "function", "s", "events", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L276-L309
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
_construct_version
def _construct_version(self, function, intrinsics_resolver): """Constructs a Lambda Version resource that will be auto-published when CodeUri of the function changes. Old versions will not be deleted without a direct reference from the CloudFormation template. :param model.lambda_.LambdaFunction function: Lambda function object that is being connected to a version :param model.intrinsics.resolver.IntrinsicsResolver intrinsics_resolver: Class that can help resolve references to parameters present in CodeUri. It is a common usecase to set S3Key of Code to be a template parameter. Need to resolve the values otherwise we will never detect a change in Code dict :return: Lambda function Version resource """ code_dict = function.Code if not code_dict: raise ValueError("Lambda function code must be a valid non-empty dictionary") if not intrinsics_resolver: raise ValueError("intrinsics_resolver is required for versions creation") # Resolve references to template parameters before creating hash. This will *not* resolve all intrinsics # because we cannot resolve runtime values like Arn of a resource. For purposes of detecting changes, this # is good enough. Here is why: # # When using intrinsic functions there are two cases when has must change: # - Value of the template parameter changes # - (or) LogicalId of a referenced resource changes ie. !GetAtt NewResource.Arn # # Later case will already change the hash because some value in the Code dictionary changes. We handle the # first case by resolving references to template parameters. It is okay even if these references are # present inside another intrinsic such as !Join. The resolver will replace the reference with the parameter's # value and keep all other parts of !Join identical. This will still trigger a change in the hash. code_dict = intrinsics_resolver.resolve_parameter_refs(code_dict) # Construct the LogicalID of Lambda version by appending 10 characters of SHA of CodeUri. This is necessary # to trigger creation of a new version every time code location changes. Since logicalId changes, CloudFormation # will drop the old version and create a new one for us. We set a DeletionPolicy on the version resource to # prevent CloudFormation from actually deleting the underlying version resource # # SHA Collisions: For purposes of triggering a new update, we are concerned about just the difference previous # and next hashes. The chances that two subsequent hashes collide is fairly low. prefix = "{id}Version".format(id=self.logical_id) logical_id = logical_id_generator.LogicalIdGenerator(prefix, code_dict).gen() attributes = self.get_passthrough_resource_attributes() if attributes is None: attributes = {} attributes["DeletionPolicy"] = "Retain" lambda_version = LambdaVersion(logical_id=logical_id, attributes=attributes) lambda_version.FunctionName = function.get_runtime_attr('name') lambda_version.Description = self.VersionDescription return lambda_version
python
def _construct_version(self, function, intrinsics_resolver): """Constructs a Lambda Version resource that will be auto-published when CodeUri of the function changes. Old versions will not be deleted without a direct reference from the CloudFormation template. :param model.lambda_.LambdaFunction function: Lambda function object that is being connected to a version :param model.intrinsics.resolver.IntrinsicsResolver intrinsics_resolver: Class that can help resolve references to parameters present in CodeUri. It is a common usecase to set S3Key of Code to be a template parameter. Need to resolve the values otherwise we will never detect a change in Code dict :return: Lambda function Version resource """ code_dict = function.Code if not code_dict: raise ValueError("Lambda function code must be a valid non-empty dictionary") if not intrinsics_resolver: raise ValueError("intrinsics_resolver is required for versions creation") # Resolve references to template parameters before creating hash. This will *not* resolve all intrinsics # because we cannot resolve runtime values like Arn of a resource. For purposes of detecting changes, this # is good enough. Here is why: # # When using intrinsic functions there are two cases when has must change: # - Value of the template parameter changes # - (or) LogicalId of a referenced resource changes ie. !GetAtt NewResource.Arn # # Later case will already change the hash because some value in the Code dictionary changes. We handle the # first case by resolving references to template parameters. It is okay even if these references are # present inside another intrinsic such as !Join. The resolver will replace the reference with the parameter's # value and keep all other parts of !Join identical. This will still trigger a change in the hash. code_dict = intrinsics_resolver.resolve_parameter_refs(code_dict) # Construct the LogicalID of Lambda version by appending 10 characters of SHA of CodeUri. This is necessary # to trigger creation of a new version every time code location changes. Since logicalId changes, CloudFormation # will drop the old version and create a new one for us. We set a DeletionPolicy on the version resource to # prevent CloudFormation from actually deleting the underlying version resource # # SHA Collisions: For purposes of triggering a new update, we are concerned about just the difference previous # and next hashes. The chances that two subsequent hashes collide is fairly low. prefix = "{id}Version".format(id=self.logical_id) logical_id = logical_id_generator.LogicalIdGenerator(prefix, code_dict).gen() attributes = self.get_passthrough_resource_attributes() if attributes is None: attributes = {} attributes["DeletionPolicy"] = "Retain" lambda_version = LambdaVersion(logical_id=logical_id, attributes=attributes) lambda_version.FunctionName = function.get_runtime_attr('name') lambda_version.Description = self.VersionDescription return lambda_version
[ "def", "_construct_version", "(", "self", ",", "function", ",", "intrinsics_resolver", ")", ":", "code_dict", "=", "function", ".", "Code", "if", "not", "code_dict", ":", "raise", "ValueError", "(", "\"Lambda function code must be a valid non-empty dictionary\"", ")", "if", "not", "intrinsics_resolver", ":", "raise", "ValueError", "(", "\"intrinsics_resolver is required for versions creation\"", ")", "# Resolve references to template parameters before creating hash. This will *not* resolve all intrinsics", "# because we cannot resolve runtime values like Arn of a resource. For purposes of detecting changes, this", "# is good enough. Here is why:", "#", "# When using intrinsic functions there are two cases when has must change:", "# - Value of the template parameter changes", "# - (or) LogicalId of a referenced resource changes ie. !GetAtt NewResource.Arn", "#", "# Later case will already change the hash because some value in the Code dictionary changes. We handle the", "# first case by resolving references to template parameters. It is okay even if these references are", "# present inside another intrinsic such as !Join. The resolver will replace the reference with the parameter's", "# value and keep all other parts of !Join identical. This will still trigger a change in the hash.", "code_dict", "=", "intrinsics_resolver", ".", "resolve_parameter_refs", "(", "code_dict", ")", "# Construct the LogicalID of Lambda version by appending 10 characters of SHA of CodeUri. This is necessary", "# to trigger creation of a new version every time code location changes. Since logicalId changes, CloudFormation", "# will drop the old version and create a new one for us. We set a DeletionPolicy on the version resource to", "# prevent CloudFormation from actually deleting the underlying version resource", "#", "# SHA Collisions: For purposes of triggering a new update, we are concerned about just the difference previous", "# and next hashes. The chances that two subsequent hashes collide is fairly low.", "prefix", "=", "\"{id}Version\"", ".", "format", "(", "id", "=", "self", ".", "logical_id", ")", "logical_id", "=", "logical_id_generator", ".", "LogicalIdGenerator", "(", "prefix", ",", "code_dict", ")", ".", "gen", "(", ")", "attributes", "=", "self", ".", "get_passthrough_resource_attributes", "(", ")", "if", "attributes", "is", "None", ":", "attributes", "=", "{", "}", "attributes", "[", "\"DeletionPolicy\"", "]", "=", "\"Retain\"", "lambda_version", "=", "LambdaVersion", "(", "logical_id", "=", "logical_id", ",", "attributes", "=", "attributes", ")", "lambda_version", ".", "FunctionName", "=", "function", ".", "get_runtime_attr", "(", "'name'", ")", "lambda_version", ".", "Description", "=", "self", ".", "VersionDescription", "return", "lambda_version" ]
Constructs a Lambda Version resource that will be auto-published when CodeUri of the function changes. Old versions will not be deleted without a direct reference from the CloudFormation template. :param model.lambda_.LambdaFunction function: Lambda function object that is being connected to a version :param model.intrinsics.resolver.IntrinsicsResolver intrinsics_resolver: Class that can help resolve references to parameters present in CodeUri. It is a common usecase to set S3Key of Code to be a template parameter. Need to resolve the values otherwise we will never detect a change in Code dict :return: Lambda function Version resource
[ "Constructs", "a", "Lambda", "Version", "resource", "that", "will", "be", "auto", "-", "published", "when", "CodeUri", "of", "the", "function", "changes", ".", "Old", "versions", "will", "not", "be", "deleted", "without", "a", "direct", "reference", "from", "the", "CloudFormation", "template", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L321-L371
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
_construct_alias
def _construct_alias(self, name, function, version): """Constructs a Lambda Alias for the given function and pointing to the given version :param string name: Name of the alias :param model.lambda_.LambdaFunction function: Lambda function object to associate the alias with :param model.lambda_.LambdaVersion version: Lambda version object to associate the alias with :return: Lambda alias object :rtype model.lambda_.LambdaAlias """ if not name: raise InvalidResourceException(self.logical_id, "Alias name is required to create an alias") logical_id = "{id}Alias{suffix}".format(id=function.logical_id, suffix=name) alias = LambdaAlias(logical_id=logical_id, attributes=self.get_passthrough_resource_attributes()) alias.Name = name alias.FunctionName = function.get_runtime_attr('name') alias.FunctionVersion = version.get_runtime_attr("version") return alias
python
def _construct_alias(self, name, function, version): """Constructs a Lambda Alias for the given function and pointing to the given version :param string name: Name of the alias :param model.lambda_.LambdaFunction function: Lambda function object to associate the alias with :param model.lambda_.LambdaVersion version: Lambda version object to associate the alias with :return: Lambda alias object :rtype model.lambda_.LambdaAlias """ if not name: raise InvalidResourceException(self.logical_id, "Alias name is required to create an alias") logical_id = "{id}Alias{suffix}".format(id=function.logical_id, suffix=name) alias = LambdaAlias(logical_id=logical_id, attributes=self.get_passthrough_resource_attributes()) alias.Name = name alias.FunctionName = function.get_runtime_attr('name') alias.FunctionVersion = version.get_runtime_attr("version") return alias
[ "def", "_construct_alias", "(", "self", ",", "name", ",", "function", ",", "version", ")", ":", "if", "not", "name", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"Alias name is required to create an alias\"", ")", "logical_id", "=", "\"{id}Alias{suffix}\"", ".", "format", "(", "id", "=", "function", ".", "logical_id", ",", "suffix", "=", "name", ")", "alias", "=", "LambdaAlias", "(", "logical_id", "=", "logical_id", ",", "attributes", "=", "self", ".", "get_passthrough_resource_attributes", "(", ")", ")", "alias", ".", "Name", "=", "name", "alias", ".", "FunctionName", "=", "function", ".", "get_runtime_attr", "(", "'name'", ")", "alias", ".", "FunctionVersion", "=", "version", ".", "get_runtime_attr", "(", "\"version\"", ")", "return", "alias" ]
Constructs a Lambda Alias for the given function and pointing to the given version :param string name: Name of the alias :param model.lambda_.LambdaFunction function: Lambda function object to associate the alias with :param model.lambda_.LambdaVersion version: Lambda version object to associate the alias with :return: Lambda alias object :rtype model.lambda_.LambdaAlias
[ "Constructs", "a", "Lambda", "Alias", "for", "the", "given", "function", "and", "pointing", "to", "the", "given", "version" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L373-L392
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
SamApi.to_cloudformation
def to_cloudformation(self, **kwargs): """Returns the API Gateway RestApi, Deployment, and Stage to which this SAM Api corresponds. :param dict kwargs: already-converted resources that may need to be modified when converting this \ macro to pure CloudFormation :returns: a list of vanilla CloudFormation Resources, to which this Function expands :rtype: list """ resources = [] api_generator = ApiGenerator(self.logical_id, self.CacheClusterEnabled, self.CacheClusterSize, self.Variables, self.depends_on, self.DefinitionBody, self.DefinitionUri, self.Name, self.StageName, endpoint_configuration=self.EndpointConfiguration, method_settings=self.MethodSettings, binary_media=self.BinaryMediaTypes, minimum_compression_size=self.MinimumCompressionSize, cors=self.Cors, auth=self.Auth, gateway_responses=self.GatewayResponses, access_log_setting=self.AccessLogSetting, canary_setting=self.CanarySetting, tracing_enabled=self.TracingEnabled, resource_attributes=self.resource_attributes, passthrough_resource_attributes=self.get_passthrough_resource_attributes()) rest_api, deployment, stage, permissions = api_generator.to_cloudformation() resources.extend([rest_api, deployment, stage]) resources.extend(permissions) return resources
python
def to_cloudformation(self, **kwargs): """Returns the API Gateway RestApi, Deployment, and Stage to which this SAM Api corresponds. :param dict kwargs: already-converted resources that may need to be modified when converting this \ macro to pure CloudFormation :returns: a list of vanilla CloudFormation Resources, to which this Function expands :rtype: list """ resources = [] api_generator = ApiGenerator(self.logical_id, self.CacheClusterEnabled, self.CacheClusterSize, self.Variables, self.depends_on, self.DefinitionBody, self.DefinitionUri, self.Name, self.StageName, endpoint_configuration=self.EndpointConfiguration, method_settings=self.MethodSettings, binary_media=self.BinaryMediaTypes, minimum_compression_size=self.MinimumCompressionSize, cors=self.Cors, auth=self.Auth, gateway_responses=self.GatewayResponses, access_log_setting=self.AccessLogSetting, canary_setting=self.CanarySetting, tracing_enabled=self.TracingEnabled, resource_attributes=self.resource_attributes, passthrough_resource_attributes=self.get_passthrough_resource_attributes()) rest_api, deployment, stage, permissions = api_generator.to_cloudformation() resources.extend([rest_api, deployment, stage]) resources.extend(permissions) return resources
[ "def", "to_cloudformation", "(", "self", ",", "*", "*", "kwargs", ")", ":", "resources", "=", "[", "]", "api_generator", "=", "ApiGenerator", "(", "self", ".", "logical_id", ",", "self", ".", "CacheClusterEnabled", ",", "self", ".", "CacheClusterSize", ",", "self", ".", "Variables", ",", "self", ".", "depends_on", ",", "self", ".", "DefinitionBody", ",", "self", ".", "DefinitionUri", ",", "self", ".", "Name", ",", "self", ".", "StageName", ",", "endpoint_configuration", "=", "self", ".", "EndpointConfiguration", ",", "method_settings", "=", "self", ".", "MethodSettings", ",", "binary_media", "=", "self", ".", "BinaryMediaTypes", ",", "minimum_compression_size", "=", "self", ".", "MinimumCompressionSize", ",", "cors", "=", "self", ".", "Cors", ",", "auth", "=", "self", ".", "Auth", ",", "gateway_responses", "=", "self", ".", "GatewayResponses", ",", "access_log_setting", "=", "self", ".", "AccessLogSetting", ",", "canary_setting", "=", "self", ".", "CanarySetting", ",", "tracing_enabled", "=", "self", ".", "TracingEnabled", ",", "resource_attributes", "=", "self", ".", "resource_attributes", ",", "passthrough_resource_attributes", "=", "self", ".", "get_passthrough_resource_attributes", "(", ")", ")", "rest_api", ",", "deployment", ",", "stage", ",", "permissions", "=", "api_generator", ".", "to_cloudformation", "(", ")", "resources", ".", "extend", "(", "[", "rest_api", ",", "deployment", ",", "stage", "]", ")", "resources", ".", "extend", "(", "permissions", ")", "return", "resources" ]
Returns the API Gateway RestApi, Deployment, and Stage to which this SAM Api corresponds. :param dict kwargs: already-converted resources that may need to be modified when converting this \ macro to pure CloudFormation :returns: a list of vanilla CloudFormation Resources, to which this Function expands :rtype: list
[ "Returns", "the", "API", "Gateway", "RestApi", "Deployment", "and", "Stage", "to", "which", "this", "SAM", "Api", "corresponds", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L456-L493
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
SamApplication._construct_nested_stack
def _construct_nested_stack(self): """Constructs a AWS::CloudFormation::Stack resource """ nested_stack = NestedStack(self.logical_id, depends_on=self.depends_on, attributes=self.get_passthrough_resource_attributes()) nested_stack.Parameters = self.Parameters nested_stack.NotificationArns = self.NotificationArns application_tags = self._get_application_tags() nested_stack.Tags = self._construct_tag_list(self.Tags, application_tags) nested_stack.TimeoutInMinutes = self.TimeoutInMinutes nested_stack.TemplateURL = self.TemplateUrl if self.TemplateUrl else "" return nested_stack
python
def _construct_nested_stack(self): """Constructs a AWS::CloudFormation::Stack resource """ nested_stack = NestedStack(self.logical_id, depends_on=self.depends_on, attributes=self.get_passthrough_resource_attributes()) nested_stack.Parameters = self.Parameters nested_stack.NotificationArns = self.NotificationArns application_tags = self._get_application_tags() nested_stack.Tags = self._construct_tag_list(self.Tags, application_tags) nested_stack.TimeoutInMinutes = self.TimeoutInMinutes nested_stack.TemplateURL = self.TemplateUrl if self.TemplateUrl else "" return nested_stack
[ "def", "_construct_nested_stack", "(", "self", ")", ":", "nested_stack", "=", "NestedStack", "(", "self", ".", "logical_id", ",", "depends_on", "=", "self", ".", "depends_on", ",", "attributes", "=", "self", ".", "get_passthrough_resource_attributes", "(", ")", ")", "nested_stack", ".", "Parameters", "=", "self", ".", "Parameters", "nested_stack", ".", "NotificationArns", "=", "self", ".", "NotificationArns", "application_tags", "=", "self", ".", "_get_application_tags", "(", ")", "nested_stack", ".", "Tags", "=", "self", ".", "_construct_tag_list", "(", "self", ".", "Tags", ",", "application_tags", ")", "nested_stack", ".", "TimeoutInMinutes", "=", "self", ".", "TimeoutInMinutes", "nested_stack", ".", "TemplateURL", "=", "self", ".", "TemplateUrl", "if", "self", ".", "TemplateUrl", "else", "\"\"", "return", "nested_stack" ]
Constructs a AWS::CloudFormation::Stack resource
[ "Constructs", "a", "AWS", "::", "CloudFormation", "::", "Stack", "resource" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L583-L595
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
SamApplication._get_application_tags
def _get_application_tags(self): """Adds tags to the stack if this resource is using the serverless app repo """ application_tags = {} if isinstance(self.Location, dict): if (self.APPLICATION_ID_KEY in self.Location.keys() and self.Location[self.APPLICATION_ID_KEY] is not None): application_tags[self._SAR_APP_KEY] = self.Location[self.APPLICATION_ID_KEY] if (self.SEMANTIC_VERSION_KEY in self.Location.keys() and self.Location[self.SEMANTIC_VERSION_KEY] is not None): application_tags[self._SAR_SEMVER_KEY] = self.Location[self.SEMANTIC_VERSION_KEY] return application_tags
python
def _get_application_tags(self): """Adds tags to the stack if this resource is using the serverless app repo """ application_tags = {} if isinstance(self.Location, dict): if (self.APPLICATION_ID_KEY in self.Location.keys() and self.Location[self.APPLICATION_ID_KEY] is not None): application_tags[self._SAR_APP_KEY] = self.Location[self.APPLICATION_ID_KEY] if (self.SEMANTIC_VERSION_KEY in self.Location.keys() and self.Location[self.SEMANTIC_VERSION_KEY] is not None): application_tags[self._SAR_SEMVER_KEY] = self.Location[self.SEMANTIC_VERSION_KEY] return application_tags
[ "def", "_get_application_tags", "(", "self", ")", ":", "application_tags", "=", "{", "}", "if", "isinstance", "(", "self", ".", "Location", ",", "dict", ")", ":", "if", "(", "self", ".", "APPLICATION_ID_KEY", "in", "self", ".", "Location", ".", "keys", "(", ")", "and", "self", ".", "Location", "[", "self", ".", "APPLICATION_ID_KEY", "]", "is", "not", "None", ")", ":", "application_tags", "[", "self", ".", "_SAR_APP_KEY", "]", "=", "self", ".", "Location", "[", "self", ".", "APPLICATION_ID_KEY", "]", "if", "(", "self", ".", "SEMANTIC_VERSION_KEY", "in", "self", ".", "Location", ".", "keys", "(", ")", "and", "self", ".", "Location", "[", "self", ".", "SEMANTIC_VERSION_KEY", "]", "is", "not", "None", ")", ":", "application_tags", "[", "self", ".", "_SAR_SEMVER_KEY", "]", "=", "self", ".", "Location", "[", "self", ".", "SEMANTIC_VERSION_KEY", "]", "return", "application_tags" ]
Adds tags to the stack if this resource is using the serverless app repo
[ "Adds", "tags", "to", "the", "stack", "if", "this", "resource", "is", "using", "the", "serverless", "app", "repo" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L597-L608
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
SamLayerVersion.to_cloudformation
def to_cloudformation(self, **kwargs): """Returns the Lambda layer to which this SAM Layer corresponds. :param dict kwargs: already-converted resources that may need to be modified when converting this \ macro to pure CloudFormation :returns: a list of vanilla CloudFormation Resources, to which this Function expands :rtype: list """ resources = [] # Append any CFN resources: intrinsics_resolver = kwargs["intrinsics_resolver"] resources.append(self._construct_lambda_layer(intrinsics_resolver)) return resources
python
def to_cloudformation(self, **kwargs): """Returns the Lambda layer to which this SAM Layer corresponds. :param dict kwargs: already-converted resources that may need to be modified when converting this \ macro to pure CloudFormation :returns: a list of vanilla CloudFormation Resources, to which this Function expands :rtype: list """ resources = [] # Append any CFN resources: intrinsics_resolver = kwargs["intrinsics_resolver"] resources.append(self._construct_lambda_layer(intrinsics_resolver)) return resources
[ "def", "to_cloudformation", "(", "self", ",", "*", "*", "kwargs", ")", ":", "resources", "=", "[", "]", "# Append any CFN resources:", "intrinsics_resolver", "=", "kwargs", "[", "\"intrinsics_resolver\"", "]", "resources", ".", "append", "(", "self", ".", "_construct_lambda_layer", "(", "intrinsics_resolver", ")", ")", "return", "resources" ]
Returns the Lambda layer to which this SAM Layer corresponds. :param dict kwargs: already-converted resources that may need to be modified when converting this \ macro to pure CloudFormation :returns: a list of vanilla CloudFormation Resources, to which this Function expands :rtype: list
[ "Returns", "the", "Lambda", "layer", "to", "which", "this", "SAM", "Layer", "corresponds", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L628-L642
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
SamLayerVersion._construct_lambda_layer
def _construct_lambda_layer(self, intrinsics_resolver): """Constructs and returns the Lambda function. :returns: a list containing the Lambda function and execution role resources :rtype: list """ # Resolve intrinsics if applicable: self.LayerName = self._resolve_string_parameter(intrinsics_resolver, self.LayerName, 'LayerName') self.LicenseInfo = self._resolve_string_parameter(intrinsics_resolver, self.LicenseInfo, 'LicenseInfo') self.Description = self._resolve_string_parameter(intrinsics_resolver, self.Description, 'Description') self.RetentionPolicy = self._resolve_string_parameter(intrinsics_resolver, self.RetentionPolicy, 'RetentionPolicy') retention_policy_value = self._get_retention_policy_value() attributes = self.get_passthrough_resource_attributes() if attributes is None: attributes = {} attributes['DeletionPolicy'] = retention_policy_value old_logical_id = self.logical_id new_logical_id = logical_id_generator.LogicalIdGenerator(old_logical_id, self.to_dict()).gen() self.logical_id = new_logical_id lambda_layer = LambdaLayerVersion(self.logical_id, depends_on=self.depends_on, attributes=attributes) # Changing the LayerName property: when a layer is published, it is given an Arn # example: arn:aws:lambda:us-west-2:123456789012:layer:MyLayer:1 # where MyLayer is the LayerName property if it exists; otherwise, it is the # LogicalId of this resource. Since a LayerVersion is an immutable resource, when # CloudFormation updates this resource, it will ALWAYS create a new version then # delete the old version if the logical ids match. What this does is change the # logical id of every layer (so a `DeletionPolicy: Retain` can work) and set the # LayerName property of the layer so that the Arn will still always be the same # with the exception of an incrementing version number. if not self.LayerName: self.LayerName = old_logical_id lambda_layer.LayerName = self.LayerName lambda_layer.Description = self.Description lambda_layer.Content = construct_s3_location_object(self.ContentUri, self.logical_id, 'ContentUri') lambda_layer.CompatibleRuntimes = self.CompatibleRuntimes lambda_layer.LicenseInfo = self.LicenseInfo return lambda_layer
python
def _construct_lambda_layer(self, intrinsics_resolver): """Constructs and returns the Lambda function. :returns: a list containing the Lambda function and execution role resources :rtype: list """ # Resolve intrinsics if applicable: self.LayerName = self._resolve_string_parameter(intrinsics_resolver, self.LayerName, 'LayerName') self.LicenseInfo = self._resolve_string_parameter(intrinsics_resolver, self.LicenseInfo, 'LicenseInfo') self.Description = self._resolve_string_parameter(intrinsics_resolver, self.Description, 'Description') self.RetentionPolicy = self._resolve_string_parameter(intrinsics_resolver, self.RetentionPolicy, 'RetentionPolicy') retention_policy_value = self._get_retention_policy_value() attributes = self.get_passthrough_resource_attributes() if attributes is None: attributes = {} attributes['DeletionPolicy'] = retention_policy_value old_logical_id = self.logical_id new_logical_id = logical_id_generator.LogicalIdGenerator(old_logical_id, self.to_dict()).gen() self.logical_id = new_logical_id lambda_layer = LambdaLayerVersion(self.logical_id, depends_on=self.depends_on, attributes=attributes) # Changing the LayerName property: when a layer is published, it is given an Arn # example: arn:aws:lambda:us-west-2:123456789012:layer:MyLayer:1 # where MyLayer is the LayerName property if it exists; otherwise, it is the # LogicalId of this resource. Since a LayerVersion is an immutable resource, when # CloudFormation updates this resource, it will ALWAYS create a new version then # delete the old version if the logical ids match. What this does is change the # logical id of every layer (so a `DeletionPolicy: Retain` can work) and set the # LayerName property of the layer so that the Arn will still always be the same # with the exception of an incrementing version number. if not self.LayerName: self.LayerName = old_logical_id lambda_layer.LayerName = self.LayerName lambda_layer.Description = self.Description lambda_layer.Content = construct_s3_location_object(self.ContentUri, self.logical_id, 'ContentUri') lambda_layer.CompatibleRuntimes = self.CompatibleRuntimes lambda_layer.LicenseInfo = self.LicenseInfo return lambda_layer
[ "def", "_construct_lambda_layer", "(", "self", ",", "intrinsics_resolver", ")", ":", "# Resolve intrinsics if applicable:", "self", ".", "LayerName", "=", "self", ".", "_resolve_string_parameter", "(", "intrinsics_resolver", ",", "self", ".", "LayerName", ",", "'LayerName'", ")", "self", ".", "LicenseInfo", "=", "self", ".", "_resolve_string_parameter", "(", "intrinsics_resolver", ",", "self", ".", "LicenseInfo", ",", "'LicenseInfo'", ")", "self", ".", "Description", "=", "self", ".", "_resolve_string_parameter", "(", "intrinsics_resolver", ",", "self", ".", "Description", ",", "'Description'", ")", "self", ".", "RetentionPolicy", "=", "self", ".", "_resolve_string_parameter", "(", "intrinsics_resolver", ",", "self", ".", "RetentionPolicy", ",", "'RetentionPolicy'", ")", "retention_policy_value", "=", "self", ".", "_get_retention_policy_value", "(", ")", "attributes", "=", "self", ".", "get_passthrough_resource_attributes", "(", ")", "if", "attributes", "is", "None", ":", "attributes", "=", "{", "}", "attributes", "[", "'DeletionPolicy'", "]", "=", "retention_policy_value", "old_logical_id", "=", "self", ".", "logical_id", "new_logical_id", "=", "logical_id_generator", ".", "LogicalIdGenerator", "(", "old_logical_id", ",", "self", ".", "to_dict", "(", ")", ")", ".", "gen", "(", ")", "self", ".", "logical_id", "=", "new_logical_id", "lambda_layer", "=", "LambdaLayerVersion", "(", "self", ".", "logical_id", ",", "depends_on", "=", "self", ".", "depends_on", ",", "attributes", "=", "attributes", ")", "# Changing the LayerName property: when a layer is published, it is given an Arn", "# example: arn:aws:lambda:us-west-2:123456789012:layer:MyLayer:1", "# where MyLayer is the LayerName property if it exists; otherwise, it is the", "# LogicalId of this resource. Since a LayerVersion is an immutable resource, when", "# CloudFormation updates this resource, it will ALWAYS create a new version then", "# delete the old version if the logical ids match. What this does is change the", "# logical id of every layer (so a `DeletionPolicy: Retain` can work) and set the", "# LayerName property of the layer so that the Arn will still always be the same", "# with the exception of an incrementing version number.", "if", "not", "self", ".", "LayerName", ":", "self", ".", "LayerName", "=", "old_logical_id", "lambda_layer", ".", "LayerName", "=", "self", ".", "LayerName", "lambda_layer", ".", "Description", "=", "self", ".", "Description", "lambda_layer", ".", "Content", "=", "construct_s3_location_object", "(", "self", ".", "ContentUri", ",", "self", ".", "logical_id", ",", "'ContentUri'", ")", "lambda_layer", ".", "CompatibleRuntimes", "=", "self", ".", "CompatibleRuntimes", "lambda_layer", ".", "LicenseInfo", "=", "self", ".", "LicenseInfo", "return", "lambda_layer" ]
Constructs and returns the Lambda function. :returns: a list containing the Lambda function and execution role resources :rtype: list
[ "Constructs", "and", "returns", "the", "Lambda", "function", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L644-L688
train
awslabs/serverless-application-model
samtranslator/model/sam_resources.py
SamLayerVersion._get_retention_policy_value
def _get_retention_policy_value(self): """ Sets the deletion policy on this resource. The default is 'Retain'. :return: value for the DeletionPolicy attribute. """ if self.RetentionPolicy is None or self.RetentionPolicy.lower() == self.RETAIN.lower(): return self.RETAIN elif self.RetentionPolicy.lower() == self.DELETE.lower(): return self.DELETE elif self.RetentionPolicy.lower() not in self.retention_policy_options: raise InvalidResourceException(self.logical_id, "'{}' must be one of the following options: {}." .format('RetentionPolicy', [self.RETAIN, self.DELETE]))
python
def _get_retention_policy_value(self): """ Sets the deletion policy on this resource. The default is 'Retain'. :return: value for the DeletionPolicy attribute. """ if self.RetentionPolicy is None or self.RetentionPolicy.lower() == self.RETAIN.lower(): return self.RETAIN elif self.RetentionPolicy.lower() == self.DELETE.lower(): return self.DELETE elif self.RetentionPolicy.lower() not in self.retention_policy_options: raise InvalidResourceException(self.logical_id, "'{}' must be one of the following options: {}." .format('RetentionPolicy', [self.RETAIN, self.DELETE]))
[ "def", "_get_retention_policy_value", "(", "self", ")", ":", "if", "self", ".", "RetentionPolicy", "is", "None", "or", "self", ".", "RetentionPolicy", ".", "lower", "(", ")", "==", "self", ".", "RETAIN", ".", "lower", "(", ")", ":", "return", "self", ".", "RETAIN", "elif", "self", ".", "RetentionPolicy", ".", "lower", "(", ")", "==", "self", ".", "DELETE", ".", "lower", "(", ")", ":", "return", "self", ".", "DELETE", "elif", "self", ".", "RetentionPolicy", ".", "lower", "(", ")", "not", "in", "self", ".", "retention_policy_options", ":", "raise", "InvalidResourceException", "(", "self", ".", "logical_id", ",", "\"'{}' must be one of the following options: {}.\"", ".", "format", "(", "'RetentionPolicy'", ",", "[", "self", ".", "RETAIN", ",", "self", ".", "DELETE", "]", ")", ")" ]
Sets the deletion policy on this resource. The default is 'Retain'. :return: value for the DeletionPolicy attribute.
[ "Sets", "the", "deletion", "policy", "on", "this", "resource", ".", "The", "default", "is", "Retain", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/sam_resources.py#L690-L704
train
awslabs/serverless-application-model
examples/apps/lex-order-flowers-python/lambda_function.py
order_flowers
def order_flowers(intent_request): """ Performs dialog management and fulfillment for ordering flowers. Beyond fulfillment, the implementation of this intent demonstrates the use of the elicitSlot dialog action in slot validation and re-prompting. """ flower_type = get_slots(intent_request)["FlowerType"] date = get_slots(intent_request)["PickupDate"] time = get_slots(intent_request)["PickupTime"] source = intent_request['invocationSource'] if source == 'DialogCodeHook': # Perform basic validation on the supplied input slots. # Use the elicitSlot dialog action to re-prompt for the first violation detected. slots = get_slots(intent_request) validation_result = validate_order_flowers(flower_type, date, time) if not validation_result['isValid']: slots[validation_result['violatedSlot']] = None return elicit_slot(intent_request['sessionAttributes'], intent_request['currentIntent']['name'], slots, validation_result['violatedSlot'], validation_result['message']) # Pass the price of the flowers back through session attributes to be used in various prompts defined # on the bot model. output_session_attributes = intent_request['sessionAttributes'] if flower_type is not None: output_session_attributes['Price'] = len(flower_type) * 5 # Elegant pricing model return delegate(output_session_attributes, get_slots(intent_request)) # Order the flowers, and rely on the goodbye message of the bot to define the message to the end user. # In a real bot, this would likely involve a call to a backend service. return close(intent_request['sessionAttributes'], 'Fulfilled', {'contentType': 'PlainText', 'content': 'Thanks, your order for {} has been placed and will be ready for pickup by {} on {}'.format(flower_type, time, date)})
python
def order_flowers(intent_request): """ Performs dialog management and fulfillment for ordering flowers. Beyond fulfillment, the implementation of this intent demonstrates the use of the elicitSlot dialog action in slot validation and re-prompting. """ flower_type = get_slots(intent_request)["FlowerType"] date = get_slots(intent_request)["PickupDate"] time = get_slots(intent_request)["PickupTime"] source = intent_request['invocationSource'] if source == 'DialogCodeHook': # Perform basic validation on the supplied input slots. # Use the elicitSlot dialog action to re-prompt for the first violation detected. slots = get_slots(intent_request) validation_result = validate_order_flowers(flower_type, date, time) if not validation_result['isValid']: slots[validation_result['violatedSlot']] = None return elicit_slot(intent_request['sessionAttributes'], intent_request['currentIntent']['name'], slots, validation_result['violatedSlot'], validation_result['message']) # Pass the price of the flowers back through session attributes to be used in various prompts defined # on the bot model. output_session_attributes = intent_request['sessionAttributes'] if flower_type is not None: output_session_attributes['Price'] = len(flower_type) * 5 # Elegant pricing model return delegate(output_session_attributes, get_slots(intent_request)) # Order the flowers, and rely on the goodbye message of the bot to define the message to the end user. # In a real bot, this would likely involve a call to a backend service. return close(intent_request['sessionAttributes'], 'Fulfilled', {'contentType': 'PlainText', 'content': 'Thanks, your order for {} has been placed and will be ready for pickup by {} on {}'.format(flower_type, time, date)})
[ "def", "order_flowers", "(", "intent_request", ")", ":", "flower_type", "=", "get_slots", "(", "intent_request", ")", "[", "\"FlowerType\"", "]", "date", "=", "get_slots", "(", "intent_request", ")", "[", "\"PickupDate\"", "]", "time", "=", "get_slots", "(", "intent_request", ")", "[", "\"PickupTime\"", "]", "source", "=", "intent_request", "[", "'invocationSource'", "]", "if", "source", "==", "'DialogCodeHook'", ":", "# Perform basic validation on the supplied input slots.", "# Use the elicitSlot dialog action to re-prompt for the first violation detected.", "slots", "=", "get_slots", "(", "intent_request", ")", "validation_result", "=", "validate_order_flowers", "(", "flower_type", ",", "date", ",", "time", ")", "if", "not", "validation_result", "[", "'isValid'", "]", ":", "slots", "[", "validation_result", "[", "'violatedSlot'", "]", "]", "=", "None", "return", "elicit_slot", "(", "intent_request", "[", "'sessionAttributes'", "]", ",", "intent_request", "[", "'currentIntent'", "]", "[", "'name'", "]", ",", "slots", ",", "validation_result", "[", "'violatedSlot'", "]", ",", "validation_result", "[", "'message'", "]", ")", "# Pass the price of the flowers back through session attributes to be used in various prompts defined", "# on the bot model.", "output_session_attributes", "=", "intent_request", "[", "'sessionAttributes'", "]", "if", "flower_type", "is", "not", "None", ":", "output_session_attributes", "[", "'Price'", "]", "=", "len", "(", "flower_type", ")", "*", "5", "# Elegant pricing model", "return", "delegate", "(", "output_session_attributes", ",", "get_slots", "(", "intent_request", ")", ")", "# Order the flowers, and rely on the goodbye message of the bot to define the message to the end user.", "# In a real bot, this would likely involve a call to a backend service.", "return", "close", "(", "intent_request", "[", "'sessionAttributes'", "]", ",", "'Fulfilled'", ",", "{", "'contentType'", ":", "'PlainText'", ",", "'content'", ":", "'Thanks, your order for {} has been placed and will be ready for pickup by {} on {}'", ".", "format", "(", "flower_type", ",", "time", ",", "date", ")", "}", ")" ]
Performs dialog management and fulfillment for ordering flowers. Beyond fulfillment, the implementation of this intent demonstrates the use of the elicitSlot dialog action in slot validation and re-prompting.
[ "Performs", "dialog", "management", "and", "fulfillment", "for", "ordering", "flowers", ".", "Beyond", "fulfillment", "the", "implementation", "of", "this", "intent", "demonstrates", "the", "use", "of", "the", "elicitSlot", "dialog", "action", "in", "slot", "validation", "and", "re", "-", "prompting", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/examples/apps/lex-order-flowers-python/lambda_function.py#L119-L158
train
awslabs/serverless-application-model
examples/apps/lex-order-flowers-python/lambda_function.py
dispatch
def dispatch(intent_request): """ Called when the user specifies an intent for this bot. """ logger.debug('dispatch userId={}, intentName={}'.format(intent_request['userId'], intent_request['currentIntent']['name'])) intent_name = intent_request['currentIntent']['name'] # Dispatch to your bot's intent handlers if intent_name == 'OrderFlowers': return order_flowers(intent_request) raise Exception('Intent with name ' + intent_name + ' not supported')
python
def dispatch(intent_request): """ Called when the user specifies an intent for this bot. """ logger.debug('dispatch userId={}, intentName={}'.format(intent_request['userId'], intent_request['currentIntent']['name'])) intent_name = intent_request['currentIntent']['name'] # Dispatch to your bot's intent handlers if intent_name == 'OrderFlowers': return order_flowers(intent_request) raise Exception('Intent with name ' + intent_name + ' not supported')
[ "def", "dispatch", "(", "intent_request", ")", ":", "logger", ".", "debug", "(", "'dispatch userId={}, intentName={}'", ".", "format", "(", "intent_request", "[", "'userId'", "]", ",", "intent_request", "[", "'currentIntent'", "]", "[", "'name'", "]", ")", ")", "intent_name", "=", "intent_request", "[", "'currentIntent'", "]", "[", "'name'", "]", "# Dispatch to your bot's intent handlers", "if", "intent_name", "==", "'OrderFlowers'", ":", "return", "order_flowers", "(", "intent_request", ")", "raise", "Exception", "(", "'Intent with name '", "+", "intent_name", "+", "' not supported'", ")" ]
Called when the user specifies an intent for this bot.
[ "Called", "when", "the", "user", "specifies", "an", "intent", "for", "this", "bot", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/examples/apps/lex-order-flowers-python/lambda_function.py#L164-L177
train
awslabs/serverless-application-model
samtranslator/model/eventsources/push.py
PushEventSource._construct_permission
def _construct_permission(self, function, source_arn=None, source_account=None, suffix="", event_source_token=None): """Constructs the Lambda Permission resource allowing the source service to invoke the function this event source triggers. :returns: the permission resource :rtype: model.lambda_.LambdaPermission """ lambda_permission = LambdaPermission(self.logical_id + 'Permission' + suffix, attributes=function.get_passthrough_resource_attributes()) try: # Name will not be available for Alias resources function_name_or_arn = function.get_runtime_attr("name") except NotImplementedError: function_name_or_arn = function.get_runtime_attr("arn") lambda_permission.Action = 'lambda:invokeFunction' lambda_permission.FunctionName = function_name_or_arn lambda_permission.Principal = self.principal lambda_permission.SourceArn = source_arn lambda_permission.SourceAccount = source_account lambda_permission.EventSourceToken = event_source_token return lambda_permission
python
def _construct_permission(self, function, source_arn=None, source_account=None, suffix="", event_source_token=None): """Constructs the Lambda Permission resource allowing the source service to invoke the function this event source triggers. :returns: the permission resource :rtype: model.lambda_.LambdaPermission """ lambda_permission = LambdaPermission(self.logical_id + 'Permission' + suffix, attributes=function.get_passthrough_resource_attributes()) try: # Name will not be available for Alias resources function_name_or_arn = function.get_runtime_attr("name") except NotImplementedError: function_name_or_arn = function.get_runtime_attr("arn") lambda_permission.Action = 'lambda:invokeFunction' lambda_permission.FunctionName = function_name_or_arn lambda_permission.Principal = self.principal lambda_permission.SourceArn = source_arn lambda_permission.SourceAccount = source_account lambda_permission.EventSourceToken = event_source_token return lambda_permission
[ "def", "_construct_permission", "(", "self", ",", "function", ",", "source_arn", "=", "None", ",", "source_account", "=", "None", ",", "suffix", "=", "\"\"", ",", "event_source_token", "=", "None", ")", ":", "lambda_permission", "=", "LambdaPermission", "(", "self", ".", "logical_id", "+", "'Permission'", "+", "suffix", ",", "attributes", "=", "function", ".", "get_passthrough_resource_attributes", "(", ")", ")", "try", ":", "# Name will not be available for Alias resources", "function_name_or_arn", "=", "function", ".", "get_runtime_attr", "(", "\"name\"", ")", "except", "NotImplementedError", ":", "function_name_or_arn", "=", "function", ".", "get_runtime_attr", "(", "\"arn\"", ")", "lambda_permission", ".", "Action", "=", "'lambda:invokeFunction'", "lambda_permission", ".", "FunctionName", "=", "function_name_or_arn", "lambda_permission", ".", "Principal", "=", "self", ".", "principal", "lambda_permission", ".", "SourceArn", "=", "source_arn", "lambda_permission", ".", "SourceAccount", "=", "source_account", "lambda_permission", ".", "EventSourceToken", "=", "event_source_token", "return", "lambda_permission" ]
Constructs the Lambda Permission resource allowing the source service to invoke the function this event source triggers. :returns: the permission resource :rtype: model.lambda_.LambdaPermission
[ "Constructs", "the", "Lambda", "Permission", "resource", "allowing", "the", "source", "service", "to", "invoke", "the", "function", "this", "event", "source", "triggers", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/push.py#L43-L66
train
awslabs/serverless-application-model
samtranslator/model/eventsources/push.py
Schedule.to_cloudformation
def to_cloudformation(self, **kwargs): """Returns the CloudWatch Events Rule and Lambda Permission to which this Schedule event source corresponds. :param dict kwargs: no existing resources need to be modified :returns: a list of vanilla CloudFormation Resources, to which this pull event expands :rtype: list """ function = kwargs.get('function') if not function: raise TypeError("Missing required keyword argument: function") resources = [] events_rule = EventsRule(self.logical_id) resources.append(events_rule) events_rule.ScheduleExpression = self.Schedule events_rule.Targets = [self._construct_target(function)] source_arn = events_rule.get_runtime_attr("arn") if CONDITION in function.resource_attributes: events_rule.set_resource_attribute(CONDITION, function.resource_attributes[CONDITION]) resources.append(self._construct_permission(function, source_arn=source_arn)) return resources
python
def to_cloudformation(self, **kwargs): """Returns the CloudWatch Events Rule and Lambda Permission to which this Schedule event source corresponds. :param dict kwargs: no existing resources need to be modified :returns: a list of vanilla CloudFormation Resources, to which this pull event expands :rtype: list """ function = kwargs.get('function') if not function: raise TypeError("Missing required keyword argument: function") resources = [] events_rule = EventsRule(self.logical_id) resources.append(events_rule) events_rule.ScheduleExpression = self.Schedule events_rule.Targets = [self._construct_target(function)] source_arn = events_rule.get_runtime_attr("arn") if CONDITION in function.resource_attributes: events_rule.set_resource_attribute(CONDITION, function.resource_attributes[CONDITION]) resources.append(self._construct_permission(function, source_arn=source_arn)) return resources
[ "def", "to_cloudformation", "(", "self", ",", "*", "*", "kwargs", ")", ":", "function", "=", "kwargs", ".", "get", "(", "'function'", ")", "if", "not", "function", ":", "raise", "TypeError", "(", "\"Missing required keyword argument: function\"", ")", "resources", "=", "[", "]", "events_rule", "=", "EventsRule", "(", "self", ".", "logical_id", ")", "resources", ".", "append", "(", "events_rule", ")", "events_rule", ".", "ScheduleExpression", "=", "self", ".", "Schedule", "events_rule", ".", "Targets", "=", "[", "self", ".", "_construct_target", "(", "function", ")", "]", "source_arn", "=", "events_rule", ".", "get_runtime_attr", "(", "\"arn\"", ")", "if", "CONDITION", "in", "function", ".", "resource_attributes", ":", "events_rule", ".", "set_resource_attribute", "(", "CONDITION", ",", "function", ".", "resource_attributes", "[", "CONDITION", "]", ")", "resources", ".", "append", "(", "self", ".", "_construct_permission", "(", "function", ",", "source_arn", "=", "source_arn", ")", ")", "return", "resources" ]
Returns the CloudWatch Events Rule and Lambda Permission to which this Schedule event source corresponds. :param dict kwargs: no existing resources need to be modified :returns: a list of vanilla CloudFormation Resources, to which this pull event expands :rtype: list
[ "Returns", "the", "CloudWatch", "Events", "Rule", "and", "Lambda", "Permission", "to", "which", "this", "Schedule", "event", "source", "corresponds", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/push.py#L78-L103
train
awslabs/serverless-application-model
samtranslator/model/eventsources/push.py
CloudWatchEvent._construct_target
def _construct_target(self, function): """Constructs the Target property for the CloudWatch Events Rule. :returns: the Target property :rtype: dict """ target = { 'Arn': function.get_runtime_attr("arn"), 'Id': self.logical_id + 'LambdaTarget' } if self.Input is not None: target['Input'] = self.Input if self.InputPath is not None: target['InputPath'] = self.InputPath return target
python
def _construct_target(self, function): """Constructs the Target property for the CloudWatch Events Rule. :returns: the Target property :rtype: dict """ target = { 'Arn': function.get_runtime_attr("arn"), 'Id': self.logical_id + 'LambdaTarget' } if self.Input is not None: target['Input'] = self.Input if self.InputPath is not None: target['InputPath'] = self.InputPath return target
[ "def", "_construct_target", "(", "self", ",", "function", ")", ":", "target", "=", "{", "'Arn'", ":", "function", ".", "get_runtime_attr", "(", "\"arn\"", ")", ",", "'Id'", ":", "self", ".", "logical_id", "+", "'LambdaTarget'", "}", "if", "self", ".", "Input", "is", "not", "None", ":", "target", "[", "'Input'", "]", "=", "self", ".", "Input", "if", "self", ".", "InputPath", "is", "not", "None", ":", "target", "[", "'InputPath'", "]", "=", "self", ".", "InputPath", "return", "target" ]
Constructs the Target property for the CloudWatch Events Rule. :returns: the Target property :rtype: dict
[ "Constructs", "the", "Target", "property", "for", "the", "CloudWatch", "Events", "Rule", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/push.py#L159-L174
train
awslabs/serverless-application-model
samtranslator/model/eventsources/push.py
S3.to_cloudformation
def to_cloudformation(self, **kwargs): """Returns the Lambda Permission resource allowing S3 to invoke the function this event source triggers. :param dict kwargs: S3 bucket resource :returns: a list of vanilla CloudFormation Resources, to which this S3 event expands :rtype: list """ function = kwargs.get('function') if not function: raise TypeError("Missing required keyword argument: function") if 'bucket' not in kwargs or kwargs['bucket'] is None: raise TypeError("Missing required keyword argument: bucket") if 'bucket_id' not in kwargs or kwargs['bucket_id'] is None: raise TypeError("Missing required keyword argument: bucket_id") bucket = kwargs['bucket'] bucket_id = kwargs['bucket_id'] resources = [] source_account = ref('AWS::AccountId') permission = self._construct_permission(function, source_account=source_account) if CONDITION in permission.resource_attributes: self._depend_on_lambda_permissions_using_tag(bucket, permission) else: self._depend_on_lambda_permissions(bucket, permission) resources.append(permission) # NOTE: `bucket` here is a dictionary representing the S3 Bucket resource in your SAM template. If there are # multiple S3 Events attached to the same bucket, we will update the Bucket resource with notification # configuration for each event. This is the reason why we continue to use existing bucket dict and append onto # it. # # NOTE: There is some fragile logic here where we will append multiple resources to output # SAM template but de-dupe them when merging into output CFN template. This is scary because the order of # merging is literally "last one wins", which works fine because we linearly loop through the template once. # The de-dupe happens inside `samtranslator.translator.Translator.translate` method when merging results of # to_cloudformation() to output template. self._inject_notification_configuration(function, bucket) resources.append(S3Bucket.from_dict(bucket_id, bucket)) return resources
python
def to_cloudformation(self, **kwargs): """Returns the Lambda Permission resource allowing S3 to invoke the function this event source triggers. :param dict kwargs: S3 bucket resource :returns: a list of vanilla CloudFormation Resources, to which this S3 event expands :rtype: list """ function = kwargs.get('function') if not function: raise TypeError("Missing required keyword argument: function") if 'bucket' not in kwargs or kwargs['bucket'] is None: raise TypeError("Missing required keyword argument: bucket") if 'bucket_id' not in kwargs or kwargs['bucket_id'] is None: raise TypeError("Missing required keyword argument: bucket_id") bucket = kwargs['bucket'] bucket_id = kwargs['bucket_id'] resources = [] source_account = ref('AWS::AccountId') permission = self._construct_permission(function, source_account=source_account) if CONDITION in permission.resource_attributes: self._depend_on_lambda_permissions_using_tag(bucket, permission) else: self._depend_on_lambda_permissions(bucket, permission) resources.append(permission) # NOTE: `bucket` here is a dictionary representing the S3 Bucket resource in your SAM template. If there are # multiple S3 Events attached to the same bucket, we will update the Bucket resource with notification # configuration for each event. This is the reason why we continue to use existing bucket dict and append onto # it. # # NOTE: There is some fragile logic here where we will append multiple resources to output # SAM template but de-dupe them when merging into output CFN template. This is scary because the order of # merging is literally "last one wins", which works fine because we linearly loop through the template once. # The de-dupe happens inside `samtranslator.translator.Translator.translate` method when merging results of # to_cloudformation() to output template. self._inject_notification_configuration(function, bucket) resources.append(S3Bucket.from_dict(bucket_id, bucket)) return resources
[ "def", "to_cloudformation", "(", "self", ",", "*", "*", "kwargs", ")", ":", "function", "=", "kwargs", ".", "get", "(", "'function'", ")", "if", "not", "function", ":", "raise", "TypeError", "(", "\"Missing required keyword argument: function\"", ")", "if", "'bucket'", "not", "in", "kwargs", "or", "kwargs", "[", "'bucket'", "]", "is", "None", ":", "raise", "TypeError", "(", "\"Missing required keyword argument: bucket\"", ")", "if", "'bucket_id'", "not", "in", "kwargs", "or", "kwargs", "[", "'bucket_id'", "]", "is", "None", ":", "raise", "TypeError", "(", "\"Missing required keyword argument: bucket_id\"", ")", "bucket", "=", "kwargs", "[", "'bucket'", "]", "bucket_id", "=", "kwargs", "[", "'bucket_id'", "]", "resources", "=", "[", "]", "source_account", "=", "ref", "(", "'AWS::AccountId'", ")", "permission", "=", "self", ".", "_construct_permission", "(", "function", ",", "source_account", "=", "source_account", ")", "if", "CONDITION", "in", "permission", ".", "resource_attributes", ":", "self", ".", "_depend_on_lambda_permissions_using_tag", "(", "bucket", ",", "permission", ")", "else", ":", "self", ".", "_depend_on_lambda_permissions", "(", "bucket", ",", "permission", ")", "resources", ".", "append", "(", "permission", ")", "# NOTE: `bucket` here is a dictionary representing the S3 Bucket resource in your SAM template. If there are", "# multiple S3 Events attached to the same bucket, we will update the Bucket resource with notification", "# configuration for each event. This is the reason why we continue to use existing bucket dict and append onto", "# it.", "#", "# NOTE: There is some fragile logic here where we will append multiple resources to output", "# SAM template but de-dupe them when merging into output CFN template. This is scary because the order of", "# merging is literally \"last one wins\", which works fine because we linearly loop through the template once.", "# The de-dupe happens inside `samtranslator.translator.Translator.translate` method when merging results of", "# to_cloudformation() to output template.", "self", ".", "_inject_notification_configuration", "(", "function", ",", "bucket", ")", "resources", ".", "append", "(", "S3Bucket", ".", "from_dict", "(", "bucket_id", ",", "bucket", ")", ")", "return", "resources" ]
Returns the Lambda Permission resource allowing S3 to invoke the function this event source triggers. :param dict kwargs: S3 bucket resource :returns: a list of vanilla CloudFormation Resources, to which this S3 event expands :rtype: list
[ "Returns", "the", "Lambda", "Permission", "resource", "allowing", "S3", "to", "invoke", "the", "function", "this", "event", "source", "triggers", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/push.py#L197-L241
train
awslabs/serverless-application-model
samtranslator/model/eventsources/push.py
S3._depend_on_lambda_permissions
def _depend_on_lambda_permissions(self, bucket, permission): """ Make the S3 bucket depends on Lambda Permissions resource because when S3 adds a Notification Configuration, it will check whether it has permissions to access Lambda. This will fail if the Lambda::Permissions is not already applied for this bucket to invoke the Lambda. :param dict bucket: Dictionary representing the bucket in SAM template. This is a raw dictionary and not a "resource" object :param model.lambda_.lambda_permission permission: Lambda Permission resource that needs to be created before the bucket. :return: Modified Bucket dictionary """ depends_on = bucket.get("DependsOn", []) # DependsOn can be either a list of strings or a scalar string if isinstance(depends_on, string_types): depends_on = [depends_on] depends_on_set = set(depends_on) depends_on_set.add(permission.logical_id) bucket["DependsOn"] = list(depends_on_set) return bucket
python
def _depend_on_lambda_permissions(self, bucket, permission): """ Make the S3 bucket depends on Lambda Permissions resource because when S3 adds a Notification Configuration, it will check whether it has permissions to access Lambda. This will fail if the Lambda::Permissions is not already applied for this bucket to invoke the Lambda. :param dict bucket: Dictionary representing the bucket in SAM template. This is a raw dictionary and not a "resource" object :param model.lambda_.lambda_permission permission: Lambda Permission resource that needs to be created before the bucket. :return: Modified Bucket dictionary """ depends_on = bucket.get("DependsOn", []) # DependsOn can be either a list of strings or a scalar string if isinstance(depends_on, string_types): depends_on = [depends_on] depends_on_set = set(depends_on) depends_on_set.add(permission.logical_id) bucket["DependsOn"] = list(depends_on_set) return bucket
[ "def", "_depend_on_lambda_permissions", "(", "self", ",", "bucket", ",", "permission", ")", ":", "depends_on", "=", "bucket", ".", "get", "(", "\"DependsOn\"", ",", "[", "]", ")", "# DependsOn can be either a list of strings or a scalar string", "if", "isinstance", "(", "depends_on", ",", "string_types", ")", ":", "depends_on", "=", "[", "depends_on", "]", "depends_on_set", "=", "set", "(", "depends_on", ")", "depends_on_set", ".", "add", "(", "permission", ".", "logical_id", ")", "bucket", "[", "\"DependsOn\"", "]", "=", "list", "(", "depends_on_set", ")", "return", "bucket" ]
Make the S3 bucket depends on Lambda Permissions resource because when S3 adds a Notification Configuration, it will check whether it has permissions to access Lambda. This will fail if the Lambda::Permissions is not already applied for this bucket to invoke the Lambda. :param dict bucket: Dictionary representing the bucket in SAM template. This is a raw dictionary and not a "resource" object :param model.lambda_.lambda_permission permission: Lambda Permission resource that needs to be created before the bucket. :return: Modified Bucket dictionary
[ "Make", "the", "S3", "bucket", "depends", "on", "Lambda", "Permissions", "resource", "because", "when", "S3", "adds", "a", "Notification", "Configuration", "it", "will", "check", "whether", "it", "has", "permissions", "to", "access", "Lambda", ".", "This", "will", "fail", "if", "the", "Lambda", "::", "Permissions", "is", "not", "already", "applied", "for", "this", "bucket", "to", "invoke", "the", "Lambda", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/push.py#L243-L266
train
awslabs/serverless-application-model
samtranslator/model/eventsources/push.py
S3._depend_on_lambda_permissions_using_tag
def _depend_on_lambda_permissions_using_tag(self, bucket, permission): """ Since conditional DependsOn is not supported this undocumented way of implicitely making dependency through tags is used. See https://stackoverflow.com/questions/34607476/cloudformation-apply-condition-on-dependson It is done by using Ref wrapped in a conditional Fn::If. Using Ref implies a dependency, so CloudFormation will automatically wait once it reaches that function, the same as if you were using a DependsOn. """ properties = bucket.get('Properties', None) if properties is None: properties = {} bucket['Properties'] = properties tags = properties.get('Tags', None) if tags is None: tags = [] properties['Tags'] = tags dep_tag = { 'sam:ConditionalDependsOn:' + permission.logical_id: { 'Fn::If': [ permission.resource_attributes[CONDITION], ref(permission.logical_id), 'no dependency' ] } } properties['Tags'] = tags + get_tag_list(dep_tag) return bucket
python
def _depend_on_lambda_permissions_using_tag(self, bucket, permission): """ Since conditional DependsOn is not supported this undocumented way of implicitely making dependency through tags is used. See https://stackoverflow.com/questions/34607476/cloudformation-apply-condition-on-dependson It is done by using Ref wrapped in a conditional Fn::If. Using Ref implies a dependency, so CloudFormation will automatically wait once it reaches that function, the same as if you were using a DependsOn. """ properties = bucket.get('Properties', None) if properties is None: properties = {} bucket['Properties'] = properties tags = properties.get('Tags', None) if tags is None: tags = [] properties['Tags'] = tags dep_tag = { 'sam:ConditionalDependsOn:' + permission.logical_id: { 'Fn::If': [ permission.resource_attributes[CONDITION], ref(permission.logical_id), 'no dependency' ] } } properties['Tags'] = tags + get_tag_list(dep_tag) return bucket
[ "def", "_depend_on_lambda_permissions_using_tag", "(", "self", ",", "bucket", ",", "permission", ")", ":", "properties", "=", "bucket", ".", "get", "(", "'Properties'", ",", "None", ")", "if", "properties", "is", "None", ":", "properties", "=", "{", "}", "bucket", "[", "'Properties'", "]", "=", "properties", "tags", "=", "properties", ".", "get", "(", "'Tags'", ",", "None", ")", "if", "tags", "is", "None", ":", "tags", "=", "[", "]", "properties", "[", "'Tags'", "]", "=", "tags", "dep_tag", "=", "{", "'sam:ConditionalDependsOn:'", "+", "permission", ".", "logical_id", ":", "{", "'Fn::If'", ":", "[", "permission", ".", "resource_attributes", "[", "CONDITION", "]", ",", "ref", "(", "permission", ".", "logical_id", ")", ",", "'no dependency'", "]", "}", "}", "properties", "[", "'Tags'", "]", "=", "tags", "+", "get_tag_list", "(", "dep_tag", ")", "return", "bucket" ]
Since conditional DependsOn is not supported this undocumented way of implicitely making dependency through tags is used. See https://stackoverflow.com/questions/34607476/cloudformation-apply-condition-on-dependson It is done by using Ref wrapped in a conditional Fn::If. Using Ref implies a dependency, so CloudFormation will automatically wait once it reaches that function, the same as if you were using a DependsOn.
[ "Since", "conditional", "DependsOn", "is", "not", "supported", "this", "undocumented", "way", "of", "implicitely", "making", "dependency", "through", "tags", "is", "used", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/push.py#L268-L297
train
awslabs/serverless-application-model
samtranslator/model/eventsources/push.py
SNS.to_cloudformation
def to_cloudformation(self, **kwargs): """Returns the Lambda Permission resource allowing SNS to invoke the function this event source triggers. :param dict kwargs: no existing resources need to be modified :returns: a list of vanilla CloudFormation Resources, to which this SNS event expands :rtype: list """ function = kwargs.get('function') if not function: raise TypeError("Missing required keyword argument: function") return [self._construct_permission(function, source_arn=self.Topic), self._inject_subscription(function, self.Topic, self.FilterPolicy)]
python
def to_cloudformation(self, **kwargs): """Returns the Lambda Permission resource allowing SNS to invoke the function this event source triggers. :param dict kwargs: no existing resources need to be modified :returns: a list of vanilla CloudFormation Resources, to which this SNS event expands :rtype: list """ function = kwargs.get('function') if not function: raise TypeError("Missing required keyword argument: function") return [self._construct_permission(function, source_arn=self.Topic), self._inject_subscription(function, self.Topic, self.FilterPolicy)]
[ "def", "to_cloudformation", "(", "self", ",", "*", "*", "kwargs", ")", ":", "function", "=", "kwargs", ".", "get", "(", "'function'", ")", "if", "not", "function", ":", "raise", "TypeError", "(", "\"Missing required keyword argument: function\"", ")", "return", "[", "self", ".", "_construct_permission", "(", "function", ",", "source_arn", "=", "self", ".", "Topic", ")", ",", "self", ".", "_inject_subscription", "(", "function", ",", "self", ".", "Topic", ",", "self", ".", "FilterPolicy", ")", "]" ]
Returns the Lambda Permission resource allowing SNS to invoke the function this event source triggers. :param dict kwargs: no existing resources need to be modified :returns: a list of vanilla CloudFormation Resources, to which this SNS event expands :rtype: list
[ "Returns", "the", "Lambda", "Permission", "resource", "allowing", "SNS", "to", "invoke", "the", "function", "this", "event", "source", "triggers", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/push.py#L350-L363
train
awslabs/serverless-application-model
samtranslator/model/eventsources/push.py
Api.resources_to_link
def resources_to_link(self, resources): """ If this API Event Source refers to an explicit API resource, resolve the reference and grab necessary data from the explicit API """ rest_api_id = self.RestApiId if isinstance(rest_api_id, dict) and "Ref" in rest_api_id: rest_api_id = rest_api_id["Ref"] # If RestApiId is a resource in the same template, then we try find the StageName by following the reference # Otherwise we default to a wildcard. This stage name is solely used to construct the permission to # allow this stage to invoke the Lambda function. If we are unable to resolve the stage name, we will # simply permit all stages to invoke this Lambda function # This hack is necessary because customers could use !ImportValue, !Ref or other intrinsic functions which # can be sometimes impossible to resolve (ie. when it has cross-stack references) permitted_stage = "*" stage_suffix = "AllStages" explicit_api = None if isinstance(rest_api_id, string_types): if rest_api_id in resources \ and "Properties" in resources[rest_api_id] \ and "StageName" in resources[rest_api_id]["Properties"]: explicit_api = resources[rest_api_id]["Properties"] permitted_stage = explicit_api["StageName"] # Stage could be a intrinsic, in which case leave the suffix to default value if isinstance(permitted_stage, string_types): if not permitted_stage: raise InvalidResourceException(rest_api_id, 'StageName cannot be empty.') stage_suffix = permitted_stage else: stage_suffix = "Stage" else: # RestApiId is a string, not an intrinsic, but we did not find a valid API resource for this ID raise InvalidEventException(self.relative_id, "RestApiId property of Api event must reference a valid " "resource in the same template.") return { 'explicit_api': explicit_api, 'explicit_api_stage': { 'permitted_stage': permitted_stage, 'suffix': stage_suffix } }
python
def resources_to_link(self, resources): """ If this API Event Source refers to an explicit API resource, resolve the reference and grab necessary data from the explicit API """ rest_api_id = self.RestApiId if isinstance(rest_api_id, dict) and "Ref" in rest_api_id: rest_api_id = rest_api_id["Ref"] # If RestApiId is a resource in the same template, then we try find the StageName by following the reference # Otherwise we default to a wildcard. This stage name is solely used to construct the permission to # allow this stage to invoke the Lambda function. If we are unable to resolve the stage name, we will # simply permit all stages to invoke this Lambda function # This hack is necessary because customers could use !ImportValue, !Ref or other intrinsic functions which # can be sometimes impossible to resolve (ie. when it has cross-stack references) permitted_stage = "*" stage_suffix = "AllStages" explicit_api = None if isinstance(rest_api_id, string_types): if rest_api_id in resources \ and "Properties" in resources[rest_api_id] \ and "StageName" in resources[rest_api_id]["Properties"]: explicit_api = resources[rest_api_id]["Properties"] permitted_stage = explicit_api["StageName"] # Stage could be a intrinsic, in which case leave the suffix to default value if isinstance(permitted_stage, string_types): if not permitted_stage: raise InvalidResourceException(rest_api_id, 'StageName cannot be empty.') stage_suffix = permitted_stage else: stage_suffix = "Stage" else: # RestApiId is a string, not an intrinsic, but we did not find a valid API resource for this ID raise InvalidEventException(self.relative_id, "RestApiId property of Api event must reference a valid " "resource in the same template.") return { 'explicit_api': explicit_api, 'explicit_api_stage': { 'permitted_stage': permitted_stage, 'suffix': stage_suffix } }
[ "def", "resources_to_link", "(", "self", ",", "resources", ")", ":", "rest_api_id", "=", "self", ".", "RestApiId", "if", "isinstance", "(", "rest_api_id", ",", "dict", ")", "and", "\"Ref\"", "in", "rest_api_id", ":", "rest_api_id", "=", "rest_api_id", "[", "\"Ref\"", "]", "# If RestApiId is a resource in the same template, then we try find the StageName by following the reference", "# Otherwise we default to a wildcard. This stage name is solely used to construct the permission to", "# allow this stage to invoke the Lambda function. If we are unable to resolve the stage name, we will", "# simply permit all stages to invoke this Lambda function", "# This hack is necessary because customers could use !ImportValue, !Ref or other intrinsic functions which", "# can be sometimes impossible to resolve (ie. when it has cross-stack references)", "permitted_stage", "=", "\"*\"", "stage_suffix", "=", "\"AllStages\"", "explicit_api", "=", "None", "if", "isinstance", "(", "rest_api_id", ",", "string_types", ")", ":", "if", "rest_api_id", "in", "resources", "and", "\"Properties\"", "in", "resources", "[", "rest_api_id", "]", "and", "\"StageName\"", "in", "resources", "[", "rest_api_id", "]", "[", "\"Properties\"", "]", ":", "explicit_api", "=", "resources", "[", "rest_api_id", "]", "[", "\"Properties\"", "]", "permitted_stage", "=", "explicit_api", "[", "\"StageName\"", "]", "# Stage could be a intrinsic, in which case leave the suffix to default value", "if", "isinstance", "(", "permitted_stage", ",", "string_types", ")", ":", "if", "not", "permitted_stage", ":", "raise", "InvalidResourceException", "(", "rest_api_id", ",", "'StageName cannot be empty.'", ")", "stage_suffix", "=", "permitted_stage", "else", ":", "stage_suffix", "=", "\"Stage\"", "else", ":", "# RestApiId is a string, not an intrinsic, but we did not find a valid API resource for this ID", "raise", "InvalidEventException", "(", "self", ".", "relative_id", ",", "\"RestApiId property of Api event must reference a valid \"", "\"resource in the same template.\"", ")", "return", "{", "'explicit_api'", ":", "explicit_api", ",", "'explicit_api_stage'", ":", "{", "'permitted_stage'", ":", "permitted_stage", ",", "'suffix'", ":", "stage_suffix", "}", "}" ]
If this API Event Source refers to an explicit API resource, resolve the reference and grab necessary data from the explicit API
[ "If", "this", "API", "Event", "Source", "refers", "to", "an", "explicit", "API", "resource", "resolve", "the", "reference", "and", "grab", "necessary", "data", "from", "the", "explicit", "API" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/push.py#L392-L439
train
awslabs/serverless-application-model
samtranslator/model/eventsources/push.py
Api.to_cloudformation
def to_cloudformation(self, **kwargs): """If the Api event source has a RestApi property, then simply return the Lambda Permission resource allowing API Gateway to call the function. If no RestApi is provided, then additionally inject the path, method, and the x-amazon-apigateway-integration into the Swagger body for a provided implicit API. :param dict kwargs: a dict containing the implicit RestApi to be modified, should no explicit RestApi \ be provided. :returns: a list of vanilla CloudFormation Resources, to which this Api event expands :rtype: list """ resources = [] function = kwargs.get('function') if not function: raise TypeError("Missing required keyword argument: function") if self.Method is not None: # Convert to lower case so that user can specify either GET or get self.Method = self.Method.lower() resources.extend(self._get_permissions(kwargs)) explicit_api = kwargs['explicit_api'] if explicit_api.get("__MANAGE_SWAGGER"): self._add_swagger_integration(explicit_api, function) return resources
python
def to_cloudformation(self, **kwargs): """If the Api event source has a RestApi property, then simply return the Lambda Permission resource allowing API Gateway to call the function. If no RestApi is provided, then additionally inject the path, method, and the x-amazon-apigateway-integration into the Swagger body for a provided implicit API. :param dict kwargs: a dict containing the implicit RestApi to be modified, should no explicit RestApi \ be provided. :returns: a list of vanilla CloudFormation Resources, to which this Api event expands :rtype: list """ resources = [] function = kwargs.get('function') if not function: raise TypeError("Missing required keyword argument: function") if self.Method is not None: # Convert to lower case so that user can specify either GET or get self.Method = self.Method.lower() resources.extend(self._get_permissions(kwargs)) explicit_api = kwargs['explicit_api'] if explicit_api.get("__MANAGE_SWAGGER"): self._add_swagger_integration(explicit_api, function) return resources
[ "def", "to_cloudformation", "(", "self", ",", "*", "*", "kwargs", ")", ":", "resources", "=", "[", "]", "function", "=", "kwargs", ".", "get", "(", "'function'", ")", "if", "not", "function", ":", "raise", "TypeError", "(", "\"Missing required keyword argument: function\"", ")", "if", "self", ".", "Method", "is", "not", "None", ":", "# Convert to lower case so that user can specify either GET or get", "self", ".", "Method", "=", "self", ".", "Method", ".", "lower", "(", ")", "resources", ".", "extend", "(", "self", ".", "_get_permissions", "(", "kwargs", ")", ")", "explicit_api", "=", "kwargs", "[", "'explicit_api'", "]", "if", "explicit_api", ".", "get", "(", "\"__MANAGE_SWAGGER\"", ")", ":", "self", ".", "_add_swagger_integration", "(", "explicit_api", ",", "function", ")", "return", "resources" ]
If the Api event source has a RestApi property, then simply return the Lambda Permission resource allowing API Gateway to call the function. If no RestApi is provided, then additionally inject the path, method, and the x-amazon-apigateway-integration into the Swagger body for a provided implicit API. :param dict kwargs: a dict containing the implicit RestApi to be modified, should no explicit RestApi \ be provided. :returns: a list of vanilla CloudFormation Resources, to which this Api event expands :rtype: list
[ "If", "the", "Api", "event", "source", "has", "a", "RestApi", "property", "then", "simply", "return", "the", "Lambda", "Permission", "resource", "allowing", "API", "Gateway", "to", "call", "the", "function", ".", "If", "no", "RestApi", "is", "provided", "then", "additionally", "inject", "the", "path", "method", "and", "the", "x", "-", "amazon", "-", "apigateway", "-", "integration", "into", "the", "Swagger", "body", "for", "a", "provided", "implicit", "API", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/push.py#L441-L468
train
awslabs/serverless-application-model
samtranslator/model/eventsources/push.py
Api._add_swagger_integration
def _add_swagger_integration(self, api, function): """Adds the path and method for this Api event source to the Swagger body for the provided RestApi. :param model.apigateway.ApiGatewayRestApi rest_api: the RestApi to which the path and method should be added. """ swagger_body = api.get("DefinitionBody") if swagger_body is None: return function_arn = function.get_runtime_attr('arn') partition = ArnGenerator.get_partition_name() uri = fnSub('arn:' + partition + ':apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/' + make_shorthand(function_arn) + '/invocations') editor = SwaggerEditor(swagger_body) if editor.has_integration(self.Path, self.Method): # Cannot add the Lambda Integration, if it is already present raise InvalidEventException( self.relative_id, 'API method "{method}" defined multiple times for path "{path}".'.format( method=self.Method, path=self.Path)) condition = None if CONDITION in function.resource_attributes: condition = function.resource_attributes[CONDITION] editor.add_lambda_integration(self.Path, self.Method, uri, self.Auth, api.get('Auth'), condition=condition) if self.Auth: method_authorizer = self.Auth.get('Authorizer') if method_authorizer: api_auth = api.get('Auth') api_authorizers = api_auth and api_auth.get('Authorizers') if method_authorizer != 'AWS_IAM': if not api_authorizers: raise InvalidEventException( self.relative_id, 'Unable to set Authorizer [{authorizer}] on API method [{method}] for path [{path}] ' 'because the related API does not define any Authorizers.'.format( authorizer=method_authorizer, method=self.Method, path=self.Path)) if method_authorizer != 'NONE' and not api_authorizers.get(method_authorizer): raise InvalidEventException( self.relative_id, 'Unable to set Authorizer [{authorizer}] on API method [{method}] for path [{path}] ' 'because it wasn\'t defined in the API\'s Authorizers.'.format( authorizer=method_authorizer, method=self.Method, path=self.Path)) if method_authorizer == 'NONE' and not api_auth.get('DefaultAuthorizer'): raise InvalidEventException( self.relative_id, 'Unable to set Authorizer on API method [{method}] for path [{path}] because \'NONE\' ' 'is only a valid value when a DefaultAuthorizer on the API is specified.'.format( method=self.Method, path=self.Path)) editor.add_auth_to_method(api=api, path=self.Path, method_name=self.Method, auth=self.Auth) api["DefinitionBody"] = editor.swagger
python
def _add_swagger_integration(self, api, function): """Adds the path and method for this Api event source to the Swagger body for the provided RestApi. :param model.apigateway.ApiGatewayRestApi rest_api: the RestApi to which the path and method should be added. """ swagger_body = api.get("DefinitionBody") if swagger_body is None: return function_arn = function.get_runtime_attr('arn') partition = ArnGenerator.get_partition_name() uri = fnSub('arn:' + partition + ':apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/' + make_shorthand(function_arn) + '/invocations') editor = SwaggerEditor(swagger_body) if editor.has_integration(self.Path, self.Method): # Cannot add the Lambda Integration, if it is already present raise InvalidEventException( self.relative_id, 'API method "{method}" defined multiple times for path "{path}".'.format( method=self.Method, path=self.Path)) condition = None if CONDITION in function.resource_attributes: condition = function.resource_attributes[CONDITION] editor.add_lambda_integration(self.Path, self.Method, uri, self.Auth, api.get('Auth'), condition=condition) if self.Auth: method_authorizer = self.Auth.get('Authorizer') if method_authorizer: api_auth = api.get('Auth') api_authorizers = api_auth and api_auth.get('Authorizers') if method_authorizer != 'AWS_IAM': if not api_authorizers: raise InvalidEventException( self.relative_id, 'Unable to set Authorizer [{authorizer}] on API method [{method}] for path [{path}] ' 'because the related API does not define any Authorizers.'.format( authorizer=method_authorizer, method=self.Method, path=self.Path)) if method_authorizer != 'NONE' and not api_authorizers.get(method_authorizer): raise InvalidEventException( self.relative_id, 'Unable to set Authorizer [{authorizer}] on API method [{method}] for path [{path}] ' 'because it wasn\'t defined in the API\'s Authorizers.'.format( authorizer=method_authorizer, method=self.Method, path=self.Path)) if method_authorizer == 'NONE' and not api_auth.get('DefaultAuthorizer'): raise InvalidEventException( self.relative_id, 'Unable to set Authorizer on API method [{method}] for path [{path}] because \'NONE\' ' 'is only a valid value when a DefaultAuthorizer on the API is specified.'.format( method=self.Method, path=self.Path)) editor.add_auth_to_method(api=api, path=self.Path, method_name=self.Method, auth=self.Auth) api["DefinitionBody"] = editor.swagger
[ "def", "_add_swagger_integration", "(", "self", ",", "api", ",", "function", ")", ":", "swagger_body", "=", "api", ".", "get", "(", "\"DefinitionBody\"", ")", "if", "swagger_body", "is", "None", ":", "return", "function_arn", "=", "function", ".", "get_runtime_attr", "(", "'arn'", ")", "partition", "=", "ArnGenerator", ".", "get_partition_name", "(", ")", "uri", "=", "fnSub", "(", "'arn:'", "+", "partition", "+", "':apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/'", "+", "make_shorthand", "(", "function_arn", ")", "+", "'/invocations'", ")", "editor", "=", "SwaggerEditor", "(", "swagger_body", ")", "if", "editor", ".", "has_integration", "(", "self", ".", "Path", ",", "self", ".", "Method", ")", ":", "# Cannot add the Lambda Integration, if it is already present", "raise", "InvalidEventException", "(", "self", ".", "relative_id", ",", "'API method \"{method}\" defined multiple times for path \"{path}\".'", ".", "format", "(", "method", "=", "self", ".", "Method", ",", "path", "=", "self", ".", "Path", ")", ")", "condition", "=", "None", "if", "CONDITION", "in", "function", ".", "resource_attributes", ":", "condition", "=", "function", ".", "resource_attributes", "[", "CONDITION", "]", "editor", ".", "add_lambda_integration", "(", "self", ".", "Path", ",", "self", ".", "Method", ",", "uri", ",", "self", ".", "Auth", ",", "api", ".", "get", "(", "'Auth'", ")", ",", "condition", "=", "condition", ")", "if", "self", ".", "Auth", ":", "method_authorizer", "=", "self", ".", "Auth", ".", "get", "(", "'Authorizer'", ")", "if", "method_authorizer", ":", "api_auth", "=", "api", ".", "get", "(", "'Auth'", ")", "api_authorizers", "=", "api_auth", "and", "api_auth", ".", "get", "(", "'Authorizers'", ")", "if", "method_authorizer", "!=", "'AWS_IAM'", ":", "if", "not", "api_authorizers", ":", "raise", "InvalidEventException", "(", "self", ".", "relative_id", ",", "'Unable to set Authorizer [{authorizer}] on API method [{method}] for path [{path}] '", "'because the related API does not define any Authorizers.'", ".", "format", "(", "authorizer", "=", "method_authorizer", ",", "method", "=", "self", ".", "Method", ",", "path", "=", "self", ".", "Path", ")", ")", "if", "method_authorizer", "!=", "'NONE'", "and", "not", "api_authorizers", ".", "get", "(", "method_authorizer", ")", ":", "raise", "InvalidEventException", "(", "self", ".", "relative_id", ",", "'Unable to set Authorizer [{authorizer}] on API method [{method}] for path [{path}] '", "'because it wasn\\'t defined in the API\\'s Authorizers.'", ".", "format", "(", "authorizer", "=", "method_authorizer", ",", "method", "=", "self", ".", "Method", ",", "path", "=", "self", ".", "Path", ")", ")", "if", "method_authorizer", "==", "'NONE'", "and", "not", "api_auth", ".", "get", "(", "'DefaultAuthorizer'", ")", ":", "raise", "InvalidEventException", "(", "self", ".", "relative_id", ",", "'Unable to set Authorizer on API method [{method}] for path [{path}] because \\'NONE\\' '", "'is only a valid value when a DefaultAuthorizer on the API is specified.'", ".", "format", "(", "method", "=", "self", ".", "Method", ",", "path", "=", "self", ".", "Path", ")", ")", "editor", ".", "add_auth_to_method", "(", "api", "=", "api", ",", "path", "=", "self", ".", "Path", ",", "method_name", "=", "self", ".", "Method", ",", "auth", "=", "self", ".", "Auth", ")", "api", "[", "\"DefinitionBody\"", "]", "=", "editor", ".", "swagger" ]
Adds the path and method for this Api event source to the Swagger body for the provided RestApi. :param model.apigateway.ApiGatewayRestApi rest_api: the RestApi to which the path and method should be added.
[ "Adds", "the", "path", "and", "method", "for", "this", "Api", "event", "source", "to", "the", "Swagger", "body", "for", "the", "provided", "RestApi", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/push.py#L507-L567
train
awslabs/serverless-application-model
samtranslator/intrinsics/resolver.py
IntrinsicsResolver.resolve_parameter_refs
def resolve_parameter_refs(self, input): """ Resolves references to parameters within the given dictionary recursively. Other intrinsic functions such as !GetAtt, !Sub or !Ref to non-parameters will be left untouched. Result is a dictionary where parameter values are inlined. Don't pass this dictionary directly into transform's output because it changes the template structure by inlining parameter values. :param input: Any primitive type (dict, array, string etc) whose values might contain intrinsic functions :return: A copy of a dictionary with parameter references replaced by actual value. """ return self._traverse(input, self.parameters, self._try_resolve_parameter_refs)
python
def resolve_parameter_refs(self, input): """ Resolves references to parameters within the given dictionary recursively. Other intrinsic functions such as !GetAtt, !Sub or !Ref to non-parameters will be left untouched. Result is a dictionary where parameter values are inlined. Don't pass this dictionary directly into transform's output because it changes the template structure by inlining parameter values. :param input: Any primitive type (dict, array, string etc) whose values might contain intrinsic functions :return: A copy of a dictionary with parameter references replaced by actual value. """ return self._traverse(input, self.parameters, self._try_resolve_parameter_refs)
[ "def", "resolve_parameter_refs", "(", "self", ",", "input", ")", ":", "return", "self", ".", "_traverse", "(", "input", ",", "self", ".", "parameters", ",", "self", ".", "_try_resolve_parameter_refs", ")" ]
Resolves references to parameters within the given dictionary recursively. Other intrinsic functions such as !GetAtt, !Sub or !Ref to non-parameters will be left untouched. Result is a dictionary where parameter values are inlined. Don't pass this dictionary directly into transform's output because it changes the template structure by inlining parameter values. :param input: Any primitive type (dict, array, string etc) whose values might contain intrinsic functions :return: A copy of a dictionary with parameter references replaced by actual value.
[ "Resolves", "references", "to", "parameters", "within", "the", "given", "dictionary", "recursively", ".", "Other", "intrinsic", "functions", "such", "as", "!GetAtt", "!Sub", "or", "!Ref", "to", "non", "-", "parameters", "will", "be", "left", "untouched", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/resolver.py#L30-L41
train
awslabs/serverless-application-model
samtranslator/intrinsics/resolver.py
IntrinsicsResolver.resolve_sam_resource_refs
def resolve_sam_resource_refs(self, input, supported_resource_refs): """ Customers can provide a reference to a "derived" SAM resource such as Alias of a Function or Stage of an API resource. This method recursively walks the tree, converting all derived references to the real resource name, if it is present. Example: {"Ref": "MyFunction.Alias"} -> {"Ref": "MyFunctionAliasLive"} This method does not attempt to validate a reference. If it is invalid or non-resolvable, it skips the occurrence and continues with the rest. It is recommended that you have an external process that detects and surfaces invalid references. For first call, it is recommended that `template` is the entire CFN template in order to handle references in Mapping or Output sections. :param dict input: CFN template that needs resolution. This method will modify the input directly resolving references. In subsequent recursions, this will be a fragment of the CFN template. :param SupportedResourceReferences supported_resource_refs: Object that contains information about the resource references supported in this SAM template, along with the value they should resolve to. :return list errors: List of dictionary containing information about invalid reference. Empty list otherwise """ return self._traverse(input, supported_resource_refs, self._try_resolve_sam_resource_refs)
python
def resolve_sam_resource_refs(self, input, supported_resource_refs): """ Customers can provide a reference to a "derived" SAM resource such as Alias of a Function or Stage of an API resource. This method recursively walks the tree, converting all derived references to the real resource name, if it is present. Example: {"Ref": "MyFunction.Alias"} -> {"Ref": "MyFunctionAliasLive"} This method does not attempt to validate a reference. If it is invalid or non-resolvable, it skips the occurrence and continues with the rest. It is recommended that you have an external process that detects and surfaces invalid references. For first call, it is recommended that `template` is the entire CFN template in order to handle references in Mapping or Output sections. :param dict input: CFN template that needs resolution. This method will modify the input directly resolving references. In subsequent recursions, this will be a fragment of the CFN template. :param SupportedResourceReferences supported_resource_refs: Object that contains information about the resource references supported in this SAM template, along with the value they should resolve to. :return list errors: List of dictionary containing information about invalid reference. Empty list otherwise """ return self._traverse(input, supported_resource_refs, self._try_resolve_sam_resource_refs)
[ "def", "resolve_sam_resource_refs", "(", "self", ",", "input", ",", "supported_resource_refs", ")", ":", "return", "self", ".", "_traverse", "(", "input", ",", "supported_resource_refs", ",", "self", ".", "_try_resolve_sam_resource_refs", ")" ]
Customers can provide a reference to a "derived" SAM resource such as Alias of a Function or Stage of an API resource. This method recursively walks the tree, converting all derived references to the real resource name, if it is present. Example: {"Ref": "MyFunction.Alias"} -> {"Ref": "MyFunctionAliasLive"} This method does not attempt to validate a reference. If it is invalid or non-resolvable, it skips the occurrence and continues with the rest. It is recommended that you have an external process that detects and surfaces invalid references. For first call, it is recommended that `template` is the entire CFN template in order to handle references in Mapping or Output sections. :param dict input: CFN template that needs resolution. This method will modify the input directly resolving references. In subsequent recursions, this will be a fragment of the CFN template. :param SupportedResourceReferences supported_resource_refs: Object that contains information about the resource references supported in this SAM template, along with the value they should resolve to. :return list errors: List of dictionary containing information about invalid reference. Empty list otherwise
[ "Customers", "can", "provide", "a", "reference", "to", "a", "derived", "SAM", "resource", "such", "as", "Alias", "of", "a", "Function", "or", "Stage", "of", "an", "API", "resource", ".", "This", "method", "recursively", "walks", "the", "tree", "converting", "all", "derived", "references", "to", "the", "real", "resource", "name", "if", "it", "is", "present", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/resolver.py#L43-L65
train
awslabs/serverless-application-model
samtranslator/intrinsics/resolver.py
IntrinsicsResolver.resolve_sam_resource_id_refs
def resolve_sam_resource_id_refs(self, input, supported_resource_id_refs): """ Some SAM resources have their logical ids mutated from the original id that the customer writes in the template. This method recursively walks the tree and updates these logical ids from the old value to the new value that is generated by SAM. Example: {"Ref": "MyLayer"} -> {"Ref": "MyLayerABC123"} This method does not attempt to validate a reference. If it is invalid or non-resolvable, it skips the occurrence and continues with the rest. It is recommended that you have an external process that detects and surfaces invalid references. For first call, it is recommended that `template` is the entire CFN template in order to handle references in Mapping or Output sections. :param dict input: CFN template that needs resolution. This method will modify the input directly resolving references. In subsequent recursions, this will be a fragment of the CFN template. :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return list errors: List of dictionary containing information about invalid reference. Empty list otherwise """ return self._traverse(input, supported_resource_id_refs, self._try_resolve_sam_resource_id_refs)
python
def resolve_sam_resource_id_refs(self, input, supported_resource_id_refs): """ Some SAM resources have their logical ids mutated from the original id that the customer writes in the template. This method recursively walks the tree and updates these logical ids from the old value to the new value that is generated by SAM. Example: {"Ref": "MyLayer"} -> {"Ref": "MyLayerABC123"} This method does not attempt to validate a reference. If it is invalid or non-resolvable, it skips the occurrence and continues with the rest. It is recommended that you have an external process that detects and surfaces invalid references. For first call, it is recommended that `template` is the entire CFN template in order to handle references in Mapping or Output sections. :param dict input: CFN template that needs resolution. This method will modify the input directly resolving references. In subsequent recursions, this will be a fragment of the CFN template. :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return list errors: List of dictionary containing information about invalid reference. Empty list otherwise """ return self._traverse(input, supported_resource_id_refs, self._try_resolve_sam_resource_id_refs)
[ "def", "resolve_sam_resource_id_refs", "(", "self", ",", "input", ",", "supported_resource_id_refs", ")", ":", "return", "self", ".", "_traverse", "(", "input", ",", "supported_resource_id_refs", ",", "self", ".", "_try_resolve_sam_resource_id_refs", ")" ]
Some SAM resources have their logical ids mutated from the original id that the customer writes in the template. This method recursively walks the tree and updates these logical ids from the old value to the new value that is generated by SAM. Example: {"Ref": "MyLayer"} -> {"Ref": "MyLayerABC123"} This method does not attempt to validate a reference. If it is invalid or non-resolvable, it skips the occurrence and continues with the rest. It is recommended that you have an external process that detects and surfaces invalid references. For first call, it is recommended that `template` is the entire CFN template in order to handle references in Mapping or Output sections. :param dict input: CFN template that needs resolution. This method will modify the input directly resolving references. In subsequent recursions, this will be a fragment of the CFN template. :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return list errors: List of dictionary containing information about invalid reference. Empty list otherwise
[ "Some", "SAM", "resources", "have", "their", "logical", "ids", "mutated", "from", "the", "original", "id", "that", "the", "customer", "writes", "in", "the", "template", ".", "This", "method", "recursively", "walks", "the", "tree", "and", "updates", "these", "logical", "ids", "from", "the", "old", "value", "to", "the", "new", "value", "that", "is", "generated", "by", "SAM", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/resolver.py#L67-L88
train
awslabs/serverless-application-model
samtranslator/intrinsics/resolver.py
IntrinsicsResolver._traverse
def _traverse(self, input, resolution_data, resolver_method): """ Driver method that performs the actual traversal of input and calls the appropriate `resolver_method` when to perform the resolution. :param input: Any primitive type (dict, array, string etc) whose value might contain an intrinsic function :param resolution_data: Data that will help with resolution. For example, when resolving parameter references, this object will contain a dictionary of parameter names and their values. :param resolver_method: Method that will be called to actually resolve an intrinsic function. This method is called with the parameters `(input, resolution_data)`. :return: Modified `input` with intrinsics resolved """ # There is data to help with resolution. Skip the traversal altogether if len(resolution_data) == 0: return input # # Traversal Algorithm: # # Imagine the input dictionary/list as a tree. We are doing a Pre-Order tree traversal here where we first # process the root node before going to its children. Dict and Lists are the only two iterable nodes. # Everything else is a leaf node. # # We do a Pre-Order traversal to handle the case where `input` contains intrinsic function as its only child # ie. input = {"Ref": "foo}. # # We will try to resolve the intrinsics if we can, otherwise return the original input. In some cases, resolving # an intrinsic will result in a terminal state ie. {"Ref": "foo"} could resolve to a string "bar". In other # cases, resolving intrinsics is only partial and we might need to continue traversing the tree (ex: Fn::Sub) # to handle nested intrinsics. All of these cases lend well towards a Pre-Order traversal where we try and # process the intrinsic, which results in a modified sub-tree to traverse. # input = resolver_method(input, resolution_data) if isinstance(input, dict): return self._traverse_dict(input, resolution_data, resolver_method) elif isinstance(input, list): return self._traverse_list(input, resolution_data, resolver_method) else: # We can iterate only over dict or list types. Primitive types are terminals return input
python
def _traverse(self, input, resolution_data, resolver_method): """ Driver method that performs the actual traversal of input and calls the appropriate `resolver_method` when to perform the resolution. :param input: Any primitive type (dict, array, string etc) whose value might contain an intrinsic function :param resolution_data: Data that will help with resolution. For example, when resolving parameter references, this object will contain a dictionary of parameter names and their values. :param resolver_method: Method that will be called to actually resolve an intrinsic function. This method is called with the parameters `(input, resolution_data)`. :return: Modified `input` with intrinsics resolved """ # There is data to help with resolution. Skip the traversal altogether if len(resolution_data) == 0: return input # # Traversal Algorithm: # # Imagine the input dictionary/list as a tree. We are doing a Pre-Order tree traversal here where we first # process the root node before going to its children. Dict and Lists are the only two iterable nodes. # Everything else is a leaf node. # # We do a Pre-Order traversal to handle the case where `input` contains intrinsic function as its only child # ie. input = {"Ref": "foo}. # # We will try to resolve the intrinsics if we can, otherwise return the original input. In some cases, resolving # an intrinsic will result in a terminal state ie. {"Ref": "foo"} could resolve to a string "bar". In other # cases, resolving intrinsics is only partial and we might need to continue traversing the tree (ex: Fn::Sub) # to handle nested intrinsics. All of these cases lend well towards a Pre-Order traversal where we try and # process the intrinsic, which results in a modified sub-tree to traverse. # input = resolver_method(input, resolution_data) if isinstance(input, dict): return self._traverse_dict(input, resolution_data, resolver_method) elif isinstance(input, list): return self._traverse_list(input, resolution_data, resolver_method) else: # We can iterate only over dict or list types. Primitive types are terminals return input
[ "def", "_traverse", "(", "self", ",", "input", ",", "resolution_data", ",", "resolver_method", ")", ":", "# There is data to help with resolution. Skip the traversal altogether", "if", "len", "(", "resolution_data", ")", "==", "0", ":", "return", "input", "#", "# Traversal Algorithm:", "#", "# Imagine the input dictionary/list as a tree. We are doing a Pre-Order tree traversal here where we first", "# process the root node before going to its children. Dict and Lists are the only two iterable nodes.", "# Everything else is a leaf node.", "#", "# We do a Pre-Order traversal to handle the case where `input` contains intrinsic function as its only child", "# ie. input = {\"Ref\": \"foo}.", "#", "# We will try to resolve the intrinsics if we can, otherwise return the original input. In some cases, resolving", "# an intrinsic will result in a terminal state ie. {\"Ref\": \"foo\"} could resolve to a string \"bar\". In other", "# cases, resolving intrinsics is only partial and we might need to continue traversing the tree (ex: Fn::Sub)", "# to handle nested intrinsics. All of these cases lend well towards a Pre-Order traversal where we try and", "# process the intrinsic, which results in a modified sub-tree to traverse.", "#", "input", "=", "resolver_method", "(", "input", ",", "resolution_data", ")", "if", "isinstance", "(", "input", ",", "dict", ")", ":", "return", "self", ".", "_traverse_dict", "(", "input", ",", "resolution_data", ",", "resolver_method", ")", "elif", "isinstance", "(", "input", ",", "list", ")", ":", "return", "self", ".", "_traverse_list", "(", "input", ",", "resolution_data", ",", "resolver_method", ")", "else", ":", "# We can iterate only over dict or list types. Primitive types are terminals", "return", "input" ]
Driver method that performs the actual traversal of input and calls the appropriate `resolver_method` when to perform the resolution. :param input: Any primitive type (dict, array, string etc) whose value might contain an intrinsic function :param resolution_data: Data that will help with resolution. For example, when resolving parameter references, this object will contain a dictionary of parameter names and their values. :param resolver_method: Method that will be called to actually resolve an intrinsic function. This method is called with the parameters `(input, resolution_data)`. :return: Modified `input` with intrinsics resolved
[ "Driver", "method", "that", "performs", "the", "actual", "traversal", "of", "input", "and", "calls", "the", "appropriate", "resolver_method", "when", "to", "perform", "the", "resolution", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/resolver.py#L90-L132
train
awslabs/serverless-application-model
samtranslator/intrinsics/resolver.py
IntrinsicsResolver._traverse_dict
def _traverse_dict(self, input_dict, resolution_data, resolver_method): """ Traverse a dictionary to resolve intrinsic functions on every value :param input_dict: Input dictionary to traverse :param resolution_data: Data that the `resolver_method` needs to operate :param resolver_method: Method that can actually resolve an intrinsic function, if it detects one :return: Modified dictionary with values resolved """ for key, value in input_dict.items(): input_dict[key] = self._traverse(value, resolution_data, resolver_method) return input_dict
python
def _traverse_dict(self, input_dict, resolution_data, resolver_method): """ Traverse a dictionary to resolve intrinsic functions on every value :param input_dict: Input dictionary to traverse :param resolution_data: Data that the `resolver_method` needs to operate :param resolver_method: Method that can actually resolve an intrinsic function, if it detects one :return: Modified dictionary with values resolved """ for key, value in input_dict.items(): input_dict[key] = self._traverse(value, resolution_data, resolver_method) return input_dict
[ "def", "_traverse_dict", "(", "self", ",", "input_dict", ",", "resolution_data", ",", "resolver_method", ")", ":", "for", "key", ",", "value", "in", "input_dict", ".", "items", "(", ")", ":", "input_dict", "[", "key", "]", "=", "self", ".", "_traverse", "(", "value", ",", "resolution_data", ",", "resolver_method", ")", "return", "input_dict" ]
Traverse a dictionary to resolve intrinsic functions on every value :param input_dict: Input dictionary to traverse :param resolution_data: Data that the `resolver_method` needs to operate :param resolver_method: Method that can actually resolve an intrinsic function, if it detects one :return: Modified dictionary with values resolved
[ "Traverse", "a", "dictionary", "to", "resolve", "intrinsic", "functions", "on", "every", "value" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/resolver.py#L134-L146
train
awslabs/serverless-application-model
samtranslator/intrinsics/resolver.py
IntrinsicsResolver._traverse_list
def _traverse_list(self, input_list, resolution_data, resolver_method): """ Traverse a list to resolve intrinsic functions on every element :param input_list: List of input :param resolution_data: Data that the `resolver_method` needs to operate :param resolver_method: Method that can actually resolve an intrinsic function, if it detects one :return: Modified list with intrinsic functions resolved """ for index, value in enumerate(input_list): input_list[index] = self._traverse(value, resolution_data, resolver_method) return input_list
python
def _traverse_list(self, input_list, resolution_data, resolver_method): """ Traverse a list to resolve intrinsic functions on every element :param input_list: List of input :param resolution_data: Data that the `resolver_method` needs to operate :param resolver_method: Method that can actually resolve an intrinsic function, if it detects one :return: Modified list with intrinsic functions resolved """ for index, value in enumerate(input_list): input_list[index] = self._traverse(value, resolution_data, resolver_method) return input_list
[ "def", "_traverse_list", "(", "self", ",", "input_list", ",", "resolution_data", ",", "resolver_method", ")", ":", "for", "index", ",", "value", "in", "enumerate", "(", "input_list", ")", ":", "input_list", "[", "index", "]", "=", "self", ".", "_traverse", "(", "value", ",", "resolution_data", ",", "resolver_method", ")", "return", "input_list" ]
Traverse a list to resolve intrinsic functions on every element :param input_list: List of input :param resolution_data: Data that the `resolver_method` needs to operate :param resolver_method: Method that can actually resolve an intrinsic function, if it detects one :return: Modified list with intrinsic functions resolved
[ "Traverse", "a", "list", "to", "resolve", "intrinsic", "functions", "on", "every", "element" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/resolver.py#L148-L160
train
awslabs/serverless-application-model
samtranslator/intrinsics/resolver.py
IntrinsicsResolver._try_resolve_parameter_refs
def _try_resolve_parameter_refs(self, input, parameters): """ Try to resolve parameter references on the given input object. The object could be of any type. If the input is not in the format used by intrinsics (ie. dictionary with one key), input is returned unmodified. If the single key in dictionary is one of the supported intrinsic function types, go ahead and try to resolve it. :param input: Input object to resolve :param parameters: Parameter values used to for ref substitution :return: """ if not self._is_intrinsic_dict(input): return input function_type = list(input.keys())[0] return self.supported_intrinsics[function_type].resolve_parameter_refs(input, parameters)
python
def _try_resolve_parameter_refs(self, input, parameters): """ Try to resolve parameter references on the given input object. The object could be of any type. If the input is not in the format used by intrinsics (ie. dictionary with one key), input is returned unmodified. If the single key in dictionary is one of the supported intrinsic function types, go ahead and try to resolve it. :param input: Input object to resolve :param parameters: Parameter values used to for ref substitution :return: """ if not self._is_intrinsic_dict(input): return input function_type = list(input.keys())[0] return self.supported_intrinsics[function_type].resolve_parameter_refs(input, parameters)
[ "def", "_try_resolve_parameter_refs", "(", "self", ",", "input", ",", "parameters", ")", ":", "if", "not", "self", ".", "_is_intrinsic_dict", "(", "input", ")", ":", "return", "input", "function_type", "=", "list", "(", "input", ".", "keys", "(", ")", ")", "[", "0", "]", "return", "self", ".", "supported_intrinsics", "[", "function_type", "]", ".", "resolve_parameter_refs", "(", "input", ",", "parameters", ")" ]
Try to resolve parameter references on the given input object. The object could be of any type. If the input is not in the format used by intrinsics (ie. dictionary with one key), input is returned unmodified. If the single key in dictionary is one of the supported intrinsic function types, go ahead and try to resolve it. :param input: Input object to resolve :param parameters: Parameter values used to for ref substitution :return:
[ "Try", "to", "resolve", "parameter", "references", "on", "the", "given", "input", "object", ".", "The", "object", "could", "be", "of", "any", "type", ".", "If", "the", "input", "is", "not", "in", "the", "format", "used", "by", "intrinsics", "(", "ie", ".", "dictionary", "with", "one", "key", ")", "input", "is", "returned", "unmodified", ".", "If", "the", "single", "key", "in", "dictionary", "is", "one", "of", "the", "supported", "intrinsic", "function", "types", "go", "ahead", "and", "try", "to", "resolve", "it", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/resolver.py#L162-L177
train
awslabs/serverless-application-model
samtranslator/intrinsics/resolver.py
IntrinsicsResolver._try_resolve_sam_resource_refs
def _try_resolve_sam_resource_refs(self, input, supported_resource_refs): """ Try to resolve SAM resource references on the given template. If the given object looks like one of the supported intrinsics, it calls the appropriate resolution on it. If not, this method returns the original input unmodified. :param dict input: Dictionary that may represent an intrinsic function :param SupportedResourceReferences supported_resource_refs: Object containing information about available resource references and the values they resolve to. :return: Modified input dictionary with references resolved """ if not self._is_intrinsic_dict(input): return input function_type = list(input.keys())[0] return self.supported_intrinsics[function_type].resolve_resource_refs(input, supported_resource_refs)
python
def _try_resolve_sam_resource_refs(self, input, supported_resource_refs): """ Try to resolve SAM resource references on the given template. If the given object looks like one of the supported intrinsics, it calls the appropriate resolution on it. If not, this method returns the original input unmodified. :param dict input: Dictionary that may represent an intrinsic function :param SupportedResourceReferences supported_resource_refs: Object containing information about available resource references and the values they resolve to. :return: Modified input dictionary with references resolved """ if not self._is_intrinsic_dict(input): return input function_type = list(input.keys())[0] return self.supported_intrinsics[function_type].resolve_resource_refs(input, supported_resource_refs)
[ "def", "_try_resolve_sam_resource_refs", "(", "self", ",", "input", ",", "supported_resource_refs", ")", ":", "if", "not", "self", ".", "_is_intrinsic_dict", "(", "input", ")", ":", "return", "input", "function_type", "=", "list", "(", "input", ".", "keys", "(", ")", ")", "[", "0", "]", "return", "self", ".", "supported_intrinsics", "[", "function_type", "]", ".", "resolve_resource_refs", "(", "input", ",", "supported_resource_refs", ")" ]
Try to resolve SAM resource references on the given template. If the given object looks like one of the supported intrinsics, it calls the appropriate resolution on it. If not, this method returns the original input unmodified. :param dict input: Dictionary that may represent an intrinsic function :param SupportedResourceReferences supported_resource_refs: Object containing information about available resource references and the values they resolve to. :return: Modified input dictionary with references resolved
[ "Try", "to", "resolve", "SAM", "resource", "references", "on", "the", "given", "template", ".", "If", "the", "given", "object", "looks", "like", "one", "of", "the", "supported", "intrinsics", "it", "calls", "the", "appropriate", "resolution", "on", "it", ".", "If", "not", "this", "method", "returns", "the", "original", "input", "unmodified", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/resolver.py#L179-L194
train
awslabs/serverless-application-model
samtranslator/intrinsics/resolver.py
IntrinsicsResolver._try_resolve_sam_resource_id_refs
def _try_resolve_sam_resource_id_refs(self, input, supported_resource_id_refs): """ Try to resolve SAM resource id references on the given template. If the given object looks like one of the supported intrinsics, it calls the appropriate resolution on it. If not, this method returns the original input unmodified. :param dict input: Dictionary that may represent an intrinsic function :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return: Modified input dictionary with id references resolved """ if not self._is_intrinsic_dict(input): return input function_type = list(input.keys())[0] return self.supported_intrinsics[function_type].resolve_resource_id_refs(input, supported_resource_id_refs)
python
def _try_resolve_sam_resource_id_refs(self, input, supported_resource_id_refs): """ Try to resolve SAM resource id references on the given template. If the given object looks like one of the supported intrinsics, it calls the appropriate resolution on it. If not, this method returns the original input unmodified. :param dict input: Dictionary that may represent an intrinsic function :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return: Modified input dictionary with id references resolved """ if not self._is_intrinsic_dict(input): return input function_type = list(input.keys())[0] return self.supported_intrinsics[function_type].resolve_resource_id_refs(input, supported_resource_id_refs)
[ "def", "_try_resolve_sam_resource_id_refs", "(", "self", ",", "input", ",", "supported_resource_id_refs", ")", ":", "if", "not", "self", ".", "_is_intrinsic_dict", "(", "input", ")", ":", "return", "input", "function_type", "=", "list", "(", "input", ".", "keys", "(", ")", ")", "[", "0", "]", "return", "self", ".", "supported_intrinsics", "[", "function_type", "]", ".", "resolve_resource_id_refs", "(", "input", ",", "supported_resource_id_refs", ")" ]
Try to resolve SAM resource id references on the given template. If the given object looks like one of the supported intrinsics, it calls the appropriate resolution on it. If not, this method returns the original input unmodified. :param dict input: Dictionary that may represent an intrinsic function :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return: Modified input dictionary with id references resolved
[ "Try", "to", "resolve", "SAM", "resource", "id", "references", "on", "the", "given", "template", ".", "If", "the", "given", "object", "looks", "like", "one", "of", "the", "supported", "intrinsics", "it", "calls", "the", "appropriate", "resolution", "on", "it", ".", "If", "not", "this", "method", "returns", "the", "original", "input", "unmodified", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/resolver.py#L196-L210
train
awslabs/serverless-application-model
samtranslator/intrinsics/resolver.py
IntrinsicsResolver._is_intrinsic_dict
def _is_intrinsic_dict(self, input): """ Can the input represent an intrinsic function in it? :param input: Object to be checked :return: True, if the input contains a supported intrinsic function. False otherwise """ # All intrinsic functions are dictionaries with just one key return isinstance(input, dict) \ and len(input) == 1 \ and list(input.keys())[0] in self.supported_intrinsics
python
def _is_intrinsic_dict(self, input): """ Can the input represent an intrinsic function in it? :param input: Object to be checked :return: True, if the input contains a supported intrinsic function. False otherwise """ # All intrinsic functions are dictionaries with just one key return isinstance(input, dict) \ and len(input) == 1 \ and list(input.keys())[0] in self.supported_intrinsics
[ "def", "_is_intrinsic_dict", "(", "self", ",", "input", ")", ":", "# All intrinsic functions are dictionaries with just one key", "return", "isinstance", "(", "input", ",", "dict", ")", "and", "len", "(", "input", ")", "==", "1", "and", "list", "(", "input", ".", "keys", "(", ")", ")", "[", "0", "]", "in", "self", ".", "supported_intrinsics" ]
Can the input represent an intrinsic function in it? :param input: Object to be checked :return: True, if the input contains a supported intrinsic function. False otherwise
[ "Can", "the", "input", "represent", "an", "intrinsic", "function", "in", "it?" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/intrinsics/resolver.py#L212-L222
train
awslabs/serverless-application-model
samtranslator/model/eventsources/cloudwatchlogs.py
CloudWatchLogs.to_cloudformation
def to_cloudformation(self, **kwargs): """Returns the CloudWatch Logs Subscription Filter and Lambda Permission to which this CloudWatch Logs event source corresponds. :param dict kwargs: no existing resources need to be modified :returns: a list of vanilla CloudFormation Resources, to which this push event expands :rtype: list """ function = kwargs.get('function') if not function: raise TypeError("Missing required keyword argument: function") source_arn = self.get_source_arn() permission = self._construct_permission(function, source_arn=source_arn) subscription_filter = self.get_subscription_filter(function, permission) resources = [permission, subscription_filter] return resources
python
def to_cloudformation(self, **kwargs): """Returns the CloudWatch Logs Subscription Filter and Lambda Permission to which this CloudWatch Logs event source corresponds. :param dict kwargs: no existing resources need to be modified :returns: a list of vanilla CloudFormation Resources, to which this push event expands :rtype: list """ function = kwargs.get('function') if not function: raise TypeError("Missing required keyword argument: function") source_arn = self.get_source_arn() permission = self._construct_permission(function, source_arn=source_arn) subscription_filter = self.get_subscription_filter(function, permission) resources = [permission, subscription_filter] return resources
[ "def", "to_cloudformation", "(", "self", ",", "*", "*", "kwargs", ")", ":", "function", "=", "kwargs", ".", "get", "(", "'function'", ")", "if", "not", "function", ":", "raise", "TypeError", "(", "\"Missing required keyword argument: function\"", ")", "source_arn", "=", "self", ".", "get_source_arn", "(", ")", "permission", "=", "self", ".", "_construct_permission", "(", "function", ",", "source_arn", "=", "source_arn", ")", "subscription_filter", "=", "self", ".", "get_subscription_filter", "(", "function", ",", "permission", ")", "resources", "=", "[", "permission", ",", "subscription_filter", "]", "return", "resources" ]
Returns the CloudWatch Logs Subscription Filter and Lambda Permission to which this CloudWatch Logs event source corresponds. :param dict kwargs: no existing resources need to be modified :returns: a list of vanilla CloudFormation Resources, to which this push event expands :rtype: list
[ "Returns", "the", "CloudWatch", "Logs", "Subscription", "Filter", "and", "Lambda", "Permission", "to", "which", "this", "CloudWatch", "Logs", "event", "source", "corresponds", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/model/eventsources/cloudwatchlogs.py#L18-L36
train
awslabs/serverless-application-model
samtranslator/policy_template_processor/processor.py
PolicyTemplatesProcessor.convert
def convert(self, template_name, parameter_values): """ Converts the given template to IAM-ready policy statement by substituting template parameters with the given values. :param template_name: Name of the template :param parameter_values: Values for all parameters of the template :return dict: Dictionary containing policy statement :raises ValueError: If the given inputs don't represent valid template :raises InsufficientParameterValues: If the parameter values don't have values for all required parameters """ if not self.has(template_name): raise TemplateNotFoundException(template_name) template = self.get(template_name) return template.to_statement(parameter_values)
python
def convert(self, template_name, parameter_values): """ Converts the given template to IAM-ready policy statement by substituting template parameters with the given values. :param template_name: Name of the template :param parameter_values: Values for all parameters of the template :return dict: Dictionary containing policy statement :raises ValueError: If the given inputs don't represent valid template :raises InsufficientParameterValues: If the parameter values don't have values for all required parameters """ if not self.has(template_name): raise TemplateNotFoundException(template_name) template = self.get(template_name) return template.to_statement(parameter_values)
[ "def", "convert", "(", "self", ",", "template_name", ",", "parameter_values", ")", ":", "if", "not", "self", ".", "has", "(", "template_name", ")", ":", "raise", "TemplateNotFoundException", "(", "template_name", ")", "template", "=", "self", ".", "get", "(", "template_name", ")", "return", "template", ".", "to_statement", "(", "parameter_values", ")" ]
Converts the given template to IAM-ready policy statement by substituting template parameters with the given values. :param template_name: Name of the template :param parameter_values: Values for all parameters of the template :return dict: Dictionary containing policy statement :raises ValueError: If the given inputs don't represent valid template :raises InsufficientParameterValues: If the parameter values don't have values for all required parameters
[ "Converts", "the", "given", "template", "to", "IAM", "-", "ready", "policy", "statement", "by", "substituting", "template", "parameters", "with", "the", "given", "values", "." ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/policy_template_processor/processor.py#L84-L100
train
awslabs/serverless-application-model
samtranslator/policy_template_processor/processor.py
PolicyTemplatesProcessor._is_valid_templates_dict
def _is_valid_templates_dict(policy_templates_dict, schema=None): """ Is this a valid policy template dictionary :param dict policy_templates_dict: Data to be validated :param dict schema: Optional, dictionary containing JSON Schema representing policy template :return: True, if it is valid. :raises ValueError: If the template dictionary doesn't match up with the schema """ if not schema: schema = PolicyTemplatesProcessor._read_schema() try: jsonschema.validate(policy_templates_dict, schema) except ValidationError as ex: # Stringifying the exception will give us useful error message raise ValueError(str(ex)) return True
python
def _is_valid_templates_dict(policy_templates_dict, schema=None): """ Is this a valid policy template dictionary :param dict policy_templates_dict: Data to be validated :param dict schema: Optional, dictionary containing JSON Schema representing policy template :return: True, if it is valid. :raises ValueError: If the template dictionary doesn't match up with the schema """ if not schema: schema = PolicyTemplatesProcessor._read_schema() try: jsonschema.validate(policy_templates_dict, schema) except ValidationError as ex: # Stringifying the exception will give us useful error message raise ValueError(str(ex)) return True
[ "def", "_is_valid_templates_dict", "(", "policy_templates_dict", ",", "schema", "=", "None", ")", ":", "if", "not", "schema", ":", "schema", "=", "PolicyTemplatesProcessor", ".", "_read_schema", "(", ")", "try", ":", "jsonschema", ".", "validate", "(", "policy_templates_dict", ",", "schema", ")", "except", "ValidationError", "as", "ex", ":", "# Stringifying the exception will give us useful error message", "raise", "ValueError", "(", "str", "(", "ex", ")", ")", "return", "True" ]
Is this a valid policy template dictionary :param dict policy_templates_dict: Data to be validated :param dict schema: Optional, dictionary containing JSON Schema representing policy template :return: True, if it is valid. :raises ValueError: If the template dictionary doesn't match up with the schema
[ "Is", "this", "a", "valid", "policy", "template", "dictionary" ]
cccb0c96b5c91e53355ebc07e542467303a5eedd
https://github.com/awslabs/serverless-application-model/blob/cccb0c96b5c91e53355ebc07e542467303a5eedd/samtranslator/policy_template_processor/processor.py#L103-L122
train
pyecharts/pyecharts
pyecharts/render/engine.py
RenderEngine.render_chart_to_file
def render_chart_to_file(self, template_name: str, chart: Any, path: str): """ Render a chart or page to local html files. :param chart: A Chart or Page object :param path: The destination file which the html code write to :param template_name: The name of template file. """ tpl = self.env.get_template(template_name) html = tpl.render(chart=self.generate_js_link(chart)) write_utf8_html_file(path, self._reg_replace(html))
python
def render_chart_to_file(self, template_name: str, chart: Any, path: str): """ Render a chart or page to local html files. :param chart: A Chart or Page object :param path: The destination file which the html code write to :param template_name: The name of template file. """ tpl = self.env.get_template(template_name) html = tpl.render(chart=self.generate_js_link(chart)) write_utf8_html_file(path, self._reg_replace(html))
[ "def", "render_chart_to_file", "(", "self", ",", "template_name", ":", "str", ",", "chart", ":", "Any", ",", "path", ":", "str", ")", ":", "tpl", "=", "self", ".", "env", ".", "get_template", "(", "template_name", ")", "html", "=", "tpl", ".", "render", "(", "chart", "=", "self", ".", "generate_js_link", "(", "chart", ")", ")", "write_utf8_html_file", "(", "path", ",", "self", ".", "_reg_replace", "(", "html", ")", ")" ]
Render a chart or page to local html files. :param chart: A Chart or Page object :param path: The destination file which the html code write to :param template_name: The name of template file.
[ "Render", "a", "chart", "or", "page", "to", "local", "html", "files", "." ]
02050acb0e94bb9453b88a25028de7a0ce23f125
https://github.com/pyecharts/pyecharts/blob/02050acb0e94bb9453b88a25028de7a0ce23f125/pyecharts/render/engine.py#L36-L46
train
pyecharts/pyecharts
pyecharts/render/snapshot.py
decode_base64
def decode_base64(data: str) -> bytes: """Decode base64, padding being optional. :param data: Base64 data as an ASCII byte string :returns: The decoded byte string. """ missing_padding = len(data) % 4 if missing_padding != 0: data += "=" * (4 - missing_padding) return base64.decodebytes(data.encode("utf-8"))
python
def decode_base64(data: str) -> bytes: """Decode base64, padding being optional. :param data: Base64 data as an ASCII byte string :returns: The decoded byte string. """ missing_padding = len(data) % 4 if missing_padding != 0: data += "=" * (4 - missing_padding) return base64.decodebytes(data.encode("utf-8"))
[ "def", "decode_base64", "(", "data", ":", "str", ")", "->", "bytes", ":", "missing_padding", "=", "len", "(", "data", ")", "%", "4", "if", "missing_padding", "!=", "0", ":", "data", "+=", "\"=\"", "*", "(", "4", "-", "missing_padding", ")", "return", "base64", ".", "decodebytes", "(", "data", ".", "encode", "(", "\"utf-8\"", ")", ")" ]
Decode base64, padding being optional. :param data: Base64 data as an ASCII byte string :returns: The decoded byte string.
[ "Decode", "base64", "padding", "being", "optional", "." ]
02050acb0e94bb9453b88a25028de7a0ce23f125
https://github.com/pyecharts/pyecharts/blob/02050acb0e94bb9453b88a25028de7a0ce23f125/pyecharts/render/snapshot.py#L58-L67
train
pyecharts/pyecharts
pyecharts/charts/basic_charts/tree.py
Tree._set_collapse_interval
def _set_collapse_interval(data, interval): """ 间隔折叠节点,当节点过多时可以解决节点显示过杂间隔。 :param data: 节点数据 :param interval: 指定间隔 """ if interval <= 0: return data if data and isinstance(data, list): for d in data: children = d.get("children", None) if children and interval > 0: for index, value in enumerate(children): if index % interval == 0: value.update(collapsed="false") return data
python
def _set_collapse_interval(data, interval): """ 间隔折叠节点,当节点过多时可以解决节点显示过杂间隔。 :param data: 节点数据 :param interval: 指定间隔 """ if interval <= 0: return data if data and isinstance(data, list): for d in data: children = d.get("children", None) if children and interval > 0: for index, value in enumerate(children): if index % interval == 0: value.update(collapsed="false") return data
[ "def", "_set_collapse_interval", "(", "data", ",", "interval", ")", ":", "if", "interval", "<=", "0", ":", "return", "data", "if", "data", "and", "isinstance", "(", "data", ",", "list", ")", ":", "for", "d", "in", "data", ":", "children", "=", "d", ".", "get", "(", "\"children\"", ",", "None", ")", "if", "children", "and", "interval", ">", "0", ":", "for", "index", ",", "value", "in", "enumerate", "(", "children", ")", ":", "if", "index", "%", "interval", "==", "0", ":", "value", ".", "update", "(", "collapsed", "=", "\"false\"", ")", "return", "data" ]
间隔折叠节点,当节点过多时可以解决节点显示过杂间隔。 :param data: 节点数据 :param interval: 指定间隔
[ "间隔折叠节点,当节点过多时可以解决节点显示过杂间隔。", ":", "param", "data", ":", "节点数据", ":", "param", "interval", ":", "指定间隔" ]
02050acb0e94bb9453b88a25028de7a0ce23f125
https://github.com/pyecharts/pyecharts/blob/02050acb0e94bb9453b88a25028de7a0ce23f125/pyecharts/charts/basic_charts/tree.py#L19-L35
train
micropython/micropython
ports/nrf/boards/make-pins.py
parse_pin
def parse_pin(name_str): """Parses a string and returns a pin-num.""" if len(name_str) < 1: raise ValueError("Expecting pin name to be at least 4 charcters.") if name_str[0] != 'P': raise ValueError("Expecting pin name to start with P") pin_str = name_str[1:].split('/')[0] if not pin_str.isdigit(): raise ValueError("Expecting numeric pin number.") return int(pin_str)
python
def parse_pin(name_str): """Parses a string and returns a pin-num.""" if len(name_str) < 1: raise ValueError("Expecting pin name to be at least 4 charcters.") if name_str[0] != 'P': raise ValueError("Expecting pin name to start with P") pin_str = name_str[1:].split('/')[0] if not pin_str.isdigit(): raise ValueError("Expecting numeric pin number.") return int(pin_str)
[ "def", "parse_pin", "(", "name_str", ")", ":", "if", "len", "(", "name_str", ")", "<", "1", ":", "raise", "ValueError", "(", "\"Expecting pin name to be at least 4 charcters.\"", ")", "if", "name_str", "[", "0", "]", "!=", "'P'", ":", "raise", "ValueError", "(", "\"Expecting pin name to start with P\"", ")", "pin_str", "=", "name_str", "[", "1", ":", "]", ".", "split", "(", "'/'", ")", "[", "0", "]", "if", "not", "pin_str", ".", "isdigit", "(", ")", ":", "raise", "ValueError", "(", "\"Expecting numeric pin number.\"", ")", "return", "int", "(", "pin_str", ")" ]
Parses a string and returns a pin-num.
[ "Parses", "a", "string", "and", "returns", "a", "pin", "-", "num", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/ports/nrf/boards/make-pins.py#L14-L23
train
micropython/micropython
ports/nrf/boards/make-pins.py
AlternateFunction.ptr
def ptr(self): """Returns the numbered function (i.e. USART6) for this AF.""" if self.fn_num is None: return self.func return '{:s}{:d}'.format(self.func, self.fn_num)
python
def ptr(self): """Returns the numbered function (i.e. USART6) for this AF.""" if self.fn_num is None: return self.func return '{:s}{:d}'.format(self.func, self.fn_num)
[ "def", "ptr", "(", "self", ")", ":", "if", "self", ".", "fn_num", "is", "None", ":", "return", "self", ".", "func", "return", "'{:s}{:d}'", ".", "format", "(", "self", ".", "func", ",", "self", ".", "fn_num", ")" ]
Returns the numbered function (i.e. USART6) for this AF.
[ "Returns", "the", "numbered", "function", "(", "i", ".", "e", ".", "USART6", ")", "for", "this", "AF", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/ports/nrf/boards/make-pins.py#L61-L65
train
micropython/micropython
ports/nrf/boards/make-pins.py
AlternateFunction.print
def print(self): """Prints the C representation of this AF.""" if self.supported: print(' AF', end='') else: print(' //', end='') fn_num = self.fn_num if fn_num is None: fn_num = 0 print('({:2d}, {:8s}, {:2d}, {:10s}, {:8s}), // {:s}'.format(self.idx, self.func, fn_num, self.pin_type, self.ptr(), self.af_str))
python
def print(self): """Prints the C representation of this AF.""" if self.supported: print(' AF', end='') else: print(' //', end='') fn_num = self.fn_num if fn_num is None: fn_num = 0 print('({:2d}, {:8s}, {:2d}, {:10s}, {:8s}), // {:s}'.format(self.idx, self.func, fn_num, self.pin_type, self.ptr(), self.af_str))
[ "def", "print", "(", "self", ")", ":", "if", "self", ".", "supported", ":", "print", "(", "' AF'", ",", "end", "=", "''", ")", "else", ":", "print", "(", "' //'", ",", "end", "=", "''", ")", "fn_num", "=", "self", ".", "fn_num", "if", "fn_num", "is", "None", ":", "fn_num", "=", "0", "print", "(", "'({:2d}, {:8s}, {:2d}, {:10s}, {:8s}), // {:s}'", ".", "format", "(", "self", ".", "idx", ",", "self", ".", "func", ",", "fn_num", ",", "self", ".", "pin_type", ",", "self", ".", "ptr", "(", ")", ",", "self", ".", "af_str", ")", ")" ]
Prints the C representation of this AF.
[ "Prints", "the", "C", "representation", "of", "this", "AF", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/ports/nrf/boards/make-pins.py#L70-L80
train
micropython/micropython
examples/switch.py
run_loop
def run_loop(leds=all_leds): """ Start the loop. :param `leds`: Which LEDs to light up upon switch press. :type `leds`: sequence of LED objects """ print('Loop started.\nPress Ctrl+C to break out of the loop.') while 1: try: if switch(): [led.on() for led in leds] else: [led.off() for led in leds] except OSError: # VCPInterrupt # Ctrl+C in interpreter mode. break
python
def run_loop(leds=all_leds): """ Start the loop. :param `leds`: Which LEDs to light up upon switch press. :type `leds`: sequence of LED objects """ print('Loop started.\nPress Ctrl+C to break out of the loop.') while 1: try: if switch(): [led.on() for led in leds] else: [led.off() for led in leds] except OSError: # VCPInterrupt # Ctrl+C in interpreter mode. break
[ "def", "run_loop", "(", "leds", "=", "all_leds", ")", ":", "print", "(", "'Loop started.\\nPress Ctrl+C to break out of the loop.'", ")", "while", "1", ":", "try", ":", "if", "switch", "(", ")", ":", "[", "led", ".", "on", "(", ")", "for", "led", "in", "leds", "]", "else", ":", "[", "led", ".", "off", "(", ")", "for", "led", "in", "leds", "]", "except", "OSError", ":", "# VCPInterrupt # Ctrl+C in interpreter mode.", "break" ]
Start the loop. :param `leds`: Which LEDs to light up upon switch press. :type `leds`: sequence of LED objects
[ "Start", "the", "loop", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/examples/switch.py#L27-L42
train
micropython/micropython
py/makemoduledefs.py
find_c_file
def find_c_file(obj_file, vpath): """ Search vpaths for the c file that matches the provided object_file. :param str obj_file: object file to find the matching c file for :param List[str] vpath: List of base paths, similar to gcc vpath :return: str path to c file or None """ c_file = None relative_c_file = os.path.splitext(obj_file)[0] + ".c" relative_c_file = relative_c_file.lstrip('/\\') for p in vpath: possible_c_file = os.path.join(p, relative_c_file) if os.path.exists(possible_c_file): c_file = possible_c_file break return c_file
python
def find_c_file(obj_file, vpath): """ Search vpaths for the c file that matches the provided object_file. :param str obj_file: object file to find the matching c file for :param List[str] vpath: List of base paths, similar to gcc vpath :return: str path to c file or None """ c_file = None relative_c_file = os.path.splitext(obj_file)[0] + ".c" relative_c_file = relative_c_file.lstrip('/\\') for p in vpath: possible_c_file = os.path.join(p, relative_c_file) if os.path.exists(possible_c_file): c_file = possible_c_file break return c_file
[ "def", "find_c_file", "(", "obj_file", ",", "vpath", ")", ":", "c_file", "=", "None", "relative_c_file", "=", "os", ".", "path", ".", "splitext", "(", "obj_file", ")", "[", "0", "]", "+", "\".c\"", "relative_c_file", "=", "relative_c_file", ".", "lstrip", "(", "'/\\\\'", ")", "for", "p", "in", "vpath", ":", "possible_c_file", "=", "os", ".", "path", ".", "join", "(", "p", ",", "relative_c_file", ")", "if", "os", ".", "path", ".", "exists", "(", "possible_c_file", ")", ":", "c_file", "=", "possible_c_file", "break", "return", "c_file" ]
Search vpaths for the c file that matches the provided object_file. :param str obj_file: object file to find the matching c file for :param List[str] vpath: List of base paths, similar to gcc vpath :return: str path to c file or None
[ "Search", "vpaths", "for", "the", "c", "file", "that", "matches", "the", "provided", "object_file", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/py/makemoduledefs.py#L22-L38
train
micropython/micropython
py/makemoduledefs.py
find_module_registrations
def find_module_registrations(c_file): """ Find any MP_REGISTER_MODULE definitions in the provided c file. :param str c_file: path to c file to check :return: List[(module_name, obj_module, enabled_define)] """ global pattern if c_file is None: # No c file to match the object file, skip return set() with io.open(c_file, encoding='utf-8') as c_file_obj: return set(re.findall(pattern, c_file_obj.read()))
python
def find_module_registrations(c_file): """ Find any MP_REGISTER_MODULE definitions in the provided c file. :param str c_file: path to c file to check :return: List[(module_name, obj_module, enabled_define)] """ global pattern if c_file is None: # No c file to match the object file, skip return set() with io.open(c_file, encoding='utf-8') as c_file_obj: return set(re.findall(pattern, c_file_obj.read()))
[ "def", "find_module_registrations", "(", "c_file", ")", ":", "global", "pattern", "if", "c_file", "is", "None", ":", "# No c file to match the object file, skip", "return", "set", "(", ")", "with", "io", ".", "open", "(", "c_file", ",", "encoding", "=", "'utf-8'", ")", "as", "c_file_obj", ":", "return", "set", "(", "re", ".", "findall", "(", "pattern", ",", "c_file_obj", ".", "read", "(", ")", ")", ")" ]
Find any MP_REGISTER_MODULE definitions in the provided c file. :param str c_file: path to c file to check :return: List[(module_name, obj_module, enabled_define)]
[ "Find", "any", "MP_REGISTER_MODULE", "definitions", "in", "the", "provided", "c", "file", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/py/makemoduledefs.py#L41-L54
train
micropython/micropython
py/makemoduledefs.py
generate_module_table_header
def generate_module_table_header(modules): """ Generate header with module table entries for builtin modules. :param List[(module_name, obj_module, enabled_define)] modules: module defs :return: None """ # Print header file for all external modules. mod_defs = [] print("// Automatically generated by makemoduledefs.py.\n") for module_name, obj_module, enabled_define in modules: mod_def = "MODULE_DEF_{}".format(module_name.upper()) mod_defs.append(mod_def) print(( "#if ({enabled_define})\n" " extern const struct _mp_obj_module_t {obj_module};\n" " #define {mod_def} {{ MP_ROM_QSTR({module_name}), MP_ROM_PTR(&{obj_module}) }},\n" "#else\n" " #define {mod_def}\n" "#endif\n" ).format(module_name=module_name, obj_module=obj_module, enabled_define=enabled_define, mod_def=mod_def) ) print("\n#define MICROPY_REGISTERED_MODULES \\") for mod_def in mod_defs: print(" {mod_def} \\".format(mod_def=mod_def)) print("// MICROPY_REGISTERED_MODULES")
python
def generate_module_table_header(modules): """ Generate header with module table entries for builtin modules. :param List[(module_name, obj_module, enabled_define)] modules: module defs :return: None """ # Print header file for all external modules. mod_defs = [] print("// Automatically generated by makemoduledefs.py.\n") for module_name, obj_module, enabled_define in modules: mod_def = "MODULE_DEF_{}".format(module_name.upper()) mod_defs.append(mod_def) print(( "#if ({enabled_define})\n" " extern const struct _mp_obj_module_t {obj_module};\n" " #define {mod_def} {{ MP_ROM_QSTR({module_name}), MP_ROM_PTR(&{obj_module}) }},\n" "#else\n" " #define {mod_def}\n" "#endif\n" ).format(module_name=module_name, obj_module=obj_module, enabled_define=enabled_define, mod_def=mod_def) ) print("\n#define MICROPY_REGISTERED_MODULES \\") for mod_def in mod_defs: print(" {mod_def} \\".format(mod_def=mod_def)) print("// MICROPY_REGISTERED_MODULES")
[ "def", "generate_module_table_header", "(", "modules", ")", ":", "# Print header file for all external modules.", "mod_defs", "=", "[", "]", "print", "(", "\"// Automatically generated by makemoduledefs.py.\\n\"", ")", "for", "module_name", ",", "obj_module", ",", "enabled_define", "in", "modules", ":", "mod_def", "=", "\"MODULE_DEF_{}\"", ".", "format", "(", "module_name", ".", "upper", "(", ")", ")", "mod_defs", ".", "append", "(", "mod_def", ")", "print", "(", "(", "\"#if ({enabled_define})\\n\"", "\" extern const struct _mp_obj_module_t {obj_module};\\n\"", "\" #define {mod_def} {{ MP_ROM_QSTR({module_name}), MP_ROM_PTR(&{obj_module}) }},\\n\"", "\"#else\\n\"", "\" #define {mod_def}\\n\"", "\"#endif\\n\"", ")", ".", "format", "(", "module_name", "=", "module_name", ",", "obj_module", "=", "obj_module", ",", "enabled_define", "=", "enabled_define", ",", "mod_def", "=", "mod_def", ")", ")", "print", "(", "\"\\n#define MICROPY_REGISTERED_MODULES \\\\\"", ")", "for", "mod_def", "in", "mod_defs", ":", "print", "(", "\" {mod_def} \\\\\"", ".", "format", "(", "mod_def", "=", "mod_def", ")", ")", "print", "(", "\"// MICROPY_REGISTERED_MODULES\"", ")" ]
Generate header with module table entries for builtin modules. :param List[(module_name, obj_module, enabled_define)] modules: module defs :return: None
[ "Generate", "header", "with", "module", "table", "entries", "for", "builtin", "modules", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/py/makemoduledefs.py#L57-L86
train
micropython/micropython
tools/gen-cpydiff.py
readfiles
def readfiles(): """ Reads test files """ tests = list(filter(lambda x: x.endswith('.py'), os.listdir(TESTPATH))) tests.sort() files = [] for test in tests: text = open(TESTPATH + test, 'r').read() try: class_, desc, cause, workaround, code = [x.rstrip() for x in \ list(filter(None, re.split(SPLIT, text)))] output = Output(test, class_, desc, cause, workaround, code, '', '', '') files.append(output) except IndexError: print('Incorrect format in file ' + TESTPATH + test) return files
python
def readfiles(): """ Reads test files """ tests = list(filter(lambda x: x.endswith('.py'), os.listdir(TESTPATH))) tests.sort() files = [] for test in tests: text = open(TESTPATH + test, 'r').read() try: class_, desc, cause, workaround, code = [x.rstrip() for x in \ list(filter(None, re.split(SPLIT, text)))] output = Output(test, class_, desc, cause, workaround, code, '', '', '') files.append(output) except IndexError: print('Incorrect format in file ' + TESTPATH + test) return files
[ "def", "readfiles", "(", ")", ":", "tests", "=", "list", "(", "filter", "(", "lambda", "x", ":", "x", ".", "endswith", "(", "'.py'", ")", ",", "os", ".", "listdir", "(", "TESTPATH", ")", ")", ")", "tests", ".", "sort", "(", ")", "files", "=", "[", "]", "for", "test", "in", "tests", ":", "text", "=", "open", "(", "TESTPATH", "+", "test", ",", "'r'", ")", ".", "read", "(", ")", "try", ":", "class_", ",", "desc", ",", "cause", ",", "workaround", ",", "code", "=", "[", "x", ".", "rstrip", "(", ")", "for", "x", "in", "list", "(", "filter", "(", "None", ",", "re", ".", "split", "(", "SPLIT", ",", "text", ")", ")", ")", "]", "output", "=", "Output", "(", "test", ",", "class_", ",", "desc", ",", "cause", ",", "workaround", ",", "code", ",", "''", ",", "''", ",", "''", ")", "files", ".", "append", "(", "output", ")", "except", "IndexError", ":", "print", "(", "'Incorrect format in file '", "+", "TESTPATH", "+", "test", ")", "return", "files" ]
Reads test files
[ "Reads", "test", "files" ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/gen-cpydiff.py#L63-L80
train
micropython/micropython
tools/gen-cpydiff.py
uimports
def uimports(code): """ converts CPython module names into MicroPython equivalents """ for uimport in UIMPORTLIST: uimport = bytes(uimport, 'utf8') code = code.replace(uimport, b'u' + uimport) return code
python
def uimports(code): """ converts CPython module names into MicroPython equivalents """ for uimport in UIMPORTLIST: uimport = bytes(uimport, 'utf8') code = code.replace(uimport, b'u' + uimport) return code
[ "def", "uimports", "(", "code", ")", ":", "for", "uimport", "in", "UIMPORTLIST", ":", "uimport", "=", "bytes", "(", "uimport", ",", "'utf8'", ")", "code", "=", "code", ".", "replace", "(", "uimport", ",", "b'u'", "+", "uimport", ")", "return", "code" ]
converts CPython module names into MicroPython equivalents
[ "converts", "CPython", "module", "names", "into", "MicroPython", "equivalents" ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/gen-cpydiff.py#L82-L87
train
micropython/micropython
tools/gen-cpydiff.py
indent
def indent(block, spaces): """ indents paragraphs of text for rst formatting """ new_block = '' for line in block.split('\n'): new_block += spaces + line + '\n' return new_block
python
def indent(block, spaces): """ indents paragraphs of text for rst formatting """ new_block = '' for line in block.split('\n'): new_block += spaces + line + '\n' return new_block
[ "def", "indent", "(", "block", ",", "spaces", ")", ":", "new_block", "=", "''", "for", "line", "in", "block", ".", "split", "(", "'\\n'", ")", ":", "new_block", "+=", "spaces", "+", "line", "+", "'\\n'", "return", "new_block" ]
indents paragraphs of text for rst formatting
[ "indents", "paragraphs", "of", "text", "for", "rst", "formatting" ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/gen-cpydiff.py#L116-L121
train
micropython/micropython
tools/gen-cpydiff.py
gen_table
def gen_table(contents): """ creates a table given any set of columns """ xlengths = [] ylengths = [] for column in contents: col_len = 0 for entry in column: lines = entry.split('\n') for line in lines: col_len = max(len(line) + 2, col_len) xlengths.append(col_len) for i in range(len(contents[0])): ymax = 0 for j in range(len(contents)): ymax = max(ymax, len(contents[j][i].split('\n'))) ylengths.append(ymax) table_divider = '+' + ''.join(['-' * i + '+' for i in xlengths]) + '\n' table = table_divider for i in range(len(ylengths)): row = [column[i] for column in contents] row = [entry + '\n' * (ylengths[i]-len(entry.split('\n'))) for entry in row] row = [entry.split('\n') for entry in row] for j in range(ylengths[i]): k = 0 for entry in row: width = xlengths[k] table += ''.join(['| {:{}}'.format(entry[j], width - 1)]) k += 1 table += '|\n' table += table_divider return table + '\n'
python
def gen_table(contents): """ creates a table given any set of columns """ xlengths = [] ylengths = [] for column in contents: col_len = 0 for entry in column: lines = entry.split('\n') for line in lines: col_len = max(len(line) + 2, col_len) xlengths.append(col_len) for i in range(len(contents[0])): ymax = 0 for j in range(len(contents)): ymax = max(ymax, len(contents[j][i].split('\n'))) ylengths.append(ymax) table_divider = '+' + ''.join(['-' * i + '+' for i in xlengths]) + '\n' table = table_divider for i in range(len(ylengths)): row = [column[i] for column in contents] row = [entry + '\n' * (ylengths[i]-len(entry.split('\n'))) for entry in row] row = [entry.split('\n') for entry in row] for j in range(ylengths[i]): k = 0 for entry in row: width = xlengths[k] table += ''.join(['| {:{}}'.format(entry[j], width - 1)]) k += 1 table += '|\n' table += table_divider return table + '\n'
[ "def", "gen_table", "(", "contents", ")", ":", "xlengths", "=", "[", "]", "ylengths", "=", "[", "]", "for", "column", "in", "contents", ":", "col_len", "=", "0", "for", "entry", "in", "column", ":", "lines", "=", "entry", ".", "split", "(", "'\\n'", ")", "for", "line", "in", "lines", ":", "col_len", "=", "max", "(", "len", "(", "line", ")", "+", "2", ",", "col_len", ")", "xlengths", ".", "append", "(", "col_len", ")", "for", "i", "in", "range", "(", "len", "(", "contents", "[", "0", "]", ")", ")", ":", "ymax", "=", "0", "for", "j", "in", "range", "(", "len", "(", "contents", ")", ")", ":", "ymax", "=", "max", "(", "ymax", ",", "len", "(", "contents", "[", "j", "]", "[", "i", "]", ".", "split", "(", "'\\n'", ")", ")", ")", "ylengths", ".", "append", "(", "ymax", ")", "table_divider", "=", "'+'", "+", "''", ".", "join", "(", "[", "'-'", "*", "i", "+", "'+'", "for", "i", "in", "xlengths", "]", ")", "+", "'\\n'", "table", "=", "table_divider", "for", "i", "in", "range", "(", "len", "(", "ylengths", ")", ")", ":", "row", "=", "[", "column", "[", "i", "]", "for", "column", "in", "contents", "]", "row", "=", "[", "entry", "+", "'\\n'", "*", "(", "ylengths", "[", "i", "]", "-", "len", "(", "entry", ".", "split", "(", "'\\n'", ")", ")", ")", "for", "entry", "in", "row", "]", "row", "=", "[", "entry", ".", "split", "(", "'\\n'", ")", "for", "entry", "in", "row", "]", "for", "j", "in", "range", "(", "ylengths", "[", "i", "]", ")", ":", "k", "=", "0", "for", "entry", "in", "row", ":", "width", "=", "xlengths", "[", "k", "]", "table", "+=", "''", ".", "join", "(", "[", "'| {:{}}'", ".", "format", "(", "entry", "[", "j", "]", ",", "width", "-", "1", ")", "]", ")", "k", "+=", "1", "table", "+=", "'|\\n'", "table", "+=", "table_divider", "return", "table", "+", "'\\n'" ]
creates a table given any set of columns
[ "creates", "a", "table", "given", "any", "set", "of", "columns" ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/gen-cpydiff.py#L123-L154
train
micropython/micropython
tools/gen-cpydiff.py
gen_rst
def gen_rst(results): """ creates restructured text documents to display tests """ # make sure the destination directory exists try: os.mkdir(DOCPATH) except OSError as e: if e.args[0] != errno.EEXIST and e.args[0] != errno.EISDIR: raise toctree = [] class_ = [] for output in results: section = output.class_.split(',') for i in range(len(section)): section[i] = section[i].rstrip() if section[i] in CLASSMAP: section[i] = CLASSMAP[section[i]] if i >= len(class_) or section[i] != class_[i]: if i == 0: filename = section[i].replace(' ', '_').lower() rst = open(DOCPATH + filename + '.rst', 'w') rst.write(HEADER) rst.write(section[i] + '\n') rst.write(RSTCHARS[0] * len(section[i])) rst.write(time.strftime("\nGenerated %a %d %b %Y %X UTC\n\n", time.gmtime())) toctree.append(filename) else: rst.write(section[i] + '\n') rst.write(RSTCHARS[min(i, len(RSTCHARS)-1)] * len(section[i])) rst.write('\n\n') class_ = section rst.write('.. _cpydiff_%s:\n\n' % output.name.rsplit('.', 1)[0]) rst.write(output.desc + '\n') rst.write('~' * len(output.desc) + '\n\n') if output.cause != 'Unknown': rst.write('**Cause:** ' + output.cause + '\n\n') if output.workaround != 'Unknown': rst.write('**Workaround:** ' + output.workaround + '\n\n') rst.write('Sample code::\n\n' + indent(output.code, TAB) + '\n') output_cpy = indent(''.join(output.output_cpy[0:2]), TAB).rstrip() output_cpy = ('::\n\n' if output_cpy != '' else '') + output_cpy output_upy = indent(''.join(output.output_upy[0:2]), TAB).rstrip() output_upy = ('::\n\n' if output_upy != '' else '') + output_upy table = gen_table([['CPy output:', output_cpy], ['uPy output:', output_upy]]) rst.write(table) template = open(INDEXTEMPLATE, 'r') index = open(DOCPATH + INDEX, 'w') index.write(HEADER) index.write(template.read()) for section in INDEXPRIORITY: if section in toctree: index.write(indent(section + '.rst', TAB)) toctree.remove(section) for section in toctree: index.write(indent(section + '.rst', TAB))
python
def gen_rst(results): """ creates restructured text documents to display tests """ # make sure the destination directory exists try: os.mkdir(DOCPATH) except OSError as e: if e.args[0] != errno.EEXIST and e.args[0] != errno.EISDIR: raise toctree = [] class_ = [] for output in results: section = output.class_.split(',') for i in range(len(section)): section[i] = section[i].rstrip() if section[i] in CLASSMAP: section[i] = CLASSMAP[section[i]] if i >= len(class_) or section[i] != class_[i]: if i == 0: filename = section[i].replace(' ', '_').lower() rst = open(DOCPATH + filename + '.rst', 'w') rst.write(HEADER) rst.write(section[i] + '\n') rst.write(RSTCHARS[0] * len(section[i])) rst.write(time.strftime("\nGenerated %a %d %b %Y %X UTC\n\n", time.gmtime())) toctree.append(filename) else: rst.write(section[i] + '\n') rst.write(RSTCHARS[min(i, len(RSTCHARS)-1)] * len(section[i])) rst.write('\n\n') class_ = section rst.write('.. _cpydiff_%s:\n\n' % output.name.rsplit('.', 1)[0]) rst.write(output.desc + '\n') rst.write('~' * len(output.desc) + '\n\n') if output.cause != 'Unknown': rst.write('**Cause:** ' + output.cause + '\n\n') if output.workaround != 'Unknown': rst.write('**Workaround:** ' + output.workaround + '\n\n') rst.write('Sample code::\n\n' + indent(output.code, TAB) + '\n') output_cpy = indent(''.join(output.output_cpy[0:2]), TAB).rstrip() output_cpy = ('::\n\n' if output_cpy != '' else '') + output_cpy output_upy = indent(''.join(output.output_upy[0:2]), TAB).rstrip() output_upy = ('::\n\n' if output_upy != '' else '') + output_upy table = gen_table([['CPy output:', output_cpy], ['uPy output:', output_upy]]) rst.write(table) template = open(INDEXTEMPLATE, 'r') index = open(DOCPATH + INDEX, 'w') index.write(HEADER) index.write(template.read()) for section in INDEXPRIORITY: if section in toctree: index.write(indent(section + '.rst', TAB)) toctree.remove(section) for section in toctree: index.write(indent(section + '.rst', TAB))
[ "def", "gen_rst", "(", "results", ")", ":", "# make sure the destination directory exists", "try", ":", "os", ".", "mkdir", "(", "DOCPATH", ")", "except", "OSError", "as", "e", ":", "if", "e", ".", "args", "[", "0", "]", "!=", "errno", ".", "EEXIST", "and", "e", ".", "args", "[", "0", "]", "!=", "errno", ".", "EISDIR", ":", "raise", "toctree", "=", "[", "]", "class_", "=", "[", "]", "for", "output", "in", "results", ":", "section", "=", "output", ".", "class_", ".", "split", "(", "','", ")", "for", "i", "in", "range", "(", "len", "(", "section", ")", ")", ":", "section", "[", "i", "]", "=", "section", "[", "i", "]", ".", "rstrip", "(", ")", "if", "section", "[", "i", "]", "in", "CLASSMAP", ":", "section", "[", "i", "]", "=", "CLASSMAP", "[", "section", "[", "i", "]", "]", "if", "i", ">=", "len", "(", "class_", ")", "or", "section", "[", "i", "]", "!=", "class_", "[", "i", "]", ":", "if", "i", "==", "0", ":", "filename", "=", "section", "[", "i", "]", ".", "replace", "(", "' '", ",", "'_'", ")", ".", "lower", "(", ")", "rst", "=", "open", "(", "DOCPATH", "+", "filename", "+", "'.rst'", ",", "'w'", ")", "rst", ".", "write", "(", "HEADER", ")", "rst", ".", "write", "(", "section", "[", "i", "]", "+", "'\\n'", ")", "rst", ".", "write", "(", "RSTCHARS", "[", "0", "]", "*", "len", "(", "section", "[", "i", "]", ")", ")", "rst", ".", "write", "(", "time", ".", "strftime", "(", "\"\\nGenerated %a %d %b %Y %X UTC\\n\\n\"", ",", "time", ".", "gmtime", "(", ")", ")", ")", "toctree", ".", "append", "(", "filename", ")", "else", ":", "rst", ".", "write", "(", "section", "[", "i", "]", "+", "'\\n'", ")", "rst", ".", "write", "(", "RSTCHARS", "[", "min", "(", "i", ",", "len", "(", "RSTCHARS", ")", "-", "1", ")", "]", "*", "len", "(", "section", "[", "i", "]", ")", ")", "rst", ".", "write", "(", "'\\n\\n'", ")", "class_", "=", "section", "rst", ".", "write", "(", "'.. _cpydiff_%s:\\n\\n'", "%", "output", ".", "name", ".", "rsplit", "(", "'.'", ",", "1", ")", "[", "0", "]", ")", "rst", ".", "write", "(", "output", ".", "desc", "+", "'\\n'", ")", "rst", ".", "write", "(", "'~'", "*", "len", "(", "output", ".", "desc", ")", "+", "'\\n\\n'", ")", "if", "output", ".", "cause", "!=", "'Unknown'", ":", "rst", ".", "write", "(", "'**Cause:** '", "+", "output", ".", "cause", "+", "'\\n\\n'", ")", "if", "output", ".", "workaround", "!=", "'Unknown'", ":", "rst", ".", "write", "(", "'**Workaround:** '", "+", "output", ".", "workaround", "+", "'\\n\\n'", ")", "rst", ".", "write", "(", "'Sample code::\\n\\n'", "+", "indent", "(", "output", ".", "code", ",", "TAB", ")", "+", "'\\n'", ")", "output_cpy", "=", "indent", "(", "''", ".", "join", "(", "output", ".", "output_cpy", "[", "0", ":", "2", "]", ")", ",", "TAB", ")", ".", "rstrip", "(", ")", "output_cpy", "=", "(", "'::\\n\\n'", "if", "output_cpy", "!=", "''", "else", "''", ")", "+", "output_cpy", "output_upy", "=", "indent", "(", "''", ".", "join", "(", "output", ".", "output_upy", "[", "0", ":", "2", "]", ")", ",", "TAB", ")", ".", "rstrip", "(", ")", "output_upy", "=", "(", "'::\\n\\n'", "if", "output_upy", "!=", "''", "else", "''", ")", "+", "output_upy", "table", "=", "gen_table", "(", "[", "[", "'CPy output:'", ",", "output_cpy", "]", ",", "[", "'uPy output:'", ",", "output_upy", "]", "]", ")", "rst", ".", "write", "(", "table", ")", "template", "=", "open", "(", "INDEXTEMPLATE", ",", "'r'", ")", "index", "=", "open", "(", "DOCPATH", "+", "INDEX", ",", "'w'", ")", "index", ".", "write", "(", "HEADER", ")", "index", ".", "write", "(", "template", ".", "read", "(", ")", ")", "for", "section", "in", "INDEXPRIORITY", ":", "if", "section", "in", "toctree", ":", "index", ".", "write", "(", "indent", "(", "section", "+", "'.rst'", ",", "TAB", ")", ")", "toctree", ".", "remove", "(", "section", ")", "for", "section", "in", "toctree", ":", "index", ".", "write", "(", "indent", "(", "section", "+", "'.rst'", ",", "TAB", ")", ")" ]
creates restructured text documents to display tests
[ "creates", "restructured", "text", "documents", "to", "display", "tests" ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/gen-cpydiff.py#L156-L213
train
micropython/micropython
tools/gen-cpydiff.py
main
def main(): """ Main function """ # set search path so that test scripts find the test modules (and no other ones) os.environ['PYTHONPATH'] = TESTPATH os.environ['MICROPYPATH'] = TESTPATH files = readfiles() results = run_tests(files) gen_rst(results)
python
def main(): """ Main function """ # set search path so that test scripts find the test modules (and no other ones) os.environ['PYTHONPATH'] = TESTPATH os.environ['MICROPYPATH'] = TESTPATH files = readfiles() results = run_tests(files) gen_rst(results)
[ "def", "main", "(", ")", ":", "# set search path so that test scripts find the test modules (and no other ones)", "os", ".", "environ", "[", "'PYTHONPATH'", "]", "=", "TESTPATH", "os", ".", "environ", "[", "'MICROPYPATH'", "]", "=", "TESTPATH", "files", "=", "readfiles", "(", ")", "results", "=", "run_tests", "(", "files", ")", "gen_rst", "(", "results", ")" ]
Main function
[ "Main", "function" ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/gen-cpydiff.py#L215-L224
train
micropython/micropython
tools/pydfu.py
init
def init(): """Initializes the found DFU device so that we can program it.""" global __dev, __cfg_descr devices = get_dfu_devices(idVendor=__VID, idProduct=__PID) if not devices: raise ValueError('No DFU device found') if len(devices) > 1: raise ValueError("Multiple DFU devices found") __dev = devices[0] __dev.set_configuration() # Claim DFU interface usb.util.claim_interface(__dev, __DFU_INTERFACE) # Find the DFU configuration descriptor, either in the device or interfaces __cfg_descr = None for cfg in __dev.configurations(): __cfg_descr = find_dfu_cfg_descr(cfg.extra_descriptors) if __cfg_descr: break for itf in cfg.interfaces(): __cfg_descr = find_dfu_cfg_descr(itf.extra_descriptors) if __cfg_descr: break # Get device into idle state for attempt in range(4): status = get_status() if status == __DFU_STATE_DFU_IDLE: break elif (status == __DFU_STATE_DFU_DOWNLOAD_IDLE or status == __DFU_STATE_DFU_UPLOAD_IDLE): abort_request() else: clr_status()
python
def init(): """Initializes the found DFU device so that we can program it.""" global __dev, __cfg_descr devices = get_dfu_devices(idVendor=__VID, idProduct=__PID) if not devices: raise ValueError('No DFU device found') if len(devices) > 1: raise ValueError("Multiple DFU devices found") __dev = devices[0] __dev.set_configuration() # Claim DFU interface usb.util.claim_interface(__dev, __DFU_INTERFACE) # Find the DFU configuration descriptor, either in the device or interfaces __cfg_descr = None for cfg in __dev.configurations(): __cfg_descr = find_dfu_cfg_descr(cfg.extra_descriptors) if __cfg_descr: break for itf in cfg.interfaces(): __cfg_descr = find_dfu_cfg_descr(itf.extra_descriptors) if __cfg_descr: break # Get device into idle state for attempt in range(4): status = get_status() if status == __DFU_STATE_DFU_IDLE: break elif (status == __DFU_STATE_DFU_DOWNLOAD_IDLE or status == __DFU_STATE_DFU_UPLOAD_IDLE): abort_request() else: clr_status()
[ "def", "init", "(", ")", ":", "global", "__dev", ",", "__cfg_descr", "devices", "=", "get_dfu_devices", "(", "idVendor", "=", "__VID", ",", "idProduct", "=", "__PID", ")", "if", "not", "devices", ":", "raise", "ValueError", "(", "'No DFU device found'", ")", "if", "len", "(", "devices", ")", ">", "1", ":", "raise", "ValueError", "(", "\"Multiple DFU devices found\"", ")", "__dev", "=", "devices", "[", "0", "]", "__dev", ".", "set_configuration", "(", ")", "# Claim DFU interface", "usb", ".", "util", ".", "claim_interface", "(", "__dev", ",", "__DFU_INTERFACE", ")", "# Find the DFU configuration descriptor, either in the device or interfaces", "__cfg_descr", "=", "None", "for", "cfg", "in", "__dev", ".", "configurations", "(", ")", ":", "__cfg_descr", "=", "find_dfu_cfg_descr", "(", "cfg", ".", "extra_descriptors", ")", "if", "__cfg_descr", ":", "break", "for", "itf", "in", "cfg", ".", "interfaces", "(", ")", ":", "__cfg_descr", "=", "find_dfu_cfg_descr", "(", "itf", ".", "extra_descriptors", ")", "if", "__cfg_descr", ":", "break", "# Get device into idle state", "for", "attempt", "in", "range", "(", "4", ")", ":", "status", "=", "get_status", "(", ")", "if", "status", "==", "__DFU_STATE_DFU_IDLE", ":", "break", "elif", "(", "status", "==", "__DFU_STATE_DFU_DOWNLOAD_IDLE", "or", "status", "==", "__DFU_STATE_DFU_UPLOAD_IDLE", ")", ":", "abort_request", "(", ")", "else", ":", "clr_status", "(", ")" ]
Initializes the found DFU device so that we can program it.
[ "Initializes", "the", "found", "DFU", "device", "so", "that", "we", "can", "program", "it", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L92-L126
train
micropython/micropython
tools/pydfu.py
mass_erase
def mass_erase(): """Performs a MASS erase (i.e. erases the entire device.""" # Send DNLOAD with first byte=0x41 __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, "\x41", __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: raise Exception("DFU: erase failed") # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: raise Exception("DFU: erase failed")
python
def mass_erase(): """Performs a MASS erase (i.e. erases the entire device.""" # Send DNLOAD with first byte=0x41 __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, "\x41", __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: raise Exception("DFU: erase failed") # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: raise Exception("DFU: erase failed")
[ "def", "mass_erase", "(", ")", ":", "# Send DNLOAD with first byte=0x41", "__dev", ".", "ctrl_transfer", "(", "0x21", ",", "__DFU_DNLOAD", ",", "0", ",", "__DFU_INTERFACE", ",", "\"\\x41\"", ",", "__TIMEOUT", ")", "# Execute last command", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_DOWNLOAD_BUSY", ":", "raise", "Exception", "(", "\"DFU: erase failed\"", ")", "# Check command state", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_DOWNLOAD_IDLE", ":", "raise", "Exception", "(", "\"DFU: erase failed\"", ")" ]
Performs a MASS erase (i.e. erases the entire device.
[ "Performs", "a", "MASS", "erase", "(", "i", ".", "e", ".", "erases", "the", "entire", "device", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L148-L160
train
micropython/micropython
tools/pydfu.py
page_erase
def page_erase(addr): """Erases a single page.""" if __verbose: print("Erasing page: 0x%x..." % (addr)) # Send DNLOAD with first byte=0x41 and page address buf = struct.pack("<BI", 0x41, addr) __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, buf, __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: raise Exception("DFU: erase failed") # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: raise Exception("DFU: erase failed")
python
def page_erase(addr): """Erases a single page.""" if __verbose: print("Erasing page: 0x%x..." % (addr)) # Send DNLOAD with first byte=0x41 and page address buf = struct.pack("<BI", 0x41, addr) __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, buf, __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: raise Exception("DFU: erase failed") # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: raise Exception("DFU: erase failed")
[ "def", "page_erase", "(", "addr", ")", ":", "if", "__verbose", ":", "print", "(", "\"Erasing page: 0x%x...\"", "%", "(", "addr", ")", ")", "# Send DNLOAD with first byte=0x41 and page address", "buf", "=", "struct", ".", "pack", "(", "\"<BI\"", ",", "0x41", ",", "addr", ")", "__dev", ".", "ctrl_transfer", "(", "0x21", ",", "__DFU_DNLOAD", ",", "0", ",", "__DFU_INTERFACE", ",", "buf", ",", "__TIMEOUT", ")", "# Execute last command", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_DOWNLOAD_BUSY", ":", "raise", "Exception", "(", "\"DFU: erase failed\"", ")", "# Check command state", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_DOWNLOAD_IDLE", ":", "raise", "Exception", "(", "\"DFU: erase failed\"", ")" ]
Erases a single page.
[ "Erases", "a", "single", "page", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L163-L179
train
micropython/micropython
tools/pydfu.py
set_address
def set_address(addr): """Sets the address for the next operation.""" # Send DNLOAD with first byte=0x21 and page address buf = struct.pack("<BI", 0x21, addr) __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, buf, __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: raise Exception("DFU: set address failed") # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: raise Exception("DFU: set address failed")
python
def set_address(addr): """Sets the address for the next operation.""" # Send DNLOAD with first byte=0x21 and page address buf = struct.pack("<BI", 0x21, addr) __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, buf, __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: raise Exception("DFU: set address failed") # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: raise Exception("DFU: set address failed")
[ "def", "set_address", "(", "addr", ")", ":", "# Send DNLOAD with first byte=0x21 and page address", "buf", "=", "struct", ".", "pack", "(", "\"<BI\"", ",", "0x21", ",", "addr", ")", "__dev", ".", "ctrl_transfer", "(", "0x21", ",", "__DFU_DNLOAD", ",", "0", ",", "__DFU_INTERFACE", ",", "buf", ",", "__TIMEOUT", ")", "# Execute last command", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_DOWNLOAD_BUSY", ":", "raise", "Exception", "(", "\"DFU: set address failed\"", ")", "# Check command state", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_DOWNLOAD_IDLE", ":", "raise", "Exception", "(", "\"DFU: set address failed\"", ")" ]
Sets the address for the next operation.
[ "Sets", "the", "address", "for", "the", "next", "operation", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L182-L194
train
micropython/micropython
tools/pydfu.py
write_memory
def write_memory(addr, buf, progress=None, progress_addr=0, progress_size=0): """Writes a buffer into memory. This routine assumes that memory has already been erased. """ xfer_count = 0 xfer_bytes = 0 xfer_total = len(buf) xfer_base = addr while xfer_bytes < xfer_total: if __verbose and xfer_count % 512 == 0: print ("Addr 0x%x %dKBs/%dKBs..." % (xfer_base + xfer_bytes, xfer_bytes // 1024, xfer_total // 1024)) if progress and xfer_count % 2 == 0: progress(progress_addr, xfer_base + xfer_bytes - progress_addr, progress_size) # Set mem write address set_address(xfer_base+xfer_bytes) # Send DNLOAD with fw data chunk = min(__cfg_descr.wTransferSize, xfer_total-xfer_bytes) __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 2, __DFU_INTERFACE, buf[xfer_bytes:xfer_bytes + chunk], __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: raise Exception("DFU: write memory failed") # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: raise Exception("DFU: write memory failed") xfer_count += 1 xfer_bytes += chunk
python
def write_memory(addr, buf, progress=None, progress_addr=0, progress_size=0): """Writes a buffer into memory. This routine assumes that memory has already been erased. """ xfer_count = 0 xfer_bytes = 0 xfer_total = len(buf) xfer_base = addr while xfer_bytes < xfer_total: if __verbose and xfer_count % 512 == 0: print ("Addr 0x%x %dKBs/%dKBs..." % (xfer_base + xfer_bytes, xfer_bytes // 1024, xfer_total // 1024)) if progress and xfer_count % 2 == 0: progress(progress_addr, xfer_base + xfer_bytes - progress_addr, progress_size) # Set mem write address set_address(xfer_base+xfer_bytes) # Send DNLOAD with fw data chunk = min(__cfg_descr.wTransferSize, xfer_total-xfer_bytes) __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 2, __DFU_INTERFACE, buf[xfer_bytes:xfer_bytes + chunk], __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: raise Exception("DFU: write memory failed") # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: raise Exception("DFU: write memory failed") xfer_count += 1 xfer_bytes += chunk
[ "def", "write_memory", "(", "addr", ",", "buf", ",", "progress", "=", "None", ",", "progress_addr", "=", "0", ",", "progress_size", "=", "0", ")", ":", "xfer_count", "=", "0", "xfer_bytes", "=", "0", "xfer_total", "=", "len", "(", "buf", ")", "xfer_base", "=", "addr", "while", "xfer_bytes", "<", "xfer_total", ":", "if", "__verbose", "and", "xfer_count", "%", "512", "==", "0", ":", "print", "(", "\"Addr 0x%x %dKBs/%dKBs...\"", "%", "(", "xfer_base", "+", "xfer_bytes", ",", "xfer_bytes", "//", "1024", ",", "xfer_total", "//", "1024", ")", ")", "if", "progress", "and", "xfer_count", "%", "2", "==", "0", ":", "progress", "(", "progress_addr", ",", "xfer_base", "+", "xfer_bytes", "-", "progress_addr", ",", "progress_size", ")", "# Set mem write address", "set_address", "(", "xfer_base", "+", "xfer_bytes", ")", "# Send DNLOAD with fw data", "chunk", "=", "min", "(", "__cfg_descr", ".", "wTransferSize", ",", "xfer_total", "-", "xfer_bytes", ")", "__dev", ".", "ctrl_transfer", "(", "0x21", ",", "__DFU_DNLOAD", ",", "2", ",", "__DFU_INTERFACE", ",", "buf", "[", "xfer_bytes", ":", "xfer_bytes", "+", "chunk", "]", ",", "__TIMEOUT", ")", "# Execute last command", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_DOWNLOAD_BUSY", ":", "raise", "Exception", "(", "\"DFU: write memory failed\"", ")", "# Check command state", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_DOWNLOAD_IDLE", ":", "raise", "Exception", "(", "\"DFU: write memory failed\"", ")", "xfer_count", "+=", "1", "xfer_bytes", "+=", "chunk" ]
Writes a buffer into memory. This routine assumes that memory has already been erased.
[ "Writes", "a", "buffer", "into", "memory", ".", "This", "routine", "assumes", "that", "memory", "has", "already", "been", "erased", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L197-L233
train
micropython/micropython
tools/pydfu.py
write_page
def write_page(buf, xfer_offset): """Writes a single page. This routine assumes that memory has already been erased. """ xfer_base = 0x08000000 # Set mem write address set_address(xfer_base+xfer_offset) # Send DNLOAD with fw data __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 2, __DFU_INTERFACE, buf, __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: raise Exception("DFU: write memory failed") # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: raise Exception("DFU: write memory failed") if __verbose: print ("Write: 0x%x " % (xfer_base + xfer_offset))
python
def write_page(buf, xfer_offset): """Writes a single page. This routine assumes that memory has already been erased. """ xfer_base = 0x08000000 # Set mem write address set_address(xfer_base+xfer_offset) # Send DNLOAD with fw data __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 2, __DFU_INTERFACE, buf, __TIMEOUT) # Execute last command if get_status() != __DFU_STATE_DFU_DOWNLOAD_BUSY: raise Exception("DFU: write memory failed") # Check command state if get_status() != __DFU_STATE_DFU_DOWNLOAD_IDLE: raise Exception("DFU: write memory failed") if __verbose: print ("Write: 0x%x " % (xfer_base + xfer_offset))
[ "def", "write_page", "(", "buf", ",", "xfer_offset", ")", ":", "xfer_base", "=", "0x08000000", "# Set mem write address", "set_address", "(", "xfer_base", "+", "xfer_offset", ")", "# Send DNLOAD with fw data", "__dev", ".", "ctrl_transfer", "(", "0x21", ",", "__DFU_DNLOAD", ",", "2", ",", "__DFU_INTERFACE", ",", "buf", ",", "__TIMEOUT", ")", "# Execute last command", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_DOWNLOAD_BUSY", ":", "raise", "Exception", "(", "\"DFU: write memory failed\"", ")", "# Check command state", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_DOWNLOAD_IDLE", ":", "raise", "Exception", "(", "\"DFU: write memory failed\"", ")", "if", "__verbose", ":", "print", "(", "\"Write: 0x%x \"", "%", "(", "xfer_base", "+", "xfer_offset", ")", ")" ]
Writes a single page. This routine assumes that memory has already been erased.
[ "Writes", "a", "single", "page", ".", "This", "routine", "assumes", "that", "memory", "has", "already", "been", "erased", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L236-L258
train
micropython/micropython
tools/pydfu.py
exit_dfu
def exit_dfu(): """Exit DFU mode, and start running the program.""" # set jump address set_address(0x08000000) # Send DNLOAD with 0 length to exit DFU __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, None, __TIMEOUT) try: # Execute last command if get_status() != __DFU_STATE_DFU_MANIFEST: print("Failed to reset device") # Release device usb.util.dispose_resources(__dev) except: pass
python
def exit_dfu(): """Exit DFU mode, and start running the program.""" # set jump address set_address(0x08000000) # Send DNLOAD with 0 length to exit DFU __dev.ctrl_transfer(0x21, __DFU_DNLOAD, 0, __DFU_INTERFACE, None, __TIMEOUT) try: # Execute last command if get_status() != __DFU_STATE_DFU_MANIFEST: print("Failed to reset device") # Release device usb.util.dispose_resources(__dev) except: pass
[ "def", "exit_dfu", "(", ")", ":", "# set jump address", "set_address", "(", "0x08000000", ")", "# Send DNLOAD with 0 length to exit DFU", "__dev", ".", "ctrl_transfer", "(", "0x21", ",", "__DFU_DNLOAD", ",", "0", ",", "__DFU_INTERFACE", ",", "None", ",", "__TIMEOUT", ")", "try", ":", "# Execute last command", "if", "get_status", "(", ")", "!=", "__DFU_STATE_DFU_MANIFEST", ":", "print", "(", "\"Failed to reset device\"", ")", "# Release device", "usb", ".", "util", ".", "dispose_resources", "(", "__dev", ")", "except", ":", "pass" ]
Exit DFU mode, and start running the program.
[ "Exit", "DFU", "mode", "and", "start", "running", "the", "program", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L261-L279
train
micropython/micropython
tools/pydfu.py
consume
def consume(fmt, data, names): """Parses the struct defined by `fmt` from `data`, stores the parsed fields into a named tuple using `names`. Returns the named tuple, and the data with the struct stripped off.""" size = struct.calcsize(fmt) return named(struct.unpack(fmt, data[:size]), names), data[size:]
python
def consume(fmt, data, names): """Parses the struct defined by `fmt` from `data`, stores the parsed fields into a named tuple using `names`. Returns the named tuple, and the data with the struct stripped off.""" size = struct.calcsize(fmt) return named(struct.unpack(fmt, data[:size]), names), data[size:]
[ "def", "consume", "(", "fmt", ",", "data", ",", "names", ")", ":", "size", "=", "struct", ".", "calcsize", "(", "fmt", ")", "return", "named", "(", "struct", ".", "unpack", "(", "fmt", ",", "data", "[", ":", "size", "]", ")", ",", "names", ")", ",", "data", "[", "size", ":", "]" ]
Parses the struct defined by `fmt` from `data`, stores the parsed fields into a named tuple using `names`. Returns the named tuple, and the data with the struct stripped off.
[ "Parses", "the", "struct", "defined", "by", "fmt", "from", "data", "stores", "the", "parsed", "fields", "into", "a", "named", "tuple", "using", "names", ".", "Returns", "the", "named", "tuple", "and", "the", "data", "with", "the", "struct", "stripped", "off", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L287-L292
train
micropython/micropython
tools/pydfu.py
read_dfu_file
def read_dfu_file(filename): """Reads a DFU file, and parses the individual elements from the file. Returns an array of elements. Each element is a dictionary with the following keys: num - The element index address - The address that the element data should be written to. size - The size of the element ddata. data - The element data. If an error occurs while parsing the file, then None is returned. """ print("File: {}".format(filename)) with open(filename, 'rb') as fin: data = fin.read() crc = compute_crc(data[:-4]) elements = [] # Decode the DFU Prefix # # <5sBIB # < little endian # 5s char[5] signature "DfuSe" # B uint8_t version 1 # I uint32_t size Size of the DFU file (not including suffix) # B uint8_t targets Number of targets dfu_prefix, data = consume('<5sBIB', data, 'signature version size targets') print (" %(signature)s v%(version)d, image size: %(size)d, " "targets: %(targets)d" % dfu_prefix) for target_idx in range(dfu_prefix['targets']): # Decode the Image Prefix # # <6sBI255s2I # < little endian # 6s char[6] signature "Target" # B uint8_t altsetting # I uint32_t named bool indicating if a name was used # 255s char[255] name name of the target # I uint32_t size size of image (not incl prefix) # I uint32_t elements Number of elements in the image img_prefix, data = consume('<6sBI255s2I', data, 'signature altsetting named name ' 'size elements') img_prefix['num'] = target_idx if img_prefix['named']: img_prefix['name'] = cstring(img_prefix['name']) else: img_prefix['name'] = '' print(' %(signature)s %(num)d, alt setting: %(altsetting)s, ' 'name: "%(name)s", size: %(size)d, elements: %(elements)d' % img_prefix) target_size = img_prefix['size'] target_data, data = data[:target_size], data[target_size:] for elem_idx in range(img_prefix['elements']): # Decode target prefix # < little endian # I uint32_t element address # I uint32_t element size elem_prefix, target_data = consume('<2I', target_data, 'addr size') elem_prefix['num'] = elem_idx print(' %(num)d, address: 0x%(addr)08x, size: %(size)d' % elem_prefix) elem_size = elem_prefix['size'] elem_data = target_data[:elem_size] target_data = target_data[elem_size:] elem_prefix['data'] = elem_data elements.append(elem_prefix) if len(target_data): print("target %d PARSE ERROR" % target_idx) # Decode DFU Suffix # < little endian # H uint16_t device Firmware version # H uint16_t product # H uint16_t vendor # H uint16_t dfu 0x11a (DFU file format version) # 3s char[3] ufd 'UFD' # B uint8_t len 16 # I uint32_t crc32 dfu_suffix = named(struct.unpack('<4H3sBI', data[:16]), 'device product vendor dfu ufd len crc') print (' usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, ' 'dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % dfu_suffix) if crc != dfu_suffix['crc']: print("CRC ERROR: computed crc32 is 0x%08x" % crc) return data = data[16:] if data: print("PARSE ERROR") return return elements
python
def read_dfu_file(filename): """Reads a DFU file, and parses the individual elements from the file. Returns an array of elements. Each element is a dictionary with the following keys: num - The element index address - The address that the element data should be written to. size - The size of the element ddata. data - The element data. If an error occurs while parsing the file, then None is returned. """ print("File: {}".format(filename)) with open(filename, 'rb') as fin: data = fin.read() crc = compute_crc(data[:-4]) elements = [] # Decode the DFU Prefix # # <5sBIB # < little endian # 5s char[5] signature "DfuSe" # B uint8_t version 1 # I uint32_t size Size of the DFU file (not including suffix) # B uint8_t targets Number of targets dfu_prefix, data = consume('<5sBIB', data, 'signature version size targets') print (" %(signature)s v%(version)d, image size: %(size)d, " "targets: %(targets)d" % dfu_prefix) for target_idx in range(dfu_prefix['targets']): # Decode the Image Prefix # # <6sBI255s2I # < little endian # 6s char[6] signature "Target" # B uint8_t altsetting # I uint32_t named bool indicating if a name was used # 255s char[255] name name of the target # I uint32_t size size of image (not incl prefix) # I uint32_t elements Number of elements in the image img_prefix, data = consume('<6sBI255s2I', data, 'signature altsetting named name ' 'size elements') img_prefix['num'] = target_idx if img_prefix['named']: img_prefix['name'] = cstring(img_prefix['name']) else: img_prefix['name'] = '' print(' %(signature)s %(num)d, alt setting: %(altsetting)s, ' 'name: "%(name)s", size: %(size)d, elements: %(elements)d' % img_prefix) target_size = img_prefix['size'] target_data, data = data[:target_size], data[target_size:] for elem_idx in range(img_prefix['elements']): # Decode target prefix # < little endian # I uint32_t element address # I uint32_t element size elem_prefix, target_data = consume('<2I', target_data, 'addr size') elem_prefix['num'] = elem_idx print(' %(num)d, address: 0x%(addr)08x, size: %(size)d' % elem_prefix) elem_size = elem_prefix['size'] elem_data = target_data[:elem_size] target_data = target_data[elem_size:] elem_prefix['data'] = elem_data elements.append(elem_prefix) if len(target_data): print("target %d PARSE ERROR" % target_idx) # Decode DFU Suffix # < little endian # H uint16_t device Firmware version # H uint16_t product # H uint16_t vendor # H uint16_t dfu 0x11a (DFU file format version) # 3s char[3] ufd 'UFD' # B uint8_t len 16 # I uint32_t crc32 dfu_suffix = named(struct.unpack('<4H3sBI', data[:16]), 'device product vendor dfu ufd len crc') print (' usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, ' 'dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % dfu_suffix) if crc != dfu_suffix['crc']: print("CRC ERROR: computed crc32 is 0x%08x" % crc) return data = data[16:] if data: print("PARSE ERROR") return return elements
[ "def", "read_dfu_file", "(", "filename", ")", ":", "print", "(", "\"File: {}\"", ".", "format", "(", "filename", ")", ")", "with", "open", "(", "filename", ",", "'rb'", ")", "as", "fin", ":", "data", "=", "fin", ".", "read", "(", ")", "crc", "=", "compute_crc", "(", "data", "[", ":", "-", "4", "]", ")", "elements", "=", "[", "]", "# Decode the DFU Prefix", "#", "# <5sBIB", "# < little endian", "# 5s char[5] signature \"DfuSe\"", "# B uint8_t version 1", "# I uint32_t size Size of the DFU file (not including suffix)", "# B uint8_t targets Number of targets", "dfu_prefix", ",", "data", "=", "consume", "(", "'<5sBIB'", ",", "data", ",", "'signature version size targets'", ")", "print", "(", "\" %(signature)s v%(version)d, image size: %(size)d, \"", "\"targets: %(targets)d\"", "%", "dfu_prefix", ")", "for", "target_idx", "in", "range", "(", "dfu_prefix", "[", "'targets'", "]", ")", ":", "# Decode the Image Prefix", "#", "# <6sBI255s2I", "# < little endian", "# 6s char[6] signature \"Target\"", "# B uint8_t altsetting", "# I uint32_t named bool indicating if a name was used", "# 255s char[255] name name of the target", "# I uint32_t size size of image (not incl prefix)", "# I uint32_t elements Number of elements in the image", "img_prefix", ",", "data", "=", "consume", "(", "'<6sBI255s2I'", ",", "data", ",", "'signature altsetting named name '", "'size elements'", ")", "img_prefix", "[", "'num'", "]", "=", "target_idx", "if", "img_prefix", "[", "'named'", "]", ":", "img_prefix", "[", "'name'", "]", "=", "cstring", "(", "img_prefix", "[", "'name'", "]", ")", "else", ":", "img_prefix", "[", "'name'", "]", "=", "''", "print", "(", "' %(signature)s %(num)d, alt setting: %(altsetting)s, '", "'name: \"%(name)s\", size: %(size)d, elements: %(elements)d'", "%", "img_prefix", ")", "target_size", "=", "img_prefix", "[", "'size'", "]", "target_data", ",", "data", "=", "data", "[", ":", "target_size", "]", ",", "data", "[", "target_size", ":", "]", "for", "elem_idx", "in", "range", "(", "img_prefix", "[", "'elements'", "]", ")", ":", "# Decode target prefix", "# < little endian", "# I uint32_t element address", "# I uint32_t element size", "elem_prefix", ",", "target_data", "=", "consume", "(", "'<2I'", ",", "target_data", ",", "'addr size'", ")", "elem_prefix", "[", "'num'", "]", "=", "elem_idx", "print", "(", "' %(num)d, address: 0x%(addr)08x, size: %(size)d'", "%", "elem_prefix", ")", "elem_size", "=", "elem_prefix", "[", "'size'", "]", "elem_data", "=", "target_data", "[", ":", "elem_size", "]", "target_data", "=", "target_data", "[", "elem_size", ":", "]", "elem_prefix", "[", "'data'", "]", "=", "elem_data", "elements", ".", "append", "(", "elem_prefix", ")", "if", "len", "(", "target_data", ")", ":", "print", "(", "\"target %d PARSE ERROR\"", "%", "target_idx", ")", "# Decode DFU Suffix", "# < little endian", "# H uint16_t device Firmware version", "# H uint16_t product", "# H uint16_t vendor", "# H uint16_t dfu 0x11a (DFU file format version)", "# 3s char[3] ufd 'UFD'", "# B uint8_t len 16", "# I uint32_t crc32", "dfu_suffix", "=", "named", "(", "struct", ".", "unpack", "(", "'<4H3sBI'", ",", "data", "[", ":", "16", "]", ")", ",", "'device product vendor dfu ufd len crc'", ")", "print", "(", "' usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, '", "'dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x'", "%", "dfu_suffix", ")", "if", "crc", "!=", "dfu_suffix", "[", "'crc'", "]", ":", "print", "(", "\"CRC ERROR: computed crc32 is 0x%08x\"", "%", "crc", ")", "return", "data", "=", "data", "[", "16", ":", "]", "if", "data", ":", "print", "(", "\"PARSE ERROR\"", ")", "return", "return", "elements" ]
Reads a DFU file, and parses the individual elements from the file. Returns an array of elements. Each element is a dictionary with the following keys: num - The element index address - The address that the element data should be written to. size - The size of the element ddata. data - The element data. If an error occurs while parsing the file, then None is returned.
[ "Reads", "a", "DFU", "file", "and", "parses", "the", "individual", "elements", "from", "the", "file", ".", "Returns", "an", "array", "of", "elements", ".", "Each", "element", "is", "a", "dictionary", "with", "the", "following", "keys", ":", "num", "-", "The", "element", "index", "address", "-", "The", "address", "that", "the", "element", "data", "should", "be", "written", "to", ".", "size", "-", "The", "size", "of", "the", "element", "ddata", ".", "data", "-", "The", "element", "data", ".", "If", "an", "error", "occurs", "while", "parsing", "the", "file", "then", "None", "is", "returned", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L305-L398
train
micropython/micropython
tools/pydfu.py
get_dfu_devices
def get_dfu_devices(*args, **kwargs): """Returns a list of USB device which are currently in DFU mode. Additional filters (like idProduct and idVendor) can be passed in to refine the search. """ # convert to list for compatibility with newer pyusb return list(usb.core.find(*args, find_all=True, custom_match=FilterDFU(), **kwargs))
python
def get_dfu_devices(*args, **kwargs): """Returns a list of USB device which are currently in DFU mode. Additional filters (like idProduct and idVendor) can be passed in to refine the search. """ # convert to list for compatibility with newer pyusb return list(usb.core.find(*args, find_all=True, custom_match=FilterDFU(), **kwargs))
[ "def", "get_dfu_devices", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "# convert to list for compatibility with newer pyusb", "return", "list", "(", "usb", ".", "core", ".", "find", "(", "*", "args", ",", "find_all", "=", "True", ",", "custom_match", "=", "FilterDFU", "(", ")", ",", "*", "*", "kwargs", ")", ")" ]
Returns a list of USB device which are currently in DFU mode. Additional filters (like idProduct and idVendor) can be passed in to refine the search.
[ "Returns", "a", "list", "of", "USB", "device", "which", "are", "currently", "in", "DFU", "mode", ".", "Additional", "filters", "(", "like", "idProduct", "and", "idVendor", ")", "can", "be", "passed", "in", "to", "refine", "the", "search", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L413-L420
train
micropython/micropython
tools/pydfu.py
get_memory_layout
def get_memory_layout(device): """Returns an array which identifies the memory layout. Each entry of the array will contain a dictionary with the following keys: addr - Address of this memory segment last_addr - Last address contained within the memory segment. size - size of the segment, in bytes num_pages - number of pages in the segment page_size - size of each page, in bytes """ cfg = device[0] intf = cfg[(0, 0)] mem_layout_str = get_string(device, intf.iInterface) mem_layout = mem_layout_str.split('/') result = [] for mem_layout_index in range(1, len(mem_layout), 2): addr = int(mem_layout[mem_layout_index], 0) segments = mem_layout[mem_layout_index + 1].split(',') seg_re = re.compile(r'(\d+)\*(\d+)(.)(.)') for segment in segments: seg_match = seg_re.match(segment) num_pages = int(seg_match.groups()[0], 10) page_size = int(seg_match.groups()[1], 10) multiplier = seg_match.groups()[2] if multiplier == 'K': page_size *= 1024 if multiplier == 'M': page_size *= 1024 * 1024 size = num_pages * page_size last_addr = addr + size - 1 result.append(named((addr, last_addr, size, num_pages, page_size), "addr last_addr size num_pages page_size")) addr += size return result
python
def get_memory_layout(device): """Returns an array which identifies the memory layout. Each entry of the array will contain a dictionary with the following keys: addr - Address of this memory segment last_addr - Last address contained within the memory segment. size - size of the segment, in bytes num_pages - number of pages in the segment page_size - size of each page, in bytes """ cfg = device[0] intf = cfg[(0, 0)] mem_layout_str = get_string(device, intf.iInterface) mem_layout = mem_layout_str.split('/') result = [] for mem_layout_index in range(1, len(mem_layout), 2): addr = int(mem_layout[mem_layout_index], 0) segments = mem_layout[mem_layout_index + 1].split(',') seg_re = re.compile(r'(\d+)\*(\d+)(.)(.)') for segment in segments: seg_match = seg_re.match(segment) num_pages = int(seg_match.groups()[0], 10) page_size = int(seg_match.groups()[1], 10) multiplier = seg_match.groups()[2] if multiplier == 'K': page_size *= 1024 if multiplier == 'M': page_size *= 1024 * 1024 size = num_pages * page_size last_addr = addr + size - 1 result.append(named((addr, last_addr, size, num_pages, page_size), "addr last_addr size num_pages page_size")) addr += size return result
[ "def", "get_memory_layout", "(", "device", ")", ":", "cfg", "=", "device", "[", "0", "]", "intf", "=", "cfg", "[", "(", "0", ",", "0", ")", "]", "mem_layout_str", "=", "get_string", "(", "device", ",", "intf", ".", "iInterface", ")", "mem_layout", "=", "mem_layout_str", ".", "split", "(", "'/'", ")", "result", "=", "[", "]", "for", "mem_layout_index", "in", "range", "(", "1", ",", "len", "(", "mem_layout", ")", ",", "2", ")", ":", "addr", "=", "int", "(", "mem_layout", "[", "mem_layout_index", "]", ",", "0", ")", "segments", "=", "mem_layout", "[", "mem_layout_index", "+", "1", "]", ".", "split", "(", "','", ")", "seg_re", "=", "re", ".", "compile", "(", "r'(\\d+)\\*(\\d+)(.)(.)'", ")", "for", "segment", "in", "segments", ":", "seg_match", "=", "seg_re", ".", "match", "(", "segment", ")", "num_pages", "=", "int", "(", "seg_match", ".", "groups", "(", ")", "[", "0", "]", ",", "10", ")", "page_size", "=", "int", "(", "seg_match", ".", "groups", "(", ")", "[", "1", "]", ",", "10", ")", "multiplier", "=", "seg_match", ".", "groups", "(", ")", "[", "2", "]", "if", "multiplier", "==", "'K'", ":", "page_size", "*=", "1024", "if", "multiplier", "==", "'M'", ":", "page_size", "*=", "1024", "*", "1024", "size", "=", "num_pages", "*", "page_size", "last_addr", "=", "addr", "+", "size", "-", "1", "result", ".", "append", "(", "named", "(", "(", "addr", ",", "last_addr", ",", "size", ",", "num_pages", ",", "page_size", ")", ",", "\"addr last_addr size num_pages page_size\"", ")", ")", "addr", "+=", "size", "return", "result" ]
Returns an array which identifies the memory layout. Each entry of the array will contain a dictionary with the following keys: addr - Address of this memory segment last_addr - Last address contained within the memory segment. size - size of the segment, in bytes num_pages - number of pages in the segment page_size - size of each page, in bytes
[ "Returns", "an", "array", "which", "identifies", "the", "memory", "layout", ".", "Each", "entry", "of", "the", "array", "will", "contain", "a", "dictionary", "with", "the", "following", "keys", ":", "addr", "-", "Address", "of", "this", "memory", "segment", "last_addr", "-", "Last", "address", "contained", "within", "the", "memory", "segment", ".", "size", "-", "size", "of", "the", "segment", "in", "bytes", "num_pages", "-", "number", "of", "pages", "in", "the", "segment", "page_size", "-", "size", "of", "each", "page", "in", "bytes" ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L423-L455
train
micropython/micropython
tools/pydfu.py
list_dfu_devices
def list_dfu_devices(*args, **kwargs): """Prints a lits of devices detected in DFU mode.""" devices = get_dfu_devices(*args, **kwargs) if not devices: print("No DFU capable devices found") return for device in devices: print("Bus {} Device {:03d}: ID {:04x}:{:04x}" .format(device.bus, device.address, device.idVendor, device.idProduct)) layout = get_memory_layout(device) print("Memory Layout") for entry in layout: print(" 0x{:x} {:2d} pages of {:3d}K bytes" .format(entry['addr'], entry['num_pages'], entry['page_size'] // 1024))
python
def list_dfu_devices(*args, **kwargs): """Prints a lits of devices detected in DFU mode.""" devices = get_dfu_devices(*args, **kwargs) if not devices: print("No DFU capable devices found") return for device in devices: print("Bus {} Device {:03d}: ID {:04x}:{:04x}" .format(device.bus, device.address, device.idVendor, device.idProduct)) layout = get_memory_layout(device) print("Memory Layout") for entry in layout: print(" 0x{:x} {:2d} pages of {:3d}K bytes" .format(entry['addr'], entry['num_pages'], entry['page_size'] // 1024))
[ "def", "list_dfu_devices", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "devices", "=", "get_dfu_devices", "(", "*", "args", ",", "*", "*", "kwargs", ")", "if", "not", "devices", ":", "print", "(", "\"No DFU capable devices found\"", ")", "return", "for", "device", "in", "devices", ":", "print", "(", "\"Bus {} Device {:03d}: ID {:04x}:{:04x}\"", ".", "format", "(", "device", ".", "bus", ",", "device", ".", "address", ",", "device", ".", "idVendor", ",", "device", ".", "idProduct", ")", ")", "layout", "=", "get_memory_layout", "(", "device", ")", "print", "(", "\"Memory Layout\"", ")", "for", "entry", "in", "layout", ":", "print", "(", "\" 0x{:x} {:2d} pages of {:3d}K bytes\"", ".", "format", "(", "entry", "[", "'addr'", "]", ",", "entry", "[", "'num_pages'", "]", ",", "entry", "[", "'page_size'", "]", "//", "1024", ")", ")" ]
Prints a lits of devices detected in DFU mode.
[ "Prints", "a", "lits", "of", "devices", "detected", "in", "DFU", "mode", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L458-L473
train
micropython/micropython
tools/pydfu.py
write_elements
def write_elements(elements, mass_erase_used, progress=None): """Writes the indicated elements into the target memory, erasing as needed. """ mem_layout = get_memory_layout(__dev) for elem in elements: addr = elem['addr'] size = elem['size'] data = elem['data'] elem_size = size elem_addr = addr if progress: progress(elem_addr, 0, elem_size) while size > 0: write_size = size if not mass_erase_used: for segment in mem_layout: if addr >= segment['addr'] and \ addr <= segment['last_addr']: # We found the page containing the address we want to # write, erase it page_size = segment['page_size'] page_addr = addr & ~(page_size - 1) if addr + write_size > page_addr + page_size: write_size = page_addr + page_size - addr page_erase(page_addr) break write_memory(addr, data[:write_size], progress, elem_addr, elem_size) data = data[write_size:] addr += write_size size -= write_size if progress: progress(elem_addr, addr - elem_addr, elem_size)
python
def write_elements(elements, mass_erase_used, progress=None): """Writes the indicated elements into the target memory, erasing as needed. """ mem_layout = get_memory_layout(__dev) for elem in elements: addr = elem['addr'] size = elem['size'] data = elem['data'] elem_size = size elem_addr = addr if progress: progress(elem_addr, 0, elem_size) while size > 0: write_size = size if not mass_erase_used: for segment in mem_layout: if addr >= segment['addr'] and \ addr <= segment['last_addr']: # We found the page containing the address we want to # write, erase it page_size = segment['page_size'] page_addr = addr & ~(page_size - 1) if addr + write_size > page_addr + page_size: write_size = page_addr + page_size - addr page_erase(page_addr) break write_memory(addr, data[:write_size], progress, elem_addr, elem_size) data = data[write_size:] addr += write_size size -= write_size if progress: progress(elem_addr, addr - elem_addr, elem_size)
[ "def", "write_elements", "(", "elements", ",", "mass_erase_used", ",", "progress", "=", "None", ")", ":", "mem_layout", "=", "get_memory_layout", "(", "__dev", ")", "for", "elem", "in", "elements", ":", "addr", "=", "elem", "[", "'addr'", "]", "size", "=", "elem", "[", "'size'", "]", "data", "=", "elem", "[", "'data'", "]", "elem_size", "=", "size", "elem_addr", "=", "addr", "if", "progress", ":", "progress", "(", "elem_addr", ",", "0", ",", "elem_size", ")", "while", "size", ">", "0", ":", "write_size", "=", "size", "if", "not", "mass_erase_used", ":", "for", "segment", "in", "mem_layout", ":", "if", "addr", ">=", "segment", "[", "'addr'", "]", "and", "addr", "<=", "segment", "[", "'last_addr'", "]", ":", "# We found the page containing the address we want to", "# write, erase it", "page_size", "=", "segment", "[", "'page_size'", "]", "page_addr", "=", "addr", "&", "~", "(", "page_size", "-", "1", ")", "if", "addr", "+", "write_size", ">", "page_addr", "+", "page_size", ":", "write_size", "=", "page_addr", "+", "page_size", "-", "addr", "page_erase", "(", "page_addr", ")", "break", "write_memory", "(", "addr", ",", "data", "[", ":", "write_size", "]", ",", "progress", ",", "elem_addr", ",", "elem_size", ")", "data", "=", "data", "[", "write_size", ":", "]", "addr", "+=", "write_size", "size", "-=", "write_size", "if", "progress", ":", "progress", "(", "elem_addr", ",", "addr", "-", "elem_addr", ",", "elem_size", ")" ]
Writes the indicated elements into the target memory, erasing as needed.
[ "Writes", "the", "indicated", "elements", "into", "the", "target", "memory", "erasing", "as", "needed", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L476-L510
train
micropython/micropython
tools/pydfu.py
cli_progress
def cli_progress(addr, offset, size): """Prints a progress report suitable for use on the command line.""" width = 25 done = offset * width // size print("\r0x{:08x} {:7d} [{}{}] {:3d}% " .format(addr, size, '=' * done, ' ' * (width - done), offset * 100 // size), end="") try: sys.stdout.flush() except OSError: pass # Ignore Windows CLI "WinError 87" on Python 3.6 if offset == size: print("")
python
def cli_progress(addr, offset, size): """Prints a progress report suitable for use on the command line.""" width = 25 done = offset * width // size print("\r0x{:08x} {:7d} [{}{}] {:3d}% " .format(addr, size, '=' * done, ' ' * (width - done), offset * 100 // size), end="") try: sys.stdout.flush() except OSError: pass # Ignore Windows CLI "WinError 87" on Python 3.6 if offset == size: print("")
[ "def", "cli_progress", "(", "addr", ",", "offset", ",", "size", ")", ":", "width", "=", "25", "done", "=", "offset", "*", "width", "//", "size", "print", "(", "\"\\r0x{:08x} {:7d} [{}{}] {:3d}% \"", ".", "format", "(", "addr", ",", "size", ",", "'='", "*", "done", ",", "' '", "*", "(", "width", "-", "done", ")", ",", "offset", "*", "100", "//", "size", ")", ",", "end", "=", "\"\"", ")", "try", ":", "sys", ".", "stdout", ".", "flush", "(", ")", "except", "OSError", ":", "pass", "# Ignore Windows CLI \"WinError 87\" on Python 3.6", "if", "offset", "==", "size", ":", "print", "(", "\"\"", ")" ]
Prints a progress report suitable for use on the command line.
[ "Prints", "a", "progress", "report", "suitable", "for", "use", "on", "the", "command", "line", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L513-L525
train
micropython/micropython
tools/pydfu.py
main
def main(): """Test program for verifying this files functionality.""" global __verbose # Parse CMD args parser = argparse.ArgumentParser(description='DFU Python Util') #parser.add_argument("path", help="file path") parser.add_argument( "-l", "--list", help="list available DFU devices", action="store_true", default=False ) parser.add_argument( "-m", "--mass-erase", help="mass erase device", action="store_true", default=False ) parser.add_argument( "-u", "--upload", help="read file from DFU device", dest="path", default=False ) parser.add_argument( "-v", "--verbose", help="increase output verbosity", action="store_true", default=False ) args = parser.parse_args() __verbose = args.verbose if args.list: list_dfu_devices(idVendor=__VID, idProduct=__PID) return init() if args.mass_erase: print ("Mass erase...") mass_erase() if args.path: elements = read_dfu_file(args.path) if not elements: return print("Writing memory...") write_elements(elements, args.mass_erase, progress=cli_progress) print("Exiting DFU...") exit_dfu() return print("No command specified")
python
def main(): """Test program for verifying this files functionality.""" global __verbose # Parse CMD args parser = argparse.ArgumentParser(description='DFU Python Util') #parser.add_argument("path", help="file path") parser.add_argument( "-l", "--list", help="list available DFU devices", action="store_true", default=False ) parser.add_argument( "-m", "--mass-erase", help="mass erase device", action="store_true", default=False ) parser.add_argument( "-u", "--upload", help="read file from DFU device", dest="path", default=False ) parser.add_argument( "-v", "--verbose", help="increase output verbosity", action="store_true", default=False ) args = parser.parse_args() __verbose = args.verbose if args.list: list_dfu_devices(idVendor=__VID, idProduct=__PID) return init() if args.mass_erase: print ("Mass erase...") mass_erase() if args.path: elements = read_dfu_file(args.path) if not elements: return print("Writing memory...") write_elements(elements, args.mass_erase, progress=cli_progress) print("Exiting DFU...") exit_dfu() return print("No command specified")
[ "def", "main", "(", ")", ":", "global", "__verbose", "# Parse CMD args", "parser", "=", "argparse", ".", "ArgumentParser", "(", "description", "=", "'DFU Python Util'", ")", "#parser.add_argument(\"path\", help=\"file path\")", "parser", ".", "add_argument", "(", "\"-l\"", ",", "\"--list\"", ",", "help", "=", "\"list available DFU devices\"", ",", "action", "=", "\"store_true\"", ",", "default", "=", "False", ")", "parser", ".", "add_argument", "(", "\"-m\"", ",", "\"--mass-erase\"", ",", "help", "=", "\"mass erase device\"", ",", "action", "=", "\"store_true\"", ",", "default", "=", "False", ")", "parser", ".", "add_argument", "(", "\"-u\"", ",", "\"--upload\"", ",", "help", "=", "\"read file from DFU device\"", ",", "dest", "=", "\"path\"", ",", "default", "=", "False", ")", "parser", ".", "add_argument", "(", "\"-v\"", ",", "\"--verbose\"", ",", "help", "=", "\"increase output verbosity\"", ",", "action", "=", "\"store_true\"", ",", "default", "=", "False", ")", "args", "=", "parser", ".", "parse_args", "(", ")", "__verbose", "=", "args", ".", "verbose", "if", "args", ".", "list", ":", "list_dfu_devices", "(", "idVendor", "=", "__VID", ",", "idProduct", "=", "__PID", ")", "return", "init", "(", ")", "if", "args", ".", "mass_erase", ":", "print", "(", "\"Mass erase...\"", ")", "mass_erase", "(", ")", "if", "args", ".", "path", ":", "elements", "=", "read_dfu_file", "(", "args", ".", "path", ")", "if", "not", "elements", ":", "return", "print", "(", "\"Writing memory...\"", ")", "write_elements", "(", "elements", ",", "args", ".", "mass_erase", ",", "progress", "=", "cli_progress", ")", "print", "(", "\"Exiting DFU...\"", ")", "exit_dfu", "(", ")", "return", "print", "(", "\"No command specified\"", ")" ]
Test program for verifying this files functionality.
[ "Test", "program", "for", "verifying", "this", "files", "functionality", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/tools/pydfu.py#L528-L583
train
micropython/micropython
ports/stm32/boards/make-pins.py
parse_port_pin
def parse_port_pin(name_str): """Parses a string and returns a (port-num, pin-num) tuple.""" if len(name_str) < 3: raise ValueError("Expecting pin name to be at least 3 charcters.") if name_str[0] != 'P': raise ValueError("Expecting pin name to start with P") if name_str[1] < 'A' or name_str[1] > 'K': raise ValueError("Expecting pin port to be between A and K") port = ord(name_str[1]) - ord('A') pin_str = name_str[2:] if not pin_str.isdigit(): raise ValueError("Expecting numeric pin number.") return (port, int(pin_str))
python
def parse_port_pin(name_str): """Parses a string and returns a (port-num, pin-num) tuple.""" if len(name_str) < 3: raise ValueError("Expecting pin name to be at least 3 charcters.") if name_str[0] != 'P': raise ValueError("Expecting pin name to start with P") if name_str[1] < 'A' or name_str[1] > 'K': raise ValueError("Expecting pin port to be between A and K") port = ord(name_str[1]) - ord('A') pin_str = name_str[2:] if not pin_str.isdigit(): raise ValueError("Expecting numeric pin number.") return (port, int(pin_str))
[ "def", "parse_port_pin", "(", "name_str", ")", ":", "if", "len", "(", "name_str", ")", "<", "3", ":", "raise", "ValueError", "(", "\"Expecting pin name to be at least 3 charcters.\"", ")", "if", "name_str", "[", "0", "]", "!=", "'P'", ":", "raise", "ValueError", "(", "\"Expecting pin name to start with P\"", ")", "if", "name_str", "[", "1", "]", "<", "'A'", "or", "name_str", "[", "1", "]", ">", "'K'", ":", "raise", "ValueError", "(", "\"Expecting pin port to be between A and K\"", ")", "port", "=", "ord", "(", "name_str", "[", "1", "]", ")", "-", "ord", "(", "'A'", ")", "pin_str", "=", "name_str", "[", "2", ":", "]", "if", "not", "pin_str", ".", "isdigit", "(", ")", ":", "raise", "ValueError", "(", "\"Expecting numeric pin number.\"", ")", "return", "(", "port", ",", "int", "(", "pin_str", ")", ")" ]
Parses a string and returns a (port-num, pin-num) tuple.
[ "Parses", "a", "string", "and", "returns", "a", "(", "port", "-", "num", "pin", "-", "num", ")", "tuple", "." ]
8031b7a25c21fb864fe9dd1fa40740030be66c11
https://github.com/micropython/micropython/blob/8031b7a25c21fb864fe9dd1fa40740030be66c11/ports/stm32/boards/make-pins.py#L33-L45
train