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

source_changes at macosforge.org source_changes at macosforge.org
Sat Sep 20 07:17:45 PDT 2014


Revision: 13986
          http://trac.calendarserver.org//changeset/13986
Author:   cdaboo at apple.com
Date:     2014-09-20 07:17:45 -0700 (Sat, 20 Sep 2014)
Log Message:
-----------
Preserve any text prior to the root element when writing. Use fixed element order when writing.

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

Modified: twext/trunk/twext/who/xml.py
===================================================================
--- twext/trunk/twext/who/xml.py	2014-09-20 14:14:26 UTC (rev 13985)
+++ twext/trunk/twext/who/xml.py	2014-09-20 14:17:45 UTC (rev 13986)
@@ -50,6 +50,7 @@
 )
 from .util import ConstantsContainer
 
+import itertools
 
 
 ##
@@ -148,7 +149,18 @@
     attribute = Attribute
     recordTypeValue = RecordTypeValue
 
+    xmlFieldOrder = (
+        BaseFieldName.recordType,
+        BaseFieldName.uid,
+        BaseFieldName.guid,
+        BaseFieldName.shortNames,
+        BaseFieldName.fullNames,
+        BaseFieldName.emailAddresses,
+        BaseFieldName.password,
+        IndexFieldName.memberUIDs,
+    )
 
+
     def __init__(self, filePath, refreshInterval=4):
         """
         @param filePath: A file path for the XML data to load into the
@@ -162,6 +174,7 @@
         BaseDirectoryService.__init__(self, realmName=noRealmName)
 
         self.filePath = filePath
+        self.filePreamble = ""
         self.refreshInterval = refreshInterval
 
 
@@ -252,6 +265,10 @@
         #
         try:
             with self.filePath.open() as fh:
+                lines = fh.read().splitlines()
+                self.filePreamble = "\n".join(itertools.takewhile(lambda x: not x.startswith("<directory"), lines))
+
+            with self.filePath.open() as fh:
                 try:
                     etree = parseXML(fh)
                 except XMLParseError as e:
@@ -472,7 +489,12 @@
 
         def fillRecordNode(recordNode, record):
             subNode = None
-            for (name, value) in record.fields.items():
+            fields = list(self.xmlFieldOrder)
+            fields.extend(filter(lambda k: k not in self.xmlFieldOrder, record.fields))
+            for name in fields:
+                if name not in record.fields:
+                    continue
+                value = record.fields[name]
                 if name == self.fieldName.recordType:
                     if value in recordTypes:
                         recordNode.set(
@@ -561,7 +583,7 @@
 
 
     def _writeDirectoryNode(self, directoryNode):
-        self.filePath.setContent(etreeToString(directoryNode, "utf-8"))
+        self.filePath.setContent("{preamble}\n{xml}\n".format(preamble=self.filePreamble, xml=etreeToString(directoryNode, "utf-8")))
         self.flush()
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140920/31918db0/attachment-0001.html>


More information about the calendarserver-changes mailing list