hamyar_paygah.utils.xml_utils

Utility functions relate to XML.

Attributes

E

Functions

get_text(document, tag, namespaces[, prefix])

Return the text content of a direct child element.

get_integer(document, tag, namespaces)

Return the integer value of a child element's text content.

get_bool(document, tag, namespaces)

Return the boolean value of a child element's text content.

get_time(document, tag, namespaces)

Return the time value of a child element's text content.

get_enum_from_boolean_flags(document, namespaces, ...)

Extract an enum member from boolean SOAP flag elements.

Module Contents

hamyar_paygah.utils.xml_utils.E
hamyar_paygah.utils.xml_utils.get_text(document, tag, namespaces, prefix='a')

Return the text content of a direct child element.

Parameters:
  • document (lxml.etree._Element) – Parent XML element containing the child.

  • tag (str) – Child element tag name (without namespace prefix).

  • namespaces (dict[str, str]) – Mapping of namespace prefixes to URIs.

  • prefix (str) – Namespace prefix used in lookup. Defaults to “a”.

Returns:

The element text if found and valid. Returns None if - The element does not exist. - The element text is None. - The element text is “-” or “#”.

Return type:

str | None

hamyar_paygah.utils.xml_utils.get_integer(document, tag, namespaces)

Return the integer value of a child element’s text content.

The function retrieves the element text using get_text and attempts to convert it to an integer. If direct conversion fails, it extracts the first integer sequence found in the text.

Parameters:
  • document (lxml.etree._Element) – Parent XML element containing the child.

  • tag (str) – Child element tag name (without namespace prefix).

  • namespaces (dict[str, str]) – Mapping of namespace prefixes to URIs.

Returns:

The parsed integer if found. Returns None if - The element does not exist. - The element text is None or empty. - No integer value can be extracted.

Return type:

int | None

hamyar_paygah.utils.xml_utils.get_bool(document, tag, namespaces)

Return the boolean value of a child element’s text content.

The function retrieves the element text using get_text and returns True only if the text is exactly “true” (case-insensitive). All other values, including missing or empty elements, return False.

Parameters:
  • document (lxml.etree._Element) – Parent XML element containing the child.

  • tag (str) – Child element tag name (without namespace prefix).

  • namespaces (dict[str, str]) – Mapping of namespace prefixes to URIs.

Returns:

True if the element text equals “true” (case-insensitive). Otherwise, False.

Return type:

bool

hamyar_paygah.utils.xml_utils.get_time(document, tag, namespaces)

Return the time value of a child element’s text content.

The function retrieves the element text using get_text and attempts to parse it using the format “%H:%M:%S”.

Parameters:
  • document (lxml.etree._Element) – Parent XML element containing the child.

  • tag (str) – Child element tag name (without namespace prefix).

  • namespaces (dict[str, str]) – Mapping of namespace prefixes to URIs.

Returns:

A datetime.time object if parsing succeeds. Returns None if: - The element does not exist. - The element text is None. - The text does not match the “%H:%M:%S” format.

Return type:

datetime.time

hamyar_paygah.utils.xml_utils.get_enum_from_boolean_flags(document, namespaces, enum_type)

Extract an enum member from boolean SOAP flag elements.

Iterates over all members of enum_type and evaluates the XML element whose tag name matches member.value using get_bool().

If one or more flags evaluate to True, the last matching enum member in iteration order is returned. If no flags evaluate to True, None is returned.

This function assumes that each enum member’s value corresponds to the XML tag name of a boolean flag element.

Parameters:
  • document (lxml.etree._Element) – XML SOAP response element.

  • namespaces (dict[str, str]) – Namespace mapping used for XPath lookups.

  • enum_type (type[E]) – Enum type whose members map to boolean flag tags.

Returns:

The matching enum member, or None if no flag is set.

Return type:

E | None