FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-14465: xml.etree.ElementTree pretty printing by dzeban · Pull Request #4016 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (2) No extension  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
24 changes: 20 additions & 4 deletions Doc/library/xml.etree.elementtree.rst
This file contains hidden or 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
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,8 @@ Functions
arguments. Returns an element instance.


.. function:: tostring(element, encoding="us-ascii", method="xml", *, \
.. function:: tostring(element, encoding="us-ascii", method="xml", \
pretty_print=False, *, indent=' ',
short_empty_elements=True)

Generates a string representation of an XML element, including all
Expand All @@ -596,13 +597,19 @@ Functions
generate a Unicode string (otherwise, a bytestring is generated). *method*
is either ``"xml"``, ``"html"`` or ``"text"`` (default is ``"xml"``).
*short_empty_elements* has the same meaning as in :meth:`ElementTree.write`.
*pretty_print* enables pretty printing with default indent. Keyword-only
*indent* allows to set custom indent.
Returns an (optionally) encoded string containing the XML data.

.. versionadded:: 3.4
The *short_empty_elements* parameter.

.. versionadded:: 3.7
The *pretty_print* and *indent* parameters.

.. function:: tostringlist(element, encoding="us-ascii", method="xml", *, \

.. function:: tostringlist(element, encoding="us-ascii", method="xml", \
pretty_print=False, *, indent=' ', \
short_empty_elements=True)

Generates a string representation of an XML element, including all
Expand All @@ -611,6 +618,8 @@ Functions
generate a Unicode string (otherwise, a bytestring is generated). *method*
is either ``"xml"``, ``"html"`` or ``"text"`` (default is ``"xml"``).
*short_empty_elements* has the same meaning as in :meth:`ElementTree.write`.
*pretty_print* enables pretty printing with default indent. Keyword-only
*indent* allows to set custom indent.
Returns a list of (optionally) encoded strings containing the XML data.
It does not guarantee any specific sequence, except that
``b"".join(tostringlist(element)) == tostring(element)``.
Expand All @@ -620,6 +629,9 @@ Functions
.. versionadded:: 3.4
The *short_empty_elements* parameter.

.. versionadded:: 3.7
The *pretty_print* and *indent* parameters.


.. function:: XML(text, parser=None)

Expand Down Expand Up @@ -921,7 +933,7 @@ ElementTree Objects

.. method:: write(file, encoding="us-ascii", xml_declaration=None, \
default_namespace=None, method="xml", *, \
short_empty_elements=True)
short_empty_elements=True, indent='')

Writes the element tree to a file, as XML. *file* is a file name, or a
:term:`file object` opened for writing. *encoding* [1]_ is the output
Expand All @@ -935,7 +947,8 @@ ElementTree Objects
The keyword-only *short_empty_elements* parameter controls the formatting
of elements that contain no content. If ``True`` (the default), they are
emitted as a single self-closed tag, otherwise they are emitted as a pair
of start/end tags.
of start/end tags. *indent* enables and sets indentation level for the
human-readable output.

The output is either a string (:class:`str`) or binary (:class:`bytes`).
This is controlled by the *encoding* argument. If *encoding* is
Expand All @@ -947,6 +960,9 @@ ElementTree Objects
.. versionadded:: 3.4
The *short_empty_elements* parameter.

.. versionadded:: 3.7
The *indent* parameter.


This is the XML file that is going to be manipulated::

Expand Down
78 changes: 78 additions & 0 deletions Lib/test/test_xml_etree.py
This file contains hidden or 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
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,84 @@ def test_html_empty_elems_serialization(self):
method='html')
self.assertEqual(serialized, expected)

def test_pretty_print_xml(self):
pi = ET.ProcessingInstruction('pi')
comment = ET.Comment('comment')

root = ET.Element(None)
root.append(pi)

child1 = ET.SubElement(root, 'child1')
child1.append(comment)
elem = ET.SubElement(child1, 'elem')
elem.set('a', '1')
elem.set('b', 'txt')
elem.text = 'Oh, wow, some text!'

child2 = ET.SubElement(root, 'child2')
elem1 = ET.SubElement(child2, 'elem1')
elem21 = ET.SubElement(elem1, 'elem21')
elem22 = ET.SubElement(elem1, 'elem22')
elem3 = ET.SubElement(elem21, 'elem3')

elem21.append(pi)
elem22.append(comment)

self.assertEqual(serialize(root, method='xml', indent=' '),
'<?pi?>\n'
'<child1>\n'
' <!--comment-->\n'
' <elem a="1" b="txt">Oh, wow, some text!</elem>\n'
'</child1>\n'
'<child2>\n'
' <elem1>\n'
' <elem21>\n'
' <elem3 />\n'
' <?pi?>\n'
' </elem21>\n'
' <elem22>\n'
' <!--comment-->\n'
' </elem22>\n'
' </elem1>\n'
'</child2>\n')

def test_pretty_print_html(self):
html = ET.Element('html')

head = ET.SubElement(html, 'head')
body = ET.SubElement(html, 'body')

style = ET.SubElement(head, 'style')
style.text = '''
body {background-color: white;}
h1 {color: blue;}
p {color: red;}
'''

p = ET.SubElement(body, 'p')
p.text = 'etree is awesome'

ET.SubElement(body, 'br')

script = ET.SubElement(body, 'script')
script.text = ' document.getElementById("some").innerHTML = "Hello JavaScript!";'

self.assertEqual(serialize(html, method='html', indent=' '),
'<html>\n'
' <head>\n'
' <style>\n'
' body {background-color: white;}\n'
' h1 {color: blue;}\n'
' p {color: red;}\n'
' </style>\n'
' </head>\n'
' <body>\n'
' <p>etree is awesome</p>\n'
' <br>\n'
' <script> document.getElementById("some").innerHTML = "Hello JavaScript!";</script>\n'
' </body>\n'
'</html>')


class XMLPullParserTest(unittest.TestCase):

Expand Down
104 changes: 75 additions & 29 deletions Lib/xml/etree/ElementTree.py
This file contains hidden or 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
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,8 @@ def write(self, file_or_filename,
xml_declaration=None,
default_namespace=None,
method=None, *,
short_empty_elements=True):
short_empty_elements=True,
indent=''):
"""Write element tree to a file as XML.

Arguments:
Expand All @@ -746,6 +747,9 @@ def write(self, file_or_filename,
tag, otherwise they are emitted as a pair
of start/end tags

*indent* -- enables and set indentation level for the human-readable
output.

"""
if not method:
method = "xml"
Expand All @@ -772,8 +776,10 @@ def write(self, file_or_filename,
_serialize_text(write, self._root)
else:
qnames, namespaces = _namespaces(self._root, default_namespace)
newline = "\n" if len(indent) > 0 else ""
serialize = _serialize[method]
serialize(write, self._root, qnames, namespaces,
indent='', addindent=indent, newline=newline,
short_empty_elements=short_empty_elements)

def write_c14n(self, file):
Expand Down Expand Up @@ -896,24 +902,31 @@ def add_qname(qname):
add_qname(text.text)
return qnames, namespaces

def _serialize_xml(write, elem, qnames, namespaces,
short_empty_elements, **kwargs):
def _serialize_xml(write, elem, qnames, namespaces, short_empty_elements,
indent='', addindent='', newline='', **kwargs):
pretty = len(addindent) > 0
tag = elem.tag
text = elem.text
if tag is Comment:
write("<!--%s-->" % text)
write(indent + "<!--%s-->" % text)
elif tag is ProcessingInstruction:
write("<?%s?>" % text)
write(indent + "<?%s?>" % text)
else:
tag = qnames[tag]
if tag is None:
if text:
write(_escape_cdata(text))
for e in elem:
write(indent + _escape_cdata(text, pretty))
for i, e in enumerate(elem):
if i > 0:
write(newline)
_serialize_xml(write, e, qnames, None,
short_empty_elements=short_empty_elements)
short_empty_elements=short_empty_elements,
indent=indent, addindent=addindent,
newline=newline)
if len(elem) > 0:
write(newline + indent)
else:
write("<" + tag)
write(indent + "<" + tag)
items = list(elem.items())
if items or namespaces:
if namespaces:
Expand All @@ -936,15 +949,20 @@ def _serialize_xml(write, elem, qnames, namespaces,
if text or len(elem) or not short_empty_elements:
write(">")
if text:
write(_escape_cdata(text))
write(_escape_cdata(text, pretty))
for e in elem:
write(newline)
_serialize_xml(write, e, qnames, None,
short_empty_elements=short_empty_elements)
short_empty_elements=short_empty_elements,
indent=indent+addindent, addindent=addindent,
newline=newline)
if len(elem) > 0:
write(newline + indent)
write("</" + tag + ">")
else:
write(" />")
if elem.tail:
write(_escape_cdata(elem.tail))
write(_escape_cdata(elem.tail, pretty))

HTML_EMPTY = ("area", "base", "basefont", "br", "col", "frame", "hr",
"img", "input", "isindex", "link", "meta", "param")
Expand All @@ -954,22 +972,29 @@ def _serialize_xml(write, elem, qnames, namespaces,
except NameError:
pass

def _serialize_html(write, elem, qnames, namespaces, **kwargs):
def _serialize_html(write, elem, qnames, namespaces, indent='', addindent='',
newline='', **kwargs):
pretty = len(addindent) > 0
tag = elem.tag
text = elem.text
if tag is Comment:
write("<!--%s-->" % _escape_cdata(text))
write(indent + "<!--%s-->" % _escape_cdata(text, pretty))
elif tag is ProcessingInstruction:
write("<?%s?>" % _escape_cdata(text))
write(indent + "<?%s?>" % _escape_cdata(text, pretty))
else:
tag = qnames[tag]
if tag is None:
if text:
write(_escape_cdata(text))
for e in elem:
_serialize_html(write, e, qnames, None)
write(indent + _escape_cdata(text, pretty))
for i, e in enumerate(elem):
if i > 0:
write(newline)
_serialize_html(write, e, qnames, None, indent=indent,
addindent=addindent, newline=newline)
if len(elem) > 0:
write(newline + indent)
else:
write("<" + tag)
write(indent + "<" + tag)
items = list(elem.items())
if items or namespaces:
if namespaces:
Expand All @@ -996,13 +1021,18 @@ def _serialize_html(write, elem, qnames, namespaces, **kwargs):
if ltag == "script" or ltag == "style":
write(text)
else:
write(_escape_cdata(text))
write(_escape_cdata(text, pretty))
for e in elem:
_serialize_html(write, e, qnames, None)
write(newline)
_serialize_html(write, e, qnames, None, indent=indent+addindent,
addindent=addindent, newline=newline)
if len(elem) > 0:
write(newline + indent)

if ltag not in HTML_EMPTY:
write("</" + tag + ">")
if elem.tail:
write(_escape_cdata(elem.tail))
write(_escape_cdata(elem.tail, pretty))

def _serialize_text(write, elem):
for part in elem.itertext():
Expand Down Expand Up @@ -1058,7 +1088,7 @@ def _raise_serialization_error(text):
"cannot serialize %r (type %s)" % (text, type(text).__name__)
)

def _escape_cdata(text):
def _escape_cdata(text, pretty_print=False):
# escape character data
try:
# it's worth avoiding do-nothing calls for strings that are
Expand All @@ -1070,6 +1100,8 @@ def _escape_cdata(text):
text = text.replace("<", "&lt;")
if ">" in text:
text = text.replace(">", "&gt;")
if pretty_print: # ignore whitespace when pretty-printing
text = text.strip()
return text
except (TypeError, AttributeError):
_raise_serialization_error(text)
Expand Down Expand Up @@ -1117,8 +1149,8 @@ def _escape_attrib_html(text):

# --------------------------------------------------------------------

def tostring(element, encoding=None, method=None, *,
short_empty_elements=True):
def tostring(element, encoding=None, method=None, pretty_print=False, *,
indent=' ', short_empty_elements=True):
"""Generate string representation of XML element.

All subelements are included. If encoding is "unicode", a string
Expand All @@ -1128,12 +1160,22 @@ def tostring(element, encoding=None, method=None, *,
encoding defaulting to US-ASCII, *method* is an optional output which can
be one of "xml" (default), "html", "text" or "c14n".

*pretty_print* enables human-readable indentation, *indent* allows to choose
indent width.

*short_empty_elements* (default) collapses pair tags with no text to the
single self-closed tag.

Returns an (optionally) encoded string containing the XML data.

"""
stream = io.StringIO() if encoding == 'unicode' else io.BytesIO()
if not pretty_print:
indent = ''

ElementTree(element).write(stream, encoding, method=method,
short_empty_elements=short_empty_elements)
short_empty_elements=short_empty_elements,
indent=indent)
return stream.getvalue()

class _ListDataStream(io.BufferedIOBase):
Expand All @@ -1153,12 +1195,16 @@ def write(self, b):
def tell(self):
return len(self.lst)

def tostringlist(element, encoding=None, method=None, *,
short_empty_elements=True):
def tostringlist(element, encoding=None, method=None, pretty_print=False, *,
indent=' ', short_empty_elements=True):
lst = []
stream = _ListDataStream(lst)
if not pretty_print:
indent = ''

ElementTree(element).write(stream, encoding, method=method,
short_empty_elements=short_empty_elements)
short_empty_elements=short_empty_elements,
indent=indent)
return lst


Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
This file contains hidden or 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
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ Eugene Dvurechenski
Karmen Dykstra
Josip Dzolonga
Maxim Dzumanenko
Alexander Dzyoba
Hans Eckardt
Rodolpho Eckhardt
Ulrich Eckhardt
Expand Down
Loading

Back | FazBrowse Home | New Git URL