Salome HOME
style: black format
[tools/sat.git] / src / ElementTree.py
1 """
2 using VERSION 1.3.0 native xml.etree.ElementTree for python3
3 appending method tostring serialize 'pretty_xml'
4 """
5
6 import sys
7 import debug as DBG
8
9 _versionPython = sys.version_info[0]
10
11 if _versionPython < 3:
12     # python2 previous historic mode
13     import src.ElementTreePython2 as etree
14
15     DBG.write(
16         "ElementTree Python2 %s" % etree.VERSION, etree.__file__, DBG.isDeveloper()
17     )
18     tostring = etree.tostring
19
20 else:
21     # python3 mode
22     # import xml.etree.ElementTree as etree # native version
23     import src.ElementTreePython3 as etree  # VERSION 1.3.0 plus _serialize 'pretty_xml'
24
25     DBG.write(
26         "ElementTree Python3 %s" % etree.VERSION, etree.__file__, DBG.isDeveloper()
27     )
28
29     def tostring(node, encoding="utf-8"):
30         """
31         fix output as str with encoding='unicode' because python3
32         If encoding is "unicode", a string is returned.
33         Otherwise a bytestring is returned
34         """
35         try:
36             aStr = etree.tostring(node, encoding="unicode", method="pretty_xml")
37         except:
38             print("*****************************\n problem node", node)
39             # try no pretty
40             aStr = etree.tostring(node, encoding="unicode")
41         # if be byte
42         # aStr = aStr.decode('utf-8')
43         return aStr
44
45
46 # common use
47 Element = etree.Element
48 parse = etree.parse