|
| 1 | +# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Functions for checking AST nodes for matches.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +import ast |
| 17 | + |
| 18 | + |
| 19 | +def matches_any(node, name_to_namespaces_dict): |
| 20 | + """Determines if the ``ast.Call`` node matches any of the provided names and namespaces. |
| 21 | +
|
| 22 | + Args: |
| 23 | + node (ast.Call): a node that represents a function call. For more, |
| 24 | + see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 25 | + name_to_namespaces_dict (dict[str, tuple]): a mapping of names to appropriate namespaces. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + bool: if the node matches any of the names and namespaces. |
| 29 | + """ |
| 30 | + return any( |
| 31 | + matches_name_or_namespaces(node, name, namespaces) |
| 32 | + for name, namespaces in name_to_namespaces_dict.items() |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def matches_name_or_namespaces(node, name, namespaces): |
| 37 | + """Determines if the ``ast.Call`` node matches the function name in the right namespace. |
| 38 | +
|
| 39 | + Args: |
| 40 | + node (ast.Call): a node that represents a function call. For more, |
| 41 | + see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 42 | + name (str): the function name. |
| 43 | + namespaces (tuple): the possible namespaces to match to. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + bool: if the node matches the name and any of the namespaces. |
| 47 | + """ |
| 48 | + if matches_name(node, name): |
| 49 | + return True |
| 50 | + |
| 51 | + if not matches_attr(node, name): |
| 52 | + return False |
| 53 | + |
| 54 | + return any(matches_namespace(node, namespace) for namespace in namespaces) |
| 55 | + |
| 56 | + |
| 57 | +def matches_name(node, name): |
| 58 | + """Determines if the ``ast.Call`` node points to an ``ast.Name`` node with a matching name. |
| 59 | +
|
| 60 | + Args: |
| 61 | + node (ast.Call): a node that represents a function call. For more, |
| 62 | + see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 63 | + name (str): the function name. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + bool: if ``node.func`` is an ``ast.Name`` node with a matching name. |
| 67 | + """ |
| 68 | + return isinstance(node.func, ast.Name) and node.func.id == name |
| 69 | + |
| 70 | + |
| 71 | +def matches_attr(node, name): |
| 72 | + """Determines if the ``ast.Call`` node points to an ``ast.Attribute`` node with a matching name. |
| 73 | +
|
| 74 | + Args: |
| 75 | + node (ast.Call): a node that represents a function call. For more, |
| 76 | + see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 77 | + name (str): the function name. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + bool: if ``node.func`` is an ``ast.Attribute`` node with a matching name. |
| 81 | + """ |
| 82 | + return isinstance(node.func, ast.Attribute) and node.func.attr == name |
| 83 | + |
| 84 | + |
| 85 | +def matches_namespace(node, namespace): |
| 86 | + """Determines if the ``ast.Call`` node corresponds to a matching namespace. |
| 87 | +
|
| 88 | + Args: |
| 89 | + node (ast.Call): a node that represents a function call. For more, |
| 90 | + see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 91 | + namespace (str): the namespace. |
| 92 | +
|
| 93 | + Returns: |
| 94 | + bool: if the node's namespaces matches the given namespace. |
| 95 | + """ |
| 96 | + names = namespace.split(".") |
| 97 | + name, value = names.pop(), node.func.value |
| 98 | + while isinstance(value, ast.Attribute) and len(names) > 0: |
| 99 | + if value.attr != name: |
| 100 | + return False |
| 101 | + name, value = names.pop(), value.value |
| 102 | + |
| 103 | + return isinstance(value, ast.Name) and value.id == name |
0 commit comments