|
24 | 24 | from .folders import DataFolder
|
25 | 25 | from .Image.core import ImageFile, ImageArray
|
26 | 26 | from .core.utils import copy_into
|
| 27 | +from .tools.file import HDFFileManager |
27 | 28 |
|
28 | 29 |
|
29 | 30 | def get_hdf_loader(f, default_loader=lambda *args, **kargs: None):
|
@@ -52,68 +53,6 @@ def get_hdf_loader(f, default_loader=lambda *args, **kargs: None):
|
52 | 53 | return getattr(globals()[typ], "read_hdf5", default_loader)
|
53 | 54 |
|
54 | 55 |
|
55 |
| -class HDFFileManager: |
56 |
| - """Context manager for HDF5 files.""" |
57 |
| - |
58 |
| - def __init__(self, filename, mode="r"): |
59 |
| - """Initialise context handler. |
60 |
| -
|
61 |
| - Works out the filename and group in cases the input flename includes a path to a sub group. |
62 |
| -
|
63 |
| - Checks the file is actually an h4py file that is openable with the given mode. |
64 |
| - """ |
65 |
| - self.mode = mode |
66 |
| - self.handle = None |
67 |
| - self.file = None |
68 |
| - self.close = True |
69 |
| - self.group = "" |
70 |
| - # Handle the case we're passed an already open h5py object |
71 |
| - if not isinstance(filename, path_types) or mode == "w": # passed an already open h5py object |
72 |
| - self.filename = filename |
73 |
| - return |
74 |
| - # Here we deal with a string or path filename |
75 |
| - parts = str(filename).split(os.path.sep) |
76 |
| - bits = len(parts) |
77 |
| - for ix in range(bits): |
78 |
| - testname = "/".join(parts[: bits - ix]) |
79 |
| - if path.exists(testname): |
80 |
| - filename = testname |
81 |
| - break |
82 |
| - |
83 |
| - try: |
84 |
| - if not mode.startswith("w"): |
85 |
| - with h5py.File(filename, "r"): |
86 |
| - pass |
87 |
| - except (IOError, OSError) as err: |
88 |
| - raise StonerLoadError(f"{filename} not at HDF5 File") from err |
89 |
| - self.filename = filename |
90 |
| - |
91 |
| - def __enter__(self): |
92 |
| - """Open the hdf file with given mode and navigate to the group.""" |
93 |
| - if isinstance(self.filename, (h5py.File, h5py.Group)): # passed an already open h5py object |
94 |
| - self.handle = self.filename |
95 |
| - if isinstance(self.filename, h5py.Group): |
96 |
| - self.file = self.filename.file |
97 |
| - else: |
98 |
| - self.file = self.filename |
99 |
| - self.close = False |
100 |
| - elif isinstance(self.filename, path_types): # Passed something to open |
101 |
| - handle = h5py.File(self.filename, self.mode) |
102 |
| - self.file = handle |
103 |
| - for grp in self.group.split("/"): |
104 |
| - if grp.strip() != "": |
105 |
| - handle = handle[grp] |
106 |
| - self.handle = handle |
107 |
| - else: |
108 |
| - raise StonerLoadError("Note a resource that can be handled with HDF") |
109 |
| - return self.handle |
110 |
| - |
111 |
| - def __exit__(self, _type, _value, _traceback): |
112 |
| - """Ensure we close the hdf file no matter what.""" |
113 |
| - if self.file is not None and self.close: |
114 |
| - self.file.close() |
115 |
| - |
116 |
| - |
117 | 56 | class HDF5File(DataFile):
|
118 | 57 | """A sub class of DataFile that sores itself in a HDF5File or group.
|
119 | 58 |
|
|
0 commit comments