Path: blob/master/thirdparty/prettyprint/prettyprint.py
2992 views
#!/usr/bin/env python12#Copyright (c) 2010, Chris Hall <[email protected]>3#All rights reserved.45#Redistribution and use in source and binary forms, with or without modification,6#are permitted provided that the following conditions are met:78#* Redistributions of source code must retain the above copyright notice,9#this list of conditions and the following disclaimer.10#* Redistributions in binary form must reproduce the above copyright notice,11#this list of conditions and the following disclaimer in the documentation12#and/or other materials provided with the distribution.1314#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND15#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED16#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE17#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR18#ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES19#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;20#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND21#ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS23#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425from xml.dom import minidom26from xml.dom import Node2728def format(text):29doc = minidom.parseString(text)30root = doc.childNodes[0]31return root.toprettyxml(indent=' ')3233def formatXML(doc, encoding=None):34root = doc.childNodes[0]35return root.toprettyxml(indent=' ', encoding=encoding)3637def _patch_minidom():38minidom.Text.writexml = _writexml_text39minidom.Element.writexml = _writexml_element40minidom.Node.toprettyxml = _toprettyxml_node4142def _collapse(node):43for child in node.childNodes:44if child.nodeType == Node.TEXT_NODE and len(child.data.strip()) == 0:45child.data = ''46else:47_collapse(child)4849def _writexml_text(self, writer, indent="", addindent="", newl=""):50minidom._write_data(writer, "%s"%(self.data.strip()))5152def _writexml_element(self, writer, indent="", addindent="", newl=""):53# indent = current indentation54# addindent = indentation to add to higher levels55# newl = newline string56writer.write(indent+"<" + self.tagName)5758attrs = self._get_attributes()59a_names = attrs.keys()60a_names.sort()6162for a_name in a_names:63writer.write(" %s=\"" % a_name)64minidom._write_data(writer, attrs[a_name].value)65writer.write("\"")66if self.childNodes:67if self.childNodes[0].nodeType == Node.TEXT_NODE and len(self.childNodes[0].data) > 0:68writer.write(">")69else:70writer.write(">%s"%(newl))71for node in self.childNodes:72node.writexml(writer,indent+addindent,addindent,newl)73if self.childNodes[-1].nodeType == Node.TEXT_NODE and len(self.childNodes[0].data) > 0:74writer.write("</%s>%s" % (self.tagName,newl))75else:76writer.write("%s</%s>%s" % (indent,self.tagName,newl))77else:78writer.write("/>%s"%(newl))7980def _toprettyxml_node(self, indent="\t", newl="\n", encoding = None):81_collapse(self)82# indent = the indentation string to prepend, per level83# newl = the newline string to append84writer = minidom._get_StringIO()85if encoding is not None:86import codecs87# Can't use codecs.getwriter to preserve 2.0 compatibility88writer = codecs.lookup(encoding)[3](writer)89if self.nodeType == Node.DOCUMENT_NODE:90# Can pass encoding only to document, to put it into XML header91self.writexml(writer, "", indent, newl, encoding)92else:93self.writexml(writer, "", indent, newl)94return writer.getvalue()9596_patch_minidom()979899