Mercurial > libervia-backend
changeset 4010:818db4ca3717
tools (xml_tools): accept several namespaces in `findAncestor`
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 16 Mar 2023 16:43:08 +0100 |
parents | 48e8b3dba793 |
children | 74d4c9ff893d |
files | sat/tools/xml_tools.py |
diffstat | 1 files changed, 21 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/sat/tools/xml_tools.py Thu Mar 16 15:56:11 2023 +0100 +++ b/sat/tools/xml_tools.py Thu Mar 16 16:43:08 2023 +0100 @@ -20,7 +20,7 @@ from collections import OrderedDict import html.entities import re -from typing import Dict, Optional, Tuple, Union, Literal, overload +from typing import Dict, Optional, Tuple, Union, Literal, overload, Iterable from xml.dom import NotFoundErr, minidom import xml.etree.ElementTree as ET from lxml import etree @@ -1926,15 +1926,32 @@ yield found -def findAncestor(elt, name: str, namespace: Optional[str] = None) -> domish.Element: - """Retrieve ancestor of an element""" +def findAncestor( + elt, + name: str, + namespace: Optional[Union[str, Iterable[str]]] = None + ) -> domish.Element: + """Retrieve ancestor of an element + + @param elt: starting element, its parent will be checked recursively until the + required one if found + @param name: name of the element to find + @param namespace: namespace of the element to find + - None to find any element with that name + - a simple string to find the namespace + - several namespaces can be specified in an iterable, if an element with any of + this namespace and given name is found, it will match + + """ + if isinstance(namespace, str): + namespace = [namespace] 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): + if current.name == name and (namespace is None or current.uri in namespace): return current current = current.parent