[CalendarServer-changes] [11344] CalendarServer/trunk/twext/python

source_changes at macosforge.org source_changes at macosforge.org
Thu Jun 13 16:07:13 PDT 2013


Revision: 11344
          http://trac.calendarserver.org//changeset/11344
Author:   glyph at apple.com
Date:     2013-06-13 16:07:13 -0700 (Thu, 13 Jun 2013)
Log Message:
-----------
Use the new formatting API.

Modified Paths:
--------------
    CalendarServer/trunk/twext/python/log.py
    CalendarServer/trunk/twext/python/test/test_log.py

Modified: CalendarServer/trunk/twext/python/log.py
===================================================================
--- CalendarServer/trunk/twext/python/log.py	2013-06-13 23:07:12 UTC (rev 11343)
+++ CalendarServer/trunk/twext/python/log.py	2013-06-13 23:07:13 UTC (rev 11344)
@@ -151,7 +151,7 @@
         self._submapping = submapping
 
     def __getitem__(self, key):
-        callit = key.endswith("()")
+        callit = key.endswith(u"()")
         realKey = key[:-2] if callit else key
         value = self._submapping[realKey]
         if callit:
@@ -186,7 +186,9 @@
     @return: The string with formatted values interpolated.
     @rtype: L{unicode}
     """
-    return _theFormatter.vformat(formatString, (), _CallMapping(mapping))
+    return unicode(
+        _theFormatter.vformat(formatString, (), _CallMapping(mapping))
+    )
 
 
 
@@ -333,7 +335,7 @@
                 # For anything else, assume we can just convert to unicode
                 format = unicode(format)
 
-            return format.format(**event)
+            return formatWithCall(format, event)
 
         except Exception as e:
             try:

Modified: CalendarServer/trunk/twext/python/test/test_log.py
===================================================================
--- CalendarServer/trunk/twext/python/test/test_log.py	2013-06-13 23:07:12 UTC (rev 11343)
+++ CalendarServer/trunk/twext/python/test/test_log.py	2013-06-13 23:07:13 UTC (rev 11344)
@@ -339,18 +339,38 @@
 
     def test_formatEvent(self):
         """
-        Test formatting.
+        L{Logger.formatEvent} will format an event according to several rules:
+
+            - A string with no formatting instructions will be passed straight
+              through.
+
+            - PEP 3101 strings will be formatted using the keys and values of
+              the event as named fields.
+
+            - PEP 3101 keys ending with C{()} will be treated as instructions
+              to call that key (which ought to be a callable) before
+              formatting.
+
+        L{Logger.formatEvent} will always return L{unicode}, and if given
+        bytes, will always treat its format string as UTF-8 encoded.
         """
         def formatEvent(log_format, **event):
             event["log_format"] = log_format
             result = Logger.formatEvent(event)
-            self.assertIdentical(type(result), unicode) # Always returns unicode
+            self.assertIdentical(type(result), unicode)
             return result
 
-        self.assertEquals("", formatEvent(""))
-        self.assertEquals("abc", formatEvent("{x}", x="abc"))
-        self.assertEquals(u'S\xe1nchez', formatEvent("S\xc3\xa1nchez")) # bytes->unicode
-        self.assertIn("Unable to format event", formatEvent("S\xe1nchez")) # Non-UTF-8 bytes
+        self.assertEquals(u"", formatEvent(""))
+        self.assertEquals(u"abc", formatEvent("{x}", x="abc"))
+        self.assertEquals(u"no, yes.",
+                          formatEvent("{not_called}, {called()}.",
+                                      not_called="no", called=lambda: "yes"))
+        self.assertEquals(u'S\xe1nchez', formatEvent("S\xc3\xa1nchez"))
+        self.assertIn(u"Unable to format event", formatEvent(b"S\xe1nchez"))
+        self.assertIn(u"Unable to format event",
+                      formatEvent(b"S{a}nchez", a=b"\xe1"))
+        self.assertIn(u"S'\\xe1'nchez",
+                      formatEvent(b"S{a!r}nchez", a=b"\xe1"))
 
 
     def test_legacy_msg(self):
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130613/1e81cb86/attachment.html>


More information about the calendarserver-changes mailing list