comparison sat/tools/xml_tools.py @ 3967:f461f11ea176

plugin XEP-0384: Implementation of Automatic Trust Management: - Implementation of Trust Messages (XEP-0434) - Implementation of Automatic Trust Management (XEP-0450) - Implementations directly as part of the OMEMO plugin, since omemo:2 is the only protocol supported by ATM at the moment - Trust system selection updated to allow choice between manual trust with ATM and BTBV - dev-requirements.txt updated to include additional requirements for the e2e tests fix 376
author Syndace <me@syndace.dev>
date Fri, 28 Oct 2022 18:50:06 +0200
parents 323017a4e4d2
children 818db4ca3717
comparison
equal deleted inserted replaced
3966:9f85369294f3 3967:f461f11ea176
18 18
19 19
20 from collections import OrderedDict 20 from collections import OrderedDict
21 import html.entities 21 import html.entities
22 import re 22 import re
23 from typing import Optional, Tuple, Union, Literal, overload 23 from typing import Dict, Optional, Tuple, Union, Literal, overload
24 from xml.dom import NotFoundErr, minidom 24 from xml.dom import NotFoundErr, minidom
25 import xml.etree.ElementTree as ET 25 import xml.etree.ElementTree as ET
26 from lxml import etree 26 from lxml import etree
27 27
28 from twisted.internet import defer 28 from twisted.internet import defer
2028 if content: 2028 if content:
2029 et_elt.text = str(elt) 2029 et_elt.text = str(elt)
2030 for child in elt.elements(): 2030 for child in elt.elements():
2031 et_elt.append(domish_elt_2_et_elt(child, lxml=lxml)) 2031 et_elt.append(domish_elt_2_et_elt(child, lxml=lxml))
2032 return et_elt 2032 return et_elt
2033
2034
2035 def domish_elt_2_et_elt2(element: domish.Element) -> ET.Element:
2036 """
2037 WIP, originally from the OMEMO plugin
2038 """
2039
2040 element_name = element.name
2041 if element.uri is not None:
2042 element_name = "{" + element.uri + "}" + element_name
2043
2044 attrib: Dict[str, str] = {}
2045 for qname, value in element.attributes.items():
2046 attribute_name = qname[1] if isinstance(qname, tuple) else qname
2047 attribute_namespace = qname[0] if isinstance(qname, tuple) else None
2048 if attribute_namespace is not None:
2049 attribute_name = "{" + attribute_namespace + "}" + attribute_name
2050
2051 attrib[attribute_name] = value
2052
2053 result = ET.Element(element_name, attrib)
2054
2055 last_child: Optional[ET.Element] = None
2056 for child in element.children:
2057 if isinstance(child, str):
2058 if last_child is None:
2059 result.text = child
2060 else:
2061 last_child.tail = child
2062 else:
2063 last_child = domish_elt_2_et_elt2(child)
2064 result.append(last_child)
2065
2066 return result