[Lxml-checkins] r41950 - in lxml/trunk: . doc src/lxml src/lxml/tests
scoder at codespeak.net
scoder at codespeak.net
Sat Apr 7 09:13:07 CEST 2007
Author: scoder
Date: Sat Apr 7 09:13:05 2007
New Revision: 41950
Modified:
lxml/trunk/CHANGES.txt
lxml/trunk/doc/objectify.txt
lxml/trunk/src/lxml/objectify.pyx
lxml/trunk/src/lxml/tests/test_objectify.py
Log:
support '.' as identity ObjectPath
Modified: lxml/trunk/CHANGES.txt
==============================================================================
--- lxml/trunk/CHANGES.txt (original)
+++ lxml/trunk/CHANGES.txt Sat Apr 7 09:13:05 2007
@@ -8,6 +8,8 @@
Features added
--------------
+* '.' represents empty ObjectPath (identity)
+
* EXSLT RegExp support in standard XPath (not only XSLT)
* ``lxml.pyclasslookup`` module that can access the entire tree in read-only
Modified: lxml/trunk/doc/objectify.txt
==============================================================================
--- lxml/trunk/doc/objectify.txt (original)
+++ lxml/trunk/doc/objectify.txt Sat Apr 7 09:13:05 2007
@@ -372,8 +372,8 @@
>>> print find(root).tag
{ns}b
-You can also use relative paths starting with a '.' that ignore the actual
-root element and only inherit its namespace::
+You can also use relative paths starting with a '.' to ignore the actual root
+element and only inherit its namespace::
>>> find = objectify.ObjectPath(".b[1]")
>>> print find(root).tag
@@ -395,6 +395,12 @@
...
AttributeError: no such child: {other}unknown
+For convenience, a single dot represents the empty ObjectPath (identity)::
+
+ >>> find = objectify.ObjectPath(".")
+ >>> print find(root).tag
+ {ns}root
+
ObjectPath objects can be used to manipulate trees::
>>> root = objectify.Element("{ns}root")
Modified: lxml/trunk/src/lxml/objectify.pyx
==============================================================================
--- lxml/trunk/src/lxml/objectify.pyx (original)
+++ lxml/trunk/src/lxml/objectify.pyx Sat Apr 7 09:13:05 2007
@@ -1166,6 +1166,9 @@
r"(\.?)\s*(?:\{([^}]*)\})?\s*([^.{}\[\]\s]+)\s*(?:\[\s*([-0-9]+)\s*\])?",
re.U).match
+cdef object _RELATIVE_PATH_SEGMENT
+_RELATIVE_PATH_SEGMENT = (None, None, 0)
+
cdef _parseObjectPathString(path):
"""Parse object path string into a 'hrefOnameOhrefOnameOOO' string and an
index list. The index list is None if no index was used in the path.
@@ -1173,6 +1176,8 @@
cdef int has_dot
new_path = []
path = cetree.utf8(path.strip())
+ if path == '.':
+ return [_RELATIVE_PATH_SEGMENT]
path_pos = 0
while python.PyString_GET_SIZE(path) > 0:
match = __MATCH_PATH_SEGMENT(path, path_pos)
@@ -1188,7 +1193,7 @@
if python.PyList_GET_SIZE(new_path) == 0:
if has_dot:
# path '.child' => ignore root
- python.PyList_Append(new_path, (None, None, 0))
+ python.PyList_Append(new_path, _RELATIVE_PATH_SEGMENT)
elif index != 0:
raise ValueError, "index not allowed on root node"
elif not has_dot:
@@ -1234,9 +1239,7 @@
if python.PyList_GET_SIZE(new_path) == 0 and index != 0:
raise ValueError, "index not allowed on root node"
python.PyList_Append(new_path, (ns, name, index))
- if python.PyList_GET_SIZE(new_path) == 0 or \
- (python.PyList_GET_SIZE(new_path) == 1 and \
- new_path[0] == (None, None, 0)):
+ if python.PyList_GET_SIZE(new_path) == 0:
raise ValueError, "invalid path"
return new_path
Modified: lxml/trunk/src/lxml/tests/test_objectify.py
==============================================================================
--- lxml/trunk/src/lxml/tests/test_objectify.py (original)
+++ lxml/trunk/src/lxml/tests/test_objectify.py Sat Apr 7 09:13:05 2007
@@ -593,6 +593,16 @@
path = objectify.ObjectPath( "root.c1[1].c2" )
self.assertFalse(path.hasattr(root))
+ def test_object_path_dot(self):
+ root = self.XML(xml_str)
+ path = objectify.ObjectPath( "." )
+ self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
+
+ def test_object_path_dot_list(self):
+ root = self.XML(xml_str)
+ path = objectify.ObjectPath( [''] )
+ self.assertEquals(root.c1.c2.text, path(root).c1.c2.text)
+
def test_object_path_dot_root(self):
root = self.XML(xml_str)
path = objectify.ObjectPath( ".c1.c2" )
@@ -652,9 +662,7 @@
['root[2]', 'c1', 'c2'])
self.assertRaises(ValueError, objectify.ObjectPath,
- ".")
- self.assertRaises(ValueError, objectify.ObjectPath,
- [''])
+ [])
self.assertRaises(ValueError, objectify.ObjectPath,
['', '', ''])
More information about the lxml-checkins
mailing list