comparison sat/tools/xml_tools.py @ 3396:a4774f5b6b17

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`
author Goffi <goffi@goffi.org>
date Thu, 12 Nov 2020 14:53:15 +0100
parents d6a482a78bda
children 987198910e56
comparison
equal deleted inserted replaced
3395:d6a482a78bda 3396:a4774f5b6b17
1810 and (not namespaces or child.uri in namespaces) 1810 and (not namespaces or child.uri in namespaces)
1811 ): 1811 ):
1812 yield child 1812 yield child
1813 for found in findAll(child, namespaces, names): 1813 for found in findAll(child, namespaces, names):
1814 yield found 1814 yield found
1815
1816
1817 def pFmtElt(elt, indent=0):
1818 """Pretty format a domish.Element"""
1819 strings = []
1820 for child in elt.children:
1821 if domish.IElement.providedBy(child):
1822 strings.append(pFmtElt(child, indent+2))
1823 else:
1824 strings.append(f"{(indent+2)*' '}{child!s}")
1825 if elt.children:
1826 nochild_elt = domish.Element(
1827 (elt.uri, elt.name), elt.defaultUri, elt.attribs, elt.localPrefixes
1828 )
1829 strings.insert(0, f"{indent*' '}{nochild_elt.toXml()[:-2]}>")
1830 strings.append(f"{indent*' '}</{nochild_elt.name}>")
1831 else:
1832 strings.append(f"{indent*' '}{elt.toXml()}")
1833 return '\n'.join(strings)
1834
1835
1836 def ppElt(elt):
1837 """Pretty print a domish.Element"""
1838 print(pFmtElt(elt))