Skip to content

Commit

Permalink
add ability to set up config and caching
Browse files Browse the repository at this point in the history
  • Loading branch information
MialLewis committed Nov 10, 2023
1 parent 3de46ad commit 292adc5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
4 changes: 3 additions & 1 deletion EVSVesuvio/config/vesuvio.user.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# properties that suit your particular installation.
# properties that suit your particular installation.
caching.location =
caching.experiment =
68 changes: 62 additions & 6 deletions EVSVesuvio/scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 292adc5

Please sign in to comment.