Skip to content

Commit 7e40c0d

Browse files
committed
refactor to use pathlib.Path(), add to UnicodeDecodeError
1 parent 485cd0a commit 7e40c0d

2 files changed

Lines changed: 10 additions & 14 deletions

File tree

dev_tools/tests/test_nxdl_utils.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
33
"""
44

5-
import os
6-
5+
from pathlib import Path
76
import lxml.etree as ET
87
import pytest
98

@@ -33,8 +32,8 @@ def test_get_nexus_classes_units_attributes():
3332

3433
def test_get_node_at_nxdl_path():
3534
"""Test to verify if we receive the right XML element for a given NXDL path"""
36-
local_dir = os.path.abspath(os.path.dirname(__file__))
37-
nxdl_file_path = os.path.join(local_dir, "./NXtest.nxdl.xml")
35+
local_dir = Path(__file__).resolve().parent
36+
nxdl_file_path = local_dir / "NXtest.nxdl.xml"
3837
elem = ET.parse(nxdl_file_path).getroot()
3938
node = nexus.get_node_at_nxdl_path("/ENTRY/NXODD_name", elem=elem)
4039
assert node.attrib["type"] == "NXdata"
@@ -49,9 +48,7 @@ def test_get_node_at_nxdl_path():
4948
)
5049
assert node.attrib["name"] == "long_name"
5150

52-
nxdl_file_path = os.path.join(
53-
local_dir, "../../contributed_definitions/NXiv_temp.nxdl.xml"
54-
)
51+
nxdl_file_path = local_dir.parent.parent / "contributed_definitions" / "NXiv_temp.nxdl.xml"
5552
elem = ET.parse(nxdl_file_path).getroot()
5653
node = nexus.get_node_at_nxdl_path(
5754
"/ENTRY/INSTRUMENT/ENVIRONMENT/voltage_controller", elem=elem
@@ -66,16 +63,14 @@ def test_get_node_at_nxdl_path():
6663

6764
def test_get_inherited_nodes():
6865
"""Test to verify if we receive the right XML element list for a given NXDL path."""
69-
local_dir = os.path.abspath(os.path.dirname(__file__))
66+
local_dir = Path(__file__).resolve().parent
67+
nxdl_file_path = local_dir / "NXtest.nxdl.xml"
7068

71-
nxdl_file_path = os.path.join(local_dir, "./NXtest.nxdl.xml")
7269
elem = ET.parse(nxdl_file_path).getroot()
7370
(_, _, elist) = nexus.get_inherited_nodes(nxdl_path="/ENTRY/NXODD_name", elem=elem)
7471
assert len(elist) == 3
7572

76-
nxdl_file_path = os.path.join(
77-
local_dir, "../../contributed_definitions/NXiv_temp.nxdl.xml"
78-
)
73+
nxdl_file_path = local_dir.parent.parent / "contributed_definitions" / "NXiv_temp.nxdl.xml"
7974

8075
elem = ET.parse(nxdl_file_path).getroot()
8176
(_, _, elist) = nexus.get_inherited_nodes(

dev_tools/utils/nxdl_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def decode_or_not(elem, encoding: str = "utf-8", decode: bool = True):
2929
If `decode` is False, always returns the initial value.
3030
3131
Raises:
32-
ValueError: If a byte string cannot be decoded using the provided encoding.
32+
UnicodeDecodeError: If a byte string cannot be decoded using the provided encoding.
3333
"""
3434
if not decode:
3535
return elem
@@ -46,7 +46,8 @@ def decode_or_not(elem, encoding: str = "utf-8", decode: bool = True):
4646
try:
4747
return elem.decode(encoding)
4848
except UnicodeDecodeError as e:
49-
raise UnicodeDecodeError(f"Error decoding bytes: {e}")
49+
e.add_note(f'Error decoding bytes object: {elem}')
50+
raise
5051

5152
return elem
5253

0 commit comments

Comments
 (0)