[Lxml-checkins] r46232 - in lxml/trunk: . doc src/lxml

scoder at codespeak.net scoder at codespeak.net
Sat Sep 1 10:59:47 CEST 2007


Author: scoder
Date: Sat Sep  1 10:59:45 2007
New Revision: 46232

Modified:
   lxml/trunk/CHANGES.txt
   lxml/trunk/doc/objectify.txt
   lxml/trunk/src/lxml/objectify.pyx
Log:
made annotation in objectify.ElementMaker optional through 'annotate' kw arg

Modified: lxml/trunk/CHANGES.txt
==============================================================================
--- lxml/trunk/CHANGES.txt	(original)
+++ lxml/trunk/CHANGES.txt	Sat Sep  1 10:59:45 2007
@@ -8,6 +8,10 @@
 Features added
 --------------
 
+* Reimplemented ``objectify.E`` for better performance and improved
+  integration with objectify.  Provides extended type support based on
+  registered PyTypes.
+
 * XSLT objects now support deep copying
 
 * New ``makeSubElement()`` C-API function that allows creating a new
@@ -35,8 +39,7 @@
 
 * Schematron validation (incomplete in libxml2)
 
-* Extended type support for ``objectify.E`` based on registered PyTypes.
-  Supports an additional argument to ``PyType()`` that takes a conversion
+* Additional ``stringify`` argument to ``PyType()`` that takes a conversion
   function to strings to support setting text values from arbitrary types.
 
 * Entity support through an ``Entity`` factory and element classes.  XML

Modified: lxml/trunk/doc/objectify.txt
==============================================================================
--- lxml/trunk/doc/objectify.txt	(original)
+++ lxml/trunk/doc/objectify.txt	Sat Sep  1 10:59:45 2007
@@ -299,17 +299,32 @@
 
     >>> ROOT = objectify.E.root
     >>> TITLE = objectify.E.title
-    >>> TYPE = objectify.E.type
+    >>> HOWMANY = getattr(objectify.E, "how-many")
 
     >>> root = ROOT(
     ...   TITLE("The title"),
-    ...   TYPE(5)
+    ...   HOWMANY(5)
     ... )
 
     >>> print etree.tostring(root, pretty_print=True)
     <root xmlns:py="http://codespeak.net/lxml/objectify/pytype">
       <title py:pytype="str">The title</title>
-      <type py:pytype="int">5</type>
+      <how-many py:pytype="int">5</how-many>
+    </root>
+
+``objectify.E`` is an instance of ``objectify.ElementMaker``.  By default, it
+creates pytype annotated Elements without a namespace.  You can switch off the
+pytype annotation by passing False to the ``annotate`` keyword argument of the
+constructor.  You can also pass a default namespace and an ``nsmap``::
+
+    >>> myE = objectify.ElementMaker(annotate=False,
+    ...           namespace="http://my/ns", nsmap={None : "http://my/ns"})
+
+    >>> root = myE.root( myE.someint(2) )
+
+    >>> print etree.tostring(root, pretty_print=True)
+    <root xmlns="http://my/ns">
+      <someint>2</someint>
     </root>
 
 

Modified: lxml/trunk/src/lxml/objectify.pyx
==============================================================================
--- lxml/trunk/src/lxml/objectify.pyx	(original)
+++ lxml/trunk/src/lxml/objectify.pyx	Sat Sep  1 10:59:45 2007
@@ -1047,7 +1047,9 @@
     cdef object _makeelement
     cdef object _namespace
     cdef object _nsmap
-    def __init__(self, namespace=None, nsmap=None, makeelement=None):
+    cdef int _annotate
+    def __init__(self, namespace=None, nsmap=None, annotate=True,
+                 makeelement=None):
         if nsmap is None:
             nsmap = _DEFAULT_NSMAP
         self._nsmap = nsmap
@@ -1055,6 +1057,7 @@
             self._namespace = None
         else:
             self._namespace = "{%s}" % namespace
+        self._annotate = bool(annotate)
         if makeelement is not None:
             assert callable(makeelement)
             self._makeelement = makeelement
@@ -1068,6 +1071,7 @@
         element_maker = NEW_ELEMENT_MAKER(_ObjectifyElementMakerCaller)
         element_maker._tag = tag
         element_maker._nsmap = self._nsmap
+        element_maker._annotate = self._annotate
         element_maker._element_factory = self._makeelement
         return element_maker
 
@@ -1075,6 +1079,7 @@
     cdef object _tag
     cdef object _nsmap
     cdef object _element_factory
+    cdef int _annotate
 
     def __call__(self, *children, **attrib):
         cdef _ObjectifyElementMakerCaller elementMaker
@@ -1088,6 +1093,7 @@
         else:
             element = self._element_factory(self._tag, attrib, self._nsmap)
 
+        pytype_name = None
         has_children = 0
         has_string_value = 0
         for child in children:
@@ -1113,7 +1119,7 @@
                 has_children = 1
             else:
                 if pytype_name is not None:
-                    # concatenation makes the result a string
+                    # concatenation always makes the result a string
                     has_string_value = 1
                 pytype_name = _typename(child)
                 pytype = python.PyDict_GetItem(_PYTYPE_DICT, pytype_name)
@@ -1124,12 +1130,11 @@
                     child = str(child)
                     _add_text(element, child)
 
-        if not has_children:
+        if self._annotate and not has_children:
             if has_string_value:
                 cetree.setAttributeValue(element, PYTYPE_ATTRIBUTE, "str")
             elif pytype_name is not None:
-                cetree.setAttributeValue(element, PYTYPE_ATTRIBUTE,
-                                         pytype_name)
+                cetree.setAttributeValue(element, PYTYPE_ATTRIBUTE, pytype_name)
 
         return element
 
@@ -1911,6 +1916,10 @@
 _parse = etree.parse
 
 def parse(f, parser=None):
+    """Parse a file or file-like object with the objectify parser.
+
+    You can pass a different parser as second argument.
+    """
     if parser is None:
         parser = objectify_parser
     return _parse(f, parser)


More information about the lxml-checkins mailing list