Skip to content

Commit

Permalink
resolve early further reading and copy/check the version between gene…
Browse files Browse the repository at this point in the history
…ral and setup in early stages
  • Loading branch information
mandresm committed Nov 9, 2023
1 parent 16ca9e6 commit 7164110
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions src/esm_parser/esm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,8 @@ class GeneralConfig(dict): # pragma: no cover
def __init__(self, model, version, user_config):
super(dict, self).__init__()

self.check_user_defined_versions(user_config)

if os.path.isfile(model + "-" + version):
config_path = model + "-" + version
elif os.path.isfile(model):
Expand All @@ -2851,6 +2853,14 @@ def __init__(self, model, version, user_config):
self.config = yaml_file_to_dict(include_path)
else:
self.config = include_path

resolve_choose_with_var(
"further_reading",
self.config,
model_config={model: self.config},
user_config=user_config,
)

for attachment in CONFIGS_TO_ALWAYS_ATTACH_AND_REMOVE:
attach_to_config_and_remove(self.config, attachment, all_config=None)

Expand Down Expand Up @@ -2980,8 +2990,6 @@ def _config_init(self, user_config):

del self.config

self.check_user_defined_versions(user_config, setup_config)

setup_config["general"].update(
{
"esm_function_dir": CONFIG_PATH,
Expand Down Expand Up @@ -3099,10 +3107,10 @@ def _config_init(self, user_config):
# pprint_config(self.config)
# sys.exit(0)

def check_user_defined_versions(self, user_config={}, setup_config={}):
def check_user_defined_versions(self, user_config):
"""
When running a standalone model, checks whether the users has defined the
variable ``version`` in more than one section and if that's the case
Checks whether the user has defined the variable ``version`` in both the
main model/coupled setup section and in ``general`` and if that's the case
throws and error. If ``version`` is only defined in the ``general`` section
it creates a ``version`` with the same value in the model section, ensuring
that the user can arbitrarily define ``version`` in either ``general`` or
Expand All @@ -3122,26 +3130,23 @@ def check_user_defined_versions(self, user_config={}, setup_config={}):
If something goes wrong with the user's version choices it exits the code
with a ``esm_parser.user_error``
"""
if "general" in self:
user_config = setup_config = self
if (
setup_config["general"].get("standalone")
and user_config["general"].get("run_or_compile", "runtime") == "runtime"
):
version_in_runscript_general = user_config["general"].get("version")
model_name = user_config["general"]["setup_name"]
version_in_runscript_model = user_config.get(model_name, {}).get("version")
if version_in_runscript_general and version_in_runscript_model:
user_error(
"Version",
"You have defined the ``version`` variable both in the "
f"``general`` and ``{model_name}`` sections of your runscript. "
"This is not supported for ``standalone`` simulations. Please "
"define ``only one version`` in one of the two sections.",
)
elif version_in_runscript_general and not version_in_runscript_model:
if model_name in user_config:
user_config[model_name]["version"] = version_in_runscript_general
version_in_runscript_general = user_config["general"].get("version")
model_name = user_config["general"]["setup_name"]
version_in_runscript_model = user_config.get(model_name, {}).get("version")
if version_in_runscript_general and version_in_runscript_model:
user_error(
"Version",
"You have defined the ``version`` variable both in the "
f"``general`` and ``{model_name}`` sections of your runscript. "
"This is not supported as it is redundant information. Please "
"define ``only one version`` in one of the two sections.",
)
elif version_in_runscript_general and not version_in_runscript_model:
if model_name in user_config:
user_config[model_name]["version"] = version_in_runscript_general
elif version_in_runscript_model and not version_in_runscript_general:
if "general" in user_config:
user_config["general"]["version"] = version_in_runscript_model

def finalize(self):
self.run_recursive_functions(self)
Expand Down

0 comments on commit 7164110

Please sign in to comment.