[CalendarServer-changes] [12499] twext/trunk/twext/who/xml.py

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:22:30 PDT 2014


Revision: 12499
          http://trac.calendarserver.org//changeset/12499
Author:   wsanchez at apple.com
Date:     2014-01-30 19:47:26 -0800 (Thu, 30 Jan 2014)
Log Message:
-----------
Handle boolean values.

Modified Paths:
--------------
    twext/trunk/twext/who/xml.py

Modified: twext/trunk/twext/who/xml.py
===================================================================
--- twext/trunk/twext/who/xml.py	2014-01-31 02:00:15 UTC (rev 12498)
+++ twext/trunk/twext/who/xml.py	2014-01-31 03:47:26 UTC (rev 12499)
@@ -35,7 +35,7 @@
     tostring as etreeToString, Element as XMLElement,
 )
 
-from twisted.python.constants import Values, ValueConstant
+from twisted.python.constants import NamedConstant, Values, ValueConstant
 from twisted.internet.defer import fail
 
 from .idirectory import (
@@ -72,6 +72,13 @@
     XML element names.
     """
 
+    # Booleans
+
+    true = ValueConstant(u"true")
+    false = ValueConstant(u"false")
+
+    # Schema hierarchy
+
     directory = ValueConstant(u"directory")
     record    = ValueConstant(u"record")
 
@@ -319,17 +326,40 @@
                     unknownFieldElements.add(fieldNode.tag)
                 continue
 
-            vType = self.fieldName.valueType(fieldName)
+            valueType = self.fieldName.valueType(fieldName)
 
-            if vType in (unicode, UUID):
-                value = vType(fieldNode.text)
+            if valueType in (unicode, UUID):
+                value = valueType(fieldNode.text)
+
+            elif valueType is bool:
+                boolElement = self._constantElement(fieldNode)
+
+                if boolElement is Element.true:
+                    value = True
+
+                elif boolElement is Element.false:
+                    value = False
+
+                else:
+                    raise ParseError(
+                        "Child element {0} of element {0} is not a boolean."
+                        .format(boolElement.value, fieldNode.tag)
+                    )
+
+                print(value)
+
+            elif valueType is NamedConstant:
+                raise NotImplementedError("named constant")
+
             else:
                 raise AssertionError(
                     "Unknown value type {0} for field {1}".format(
-                        vType, fieldName
+                        valueType, fieldName
                     )
                 )
 
+            assert value is not None
+
             if self.fieldName.isMultiValue(fieldName):
                 values = fields.setdefault(fieldName, [])
                 values.append(value)
@@ -347,6 +377,47 @@
         return uidNode.text
 
 
+    def _constantElement(self, node):
+        """
+        Find the name of the single empty node in a given node.
+
+        @param node: a node
+        @type node: L{XMLElement}
+
+        @return: L{unicode}
+        """
+        child = None
+
+        for c in node:
+            if child is not None:
+                raise ParseError(
+                    "Element {0} may only have a single child element, "
+                    "not: {1}"
+                    .format(node.tag, [c.tag for c in node])
+                )
+            child = c
+
+        if child is None:
+            raise ParseError(
+                "Element {0} must contain a single empty child element."
+            )
+
+        if child.text:
+            raise ParseError(
+                "Child element {0} of element {1} may not have text: {2!r}"
+                .format(child.tag, node.tag, child.text)
+            )
+
+        try:
+            return self.element.lookupByValue(child.tag)
+        except ValueError:
+            raise ParseError(
+                "Unknown child element {0} of element {1}."
+                .format(child.tag, node.tag)
+            )
+
+
+
     def flush(self):
         BaseDirectoryService.flush(self)
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/ad080309/attachment.html>


More information about the calendarserver-changes mailing list