comparison sat/tools/xml_tools.py @ 3491:2bd75fc2555d

tools (xml_tools): new findAncestor method: this methods help to find a `domish.Element` ancestor with specific name/namespace
author Goffi <goffi@goffi.org>
date Sat, 27 Mar 2021 14:35:07 +0100
parents be6d91572633
children a8259a1f89b2
comparison
equal deleted inserted replaced
3490:509f7a1c67dc 3491:2bd75fc2555d
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2
3 2
4 # SAT: a jabber client 3 # SAT: a jabber client
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6 5
7 # This program is free software: you can redistribute it and/or modify 6 # This program is free software: you can redistribute it and/or modify
15 # GNU Affero General Public License for more details. 14 # GNU Affero General Public License for more details.
16 15
17 # 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
18 # 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/>.
19 18
19
20 import re
21 from typing import Optional
22 import html.entities
23 from collections import OrderedDict
24 from xml.dom import minidom, NotFoundErr
25 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
29 from sat.core import exceptions
20 from sat.core.i18n import _ 30 from sat.core.i18n import _
21 from sat.core.constants import Const as C 31 from sat.core.constants import Const as C
22 from sat.core.log import getLogger 32 from sat.core.log import getLogger
23 33
34
24 log = getLogger(__name__) 35 log = getLogger(__name__)
25
26 from xml.dom import minidom, NotFoundErr
27 from wokkel import data_form
28 from twisted.words.xish import domish
29 from twisted.words.protocols.jabber import jid
30 from twisted.internet import defer
31 from sat.core import exceptions
32 from collections import OrderedDict
33 import html.entities
34 import re
35 36
36 """This library help manage XML used in SàT (parameters, registration, etc)""" 37 """This library help manage XML used in SàT (parameters, registration, etc)"""
37 38
38 SAT_FORM_PREFIX = "SAT_FORM_" 39 SAT_FORM_PREFIX = "SAT_FORM_"
39 SAT_PARAM_SEPARATOR = "_XMLUI_PARAM_" # used to have unique elements names 40 SAT_PARAM_SEPARATOR = "_XMLUI_PARAM_" # used to have unique elements names
1902 yield child 1903 yield child
1903 for found in findAll(child, namespaces, names): 1904 for found in findAll(child, namespaces, names):
1904 yield found 1905 yield found
1905 1906
1906 1907
1908 def findAncestor(elt, name: str, namespace: Optional[str] = None) -> domish.Element:
1909 """Retrieve ancestor of an element"""
1910 current = elt.parent
1911 while True:
1912 if current is None:
1913 raise exceptions.NotFound(
1914 f"Can't find any ancestor {name!r} (xmlns: {namespace!r})"
1915 )
1916 if current.name == name and (namespace is None or current.uri == namespace):
1917 return current
1918 current = current.parent
1919
1920
1907 def pFmtElt(elt, indent=0, defaultUri=""): 1921 def pFmtElt(elt, indent=0, defaultUri=""):
1908 """Pretty format a domish.Element""" 1922 """Pretty format a domish.Element"""
1909 strings = [] 1923 strings = []
1910 for child in elt.children: 1924 for child in elt.children:
1911 if domish.IElement.providedBy(child): 1925 if domish.IElement.providedBy(child):