From 68cd2b1ed30e98a0cb7d89b45662dff22f3249db Mon Sep 17 00:00:00 2001 From: Loic Huder Date: Fri, 22 Oct 2021 08:35:05 +0200 Subject: [PATCH] Fix joining of paths in hdf file (was OS-dependent) --- h5grove/content.py | 7 ++++--- h5grove/utils.py | 7 +++++++ test/test_utils.py | 13 +++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 test/test_utils.py diff --git a/h5grove/content.py b/h5grove/content.py index 62c1d39..cee60ea 100644 --- a/h5grove/content.py +++ b/h5grove/content.py @@ -13,6 +13,7 @@ attr_metadata, get_array_stats, get_entity_from_file, + hdf_path_join, parse_slice, sorted_dict, ) @@ -189,9 +190,9 @@ def __init__(self, path: str, h5py_entity: h5py.Group, h5file: h5py.File): def _get_child_metadata_content(self, depth=0): return [ - create_content(self._h5file, os.path.join(self._path, child_path)).metadata( - depth - ) + create_content( + self._h5file, hdf_path_join(self._path, child_path) + ).metadata(depth) for child_path in self._h5py_entity.keys() ] diff --git a/h5grove/utils.py b/h5grove/utils.py index b41240f..7a8ea07 100644 --- a/h5grove/utils.py +++ b/h5grove/utils.py @@ -158,3 +158,10 @@ def get_array_stats(data: np.ndarray) -> Dict[str, Union[float, int, None]]: "mean": cast(np.mean(data)), "std": cast(np.std(data)), } + + +def hdf_path_join(prefix, suffix): + if prefix == "/": + return f"/{suffix}" + + return f'{prefix.rstrip("/")}/{suffix}' diff --git a/test/test_utils.py b/test/test_utils.py new file mode 100644 index 0000000..31e480b --- /dev/null +++ b/test/test_utils.py @@ -0,0 +1,13 @@ +from h5grove.utils import hdf_path_join + + +def test_root_path_join(): + assert hdf_path_join("/", "child") == "/child" + + +def test_group_path_join(): + assert hdf_path_join("/group1/group2", "data") == "/group1/group2/data" + + +def test_group_path_join_trailing(): + assert hdf_path_join("/group1/group2/", "data") == "/group1/group2/data"