Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions ush/python_utils/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,25 +465,26 @@ def update_dict(dict_o, dict_t, provide_default=False):

def check_structure_dict(dict_o, dict_t):
"""Check if a dictionary's structure follows a template.
The invalid entries are printed to the screen.
The invalid entries are returned as a list of lists.
If all entries are valid, returns an empty dictionary

Args:
dict_o: target dictionary
dict_t: template dictionary to compare structure to
Returns:
Boolean
dict: Invalid key-value pairs.
"""
inval = {}
for k, v in dict_o.items():
if k in dict_t.keys():
v1 = dict_t[k]
if isinstance(v, dict) and isinstance(v1, dict):
r = check_structure_dict(v, v1)
if not r:
return False
if r:
inval.update(r)
else:
print(f"INVALID ENTRY: {k}={v}")
return False
return True
inval[k] = v
return inval


def filter_dict(dict_o, keys_regex):
Expand Down Expand Up @@ -579,9 +580,11 @@ def cfg_main():
cfg_t = load_config_file(args.validate, 1)
r = check_structure_dict(cfg, cfg_t)
if r:
print("SUCCESS")
else:
for k in r:
print(f"INVALID ENTRY: {k}={r[k]}")
print("FAILURE")
else:
print("SUCCESS")
else:
if args.template:
cfg = flatten_dict(cfg)
Expand Down
16 changes: 7 additions & 9 deletions ush/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,13 @@ def load_config_for_setup(ushdir, default_config, user_config):

# Make sure the keys in user config match those in the default
# config.
if not check_structure_dict(cfg_u, cfg_d):
raise Exception(
dedent(
f"""
User-specified variable "{key}" in {user_config} is not valid
Check {EXPT_DEFAULT_CONFIG_FN} for allowed user-specified variables\n
"""
)
)
invalid = check_structure_dict(cfg_u, cfg_d)
if invalid:
errmsg = "Invalid key(s) specified in {user_config}:\n"
for entry in invalid:
errmsg = errmsg + f"{entry} = {invalid[entry]}\n"
errmsg = errmsg + f"\nCheck {default_config} for allowed user-specified variables\n"
raise Exception(errmsg)

# Mandatory variables *must* be set in the user's config; the default value is invalid
mandatory = ["user.MACHINE"]
Expand Down