Skip to content

Commit

Permalink
Remove xpath_function from API for text extraction. Instead, only u…
Browse files Browse the repository at this point in the history
…se `element_as_text` which internally creates the string representation.
  • Loading branch information
MichaelMauderer committed Jan 29, 2025
1 parent 0ac2b91 commit 1309ac3
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions colour_clf_io/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
-------
Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -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
-------
Expand All @@ -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

Expand All @@ -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
------
Expand All @@ -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 "
Expand Down Expand Up @@ -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(
Expand All @@ -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

Expand Down Expand Up @@ -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]

0 comments on commit 1309ac3

Please sign in to comment.