diff --git a/sat/tools/xml_tools.py b/sat/tools/xml_tools.py index 719fb8a2..e21e219e 100644 --- a/sat/tools/xml_tools.py +++ b/sat/tools/xml_tools.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 - # SAT: a jabber client # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) @@ -17,21 +16,23 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . + +import re +from typing import Optional +import html.entities +from collections import OrderedDict +from xml.dom import minidom, NotFoundErr +from twisted.words.xish import domish +from twisted.words.protocols.jabber import jid +from twisted.internet import defer +from wokkel import data_form +from sat.core import exceptions from sat.core.i18n import _ from sat.core.constants import Const as C from sat.core.log import getLogger -log = getLogger(__name__) -from xml.dom import minidom, NotFoundErr -from wokkel import data_form -from twisted.words.xish import domish -from twisted.words.protocols.jabber import jid -from twisted.internet import defer -from sat.core import exceptions -from collections import OrderedDict -import html.entities -import re +log = getLogger(__name__) """This library help manage XML used in SàT (parameters, registration, etc)""" @@ -1904,6 +1905,19 @@ def findAll(elt, namespaces=None, names=None): yield found +def findAncestor(elt, name: str, namespace: Optional[str] = None) -> domish.Element: + """Retrieve ancestor of an element""" + current = elt.parent + while True: + if current is None: + raise exceptions.NotFound( + f"Can't find any ancestor {name!r} (xmlns: {namespace!r})" + ) + if current.name == name and (namespace is None or current.uri == namespace): + return current + current = current.parent + + def pFmtElt(elt, indent=0, defaultUri=""): """Pretty format a domish.Element""" strings = []