comparison sat/tools/xml_tools.py @ 3955:323017a4e4d2

tools (xml_tools): `domish_elt_2_et_elt` and `et_elt_2_domish_elt` now handle lxml.etree
author Goffi <goffi@goffi.org>
date Fri, 28 Oct 2022 18:47:17 +0200
parents 4cb38c8312a1
children f461f11ea176
comparison
equal deleted inserted replaced
3954:1ab16449577b 3955:323017a4e4d2
15 15
16 # You should have received a copy of the GNU Affero General Public License 16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 19
20 from collections import OrderedDict
21 import html.entities
20 import re 22 import re
21 from typing import Optional, Tuple 23 from typing import Optional, Tuple, Union, Literal, overload
22 import html.entities 24 from xml.dom import NotFoundErr, minidom
23 from collections import OrderedDict 25 import xml.etree.ElementTree as ET
24 from xml.dom import minidom, NotFoundErr 26 from lxml import etree
27
28 from twisted.internet import defer
29 from twisted.words.protocols.jabber import jid
25 from twisted.words.xish import domish 30 from twisted.words.xish import domish
26 from twisted.words.protocols.jabber import jid
27 from twisted.internet import defer
28 from wokkel import data_form 31 from wokkel import data_form
32
29 from sat.core import exceptions 33 from sat.core import exceptions
34 from sat.core.constants import Const as C
30 from sat.core.i18n import _ 35 from sat.core.i18n import _
31 from sat.core.constants import Const as C
32 from sat.core.log import getLogger 36 from sat.core.log import getLogger
33 import xml.etree.ElementTree as ET
34 37
35 38
36 log = getLogger(__name__) 39 log = getLogger(__name__)
37 40
38 """This library help manage XML used in SàT (parameters, registration, etc)""" 41 """This library help manage XML used in SàT (parameters, registration, etc)"""
1978 if end_idx == -1: 1981 if end_idx == -1:
1979 raise ValueError("Invalid ET name") 1982 raise ValueError("Invalid ET name")
1980 return name[1:end_idx], name[end_idx+1:] 1983 return name[1:end_idx], name[end_idx+1:]
1981 1984
1982 1985
1983 def et_elt_2_domish_elt(et_elt: ET.Element) -> domish.Element: 1986 def et_elt_2_domish_elt(et_elt: Union[ET.Element, etree.Element]) -> domish.Element:
1984 """Convert ElementTree element to Twisted's domish.Element 1987 """Convert ElementTree element to Twisted's domish.Element
1985 1988
1986 Note: this is a naive implementation, adapted to XMPP, and some content are ignored 1989 Note: this is a naive implementation, adapted to XMPP, and some content are ignored
1987 (attributes namespaces, tail) 1990 (attributes namespaces, tail)
1988 """ 1991 """
1993 for child in et_elt: 1996 for child in et_elt:
1994 elt.addChild(et_elt_2_domish_elt(child)) 1997 elt.addChild(et_elt_2_domish_elt(child))
1995 return elt 1998 return elt
1996 1999
1997 2000
1998 def domish_elt_2_et_elt(elt: domish.Element) -> ET.Element: 2001 @overload
2002 def domish_elt_2_et_elt(elt: domish.Element, lxml: Literal[False]) -> ET.Element:
2003 ...
2004
2005 @overload
2006 def domish_elt_2_et_elt(elt: domish.Element, lxml: Literal[True]) -> etree.Element:
2007 ...
2008
2009 @overload
2010 def domish_elt_2_et_elt(
2011 elt: domish.Element, lxml: bool
2012 ) -> Union[ET.Element, etree.Element]:
2013 ...
2014
2015 def domish_elt_2_et_elt(elt, lxml = False):
1999 """Convert Twisted's domish.Element to ElementTree equivalent 2016 """Convert Twisted's domish.Element to ElementTree equivalent
2000 2017
2001 Note: this is a naive implementation, adapter to XMPP, and some text content may be 2018 Note: this is a naive implementation, adapted to XMPP, and some text content may be
2002 missing (content put after a tag, i.e. what would go to the "tail" attribute of ET 2019 missing (content put after a tag, i.e. what would go to the "tail" attribute of ET
2003 Element) 2020 Element)
2004 """ 2021 """
2005 tag = f"{{{elt.uri}}}{elt.name}" if elt.uri else elt.name 2022 tag = f"{{{elt.uri}}}{elt.name}" if elt.uri else elt.name
2006 et_elt = ET.Element(tag, attrib=elt.attributes) 2023 if lxml:
2024 et_elt = etree.Element(tag, attr=elt.attributes)
2025 else:
2026 et_elt = ET.Element(tag, attrib=elt.attributes)
2007 content = str(elt) 2027 content = str(elt)
2008 if content: 2028 if content:
2009 et_elt.text = str(elt) 2029 et_elt.text = str(elt)
2010 for child in elt.elements(): 2030 for child in elt.elements():
2011 et_elt.append(domish_elt_2_et_elt(child)) 2031 et_elt.append(domish_elt_2_et_elt(child, lxml=lxml))
2012 return et_elt 2032 return et_elt