From 1309ac3354cda7c426569e7e23c63616fb3a4d37 Mon Sep 17 00:00:00 2001 From: Michael Mauderer Date: Thu, 23 Jan 2025 13:39:32 +0000 Subject: [PATCH] Remove `xpath_function` from API for text extraction. Instead, only use `element_as_text` which internally creates the string representation. --- colour_clf_io/parsing.py | 44 ++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/colour_clf_io/parsing.py b/colour_clf_io/parsing.py index a0e8110..60f567d 100644 --- a/colour_clf_io/parsing.py +++ b/colour_clf_io/parsing.py @@ -235,8 +235,8 @@ def check_none(value: T | None, message: str) -> TypeGuard[T]: def child_element( - xml: lxml.etree._Element, name: str, config: ParserConfig, xpath_function: str = "" -) -> lxml.etree._Element | str | None: + xml, name, config: ParserConfig +) -> xml.etree.ElementTree.Element | None: """ Return a named child element of the given XML element. @@ -248,8 +248,6 @@ def child_element( Name of the child element to look for. config Additional parser configuration. - xpath_function - Optional XPath function to evaluate on the child element. Returns ------- @@ -258,7 +256,7 @@ def child_element( :py:data:`None` if the child was not found. """ - elements = child_elements(xml, name, config, xpath_function) + elements = child_elements(xml, name, config) element_count = len(elements) if element_count == 0: @@ -276,8 +274,8 @@ def child_element( def child_elements( - xml: lxml.etree._Element, name: str, config: ParserConfig, xpath_function: str = "" -) -> list[lxml.etree._Element] | list[str]: + xml, name, config: ParserConfig +) -> list[xml.etree.ElementTree.Element]: """ Return all child elements with a given name of an XML element. @@ -289,8 +287,6 @@ def child_elements( Name of the child element to look for. config Additional parser configuration. - xpath_function - Optional XPath function to evaluate on the child element. Returns ------- @@ -301,11 +297,11 @@ def child_elements( if config.clf_namespace_prefix_mapping(): elements = xml.xpath( - f"clf:{name}{xpath_function}", + f"clf:{name}", namespaces=config.clf_namespace_prefix_mapping(), ) else: - elements = xml.xpath(f"{name}{xpath_function}") + elements = xml.xpath(f"{name}") return elements # pyright: ignore @@ -325,8 +321,6 @@ def child_element_or_exception( Name of the child element to look for. config Additional parser configuration. - xpath_function - Optional XPath function to evaluate on the child element. Raises ------ @@ -339,12 +333,6 @@ def child_element_or_exception( """ element = child_element(xml, name, config) - - if isinstance(element, str): - exception = f'Element "{element}" cannot be a string!' - - raise TypeError(exception) - if element is None: exception = ( f"Tried to retrieve child element '{name}' from '{xml}' but child was " @@ -376,12 +364,12 @@ def element_as_text(xml: lxml.etree._Element, name: str, config: ParserConfig) - an empty string is returned. """ - text = child_element(xml, name, config, xpath_function="/text()") + element = child_element(xml, name, config) - if text is None: + if element is None: return "" - return str(text) + return str(element.text) def element_as_float( @@ -406,8 +394,7 @@ def element_as_float( an invalid float representation, ``None`` is returned. """ - text = child_element(xml, name, config, xpath_function="/text()") - + text = element_as_text(xml, name, config) if text is None: return None @@ -504,8 +491,7 @@ def three_floats(text: str | None) -> tuple[float, float, float]: parts = text.split() if len(parts) != 3: - exception = f"Failed to parse three float values from {text}" - - raise ParsingError(exception) - - return float(parts[0]), float(parts[1]), float(parts[2]) + raise ParsingError(f"Failed to parse three float values from {s}") + values = tuple(map(float, parts)) + # Note: Repacking here to satisfy type check. + return values[0], values[1], values[2]