From cdc1719c85a244eed49864a883b72f7745f07e18 Mon Sep 17 00:00:00 2001 From: rettigl Date: Tue, 10 Sep 2024 10:31:36 +0200 Subject: [PATCH 1/2] decode binary strings in nxdl_utils.py --- dev_tools/utils/nxdl_utils.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/dev_tools/utils/nxdl_utils.py b/dev_tools/utils/nxdl_utils.py index ee4ac8473e..00add0b0e7 100644 --- a/dev_tools/utils/nxdl_utils.py +++ b/dev_tools/utils/nxdl_utils.py @@ -765,16 +765,28 @@ 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 - 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) + 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) for child in nxdl_elem: if not isinstance(child.tag, str): continue From bbf806016a5d62dd5630c4bec53e6bef04942d93 Mon Sep 17 00:00:00 2001 From: rettigl Date: Tue, 10 Sep 2024 22:59:37 +0200 Subject: [PATCH 2/2] add function for decoding --- dev_tools/utils/nxdl_utils.py | 39 +++++++++++++++-------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/dev_tools/utils/nxdl_utils.py b/dev_tools/utils/nxdl_utils.py index 00add0b0e7..42a96e9f40 100644 --- a/dev_tools/utils/nxdl_utils.py +++ b/dev_tools/utils/nxdl_utils.py @@ -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.""" @@ -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