Skip to content

Commit

Permalink
add: added monkey patch to get at least a ugly output
Browse files Browse the repository at this point in the history
  • Loading branch information
delijati committed Feb 10, 2012
1 parent 8afa021 commit c873eac
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions collective/xmltestreport/utils.py
Expand Up @@ -41,26 +41,26 @@ def prettyXML(tree):
indent(tree)
return ElementTree.tostring(tree)

#XXX Ugly monkey patch to get it at least working,
# better ulgy output than no output

#XXX ugly monkey patch
def _escape_cdata(text, encoding):
# escape character data
def patched_write_encoded(fname, text, encoding='utf8', errors='strict'):
'''Write utf8 string `text` to file names `fname`, with encoding.'''
import codecs
f = codecs.open(fname, 'w', encoding=encoding, errors=errors)
try:
# it's worth avoiding do-nothing calls for strings that are
# shorter than 500 character, or so. assume that's, by far,
# the most common case in most applications.
if "&" in text:
text = text.replace("&", "&")
if "<" in text:
text = text.replace("<", "&lt;")
if ">" in text:
text = text.replace(">", "&gt;")
return text.encode(encoding, "xmlcharrefreplace")
except (TypeError, AttributeError):
ElementTree._raise_serialization_error(text)
#XXX this is the patch
except (UnicodeDecodeError):
return text.decode("utf-8").encode(encoding, "xmlcharrefreplace")
f.write(text.decode('utf8'))
except UnicodeDecodeError:
print "UnicodeDecodeError in file (%s)" % fname
f = codecs.open(fname, 'w', errors=errors)
f.write(text)
finally:
f.close()

ElementTree._escape_cdata = _escape_cdata
def write_html(self, fname, html):
"""Write `html` to `fname`, properly encoded."""
patched_write_encoded(fname, html, 'ascii', 'xmlcharrefreplace')

import coverage.html
coverage.html.HtmlReporter.write_html = write_html

0 comments on commit c873eac

Please sign in to comment.