tools (xml_tools): domish.Element pretty formatting functions:

- `pFmtElt`: pretty format a `domish.Element`
- `ppElt`: use the above function to pretty print a `domish.Element`
This commit is contained in:
Goffi 2020-11-12 14:53:15 +01:00
commit 1aeeffd399

View file

@ -1812,3 +1812,27 @@ def findAll(elt, namespaces=None, names=None):
yield child
for found in findAll(child, namespaces, names):
yield found
def pFmtElt(elt, indent=0):
"""Pretty format a domish.Element"""
strings = []
for child in elt.children:
if domish.IElement.providedBy(child):
strings.append(pFmtElt(child, indent+2))
else:
strings.append(f"{(indent+2)*' '}{child!s}")
if elt.children:
nochild_elt = domish.Element(
(elt.uri, elt.name), elt.defaultUri, elt.attribs, elt.localPrefixes
)
strings.insert(0, f"{indent*' '}{nochild_elt.toXml()[:-2]}>")
strings.append(f"{indent*' '}</{nochild_elt.name}>")
else:
strings.append(f"{indent*' '}{elt.toXml()}")
return '\n'.join(strings)
def ppElt(elt):
"""Pretty print a domish.Element"""
print(pFmtElt(elt))