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

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


Revision: 11342
          http://trac.calendarserver.org//changeset/11342
Author:   glyph at apple.com
Date:     2013-06-13 16:07:11 -0700 (Thu, 13 Jun 2013)
Log Message:
-----------
Add formatWithCall.

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:03:13 UTC (rev 11341)
+++ CalendarServer/trunk/twext/python/log.py	2013-06-13 23:07:11 UTC (rev 11342)
@@ -18,7 +18,7 @@
 """
 Classes and functions to do granular logging.
 
-Example usage in a module:
+Example usage in a module::
 
     from twext.python.log import Logger
     log = Logger()
@@ -27,7 +27,7 @@
 
     log.debug("Got data: {data}.", data=data)
 
-Or in a class:
+Or in a class::
 
     from twext.python.log import Logger
 
@@ -80,6 +80,8 @@
 import logging
 import time
 
+from string import Formatter
+
 from zope.interface import Interface, implementer
 from twisted.python.constants import NamedConstant, Names
 from twisted.python.failure import Failure
@@ -140,8 +142,38 @@
    #LogLevel.critical: logging.CRITICAL,
 }
 
+_theFormatter = Formatter()
 
 
+
+class _CallMapping(object):
+    def __init__(self, submapping):
+        self._submapping = submapping
+
+    def __getitem__(self, key):
+        callit = key.endswith("()")
+        realKey = key[:-2] if callit else key
+        value = self._submapping[realKey]
+        if callit:
+            value = value()
+        return value
+
+
+
+def formatWithCall(formatString, mapping):
+    """
+    @param formatString: A PEP-3101 format string.
+    @type formatString: L{unicode}
+
+    @param mapping: A L{dict}-like object to format.
+
+    @return: The string with formatted values interpolated.
+    @rtype: L{unicode}
+    """
+    return _theFormatter.vformat(formatString, (), _CallMapping(mapping))
+
+
+
 #
 # Tools for managing log levels
 #

Modified: CalendarServer/trunk/twext/python/test/test_log.py
===================================================================
--- CalendarServer/trunk/twext/python/test/test_log.py	2013-06-13 23:03:13 UTC (rev 11341)
+++ CalendarServer/trunk/twext/python/test/test_log.py	2013-06-13 23:07:11 UTC (rev 11342)
@@ -23,6 +23,7 @@
 from twext.python.log import pythonLogLevelMapping
 from twext.python.log import Logger, LegacyLogger
 
+from twext.python.log import formatWithCall
 from twistedcaldav.test.util import TestCase
 
 
@@ -322,6 +323,20 @@
         self.assertEquals(len(errors), 1)
 
 
+    def test_formatWithCall(self):
+        """
+        L{formatWithCall} is an extended version of L{unicode.format} that will
+        interpret a set of parentheses "C{()}" at the end of a format key to
+        mean that the format key ought to be I{called} rather than stringified.
+        """
+        self.assertEquals(
+            formatWithCall(u"Hello, {world}. {callme()}.",
+                           dict(world="earth",
+                                callme=lambda: "maybe")),
+            "Hello, earth. maybe."
+        )
+
+
     def test_formatEvent(self):
         """
         Test formatting.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130613/5df4dad1/attachment-0001.html>


More information about the calendarserver-changes mailing list