From babbb1dd188a0d4fb72d99264dc7ced8e3e76f00 Mon Sep 17 00:00:00 2001 From: Rubel Date: Mon, 20 Nov 2023 13:51:45 +0100 Subject: [PATCH] Clsing hdf file at any situation. --- pynxtools/dataconverter/writer.py | 14 ++++++------ pynxtools/nexus/nexus.py | 38 ++++++++++++++++--------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/pynxtools/dataconverter/writer.py b/pynxtools/dataconverter/writer.py index 23aec41da..903751f99 100644 --- a/pynxtools/dataconverter/writer.py +++ b/pynxtools/dataconverter/writer.py @@ -317,13 +317,13 @@ def add_units_key(dataset, path): def write(self): """Writes the NeXus file with previously validated data from the reader with NXDL attrs.""" - - if self.write_in_memory: - raise ValueError(f"To write in memory and get the file obhect please use " - f"the method get_in_memory_obj()") - - self._process_data_into_hdf5() - self.output_nexus.close() + try: + if self.write_in_memory: + raise ValueError(f"To write in memory and get the file obhect please use " + f"the method get_in_memory_obj()") + self._process_data_into_hdf5() + finally: + self.output_nexus.close() def get_in_memory_obj(self): """Write the nexus file as in-memory obj. diff --git a/pynxtools/nexus/nexus.py b/pynxtools/nexus/nexus.py index a3c2537d7..fdfb36da5 100644 --- a/pynxtools/nexus/nexus.py +++ b/pynxtools/nexus/nexus.py @@ -642,24 +642,26 @@ def full_visit(self, root, hdf_node, name, func): def process_nexus_master_file(self, parser): """Process a nexus master file by processing all its nodes and their attributes""" self.parser = parser - if not self.is_hdf5_file_obj: - self.in_file = h5py.File( - self.input_file_name[0] - if isinstance(self.input_file_name, list) - else self.input_file_name, 'r' - ) - else: - self.in_file = self.input_file_name - self.full_visit(self.in_file, self.in_file, '', self.visit_node) - if self.d_inq_nd is None and self.c_inq_nd is None: - get_default_plotable(self.in_file, self.logger) - # To log the provided concept and concepts founded - if self.c_inq_nd is not None: - for hdf_path in self.hdf_path_list_for_c_inq_nd: - self.logger.info(hdf_path) - # To test if hdf_file is open print(self.in_file.id.valid) - self.in_file.close() - # To test if hdf_file is open print(self.in_file.id.valid) + try: + if not self.is_hdf5_file_obj: + self.in_file = h5py.File( + self.input_file_name[0] + if isinstance(self.input_file_name, list) + else self.input_file_name, 'r' + ) + else: + self.in_file = self.input_file_name + self.full_visit(self.in_file, self.in_file, '', self.visit_node) + if self.d_inq_nd is None and self.c_inq_nd is None: + get_default_plotable(self.in_file, self.logger) + # To log the provided concept and concepts founded + if self.c_inq_nd is not None: + for hdf_path in self.hdf_path_list_for_c_inq_nd: + self.logger.info(hdf_path) + finally: + # To test if hdf_file is open print(self.in_file.id.valid) + self.in_file.close() + # To test if hdf_file is open print(self.in_file.id.valid) @click.command()