# HG changeset patch # User Goffi # Date 1605189195 -3600 # Node ID a4774f5b6b17e993da320b6e5943960cd4036066 # Parent d6a482a78bdaa2925845abcb76ad9cfc7db92386 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` diff -r d6a482a78bda -r a4774f5b6b17 sat/tools/xml_tools.py --- a/sat/tools/xml_tools.py Thu Nov 12 14:53:15 2020 +0100 +++ b/sat/tools/xml_tools.py Thu Nov 12 14:53:15 2020 +0100 @@ -1812,3 +1812,27 @@ 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*' '}") + else: + strings.append(f"{indent*' '}{elt.toXml()}") + return '\n'.join(strings) + + +def ppElt(elt): + """Pretty print a domish.Element""" + print(pFmtElt(elt))