[Lxml-checkins] r44176 - in lxml/branch/lxml-1.3: . doc src/lxml src/lxml/tests
scoder at codespeak.net
scoder at codespeak.net
Tue Jun 12 18:20:49 CEST 2007
Author: scoder
Date: Tue Jun 12 18:20:49 2007
New Revision: 44176
Modified:
lxml/branch/lxml-1.3/CHANGES.txt
lxml/branch/lxml-1.3/doc/objectify.txt
lxml/branch/lxml-1.3/src/lxml/objectify.pyx
lxml/branch/lxml-1.3/src/lxml/sax.py
lxml/branch/lxml-1.3/src/lxml/tests/test_objectify.py
Log:
merged in 41651:41955 from trunk
Modified: lxml/branch/lxml-1.3/CHANGES.txt
==============================================================================
--- lxml/branch/lxml-1.3/CHANGES.txt (original)
+++ lxml/branch/lxml-1.3/CHANGES.txt Tue Jun 12 18:20:49 2007
@@ -8,6 +8,10 @@
Features added
--------------
+* Support for custom Element class instantiation in lxml.sax
+
+* '.' represents empty ObjectPath (identity)
+
* ``Element.values()`` to accompany the existing ``.keys()`` and ``.items()``
Bugs fixed
Modified: lxml/branch/lxml-1.3/doc/objectify.txt
==============================================================================
--- lxml/branch/lxml-1.3/doc/objectify.txt (original)
+++ lxml/branch/lxml-1.3/doc/objectify.txt Tue Jun 12 18:20:49 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/branch/lxml-1.3/src/lxml/objectify.pyx
==============================================================================
--- lxml/branch/lxml-1.3/src/lxml/objectify.pyx (original)
+++ lxml/branch/lxml-1.3/src/lxml/objectify.pyx Tue Jun 12 18:20:49 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/branch/lxml-1.3/src/lxml/sax.py
==============================================================================
--- lxml/branch/lxml-1.3/src/lxml/sax.py (original)
+++ lxml/branch/lxml-1.3/src/lxml/sax.py Tue Jun 12 18:20:49 2007
@@ -13,12 +13,15 @@
class ElementTreeContentHandler(object, ContentHandler):
"""Build an lxml ElementTree from SAX events.
"""
- def __init__(self):
+ def __init__(self, makeelement=None):
self._root = None
self._element_stack = []
self._default_ns = None
self._ns_mapping = { None : [None] }
self._new_mappings = {}
+ if makeelement is None:
+ makeelement = Element
+ self._makeelement = makeelement
def _get_etree(self):
"Contains the generated ElementTree after parsing is finished."
@@ -77,7 +80,8 @@
element_stack = self._element_stack
if self._root is None:
- element = self._root = Element(el_name, attrs, self._new_mappings)
+ element = self._root = \
+ self._makeelement(el_name, attrs, self._new_mappings)
else:
element = SubElement(element_stack[-1], el_name,
attrs, self._new_mappings)
Modified: lxml/branch/lxml-1.3/src/lxml/tests/test_objectify.py
==============================================================================
--- lxml/branch/lxml-1.3/src/lxml/tests/test_objectify.py (original)
+++ lxml/branch/lxml-1.3/src/lxml/tests/test_objectify.py Tue Jun 12 18:20:49 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