[CalendarServer-changes] [5120] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon Feb 15 11:19:19 PST 2010


Revision: 5120
          http://trac.macosforge.org/projects/calendarserver/changeset/5120
Author:   cdaboo at apple.com
Date:     2010-02-15 11:19:19 -0800 (Mon, 15 Feb 2010)
Log Message:
-----------
Handle changing an non-existent XML node. Add some unit tests for xmlutil.py.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/principals.py
    CalendarServer/trunk/twistedcaldav/directory/augment.py
    CalendarServer/trunk/twistedcaldav/xmlutil.py

Added Paths:
-----------
    CalendarServer/trunk/twistedcaldav/test/test_xmlutil.py

Modified: CalendarServer/trunk/calendarserver/tools/principals.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/principals.py	2010-02-15 19:18:24 UTC (rev 5119)
+++ CalendarServer/trunk/calendarserver/tools/principals.py	2010-02-15 19:19:19 UTC (rev 5120)
@@ -473,7 +473,7 @@
         # removing the record before adding it.
         (yield augment.AugmentService.removeAugmentRecords([record.guid]))
 
-        (yield augment.AugmentService.addAugmentRecords([aug], update=False))
+        (yield augment.AugmentService.addAugmentRecords([aug]))
 
 def action_getAutoSchedule(principal):
     autoSchedule = principal.getAutoSchedule()

Modified: CalendarServer/trunk/twistedcaldav/directory/augment.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/augment.py	2010-02-15 19:18:24 UTC (rev 5119)
+++ CalendarServer/trunk/twistedcaldav/directory/augment.py	2010-02-15 19:19:19 UTC (rev 5120)
@@ -273,20 +273,22 @@
 
         # Make sure UID is present
         changed = False
-        for child in augments_node.getchildren():
+        for record_node in augments_node.getchildren():
             
-            if child.tag != xmlaugmentsparser.ELEMENT_RECORD:
+            if record_node.tag != xmlaugmentsparser.ELEMENT_RECORD:
                 continue
     
-            uid = child.find(xmlaugmentsparser.ELEMENT_UID).text
+            uid = record_node.find(xmlaugmentsparser.ELEMENT_UID).text
             if uid in recordMap:
                 # Modify record
                 record = recordMap[uid]
-                child.find(xmlaugmentsparser.ELEMENT_ENABLE).text = "true" if record.enabled else "false"
-                child.find(xmlaugmentsparser.ELEMENT_HOSTEDAT).text = record.hostedAt
-                child.find(xmlaugmentsparser.ELEMENT_ENABLECALENDAR).text = "true" if record.enabledForCalendaring else "false"
-                child.find(xmlaugmentsparser.ELEMENT_ENABLEADDRESSBOOK).text = "true" if record.enabledForAddressBooks else "false"
-                child.find(xmlaugmentsparser.ELEMENT_AUTOSCHEDULE).text = "true" if record.autoSchedule else "false"
+                del record_node.getchildren()[:]
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_UID, record.uid)
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLE, "true" if record.enabled else "false")
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_HOSTEDAT, record.hostedAt)
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLECALENDAR, "true" if record.enabledForCalendaring else "false")
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLEADDRESSBOOK, "true" if record.enabledForAddressBooks else "false")
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_AUTOSCHEDULE, "true" if record.autoSchedule else "false")
                 changed = True
         
         

Added: CalendarServer/trunk/twistedcaldav/test/test_xmlutil.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/test_xmlutil.py	                        (rev 0)
+++ CalendarServer/trunk/twistedcaldav/test/test_xmlutil.py	2010-02-15 19:19:19 UTC (rev 5120)
@@ -0,0 +1,141 @@
+##
+# Copyright (c) 2005-2007 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+import twistedcaldav.test.util
+from cStringIO import StringIO
+from twistedcaldav.xmlutil import readXML, writeXML, addSubElement,\
+    changeSubElementText
+
+class XMLUtil(twistedcaldav.test.util.TestCase):
+    """
+    XML Util tests
+    """
+    
+    data1 = """<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE test SYSTEM "test.dtd">
+<test>
+  <help>me</help>
+  <nesting>
+    <nested/>
+  </nesting>
+</test>
+"""
+
+    data2 = """Not XML!"""
+
+    data3 = """<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE test SYSTEM "test.dtd">
+
+<test>
+  <help>me</help>
+  <nesting>
+    <nested />
+  </nesting>
+</test>
+"""
+
+    data4 = """<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE test SYSTEM "test.dtd">
+
+<test>
+  <help>me</help>
+  <nesting>
+    <nested />
+  </nesting>
+  <added>added text</added>
+</test>
+"""
+
+    data5 = """<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE test SYSTEM "test.dtd">
+
+<test>
+  <help>changed text</help>
+  <nesting>
+    <nested />
+  </nesting>
+</test>
+"""
+
+    data6 = """<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE test SYSTEM "test.dtd">
+
+<test>
+  <help>me</help>
+  <nesting>
+    <nested />
+  </nesting>
+  <new>new text</new>
+</test>
+"""
+
+    def _checkXML(self, node, data):
+        xmlfile = self.mktemp()
+        writeXML(xmlfile, node)
+        newdata = open(xmlfile).read()
+        self.assertEqual(newdata, data)
+        
+    def test_readXML_noverify(self):
+        
+        io = StringIO(XMLUtil.data1)
+        etree, root = readXML(io)
+        self.assertEqual(etree.getroot(), root)
+        self.assertEqual(root.tag, "test")
+
+    def test_readXML_verify_ok(self):
+        
+        io = StringIO(XMLUtil.data1)
+        etree, root = readXML(io, expectedRootTag="test")
+        self.assertEqual(etree.getroot(), root)
+        self.assertEqual(root.tag, "test")
+
+    def test_readXML_verify_bad(self):
+        
+        io = StringIO(XMLUtil.data1)
+        self.assertRaises(ValueError, readXML, io, "test1")
+
+    def test_readXML_data_bad(self):
+        
+        io = StringIO(XMLUtil.data2)
+        self.assertRaises(ValueError, readXML, io)
+
+    def test_writeXML(self):
+        
+        io = StringIO(XMLUtil.data1)
+        _ignore_etree, root = readXML(io)
+        self._checkXML(root, XMLUtil.data3)
+
+    def test_addElement(self):
+        
+        io = StringIO(XMLUtil.data1)
+        _ignore_etree, root = readXML(io)
+        addSubElement(root, "added", "added text")
+        self._checkXML(root, XMLUtil.data4)
+
+    def test_changeElement_existing(self):
+        
+        io = StringIO(XMLUtil.data1)
+        _ignore_etree, root = readXML(io)
+        changeSubElementText(root, "help", "changed text")
+        self._checkXML(root, XMLUtil.data5)
+
+    def test_changeElement_new(self):
+        
+        io = StringIO(XMLUtil.data1)
+        _ignore_etree, root = readXML(io)
+        changeSubElementText(root, "new", "new text")
+        self._checkXML(root, XMLUtil.data6)
+

Modified: CalendarServer/trunk/twistedcaldav/xmlutil.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/xmlutil.py	2010-02-15 19:18:24 UTC (rev 5119)
+++ CalendarServer/trunk/twistedcaldav/xmlutil.py	2010-02-15 19:19:19 UTC (rev 5120)
@@ -35,12 +35,12 @@
     try:
         etree = ElementTree(file=xmlfile)
     except ExpatError, e:
-        ValueError("Unable to parse file '%s' because: %s" % (xmlfile, e,))
+        raise ValueError("Unable to parse file '%s' because: %s" % (xmlfile, e,))
 
     if expectedRootTag:
         root = etree.getroot()
         if root.tag != expectedRootTag:
-            ValueError("Ignoring file '%s' because it is not a %s file" % (xmlfile, expectedRootTag,))
+            raise ValueError("Ignoring file '%s' because it is not a %s file" % (xmlfile, expectedRootTag,))
     
     return etree, etree.getroot()
 
@@ -95,5 +95,7 @@
 def changeSubElementText(parent, tag, text):
     
     child = parent.find(tag)
-    child.text = text
-
+    if child is not None:
+        child.text = text
+    else:
+        addSubElement(parent, tag, text)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100215/355fa815/attachment.html>


More information about the calendarserver-changes mailing list