Renamed open_file() --> load_file_obj() and close_file() --> unload_file_obj() to focus more on the management operations on the files that actual file handling operations.

This commit is contained in:
2024-10-10 10:47:44 +02:00
parent 568f747a69
commit 6be3b31247

View File

@ -44,11 +44,11 @@ class HDF5DataOpsManager():
# Define public methods
def open_file(self):
def load_file_obj(self):
if self.file_obj is None:
self.file_obj = h5py.File(self.file_path, self.mode)
def close_file(self):
def unload_file_obj(self):
if self.file_obj:
self.file_obj.flush() # Ensure all data is written to disk
self.file_obj.close()
@ -76,7 +76,7 @@ class HDF5DataOpsManager():
returns a copy of the dataset content in the form of dataframe when possible or numpy array
"""
if self.file_obj is None:
self.open_file()
self.load_file_obj()
dataset_obj = self.file_obj[dataset_name]
# Read dataset content from dataset obj
@ -148,7 +148,7 @@ class HDF5DataOpsManager():
obj.attrs.update(annotation_dict_copy)
except Exception as e:
self.close_file()
self.unload_file_obj()
print(f"An unexpected error occurred: {e}. The file object has been properly closed.")
@ -204,7 +204,7 @@ class HDF5DataOpsManager():
obj.attrs.update(update_dict)
except Exception as e:
self.close_file()
self.unload_file_obj()
print(f"An unexpected error occurred: {e}. The file object has been properly closed.")
def delete_metadata(self, obj_name, annotation_dict):
@ -247,7 +247,7 @@ class HDF5DataOpsManager():
print(msg)
except Exception as e:
self.close_file()
self.unload_file_obj()
print(f"An unexpected error occurred: {e}. The file object has been properly closed.")
@ -293,13 +293,13 @@ class HDF5DataOpsManager():
msg = f"Skipping: Attribute '{old_attr}' does not exist."
print(msg) # Optionally, replace with warnings.warn(msg)
except Exception as e:
self.close_file()
self.unload_file_obj()
print(
f"An unexpected error occurred: {e}. The file object has been properly closed. "
"Please ensure that 'obj_name' exists in the file, and that the keys in 'renaming_map' are valid attributes of the object."
)
self.close_file()
self.unload_file_obj()
def get_metadata(self, obj_path):
""" Get file attributes from object at path = obj_path. For example,
@ -392,7 +392,7 @@ class HDF5DataOpsManager():
# Close the file if it's already open
if self.file_obj is not None:
self.close_file()
self.unload_file_obj()
# Attempt to open the file in 'r+' mode for appending
try: