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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
""" Classes used to represent assignment statements. """
# Standard library imports.
import compiler
from compiler.visitor import ASTVisitor
# Enthought library imports.
from enthought.traits.api import Any, Bool, HasTraits, Instance, Int, List, Str
class Assign(HasTraits):
""" An assignment statement. """
#### 'Assign' interface ###################################################
# The namespace that the assignment statement is in.
namespace = Instance(
'enthought.envisage.developer.code_browser.namespace.Namespace'
)
# The line number within the module at which the assignment statement
# appears.
lineno = Int
# The names being assigned to (in Python there can be more than one).
targets = List(Str)
# The expression being assigned to the targets (an AST node).
expr = Any
# We only care about assignments to trait types, not literals or
# expressions etc.
source = Str
# Is this a trait assignment?
is_trait = Bool(False)
###########################################################################
# 'object' interface.
###########################################################################
def __str__(self):
""" Returns an informal string representation of the object. """
return 'Assign %s at %d' % (', '.join(self.targets), self.lineno)
class AssignFactory(HasTraits):
""" A factory for assignment statements. """
###########################################################################
# 'AssignFactory' interface.
###########################################################################
def from_ast(self, namespace, node):
""" Creates an assignment statement from an AST node. """
# Create a new assignment statement.
assign = Assign(
namespace = namespace,
lineno = node.lineno,
expr = node.expr
)
# Walk the AST picking out the things we care about!
compiler.walk(node, AssignVisitor(assign))
return assign
class AssignVisitor(ASTVisitor):
""" An AST visitor for assigment statements. """
###########################################################################
# 'object' interface.
###########################################################################
def __init__(self, assign):
""" Creates a new visitor. """
self.assign = assign
return
###########################################################################
# 'ASTVisitor' interface.
###########################################################################
def visitAssName(self, node):
""" Visits an assignment node. """
self.assign.targets.append(node.name)
return
def visitCallFunc(self, node):
""" Visits a function call node. """
function_name = self._get_name(node.node)
self.assign.source = function_name
return
def visitName(self, node):
""" Visits a name node. """
self.assign.source = node.name
return
def visitGetattr(self, node):
""" Visits a getattr node. """
self.assign.source = self._get_name(node)
return
###########################################################################
# Private interface.
###########################################################################
def _get_name(self, node):
""" Returns the (possibly dotted) name from a node. """
# fixme: Work out when node can be none here!!!!! I think it is safe
# to ignore it in terms of working out what is a trait, but it would
# be nice to know what is going on ;^)
if node is not None:
if isinstance(node, basestring):
name = node
elif not hasattr(node, 'getType'):
name = ''
elif node.getType() == compiler.ast.Name:
name = node.name
else:
names = [self._get_name(child) for child in node.getChildren()]
name = '.'.join(names)
else:
name = ''
return name
#### EOF ######################################################################
|