Modified code to point to new instrument folders location. Also, upgrated code to accept either a user specified location or the default location

This commit is contained in:
2024-08-12 13:40:01 +02:00
parent 7f0e5384ea
commit c876e925a7

View File

@ -16,13 +16,19 @@ ROOT_DIR = os.path.abspath(os.curdir)
file_extensions = ['.ibw','.txt','.dat','.h5','.TXT','.csv']
file_readers = {'ibw': lambda a1: read_xps_ibw_file_as_dict(a1),
'txt': lambda a1: read_txt_files_as_dict(a1,False),
'TXT': lambda a1: read_txt_files_as_dict(a1,False),
'dat': lambda a1: read_txt_files_as_dict(a1,False),
'h5': lambda a1,a2,a3: copy_file_in_group(a1,a2,a3,False),
'ACSM_TOFWARE_txt': lambda a1: read_txt_files_as_dict(a1,False),
'ACSM_TOFWARE_csv': lambda a1: read_txt_files_as_dict(a1,False)}
# Define the instruments directory (modify this as needed or set to None)
default_instruments_dir = None # or provide an absolute path
file_readers = {
'ibw': lambda a1: read_xps_ibw_file_as_dict(a1),
'txt': lambda a1: read_txt_files_as_dict(a1, instruments_dir=default_instruments_dir, work_with_copy=False),
'TXT': lambda a1: read_txt_files_as_dict(a1, instruments_dir=default_instruments_dir, work_with_copy=False),
'dat': lambda a1: read_txt_files_as_dict(a1, instruments_dir=default_instruments_dir, work_with_copy=False),
'h5': lambda a1, a2, a3: copy_file_in_group(a1, a2, a3, work_with_copy=False),
'ACSM_TOFWARE_txt': lambda a1: read_txt_files_as_dict(a1, instruments_dir=default_instruments_dir, work_with_copy=False),
'ACSM_TOFWARE_csv': lambda a1: read_txt_files_as_dict(a1, instruments_dir=default_instruments_dir, work_with_copy=False)
}
def read_xps_ibw_file_as_dict(filename):
"""
@ -116,12 +122,15 @@ def copy_file_in_group(source_file_path, dest_file_obj : h5py.File, dest_group_n
if 'tmp_files' in tmp_file_path:
os.remove(tmp_file_path)
def read_txt_files_as_dict(filename : str , work_with_copy : bool = True ):
def read_txt_files_as_dict(filename: str, instruments_dir: str = None, work_with_copy: bool = True):
# If instruments_dir is not provided, use the default path relative to the module directory
if not instruments_dir:
# Assuming the instruments folder is one level up from the source module directory
module_dir = os.path.dirname(__file__)
instruments_dir = os.path.join(module_dir, '..', 'instruments')
# Get the directory of the current module
module_dir = os.path.dirname(__file__)
# Construct the relative file path
instrument_configs_path = os.path.join(module_dir, 'instruments', 'text_data_sources.yaml')
# Normalize the path (resolves any '..' in the path)
instrument_configs_path = os.path.abspath(os.path.join(instruments_dir,'text_data_sources.yaml'))
with open(instrument_configs_path,'r') as stream:
try:
@ -150,7 +159,7 @@ def read_txt_files_as_dict(filename : str , work_with_copy : bool = True ):
link_to_description = config_dict[key].get('link_to_description', '').replace('/', os.sep)
if link_to_description:
path = os.path.join(module_dir, link_to_description)
path = os.path.join(instruments_dir, link_to_description)
try:
with open(path, 'r') as stream:
description_dict = yaml.load(stream, Loader=yaml.FullLoader)