Moved make_file_copy internal function to g5505_utils.py module because it can be reused accross file readers.

This commit is contained in:
2024-03-19 11:58:08 +01:00
parent 7fe254755f
commit 2271be6ecc

View File

@@ -11,6 +11,7 @@ import os
import tempfile
import shutil
import h5py
import g5505_utils as utils
ROOT_DIR = os.path.abspath(os.curdir)
@@ -63,38 +64,13 @@ def read_xps_ibw_file_as_dict(filename):
return file_dict
def make_file_copy(source_file_path):
pathtail, filename = os.path.split(source_file_path)
backup_filename = 'backup_'+ filename
# Path
ROOT_DIR = os.path.abspath(os.curdir)
tmp_dirpath = os.path.join(ROOT_DIR,'tmp_files')
if not os.path.exists(tmp_dirpath):
os.mkdir(tmp_dirpath)
tmp_file_path = os.path.join(tmp_dirpath,backup_filename)
shutil.copy(source_file_path, tmp_file_path)
return tmp_file_path
def copy_file_in_group(source_file_path, dest_file_obj : h5py.File, dest_group_name):
# Create copy of original file to avoid possible file corruption and work with it.
pathtail, filename = os.path.split(source_file_path)
backup_filename = 'backup_'+ filename
# Path
ROOT_DIR = os.path.abspath(os.curdir)
tmp_file_path = utils.make_file_copy(source_file_path)
tmp_dirpath = os.path.join(ROOT_DIR,'tmp_files')
if not os.path.exists(tmp_dirpath):
os.mkdir(tmp_dirpath)
tmp_file_path = os.path.join(tmp_dirpath,backup_filename)
shutil.copy(source_file_path, tmp_file_path)
# Open backup h5 file and copy complet filesystem directory onto a group in h5file
with h5py.File(os.path.join(tmp_dirpath,backup_filename),'r') as src_file:
with h5py.File(tmp_file_path,'r') as src_file:
dest_file_obj.copy(source= src_file['/'], dest= dest_group_name)
if 'tmp_files' in tmp_file_path:
@@ -131,9 +107,9 @@ def read_txt_files_as_dict(filename : str ):
header_dict = {}
data_start = False
# Work with copy of the file for safety
tmp_filename = make_file_copy(filename)
tmp_filename = utils.make_file_copy(source_file_path=filename)
with open(filename,'r') as f:
with open(tmp_filename,'r') as f:
file_encoding = f.encoding
table_preamble = ""
for line_number, line in enumerate(f):