[CalendarServer-changes] [4722] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon Nov 9 09:18:12 PST 2009


Revision: 4722
          http://trac.macosforge.org/projects/calendarserver/changeset/4722
Author:   glyph at apple.com
Date:     2009-11-09 09:18:08 -0800 (Mon, 09 Nov 2009)
Log Message:
-----------
Consistently represent config strings from plists as 'str' objects.

Modified Paths:
--------------
    CalendarServer/trunk/twext/python/plistlib.py
    CalendarServer/trunk/twistedcaldav/stdconfig.py

Added Paths:
-----------
    CalendarServer/trunk/twistedcaldav/test/test_stdconfig.py

Modified: CalendarServer/trunk/twext/python/plistlib.py
===================================================================
--- CalendarServer/trunk/twext/python/plistlib.py	2009-11-09 15:42:57 UTC (rev 4721)
+++ CalendarServer/trunk/twext/python/plistlib.py	2009-11-09 17:18:08 UTC (rev 4722)
@@ -16,15 +16,8 @@
 
 try:
     _plistlib = __import__("plistlib")
-
 except ImportError:
-    from twext.python._plistlib import *
+    from twext.python import _plistlib
 
-    import twext.python._plistlib
-    __all__ = twext.python._plistlib.__all__
-    del twext
-
-else:
-    for symbol in _plistlib.__all__:
-        globals()[symbol] = getattr(_plistlib, symbol)
-    del _plistlib
+import sys
+sys.modules[__name__] = _plistlib

Modified: CalendarServer/trunk/twistedcaldav/stdconfig.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/stdconfig.py	2009-11-09 15:42:57 UTC (rev 4721)
+++ CalendarServer/trunk/twistedcaldav/stdconfig.py	2009-11-09 17:18:08 UTC (rev 4722)
@@ -1,3 +1,4 @@
+# -*- test-case-name: twistedcaldav.test.test_stdconfig -*-
 ##
 # Copyright (c) 2005-2009 Apple Inc. All rights reserved.
 #
@@ -21,7 +22,7 @@
 from twisted.web2.dav import davxml
 from twisted.web2.dav.resource import TwistedACLInheritable
 
-from twext.python.plistlib import readPlist
+from twext.python.plistlib import PlistParser
 
 from twistedcaldav.config import (
     ConfigProvider, ConfigurationError, config, _mergeData, )
@@ -392,15 +393,36 @@
     "ResponseCacheTimeout": 30, # Minutes
 }
 
+
+
+class NoUnicodePlistParser(PlistParser):
+    """
+    A variant of L{PlistParser} which avoids exposing the 'unicode' data-type
+    to application code when non-ASCII characters are found, instead
+    consistently exposing UTF-8 encoded 'str' objects.
+    """
+
+    def getData(self):
+        """
+        Get the currently-parsed data as a 'str' object.
+        """
+        data = "".join(self.data).encode("utf-8")
+        self.data = []
+        return data
+
+
+
 class PListConfigProvider(ConfigProvider):
     
     def loadConfig(self):
         configDict = {}
         if self._configFileName:
+            parser = NoUnicodePlistParser()
             try:
-                configDict = readPlist(self._configFileName)
+                configDict = parser.parse(open(self._configFileName))
             except (IOError, OSError):                                    
-                log.error("Configuration file does not exist or is inaccessible: %s" % (self._configFileName,))
+                log.error("Configuration file does not exist or is inaccessible: %s" %
+                          (self._configFileName,))
             else:
                 configDict = _cleanup(configDict, self._defaults)
         return configDict

Added: CalendarServer/trunk/twistedcaldav/test/test_stdconfig.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/test_stdconfig.py	                        (rev 0)
+++ CalendarServer/trunk/twistedcaldav/test/test_stdconfig.py	2009-11-09 17:18:08 UTC (rev 4722)
@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+##
+# Copyright (c) 2005-2009 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from cStringIO import StringIO
+
+from twisted.python.filepath import FilePath
+from twisted.trial.unittest import TestCase
+
+from twistedcaldav.config import Config
+from twistedcaldav.stdconfig import NoUnicodePlistParser, PListConfigProvider
+
+nonASCIIValue = "→←"
+nonASCIIPlist = "<plist version='1.0'><string>%s</string></plist>" % (
+    nonASCIIValue,
+)
+
+nonASCIIConfigPList = """
+<plist version="1.0">
+  <dict>
+    <key>DataRoot</key>
+    <string>%s</string>
+  </dict>
+</plist>
+""" % (nonASCIIValue,)
+
+class ConfigParsingTests(TestCase):
+    """
+    Tests to verify the behavior of the configuration parser.
+    """
+
+    def test_noUnicodePListParser(self):
+        """
+        L{NoUnicodePlistParser.parse} retrieves non-ASCII property list values
+        as (UTF-8 encoded) 'str' objects, so that a single type is consistently
+        used regardless of the input data.
+        """
+        parser = NoUnicodePlistParser()
+        self.assertEquals(parser.parse(StringIO(nonASCIIPlist)),
+                          nonASCIIValue)
+
+
+    def test_parseNonASCIIConfig(self):
+        """
+        Non-ASCII <string>s found as part of a configuration file will be
+        retrieved as UTF-8 encoded 'str' objects, as parsed by
+        L{NoUnicodePlistParser}.
+        """
+        cfg = Config(PListConfigProvider({"DataRoot": ""}))
+        tempfile = FilePath(self.mktemp())
+        tempfile.setContent(nonASCIIConfigPList)
+        cfg.load(tempfile.path)
+        self.assertEquals(cfg.DataRoot, nonASCIIValue)
+
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20091109/5c194b76/attachment.html>


More information about the calendarserver-changes mailing list