dabo Commit
Revision 5092
Date: 2009-03-01 09:13:59 -0800 (Sun, 01 Mar 2009)
Author: Ed
Trac: http://trac.dabodev.com/changeset/5092
Changed:
U trunk/dabo/lib/xmltodict.py
Log:
Added code to handle the fact that we were doubling up ampersands to 'escape'
them when converting dicts to xml, but not un-doubling them when going the
other way.
Diff:
Modified: trunk/dabo/lib/xmltodict.py
===================================================================
--- trunk/dabo/lib/xmltodict.py 2009-02-27 23:04:02 UTC (rev 5091)
+++ trunk/dabo/lib/xmltodict.py 2009-03-01 17:13:59 UTC (rev 5092)
@@ -91,10 +91,15 @@
self._currPropAtt = ""
else:
element = {"name": name} #.encode()}
- if len(attributes) > 0:
+ if attributes:
for att in self.attsToSkip:
- if attributes.has_key(att):
+ try:
del attributes[att]
+ except KeyError:
+ pass
+ # Unescape any escaped values
+ for kk, vv in attributes.items():
+ attributes[kk] = unescape(vv)
element["attributes"] = attributes
# Push element onto the stack and make it a
child of parent
@@ -230,23 +235,36 @@
slsh = "\\"
# val = val.replace(slsh, slsh+slsh)
if not noEscape:
- # First escape internal ampersands. We need to double them up
due to a
- # quirk in wxPython and the way it displays this character.
- val = val.replace("&", "&&")
- # Escape any internal quotes
- val = val.replace('"', '"').replace("'", "'")
- # Escape any high-order characters
- chars = []
- for pos, char in enumerate(list(val)):
- if ord(char) > 127:
- chars.append("&#%s;" % ord(char))
- else:
- chars.append(char)
- val = "".join(chars)
- val = val.replace("<", "<").replace(">", ">")
+ val = escape(val)
return "%s%s%s" % (qt, val, qt)
+def escape(val):
+ """Escape any characters that cannot be stored directly in XML."""
+ # First escape internal ampersands. We need to double them up due to a
+ # quirk in wxPython and the way it displays this character.
+ val = val.replace("&", "&&")
+ # Escape any internal quotes
+ val = val.replace('"', '"').replace("'", "'")
+ # Escape any high-order characters
+ chars = []
+ for pos, char in enumerate(list(val)):
+ if ord(char) > 127:
+ chars.append("&#%s;" % ord(char))
+ else:
+ chars.append(char)
+ val = "".join(chars)
+ val = val.replace("<", "<").replace(">", ">")
+ return val
+
+
+def unescape(val):
+ """Reverse the escape() process to re-create the original values. The
parser
+ handles the XML conventions; we need to reverse the doubled-up
ampersands."""
+ val = val.replace("&&", "&")
+ return val
+
+
def dicttoxml(dct, level=0, header=None, linesep=None):
"""Given a Python dictionary, return an xml string.
@@ -284,7 +302,6 @@
for mthd, cd in dct["code"].items():
# Convert \n's in the code to eol:
cd = eol.join(cd.splitlines())
-
# Make sure that the code ends with a
linefeed
if not cd.endswith(eol):
cd += eol
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message:
http://leafe.com/archives/byMID/[email protected]