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): ...@@ -36,6 +36,11 @@ class TestTreePath(TransformTest):
self.assertEquals(2, len(find_all(t, "//NameNode/@name"))) self.assertEquals(2, len(find_all(t, "//NameNode/@name")))
self.assertEquals(['fun', 'decorator'], 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): def test_node_path_child(self):
t = self._build_tree() t = self._build_tree()
self.assertEquals(1, len(find_all(t, "//DefNode/ReturnStatNode/NameNode"))) self.assertEquals(1, len(find_all(t, "//DefNode/ReturnStatNode/NameNode")))
......
...@@ -7,6 +7,7 @@ specific descendant or a node that holds an attribute. ...@@ -7,6 +7,7 @@ specific descendant or a node that holds an attribute.
""" """
import re import re
import sys
path_tokenizer = re.compile( path_tokenizer = re.compile(
"(" "("
...@@ -144,14 +145,21 @@ def handle_attribute(next, token): ...@@ -144,14 +145,21 @@ def handle_attribute(next, token):
else: else:
if token[0] == '=': if token[0] == '=':
value = parse_path_value(next) value = parse_path_value(next)
name_path = name.split('.') 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: if value is None:
def select(result): def select(result):
for node in result: for node in result:
try: try:
attr_value = node attr_value = readattr(node)
for attr in name_path:
attr_value = getattr(attr_value, attr)
except AttributeError: except AttributeError:
continue continue
if attr_value is not None: if attr_value is not None:
...@@ -160,9 +168,7 @@ def handle_attribute(next, token): ...@@ -160,9 +168,7 @@ def handle_attribute(next, token):
def select(result): def select(result):
for node in result: for node in result:
try: try:
attr_value = node attr_value = readattr(node)
for attr in name_path:
attr_value = getattr(attr_value, attr)
except AttributeError: except AttributeError:
continue continue
if attr_value == value: 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