Skip to content

Commit

Permalink
add function for decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
rettigl authored and lukaspie committed Sep 11, 2024
1 parent cdc1719 commit bbf8060
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions dev_tools/utils/nxdl_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
from lxml.etree import ParseError as xmlER


def decode_or_not(elem):
"""Decodes a byte array to string if necessary"""
if isinstance(elem, bytes):
elem = elem.decode("UTF-8")
return elem


def remove_namespace_from_tag(tag):
"""Helper function to remove the namespace from an XML tag."""

Expand Down Expand Up @@ -765,28 +772,16 @@ def get_best_child(nxdl_elem, hdf_node, hdf_name, hdf_class_name, nexus_type):
corresponding to the html documentation name html_name"""
bestfit = -1
bestchild = None
try:
if (
"name" in nxdl_elem.attrib.keys()
and nxdl_elem.attrib["name"] == "NXdata"
and hdf_node is not None
and hdf_node.parent is not None
and hdf_node.parent.attrs.get("NX_class").decode("UTF-8") == "NXdata"
):
(fnd_child, fit) = get_best_nxdata_child(nxdl_elem, hdf_node, hdf_name)
if fnd_child is not None:
return (fnd_child, fit)
except AttributeError:
if (
"name" in nxdl_elem.attrib.keys()
and nxdl_elem.attrib["name"] == "NXdata"
and hdf_node is not None
and hdf_node.parent is not None
and hdf_node.parent.attrs.get("NX_class") == "NXdata"
):
(fnd_child, fit) = get_best_nxdata_child(nxdl_elem, hdf_node, hdf_name)
if fnd_child is not None:
return (fnd_child, fit)
if (
"name" in nxdl_elem.attrib.keys()
and nxdl_elem.attrib["name"] == "NXdata"
and hdf_node is not None
and hdf_node.parent is not None
and decode_or_not(hdf_node.parent.attrs.get("NX_class")) == "NXdata"
):
(fnd_child, fit) = get_best_nxdata_child(nxdl_elem, hdf_node, hdf_name)
if fnd_child is not None:
return (fnd_child, fit)
for child in nxdl_elem:
if not isinstance(child.tag, str):
continue
Expand Down

0 comments on commit bbf8060

Please sign in to comment.