From 292adc51647dc6332835ecd4177f243616b8a86c Mon Sep 17 00:00:00 2001 From: MialLewis <95620982+MialLewis@users.noreply.github.com> Date: Fri, 10 Nov 2023 17:12:28 +0000 Subject: [PATCH] add ability to set up config and caching --- EVSVesuvio/config/vesuvio.user.properties | 4 +- EVSVesuvio/scripts/__init__.py | 68 +++++++++++++++++++++-- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/EVSVesuvio/config/vesuvio.user.properties b/EVSVesuvio/config/vesuvio.user.properties index 66768fd7..912419db 100644 --- a/EVSVesuvio/config/vesuvio.user.properties +++ b/EVSVesuvio/config/vesuvio.user.properties @@ -1 +1,3 @@ -# properties that suit your particular installation. \ No newline at end of file +# properties that suit your particular installation. +caching.location = +caching.experiment = \ No newline at end of file diff --git a/EVSVesuvio/scripts/__init__.py b/EVSVesuvio/scripts/__init__.py index 99787b72..af648857 100644 --- a/EVSVesuvio/scripts/__init__.py +++ b/EVSVesuvio/scripts/__init__.py @@ -1,20 +1,76 @@ """Package defining top-level application and entry points. """ +import argparse import os from shutil import copyfile +VESUVIO_CONFIG_FILE = "vesuvio.user.properties" + def main(): - make_config_dir() + parser = __set_up_parser() + args = parser.parse_args() + config_dir = os.path.join(os.path.expanduser("~"), '.mvesuvio') + cache_dir = config_dir if not args.set_cache else args.set_cache + experiment = "default" if not args.set_experiment else args.set_experiment + if __setup_config_dir(config_dir): + __set_config_vars(config_dir, {'caching.location': cache_dir, + 'caching.experiment': experiment}) + __setup_expr_dir(cache_dir, experiment) -def make_config_dir(): - config_dir = os.path.join(os.path.expanduser("~"), '.mvesuvio') - if not os.path.isdir(config_dir): - os.mkdir(config_dir) - copyfile('EVSVesuvio/config/vesuvio.user.properties', f'{config_dir}/vesuvio.user.properties') + +def __set_up_parser(): + parser = argparse.ArgumentParser(description="Package to analyse Vesuvio instrument data") + parser.add_argument("--set-cache", "-c", help="set the cache directory", default="", type=str) + parser.add_argument("--set-experiment", "-e", help="set the current experiment", default="", type=str) + return parser + + +def __setup_config_dir(config_dir): + success = __mk_dir('config', config_dir) + if success: + copyfile('EVSVesuvio/config/vesuvio.user.properties', f'{config_dir}/{VESUVIO_CONFIG_FILE}') + return success + + +def __setup_expr_dir(cache_dir, experiment): + expr_path = os.path.join(cache_dir, "experiments", experiment) + __mk_dir('experiment', expr_path) + + +def __mk_dir(type, path): + success = False + if not os.path.isdir(path): + try: + os.makedirs(path) + success = True + except: + print(f'Unable to make {type} directory at location: {path}') + return success + + +def __set_config_vars(config_dir, var_dict): + file_path = f'{config_dir}/{VESUVIO_CONFIG_FILE}' + with open(file_path, 'r') as file: + lines = file.readlines() + + updated_lines = [] + for line in lines: + match = False + for var in var_dict: + if line.startswith(var): + updated_lines.append(f'{var}={var_dict[var]}\n') + match = True + break + if not match: + updated_lines.append(line) + + with open(file_path, 'w') as file: + file.writelines(updated_lines) if __name__ == '__main__': + print("test") main()