Skip to content

Instantly share code, notes, and snippets.

@rbanffy
Created August 28, 2012 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbanffy/3498469 to your computer and use it in GitHub Desktop.
Save rbanffy/3498469 to your computer and use it in GitHub Desktop.
--------------- quintagroup/transmogrifier/adapters/importing.py ---------------
index 70f21ae..57e3bcd 100644
@@ -11,9 +11,23 @@ from collective.transmogrifier.interfaces import ITransmogrifier
from quintagroup.transmogrifier.interfaces import IImportDataCorrector
+from xml.parsers.expat import ExpatError
+
EXISTING_UIDS = {}
REFERENCE_QUEUE = {}
+def guess_encoding(text):
+ for best_enc in ['utf-8', 'us-ascii', 'iso-8859-1', 'iso-8859-2',
+ 'windows-1250', 'windows-1252']:
+ try:
+ unicode(text,best_enc,"strict")
+ except:
+ pass
+ else:
+ break
+ return best_enc
+
+
class ReferenceImporter(object):
""" Demarshall content from xml file by using of Marshall product.
"""
@@ -45,14 +59,20 @@ class ReferenceImporter(object):
def importReferences(self, data):
""" Marshall 1.0.0 doesn't import references, do it manually.
"""
- doc = minidom.parseString(data)
+ try:
+ doc = minidom.parseString(data)
+ except ExpatError:
+ doc = minidom.parseString(data.decode(
+ guess_encoding(data)).encode('utf-8'))
root = doc.documentElement
for fname in self.context.Schema().keys():
- if not isinstance(self.context.Schema()[fname], atapi.ReferenceField):
+ if not isinstance(self.context.Schema()[fname],
+ atapi.ReferenceField):
continue
uids = []
validUIDs = True
- elements = [i for i in root.getElementsByTagName('field') if i.getAttribute('name') == fname]
+ elements = [i for i in root.getElementsByTagName('field')
+ if i.getAttribute('name') == fname]
if not elements:
# if needed elements are absent skip this field
# update as much as posible fields and don't raise exceptions
@@ -67,7 +87,8 @@ class ReferenceImporter(object):
mutator = self.context.Schema()[fname].getMutator(self.context)
mutator(uids)
else:
- suid = str(root.getElementsByTagName('uid')[0].firstChild.nodeValue.strip())
+ suid = str(root.getElementsByTagName('uid')\
+ [0].firstChild.nodeValue.strip())
REFERENCE_QUEUE[suid] = {}
REFERENCE_QUEUE[suid][fname] = uids
root.removeChild(elem)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment