[Checkins] SVN: Products.GenericSetup/branches/1.7/ Get more picky with unicode and strings

Patrick Gerken do3ccqrv at gmail.com
Sat Nov 19 15:25:29 UTC 2011


Log message for revision 123435:
  Get more picky with unicode and strings
  
  the exporters prevent dumping unicode objects,
  the registry handlers always encode unicode to utf-8 strings.

Changed:
  U   Products.GenericSetup/branches/1.7/Products/GenericSetup/context.py
  U   Products.GenericSetup/branches/1.7/Products/GenericSetup/registry.py
  U   Products.GenericSetup/branches/1.7/Products/GenericSetup/tests/test_context.py
  U   Products.GenericSetup/branches/1.7/Products/GenericSetup/utils.py
  U   Products.GenericSetup/branches/1.7/Products/GenericSetup/version.txt
  U   Products.GenericSetup/branches/1.7/docs/CHANGES.rst

-=-
Modified: Products.GenericSetup/branches/1.7/Products/GenericSetup/context.py
===================================================================
--- Products.GenericSetup/branches/1.7/Products/GenericSetup/context.py	2011-11-19 14:58:04 UTC (rev 123434)
+++ Products.GenericSetup/branches/1.7/Products/GenericSetup/context.py	2011-11-19 15:25:29 UTC (rev 123435)
@@ -311,6 +311,9 @@
 
         """ See IExportContext.
         """
+        if isinstance(text, unicode):
+            raise ValueError("Unicode text is not supported, even if it only "
+                             "contains ascii. Please encode your data")
         file = self.openDataFile( filename, content_type, subdir )
         file.write( text )
         file.close()
@@ -456,9 +459,12 @@
             parents.pop()
 
         info = TarInfo(filename)
-        if isinstance(text, basestring):
+        if isinstance(text, str):
             stream = StringIO(text)
             info.size = len(text)
+        elif isinstance(text, unicode):
+            raise ValueError("Unicode text is not supported, even if it only "
+                             "contains ascii. Please encode your data")
         else:
             # Assume text is a an instance of a class like
             # Products.Archetypes.WebDAVSupport.PdataStreamIterator, 
@@ -509,6 +515,11 @@
         if sep != -1:
             subdir = filename[:sep]
             filename = filename[sep+1:]
+
+        if isinstance(text, unicode):
+            raise ValueError("Unicode text is not supported, even if it only "
+                             "contains ascii. Please encode your data")
+
         folder = self._ensureSnapshotsFolder( subdir )
 
         # TODO: switch on content_type

Modified: Products.GenericSetup/branches/1.7/Products/GenericSetup/registry.py
===================================================================
--- Products.GenericSetup/branches/1.7/Products/GenericSetup/registry.py	2011-11-19 14:58:04 UTC (rev 123434)
+++ Products.GenericSetup/branches/1.7/Products/GenericSetup/registry.py	2011-11-19 15:25:29 UTC (rev 123435)
@@ -306,13 +306,13 @@
         return [ self.getStepMetadata( x ) for x in step_ids ]
 
     security.declareProtected( ManagePortal, 'generateXML' )
-    def generateXML( self ):
+    def generateXML( self, encoding='utf-8' ):
 
         """ Return a round-trippable XML representation of the registry.
 
         o 'handler' values are serialized using their dotted names.
         """
-        return self._exportTemplate()
+        return self._exportTemplate().encode('utf-8')
 
     security.declarePrivate( 'getStep' )
     def getStep( self, key, default=None ):
@@ -338,7 +338,7 @@
         self._registered.clear()
 
     security.declarePrivate( 'parseXML' )
-    def parseXML( self, text, encoding=None ):
+    def parseXML( self, text, encoding='utf-8' ):
 
         """ Parse 'text'.
         """
@@ -636,14 +636,14 @@
                                     }
 
     security.declareProtected( ManagePortal, 'generateXML' )
-    def generateXML( self ):
+    def generateXML( self, encoding='utf-8' ):
 
         """ Pseudo API.
         """
-        return self._toolsetConfig()
+        return self._toolsetConfig().encode('utf-8')
 
     security.declareProtected( ManagePortal, 'parseXML' )
-    def parseXML( self, text, encoding=None ):
+    def parseXML( self, text, encoding='utf-8' ):
 
         """ Pseudo-API
         """

Modified: Products.GenericSetup/branches/1.7/Products/GenericSetup/tests/test_context.py
===================================================================
--- Products.GenericSetup/branches/1.7/Products/GenericSetup/tests/test_context.py	2011-11-19 14:58:04 UTC (rev 123434)
+++ Products.GenericSetup/branches/1.7/Products/GenericSetup/tests/test_context.py	2011-11-19 15:25:29 UTC (rev 123435)
@@ -1,3 +1,4 @@
+#encoding: utf-8
 ##############################################################################
 #
 # Copyright (c) 2004 Zope Foundation and Contributors.
@@ -380,6 +381,22 @@
 
         self.assertEqual( open( fqname, 'rb' ).read(), digits )
 
+    def test_writeDataFile_unicode( self ):
+
+        from string import printable
+        text = u'Kein Weltraum links vom Gerät'
+        FILENAME = 'unicode.txt'
+        fqname = self._makeFile( FILENAME, printable )
+
+        site = DummySite('site').__of__(self.app)
+        ctx = self._makeOne( site, self._PROFILE_PATH )
+
+        self.assertRaises(ValueError, ctx.writeDataFile,
+                          FILENAME, text, 'text/plain' )
+        text = u'No umlauts'
+        self.assertRaises(ValueError, ctx.writeDataFile,
+                          FILENAME, text, 'text/plain' )
+
     def test_writeDataFile_new_subdir( self ):
         from string import digits
 
@@ -774,6 +791,19 @@
         self._verifyTarballContents( fileish, [ 'foo.txt' ], now )
         self._verifyTarballEntry( fileish, 'foo.txt', printable )
 
+    def test_writeDataFile_umluats( self ):
+
+        text = u'Kein Weltraum links vom Gerät'
+
+        site = DummySite('site').__of__(self.app)
+        ctx = self._getTargetClass()( site )
+
+        self.assertRaises(ValueError, ctx.writeDataFile,
+                          'foo.txt', text, 'text/plain' )
+        text = u'No umlauts'
+        self.assertRaises(ValueError, ctx.writeDataFile,
+                          'foo.txt', text, 'text/plain' )
+
     def test_writeDataFile_multiple( self ):
 
         from string import printable
@@ -928,21 +958,12 @@
         tool = site.setup_tool
         ctx = self._makeOne( tool, 'simple', 'latin_1' )
 
-        ctx.writeDataFile( FILENAME, CONTENT, CONTENT_TYPE )
+        self.assertRaises(ValueError, ctx.writeDataFile,
+                          FILENAME, CONTENT, CONTENT_TYPE )
+        CONTENT = u'No unicode chars'
+        self.assertRaises(ValueError, ctx.writeDataFile,
+                          FILENAME, CONTENT, CONTENT_TYPE )
 
-        snapshot = tool.snapshots._getOb( 'simple' )
-
-        self.assertEqual( len( snapshot.objectIds() ), 1 )
-        self.failUnless( FILENAME in snapshot.objectIds() )
-
-        fileobj = snapshot._getOb( FILENAME )
-
-        self.assertEqual( fileobj.getId(), FILENAME )
-        self.assertEqual( fileobj.meta_type, File.meta_type )
-        self.assertEqual( fileobj.getContentType(), CONTENT_TYPE )
-        saved = fileobj.manage_FTPget().decode('latin_1')
-        self.assertEqual( saved, CONTENT )
-
     def test_writeDataFile_simple_xml( self ):
 
         from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
@@ -969,32 +990,6 @@
         self.assertEqual( template.read(), _XML )
         self.failIf( template.html() )
 
-    def test_writeDataFile_unicode_xml( self ):
-
-        from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
-        FILENAME = 'simple.xml'
-        CONTENT_TYPE = 'text/xml'
-        _XML = u"""<?xml version="1.0"?><simple />"""
-
-        site = DummySite('site').__of__(self.app)
-        site.setup_tool = DummyTool( 'setup_tool' )
-        tool = site.setup_tool
-        ctx = self._makeOne( tool, 'simple' )
-
-        ctx.writeDataFile( FILENAME, _XML, CONTENT_TYPE )
-
-        snapshot = tool.snapshots._getOb( 'simple' )
-
-        self.assertEqual( len( snapshot.objectIds() ), 1 )
-        self.failUnless( FILENAME in snapshot.objectIds() )
-
-        template = snapshot._getOb( FILENAME )
-
-        self.assertEqual( template.getId(), FILENAME )
-        self.assertEqual( template.meta_type, ZopePageTemplate.meta_type )
-        self.assertEqual( template.read(), _XML )
-        self.failIf( template.html() )
-
     def test_writeDataFile_subdir_dtml( self ):
 
         from OFS.DTMLDocument import DTMLDocument

Modified: Products.GenericSetup/branches/1.7/Products/GenericSetup/utils.py
===================================================================
--- Products.GenericSetup/branches/1.7/Products/GenericSetup/utils.py	2011-11-19 14:58:04 UTC (rev 123434)
+++ Products.GenericSetup/branches/1.7/Products/GenericSetup/utils.py	2011-11-19 15:25:29 UTC (rev 123435)
@@ -158,7 +158,7 @@
     security = ClassSecurityInfo()
     security.setDefaultAccess('allow')
 
-    def __init__(self, site, encoding=None):
+    def __init__(self, site, encoding='utf-8'):
 
         self._site = site
         self._encoding = encoding
@@ -271,7 +271,7 @@
     security = ClassSecurityInfo()
     security.setDefaultAccess('allow')
 
-    def __init__(self, site, encoding=None):
+    def __init__(self, site, encoding='utf-8'):
 
         self._site = site
         self._encoding = encoding
@@ -281,7 +281,10 @@
     def generateXML(self, **kw):
         """ Pseudo API.
         """
-        return self._template(**kw)
+        if self._encoding:
+            return self._template(**kw).encode(self._encoding)
+        else:
+            return self._template(**kw)
 
 InitializeClass(ExportConfiguratorBase)
 
@@ -395,7 +398,7 @@
         return e
 
     def writexml(self, writer, indent="", addindent="", newl="",
-                 encoding = None):
+                 encoding = 'utf-8'):
         if encoding is None:
             writer.write('<?xml version="1.0"?>\n')
         else:

Modified: Products.GenericSetup/branches/1.7/Products/GenericSetup/version.txt
===================================================================
--- Products.GenericSetup/branches/1.7/Products/GenericSetup/version.txt	2011-11-19 14:58:04 UTC (rev 123434)
+++ Products.GenericSetup/branches/1.7/Products/GenericSetup/version.txt	2011-11-19 15:25:29 UTC (rev 123435)
@@ -1 +1 @@
-1.6.5dev
+1.7.0dev

Modified: Products.GenericSetup/branches/1.7/docs/CHANGES.rst
===================================================================
--- Products.GenericSetup/branches/1.7/docs/CHANGES.rst	2011-11-19 14:58:04 UTC (rev 123434)
+++ Products.GenericSetup/branches/1.7/docs/CHANGES.rst	2011-11-19 15:25:29 UTC (rev 123435)
@@ -1,10 +1,12 @@
 Products.GenericSetup Changelog
 ===============================
 
-1.6.5 (unreleased)
+1.7.0 (unreleased)
 ------------------
 
-- TBD
+- Exporters now explicitly only understand strings. The provided
+  registry handlers encode and decode data automatically to and from
+  utf-8. Their default encoding changed from None to utf-8.
 
 
 1.6.4 (2011-10-31)



More information about the checkins mailing list