Historically called 'flattening' but really a function that takes a parameter override
json config that includes a Default_Config_Path and produces a config.json from the two.
Source code in emod_api/config/from_overrides.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 | def flattenConfig( configjson_path, new_config_name="config.json" ):
"""
Historically called 'flattening' but really a function that takes a parameter override
json config that includes a Default_Config_Path and produces a config.json from the two.
"""
if os.path.exists( configjson_path ) == False:
raise
configjson_flat = {}
#print( "configjson_path = " + configjson_path )
configjson = _load_json(configjson_path)
_recursive_json_overrider( configjson, configjson_flat )
# get defaults from config.json and synthesize output from default and overrides
if "Default_Config_Path" in configjson_flat:
default_config_path = configjson_flat["Default_Config_Path"]
stripped_path = default_config_path.strip()
if stripped_path != default_config_path:
print("Warning: config parameter 'Default_Config_Path' has leading or trailing whitespace in value \"{0}\"."
" Trimming whitespace and continuing.".format(default_config_path))
default_config_path = stripped_path
try:
# This code has always worked by treating the default_configpath as relative the Regression directory.
# No longer doing that, but preserving that capability for back-compat. Going forward, relative to the
# configjson_path.
simdir = Path( configjson_path ).parent
default_config_json = None
if Path( os.path.join( str( simdir ), default_config_path) ).exists():
default_config_json = _load_json(os.path.join( str(simdir), default_config_path))
else:
default_config_json = _load_json(os.path.join( '.', default_config_path))
_recursive_json_overrider( default_config_json, configjson_flat )
except Exception as ex:
print( f"Exception opening default config {default_config_path}: {ex}." )
raise ex
else:
print( "Didn't find 'Default_Config_Path' in '{0}'".format( configjson_path ) )
raise RuntimeError( "Bad Default_Config_Path!!!" )
# still need that parameter top level node
configjson = {}
configjson["parameters"] = configjson_flat
# don't need backslashes in the default config path
# messing with anything else downstream now that it is flattened
if "Default_Config_Path" in configjson["parameters"]:
configjson["parameters"].pop("Default_Config_Path")
# let's write out a flat version in case someone wants
# to use regression examples as configs for debug mode
with open( configjson_path.replace( os.path.basename(configjson_path), new_config_name ), 'w', newline='\r\n') as handle:
handle.write( json.dumps(configjson, sort_keys=True, indent=4) )
return configjson
|