Skip to content

h5_copy_except

h5_copy_except_paths(src_group, dest_group, except_paths)

Copy an HDF5 file except for a list of paths to ignore

This tries to copy entire groups where possible, to minimise overhead.

Source code in /usr/src/app/checkouts/readthedocs.org/user_builds/european-xfel-offline-calibration/envs/latest/lib/python3.8/site-packages/cal_tools/h5_copy_except.py
def h5_copy_except_paths(src_group, dest_group, except_paths):
    """Copy an HDF5 file except for a list of paths to ignore

    This tries to copy entire groups where possible, to minimise overhead.
    """
    # If src_group/dest_group are file paths, open them with h5py.
    if isinstance(src_group, (str, bytes, os.PathLike)):
        with h5py.File(src_group, 'r') as src_file:
            return h5_copy_except_paths(src_file, dest_group, except_paths)
    if isinstance(dest_group, (str, bytes, os.PathLike)):
        with h5py.File(dest_group, 'a') as dest_file:
            return h5_copy_except_paths(src_group, dest_file, except_paths)

    copy_except_tree(src_group, dest_group, paths_to_tree(except_paths))

paths_to_tree(paths)

Convert paths to a nested-dict tree, with True at leaves

Source code in /usr/src/app/checkouts/readthedocs.org/user_builds/european-xfel-offline-calibration/envs/latest/lib/python3.8/site-packages/cal_tools/h5_copy_except.py
def paths_to_tree(paths):
    """Convert paths to a nested-dict tree, with True at leaves"""
    tree = {}

    for path in paths:
        tree_part = tree
        path_names = path.strip('/').split('/')
        for name in path_names[:-1]:
            tree_part = tree_part.setdefault(name, {})
            if tree_part is True:
                break  # A previous path was a prefix of this one
        else:
            tree_part[path_names[-1]] = True

    return tree