Repository: boto/boto3
File: boto3/docs/utils.py (Python)

Code before:
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import inspect
import re
from botocore.compat import six

TARGET_COMPONENT_RE = re.compile(r'[^.\[\]]+(?![^\[]*\])')


def get_resource_ignore_params(params):
    """Helper method to determine which parameters to ignore for actions

    :returns: A list of the parameter names that does not need to be
        included in a resource's method call for documentation purposes.
    """
    ignore_params = []
    for param in params:
        components = TARGET_COMPONENT_RE.findall(param.target)
        ignore_params.append(components[0])
    return ignore_params


def is_resource_action(action_handle):
    if six.PY3:
        return inspect.isfunction(action_handle)
    else:
        return inspect.ismethod(action_handle)


def get_resource_public_actions(resource_class):
    resource_class_members = inspect.getmembers(resource_class)
    resource_methods = {}
    for name, member in resource_class_members:
        if not name.startswith('_'):
            if not name[0].isupper():
                if not name.startswith('wait_until'):

Review comment (bug):
`"foo[]bar"[].baz`.  Basically this is assuming that these characters in the regex cannot appear anywhere except in the places we want to ignore them (i.e `foo[]`).  This is likely ok for most, if not all the cases we'll ever need to consider but is not 100% correct.

This function is basically trying to parse a JMESPath expression and select out the first field name using a regex.  Rather than use this regex based approach which has edge cases and isn't 100% correct, why not use the existing JMESPath parser and not have any edge cases?


Code after:
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import inspect
import re

import jmespath

from botocore.compat import six


def get_resource_ignore_params(params):
    """Helper method to determine which parameters to ignore for actions

    :returns: A list of the parameter names that does not need to be
        included in a resource's method call for documentation purposes.
    """
    ignore_params = []
    for param in params:
        result = jmespath.compile(param.target)
        current = result.parsed
        # Use JMESPath to find the left most element in the target expression
        # which will be the parameter to ignore in the action call.
        while current['children']:
            current = current['children'][0]
        # Make sure the parameter we are about to ignore is a field.
        # If it is not, we should ignore the result to avoid false positives.
        if current['type'] == 'field':
            ignore_params.append(current['value'])
    return ignore_params


def is_resource_action(action_handle):
    if six.PY3:
        return inspect.isfunction(action_handle)
    else:
        return inspect.ismethod(action_handle)