Commit 6092931e authored by Stefan Behnel's avatar Stefan Behnel

test case for dotted attribute names, some cleanup

parent 20f13f93
......@@ -36,6 +36,11 @@ class TestTreePath(TransformTest):
self.assertEquals(2, len(find_all(t, "//NameNode/@name")))
self.assertEquals(['fun', 'decorator'], find_all(t, "//NameNode/@name"))
def test_node_path_attribute_dotted(self):
t = self._build_tree()
self.assertEquals(1, len(find_all(t, "//ReturnStatNode/@value.name")))
self.assertEquals(['fun'], find_all(t, "//ReturnStatNode/@value.name"))
def test_node_path_child(self):
t = self._build_tree()
self.assertEquals(1, len(find_all(t, "//DefNode/ReturnStatNode/NameNode")))
......
......@@ -7,6 +7,7 @@ specific descendant or a node that holds an attribute.
"""
import re
import sys
path_tokenizer = re.compile(
"("
......@@ -144,14 +145,21 @@ def handle_attribute(next, token):
else:
if token[0] == '=':
value = parse_path_value(next)
if sys.version_info >= (2,6) or (sys.version_info >= (2,4) and '.' not in name):
import operator
readattr = operator.attrgetter(name)
else:
name_path = name.split('.')
def readattr(node):
attr_value = node
for attr in name_path:
attr_value = getattr(attr_value, attr)
return attr_value
if value is None:
def select(result):
for node in result:
try:
attr_value = node
for attr in name_path:
attr_value = getattr(attr_value, attr)
attr_value = readattr(node)
except AttributeError:
continue
if attr_value is not None:
......@@ -160,9 +168,7 @@ def handle_attribute(next, token):
def select(result):
for node in result:
try:
attr_value = node
for attr in name_path:
attr_value = getattr(attr_value, attr)
attr_value = readattr(node)
except AttributeError:
continue
if attr_value == value:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment