Created
August 28, 2012 14:25
-
-
Save rbanffy/3498469 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------- 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