From: <fel...@us...> - 2005-06-05 23:06:05
|
Author: felixwiemann Date: 2005-06-06 01:05:51 +0200 (Mon, 06 Jun 2005) New Revision: 3438 Modified: trunk/docutils/docutils/nodes.py trunk/docutils/test/test_nodes.py Log: added possibility to pass node classes to node.traverse() Modified: trunk/docutils/docutils/nodes.py =================================================================== --- trunk/docutils/docutils/nodes.py 2005-06-05 21:30:29 UTC (rev 3437) +++ trunk/docutils/docutils/nodes.py 2005-06-05 23:05:51 UTC (rev 3438) @@ -23,11 +23,14 @@ __docformat__ = 'reStructuredText' +from __future__ import nested_scopes + import sys import os import re import copy import warnings +import inspect import xml.dom.minidom from types import IntType, SliceType, StringType, UnicodeType, \ TupleType, ListType @@ -175,6 +178,11 @@ * the siblings of the parent (if ascend is true) and their descendants (if also descend is true), and so on + If `condition` is not None, the iterable contains only nodes + for which ``condition(node)`` is true. If `condition` is a + node class ``cls``, it is equivalent to ``lambda n: + isinstance(n, cls)``. + If ascend is true, assume siblings to be true as well. For example, given the following tree:: @@ -198,6 +206,9 @@ r = [] if ascend: siblings=1 + if inspect.isclass(condition) and issubclass(condition, Node): + node_class = condition + condition = lambda n: isinstance(n, node_class) if include_self and (condition is None or condition(self)): r.append(self) if descend and len(self.children): Modified: trunk/docutils/test/test_nodes.py =================================================================== --- trunk/docutils/test/test_nodes.py 2005-06-05 21:30:29 UTC (rev 3437) +++ trunk/docutils/test/test_nodes.py 2005-06-05 23:05:51 UTC (rev 3438) @@ -163,6 +163,7 @@ self.testlist.append(e[0][1][0]) self.assertEquals(list(e[0].traverse(condition=self.not_in_testlist)), [e[0]]) + self.assertEquals(list(e.traverse(nodes.TextElement)), [e[0][1]]) def test_next_node(self): e = nodes.Element() |