Skip to content

Commit

Permalink
- add support for unicode data in writeDataFile (fixes #79)
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed Apr 2, 2019
1 parent f8b1c03 commit 3b11531
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
2.0b6 (unreleased)
------------------

- add support for unicode data in ``writeDataFile``
(`#79 <https://github.com/zopefoundation/Products.GenericSetup/issues/79>`_)

- Specify supported Python versions using ``python_requires`` in setup.py

- Adding suport for Python 3.8
Expand Down
18 changes: 8 additions & 10 deletions Products/GenericSetup/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,8 @@ def writeDataFile(self, filename, text, content_type, subdir=None):
""" See IExportContext.
"""
if isinstance(text, six.text_type):
raise ValueError("Unicode text is not supported, even if it only "
"contains ascii. Please encode your data. See "
"GS 1.7.0 changes for more")
encoding = self.getEncoding() or 'utf-8'
text = text.encode(encoding)
file = self.openDataFile(filename, content_type, subdir)
file.write(text)
file.close()
Expand Down Expand Up @@ -435,13 +434,13 @@ def writeDataFile(self, filename, text, content_type, subdir=None):
parents.pop()

info = TarInfo(filename)
if isinstance(text, six.text_type):
encoding = self.getEncoding() or 'utf-8'
text = text.encode(encoding)

if isinstance(text, six.binary_type):
stream = BytesIO(text)
info.size = len(text)
elif isinstance(text, six.text_type):
raise ValueError("Unicode text is not supported, even if it only "
"contains ascii. Please encode your data. See "
"GS 1.7.0 changes for more")
else:
# Assume text is a an instance of a class like
# Products.Archetypes.WebDAVSupport.PdataStreamIterator,
Expand Down Expand Up @@ -491,9 +490,8 @@ def writeDataFile(self, filename, text, content_type, subdir=None):
filename = filename[sep+1:]

if six.PY2 and isinstance(text, six.text_type):
raise ValueError("Unicode text is not supported, even if it only "
"contains ascii. Please encode your data. See "
"GS 1.7.0 changes for more")
encoding = self.getEncoding() or 'utf-8'
text = text.encode(encoding)

folder = self._ensureSnapshotsFolder(subdir)

Expand Down
45 changes: 12 additions & 33 deletions Products/GenericSetup/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,15 @@ def test_writeDataFile_unicode(self):

text = u'Kein Weltraum links vom Gerät'
FILENAME = 'unicode.txt'
self._makeFile(FILENAME, printable_bytes)
fqname = self._makeFile(FILENAME, printable_bytes)

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')
ctx.writeDataFile(FILENAME, text, 'text/plain')

with open(fqname, 'rb') as fp:
self.assertEqual(fp.read(), text.encode('UTF-8'))

def test_writeDataFile_new_subdir(self):

Expand Down Expand Up @@ -752,15 +751,17 @@ def test_writeDataFile_simple(self):
def test_writeDataFile_umlauts(self):

text = u'Kein Weltraum links vom Gerät'
now = int(time.time())

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')
ctx.writeDataFile('foo.txt', text, 'text/plain')

fileish = BytesIO(ctx.getArchive())

self._verifyTarballContents(fileish, ['foo.txt'], now)
self._verifyTarballEntry(fileish, 'foo.txt', text.encode('UTF-8'))

def test_writeDataFile_multiple(self):

Expand Down Expand Up @@ -898,9 +899,6 @@ def test_writeDataFile_simple_plain_text(self):
self.assertEqual(fileobj.getContentType(), CONTENT_TYPE)
self.assertEqual(fileobj.data, digits_bytes)

@unittest.skipIf(
six.PY2, 'On Python2 writing unicode files is not supported'
)
def test_writeDataFile_simple_plain_text_unicode(self):
FILENAME = 'simple.txt'
CONTENT_TYPE = 'text/plain'
Expand All @@ -924,25 +922,6 @@ def test_writeDataFile_simple_plain_text_unicode(self):
self.assertEqual(fileobj.getContentType(), CONTENT_TYPE)
self.assertEqual(fileobj.data, CONTENT.encode(ctx._encoding))

@unittest.skipUnless(
six.PY2, 'On Python2 writing unicode files is not supported'
)
def test_writeDataFile_simple_plain_text_unicode_raises(self):
FILENAME = 'simple.txt'
CONTENT_TYPE = 'text/plain'
CONTENT = u'Unicode, with non-ASCII: %s.' % six.unichr(150)

site = DummySite('site').__of__(self.app)
site.setup_tool = DummyTool('setup_tool')
tool = site.setup_tool
ctx = self._makeOne(tool, 'simple', 'latin_1')

self.assertRaises(ValueError, ctx.writeDataFile,
FILENAME, CONTENT, CONTENT_TYPE)
CONTENT = u'No unicode chars'
self.assertRaises(ValueError, ctx.writeDataFile,
FILENAME, CONTENT, CONTENT_TYPE)

def test_writeDataFile_simple_xml(self):

from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
Expand Down

0 comments on commit 3b11531

Please sign in to comment.