Source code for law.contrib.hdf5.formatter

# coding: utf-8

"""
HDF5 target formatters.
"""

from __future__ import annotations

__all__ = ["H5pyFormatter"]

import pathlib

from law.target.formatter import Formatter
from law.target.file import FileSystemFileTarget, get_path
from law.util import no_value
from law._types import Any


[docs] class H5pyFormatter(Formatter): name = "h5py" @classmethod def accepts(cls, path: str | pathlib.Path | FileSystemFileTarget, mode: str) -> bool: return get_path(path).endswith((".hdf5", ".h5")) @classmethod def load(cls, path: str | pathlib.Path | FileSystemFileTarget, *args, **kwargs) -> Any: import h5py # type: ignore[import-untyped, import-not-found] return h5py.File(get_path(path), "r", *args, **kwargs) @classmethod def dump(cls, path: str | pathlib.Path | FileSystemFileTarget, *args, **kwargs) -> Any: import h5py # type: ignore[import-untyped, import-not-found] perm = kwargs.pop("perm", no_value) ret = h5py.File(get_path(path), "w", *args, **kwargs) if perm != no_value: cls.chmod(path, perm) return ret