[CalendarServer-changes] [7376] CalendarServer/trunk/twistedcaldav/directory

source_changes at macosforge.org source_changes at macosforge.org
Wed Apr 27 13:10:50 PDT 2011


Revision: 7376
          http://trac.macosforge.org/projects/calendarserver/changeset/7376
Author:   sagen at apple.com
Date:     2011-04-27 13:10:49 -0700 (Wed, 27 Apr 2011)
Log Message:
-----------
Adds the ability to generate "names" within "repeated" users in accounts.xml (9339200).

Putting in ~<number> within the name, first-name, last-name, and email-address XML elements will substitute that with the first <number> characters from the hash of the repeat counter.

Modified Paths:
--------------
    CalendarServer/trunk/twistedcaldav/directory/test/accounts.xml
    CalendarServer/trunk/twistedcaldav/directory/test/test_xmlfile.py
    CalendarServer/trunk/twistedcaldav/directory/xmlaccountsparser.py

Modified: CalendarServer/trunk/twistedcaldav/directory/test/accounts.xml
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/test/accounts.xml	2011-04-27 18:52:55 UTC (rev 7375)
+++ CalendarServer/trunk/twistedcaldav/directory/test/accounts.xml	2011-04-27 20:10:49 UTC (rev 7376)
@@ -107,7 +107,10 @@
     <uid>user%02d</uid>
     <guid>user%02d</guid>
     <password>%02duser</password>
-    <name>User %02d</name>
+    <name>~35 User %02d</name>
+    <first-name>~5</first-name>
+    <last-name>~9 User %02d</last-name>
+    <email-address>~10 at example.com</email-address>
   </user>
   <group>
     <uid>managers</uid>

Modified: CalendarServer/trunk/twistedcaldav/directory/test/test_xmlfile.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/test/test_xmlfile.py	2011-04-27 18:52:55 UTC (rev 7375)
+++ CalendarServer/trunk/twistedcaldav/directory/test/test_xmlfile.py	2011-04-27 20:10:49 UTC (rev 7376)
@@ -39,8 +39,8 @@
         "lecroy"     : { "password": "yorcel",     "guid": "8B4288F6-CC82-491D-8EF9-642EF4F3E7D0", "addresses": ("mailto:lecroy at example.com",)   },
         "dreid"      : { "password": "dierd",      "guid": "5FF60DAD-0BDE-4508-8C77-15F0CA5C8DD1", "addresses": ("mailto:dreid at example.com",)    },
         "nocalendar" : { "password": "radnelacon", "guid": "543D28BA-F74F-4D5F-9243-B3E3A61171E5", "addresses": () },
-        "user01"     : { "password": "01user",     "guid": None                                  , "addresses": () },
-        "user02"     : { "password": "02user",     "guid": None                                  , "addresses": () },
+        "user01"     : { "password": "01user",     "guid": None                                  , "addresses": ("mailto:c4ca4238a0 at example.com",) },
+        "user02"     : { "password": "02user",     "guid": None                                  , "addresses": ("mailto:c81e728d9d at example.com",) },
     }
 
     groups = {
@@ -297,6 +297,15 @@
         self.assertNotEquals(None, service._lookupInIndex(service.recordType_locations, service.INDEX_TYPE_SHORTNAME, "orion"))
         self.assertEquals(None, service._lookupInIndex(service.recordType_users, service.INDEX_TYPE_CUA, "mailto:nobody at example.com"))
 
+    def test_repeat(self):
+        service = self.service()
+        record = service.recordWithShortName(
+            DirectoryService.recordType_users, "user01")
+        self.assertEquals(record.fullName, "c4ca4238a0b923820dcc509a6f75849bc4c User 01")
+        self.assertEquals(record.firstName, "c4ca4")
+        self.assertEquals(record.lastName, "c4ca4238a User 01")
+        self.assertEquals(record.emailAddresses, set(['c4ca4238a0 at example.com']))
+
 class XMLFileSubset (XMLFileBase, TestCase):
     """
     Test the recordTypes subset feature of XMLFile service.

Modified: CalendarServer/trunk/twistedcaldav/directory/xmlaccountsparser.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/xmlaccountsparser.py	2011-04-27 18:52:55 UTC (rev 7375)
+++ CalendarServer/trunk/twistedcaldav/directory/xmlaccountsparser.py	2011-04-27 20:10:49 UTC (rev 7376)
@@ -31,6 +31,9 @@
 
 from twistedcaldav.directory.directory import DirectoryService
 
+import re
+import hashlib
+
 log = Logger()
 
 ELEMENT_ACCOUNTS          = "accounts"
@@ -165,6 +168,36 @@
         done on them with the numeric value provided.
         @param ctr: an integer to substitute into text.
         """
+
+        # Regular expression which matches ~ followed by a number
+        matchNumber = re.compile(r"(~\d+)")
+
+        def expand(text, ctr):
+            """
+            Returns a string where ~<number> is replaced by the first <number>
+            characters from the md5 hexdigest of str(ctr), e.g.:
+
+                expand("~9 foo", 1)
+
+            returns:
+
+                "c4ca4238a foo"
+
+            ...since "c4ca4238a" is the first 9 characters of:
+
+                hashlib.md5(str(1)).hexdigest()
+
+            If <number> is larger than 32, the hash will repeat as needed.
+            """
+            if text:
+                m = matchNumber.search(text)
+                if m:
+                    length = int(m.group(0)[1:])
+                    hash = hashlib.md5(str(ctr)).hexdigest()
+                    string = (hash*((length/32)+1))[:-(32-(length%32))]
+                    return text.replace(m.group(0), string)
+            return text
+
         shortNames = []
         for shortName in self.shortNames:
             if shortName.find("%") != -1:
@@ -183,16 +216,20 @@
             fullName = self.fullName % ctr
         else:
             fullName = self.fullName
+        fullName = expand(fullName, ctr)
         if self.firstName and self.firstName.find("%") != -1:
             firstName = self.firstName % ctr
         else:
             firstName = self.firstName
+        firstName = expand(firstName, ctr)
         if self.lastName and self.lastName.find("%") != -1:
             lastName = self.lastName % ctr
         else:
             lastName = self.lastName
+        lastName = expand(lastName, ctr)
         emailAddresses = set()
         for emailAddr in self.emailAddresses:
+            emailAddr = expand(emailAddr, ctr)
             if emailAddr.find("%") != -1:
                 emailAddresses.add(emailAddr % ctr)
             else:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110427/f8edf4b6/attachment.html>


More information about the calendarserver-changes mailing list