Hi,
GenericSetup has problems handling non-ASCII data. It treats strings
sometimes as ASCII, sometimes as UTF-8, yet it has access to two
variables: its own ISetupContext.getEncoding() (whose use I didn't fully
grok) and CMF's ISetupContext.getSite().getProperty('default_charset').
Attached is a patch using both of them and somewhat working in my setup.
Can knowledgeable people comment on it before I enter a collector issue?
(I'm using GS alongside with CPS, which also needs some patching; yet
basic things, such as exporting-importing an iso8859-15 Title in a CMF
charset-default'ed to iso8859-15, should work)
Thanks!
Yves
Index: GenericSetup/utils.py
===================================================================
--- GenericSetup/utils.py (revision 68510)
+++ GenericSetup/utils.py (working copy)
@@ -498,7 +498,8 @@
"""Export the object as a file body.
"""
self._doc.appendChild(self._exportNode())
- return self._doc.toprettyxml(' ')
+ encoding = self.environ.getEncoding() or 'UTF-8'
+ return self._doc.toprettyxml(' ', encoding=encoding)
def _importBody(self, body):
"""Import the object from the file body.
@@ -617,6 +618,7 @@
node.setAttribute('name', prop_id)
prop = self.context.getProperty(prop_id)
+ encoding = self.environ.getSite().getProperty('default_charset', '') or 'UTF-8'
if isinstance(prop, (tuple, list)):
for value in prop:
child = self._doc.createElement('element')
@@ -625,8 +627,10 @@
else:
if prop_map.get('type') == 'boolean':
prop = str(bool(prop))
+ elif isinstance(prop, str):
+ prop = prop.decode(encoding)
elif not isinstance(prop, basestring):
- prop = str(prop)
+ prop = unicode(prop)
child = self._doc.createTextNode(prop)
node.appendChild(child)
@@ -685,9 +689,10 @@
raise BadRequest('%s cannot be changed' % prop_id)
elements = []
+ encoding = self.environ.getEncoding()
for sub in child.childNodes:
if sub.nodeName == 'element':
- elements.append(sub.getAttribute('value').encode('utf-8'))
+ elements.append(sub.getAttribute('value').encode(encoding))
if elements or prop_map.get('type') == 'multiple selection':
prop_value = tuple(elements) or ()
@@ -696,7 +701,7 @@
else:
# if we pass a *string* to _updateProperty, all other values
# are converted to the right type
- prop_value = self._getNodeText(child).encode('utf-8')
+ prop_value = self._getNodeText(child).encode(encoding)
if not self._convertToBoolean(child.getAttribute('purge')
or 'True'):
_______________________________________________
Zope-CMF maillist - [email protected]
http://mail.zope.org/mailman/listinfo/zope-cmf
See http://collector.zope.org/CMF for bug reports and feature requests