/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/base/ast_utils.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | """Utilities to work with the Python AST.
"""
import ast
class NameLister(ast.NodeVisitor):
"""Utility class to collect the Names in an AST.
"""
def __init__(self, ctx=(ast.Load, ast.Store)):
self.names = set()
self.ctx = ctx
def visit_Name(self, node):
if isinstance(node.ctx, self.ctx):
self.names.add(node.id)
self.generic_visit(node)
class AugAssignLister(ast.NodeVisitor):
"""Utility class to collect the augmented assignments.
"""
def __init__(self):
self.names = set()
def visit_AugAssign(self, node):
if isinstance(node.target, ast.Name):
self.names.add(node.target.id)
elif isinstance(node.target, ast.Subscript):
self.names.add(node.target.value.id)
self.generic_visit(node)
class AssignLister(ast.NodeVisitor):
"""Utility class to collect the augmented assignments.
"""
def __init__(self):
self.names = set()
def visit_Assign(self, node):
for target in node.targets:
if isinstance(target, ast.Name):
self.names.add(target.id)
elif isinstance(target, ast.Subscript):
n = target.value
while not isinstance(n, ast.Name):
n = n.value
self.names.add(n.id)
elif isinstance(target, (ast.List, ast.Tuple)):
for n in target.elts:
if isinstance(n, ast.Name):
self.names.add(n.id)
self.generic_visit(node)
def get_symbols(code, ctx=(ast.Load, ast.Store)):
"""Given an AST or code string return the symbols used therein.
Parameters
----------
code: A code string or the result of an ast.parse.
ctx: The context of the names, can be one of ast.Load, ast.Store, ast.Del.
"""
if isinstance(code, str):
tree = ast.parse(code)
else:
tree = code
n = NameLister(ctx=ctx)
n.visit(tree)
return n.names
def get_aug_assign_symbols(code):
"""Given an AST or code string return the symbols that are augmented
assign.
Parameters
----------
code: A code string or the result of an ast.parse.
"""
if isinstance(code, str):
tree = ast.parse(code)
else:
tree = code
n = AugAssignLister()
n.visit(tree)
return n.names
def get_assigned(code):
"""Given an AST or code string return the symbols that are augmented
assigned or assigned.
Parameters
----------
code: A code string or the result of an ast.parse.
"""
if isinstance(code, str):
tree = ast.parse(code)
else:
tree = code
result = set()
for l in (AugAssignLister(), AssignLister()):
l.visit(tree)
result.update(l.names)
return result
def has_node(code, node):
"""Given an AST or code string returns True if the code contains
any particular node statement.
Parameters
----------
code: A code string or the result of an ast.parse.
node: A node type or tuple of node types to check for. If a tuple is passed
it returns True if any one of them is in the code.
"""
tree = ast.parse(code) if isinstance(code, str) else code
for n in ast.walk(tree):
if isinstance(n, node):
return True
return False
def has_return(code):
"""Returns True of the node has a return statement.
"""
return has_node(code, ast.Return)
|