logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

r30649 - in lxml/branch/capi: doc src/lxml src/lxml/tests: msg#00126

Subject: r30649 - in lxml/branch/capi: doc src/lxml src/lxml/tests
Author: scoder
Date: Thu Jul 27 17:54:15 2006
New Revision: 30649

Modified:
   lxml/branch/capi/doc/objectify.txt
   lxml/branch/capi/src/lxml/objectify.pyx
   lxml/branch/capi/src/lxml/tests/test_objectify.py
Log:
doc updates, NumberElement._setValueParser() method for subclasses, renamed 
typedef() to annotate(), some cleanup

Modified: lxml/branch/capi/doc/objectify.txt
==============================================================================
--- lxml/branch/capi/doc/objectify.txt  (original)
+++ lxml/branch/capi/doc/objectify.txt  Thu Jul 27 17:54:15 2006
@@ -402,8 +402,12 @@
 .. _`namespace classes API`: namespace_extensions.html
 
 Data classes can either inherit from ``ObjectifiedElement`` directly or from
-one of the specialised classes like ``NumberElement`` or ``BoolElement``.
-Their registration uses the ``PyType`` class.
+one of the specialised classes like ``NumberElement`` or ``BoolElement``.  The
+numeric types require an initial call to ``self._setValueParser(function)`` to
+set the type conversion funtion (string -> Python type).  This call should be
+placed into the element ``_init()`` method.
+
+The registration of data classes uses the ``PyType`` class::
 
     >>> class ChristmasDate(objectify.ObjectifiedElement):
     ...     def callSanta(self):

Modified: lxml/branch/capi/src/lxml/objectify.pyx
==============================================================================
--- lxml/branch/capi/src/lxml/objectify.pyx     (original)
+++ lxml/branch/capi/src/lxml/objectify.pyx     Thu Jul 27 17:54:15 2006
@@ -383,6 +383,9 @@
 
 cdef class NumberElement(ObjectifiedElement):
     cdef object _type
+    def _setValueParser(self, function):
+        "Set the function that parses the Python value from a string."
+        self._type = function
 
     cdef _value(self):
         return self._type(textOf(self._c_node))
@@ -801,9 +804,7 @@
         return NoneElement
 
     # check for Python type hint
-    value = cetree.attributeValueFromNsName(
-        c_node, _PYTYPE_NAMESPACE, _PYTYPE_ATTRIBUTE_NAME)
-
+    value = _getPytypeAttribute(c_node)
     if value is not None:
         dict_result = python.PyDict_GetItem(_PYTYPE_DICT, value)
         if dict_result is not NULL:
@@ -828,8 +829,9 @@
     # default to string element class if type attribute is not exploitable
     return _StringElement
 
-def typedef(element_or_tree, ignore_old=True):
-    """Recursively creates pytype attributes on the elements of an XML tree.
+def annotate(element_or_tree, ignore_old=True):
+    """Recursively annotates the elements of an XML tree with 'pytype'
+    attributes.
 
     If the second argument is True (the default), current attributes will be
     ignored and replaced.  Otherwise, they will be checked and only replaced
@@ -839,7 +841,6 @@
     cdef _Document doc
     cdef int ignore
     cdef tree.xmlNode* c_node
-    cdef tree.xmlAttr* c_attr
     cdef tree.xmlNs*   c_ns
     cdef python.PyObject* dict_result
     element = cetree.rootNodeOrRaise(element_or_tree)
@@ -854,8 +855,7 @@
     value  = None
     if not ignore:
         # check that old value is valid
-        old_value = cetree.attributeValueFromNsName(
-            c_node, _PYTYPE_NAMESPACE, _PYTYPE_ATTRIBUTE_NAME)
+        old_value = _getPytypeAttribute(c_node)
         if old_value is not None:
             pytype = _PYTYPE_DICT.get(old_value)
             if pytype is not None:
@@ -901,10 +901,8 @@
 
     if pytype is None:
         # delete attribute if it exists
-        c_attr = tree.xmlHasNsProp(c_node, _PYTYPE_NAMESPACE,
-                                   _PYTYPE_ATTRIBUTE_NAME)
-        if c_attr is not NULL:
-            tree.xmlRemoveProp(c_attr)
+        cetree.delAttributeFromNsName(
+            c_node, _PYTYPE_NAMESPACE, _PYTYPE_ATTRIBUTE_NAME)
     else:
         # update or create attribute
         c_ns = cetree.findOrBuildNodeNs(doc, c_node, _PYTYPE_NAMESPACE)
@@ -912,6 +910,11 @@
                           _cstr(pytype.name))
     tree.END_FOR_EACH_ELEMENT_FROM(c_node)
 
+cdef _getPytypeAttribute(tree.xmlNode* c_element):
+    return cetree.attributeValueFromNsName(
+        c_element, _PYTYPE_NAMESPACE, _PYTYPE_ATTRIBUTE_NAME)
+
+
 
################################################################################
 # Module setup
 

Modified: lxml/branch/capi/src/lxml/tests/test_objectify.py
==============================================================================
--- lxml/branch/capi/src/lxml/tests/test_objectify.py   (original)
+++ lxml/branch/capi/src/lxml/tests/test_objectify.py   Thu Jul 27 17:54:15 2006
@@ -249,7 +249,7 @@
         self.assert_(root.b[0] <  5)
         self.assert_(5 > root.b[0])
 
-    def test_typedef(self):
+    def test_type_annotation(self):
         XML = self.etree.XML
         root = XML(u'''\
         <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
@@ -263,7 +263,7 @@
           <b xsi:type="double">5</b>
         </a>
         ''')
-        objectify.typedef(root)
+        objectify.annotate(root)
 
         child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
                         for c in root.iterchildren() ]


<Prev in Thread] Current Thread [Next in Thread>