From 7726911562246ce50851fc300143541def7cf6a7 Mon Sep 17 00:00:00 2001 From: Jaret Burkett Date: Mon, 31 Jul 2023 11:56:57 -0600 Subject: [PATCH] Allow name to be passed through command line --- run.py | 10 +++++++++- toolkit/config.py | 15 +++++++++------ toolkit/job.py | 4 ++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/run.py b/run.py index 15c57a2a..e6a250be 100644 --- a/run.py +++ b/run.py @@ -36,6 +36,14 @@ def main(): action='store_true', help='Continue running additional jobs even if a job fails' ) + + # flag to continue if failed job + parser.add_argument( + '-n', '--name', + type=str, + default=None, + help='Name to replace [name] tag in config file, useful for shared config file' + ) args = parser.parse_args() config_file_list = args.config_file_list @@ -49,7 +57,7 @@ def main(): for config_file in config_file_list: try: - job = get_job(config_file) + job = get_job(config_file, args.name) job.run() job.cleanup() jobs_completed += 1 diff --git a/toolkit/config.py b/toolkit/config.py index 9d51c3be..602e04ae 100644 --- a/toolkit/config.py +++ b/toolkit/config.py @@ -15,22 +15,24 @@ def get_cwd_abs_path(path): return path -def preprocess_config(config: OrderedDict): +def preprocess_config(config: OrderedDict, name: str = None): if "job" not in config: raise ValueError("config file must have a job key") if "config" not in config: raise ValueError("config file must have a config section") - if "name" not in config["config"]: + if "name" not in config["config"] and name is None: raise ValueError("config file must have a config.name key") # we need to replace tags. For now just [name] - name = config["config"]["name"] + if name is not None: + config["config"]["name"] = name + else: + name = config["config"]["name"] config_string = json.dumps(config) config_string = config_string.replace("[name]", name) config = json.loads(config_string, object_pairs_hook=OrderedDict) return config - # Fixes issue where yaml doesnt load exponents correctly fixed_loader = yaml.SafeLoader fixed_loader.add_implicit_resolver( @@ -44,7 +46,8 @@ fixed_loader.add_implicit_resolver( |\\.(?:nan|NaN|NAN))$''', re.X), list(u'-+0123456789.')) -def get_config(config_file_path): + +def get_config(config_file_path, name=None): # first check if it is in the config folder config_path = os.path.join(TOOLKIT_ROOT, 'config', config_file_path) # see if it is in the config folder with any of the possible extensions if it doesnt have one @@ -75,4 +78,4 @@ def get_config(config_file_path): else: raise ValueError(f"Config file {config_file_path} must be a json or yaml file") - return preprocess_config(config) + return preprocess_config(config, name) diff --git a/toolkit/job.py b/toolkit/job.py index 62d1bcfd..da85505b 100644 --- a/toolkit/job.py +++ b/toolkit/job.py @@ -1,8 +1,8 @@ from toolkit.config import get_config -def get_job(config_path): - config = get_config(config_path) +def get_job(config_path, name=None): + config = get_config(config_path, name) if not config['job']: raise ValueError('config file is invalid. Missing "job" key')