[CalendarServer-changes] [12675] CalDAVTester/trunk/src/serverinfo.py

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:16:52 PDT 2014


Revision: 12675
          http://trac.calendarserver.org//changeset/12675
Author:   cdaboo at apple.com
Date:     2014-02-12 10:39:41 -0800 (Wed, 12 Feb 2014)
Log Message:
-----------
Speed up (a lot) the loading phase.

Modified Paths:
--------------
    CalDAVTester/trunk/src/serverinfo.py

Modified: CalDAVTester/trunk/src/serverinfo.py
===================================================================
--- CalDAVTester/trunk/src/serverinfo.py	2014-02-12 15:30:33 UTC (rev 12674)
+++ CalDAVTester/trunk/src/serverinfo.py	2014-02-12 18:39:41 UTC (rev 12675)
@@ -19,13 +19,18 @@
 """
 
 import datetime
+import re
 import src.xmlDefs
 
+
 class serverinfo(object):
     """
     Maintains information about the server being targeted.
     """
 
+    # RegEx pattern to match substitution variables
+    subspattern = re.compile("(?P<name>\\$[_a-zA-Z][_a-zA-Z0-9\\-]*\\:)")
+
     def __init__(self):
         self.host = ""
         self.nonsslport = 80
@@ -48,6 +53,25 @@
         self.dtnow = datetime.date.today()
 
 
+    def _re_subs(self, sub, mapping):
+        """
+        Do a regex substitution via the supplied mapping, only if the mapping exists.
+
+        @param sub: string to do substitution in
+        @type sub: L{str}
+        @param mapping: mapping of substitution name to value
+        @type mapping: L{dict}
+        """
+        # Helper function for .sub()
+        def convert(mo):
+            named = mo.group('name')
+            if named is not None and named in mapping:
+                return mapping[named]
+            else:
+                return named
+        return self.subspattern.sub(convert, sub)
+
+
     def subs(self, sub, db=None):
 
         # Special handling for relative date-times
@@ -76,16 +100,11 @@
 
         if db is None:
             db = self.subsdict
-        count = 0
-        while count < 10:
-            do_again = False
-            for key, value in db.iteritems():
-                newstr = sub.replace(key, value)
-                do_again = do_again or (newstr != sub)
-                sub = newstr
-            if not do_again:
+        while '$' in sub:
+            newstr = self._re_subs(sub, db)
+            if newstr == sub:
                 break
-            count += 1
+            sub = newstr
         return sub
 
 
@@ -168,6 +187,13 @@
 
 
     def updateParams(self):
+
+        # Expand substitutions fully at this point
+        for k, v in self.subsdict.items():
+            while '$' in v:
+                v = self._re_subs(v, self.subsdict)
+            self.subsdict[k] = v
+
         # Now cache some useful substitutions
         if "$userid1:" not in self.subsdict:
             raise ValueError("Must have $userid1: substitution")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/7c5fd349/attachment.html>


More information about the calendarserver-changes mailing list