Skip to content

Commit

Permalink
add get_first_provenance and error handling for #1268
Browse files Browse the repository at this point in the history
  • Loading branch information
mandresm committed Jan 21, 2025
1 parent 89fc2d1 commit 2ec4500
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
34 changes: 34 additions & 0 deletions src/esm_parser/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,23 @@ def get_provenance(self, index=-1):

return provenance_dict

def get_first_provenance(self):
"""
Recursively loops through the dictionary keys and returns the first provenance
found in the nested values.
Returns
-------
first_provenance : esm_parser.provenance.Provenance
The first provenance found in the nested values
"""
first_provenance = None
for key, val in self.items():
if isinstance(val, PROVENANCE_MAPPINGS):
return val.get_first_provenance()
elif hasattr(val, "provenance"):
return val.provenance[-1]

def __setitem__(self, key, val):
"""
Any time an item in a DictWithProvenance is set, extend the old provenance of
Expand Down Expand Up @@ -773,6 +790,23 @@ def get_provenance(self, index=-1):

return provenance_list

def get_first_provenance(self):
"""
Recursively loops through the list elements and returns the first provenance
found in the nested values.
Returns
-------
first_provenance : esm_parser.provenance.Provenance
The first provenance found in the nested values
"""
first_provenance = None
for elem in self:
if isinstance(elem, PROVENANCE_MAPPINGS):
return elem.get_first_provenance()
elif hasattr(elem, "provenance"):
return elem.provenance[-1]

def __setitem__(self, indx, val):
"""
Any time an item in a ListWithProvenance is set, extend the old provenance of
Expand Down
31 changes: 23 additions & 8 deletions src/esm_runscripts/coupler.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,29 @@ def add_files(self, full_config):
sys.exit(0)

direction_info = None
if "coupling_directions" in full_config[self.name]:
if (
right_grid + "->" + left_grid
in full_config[self.name]["coupling_directions"]
):
direction_info = full_config[self.name][
"coupling_directions"
][right_grid + "->" + left_grid]
coupling_directions = full_config[self.name].get(
"coupling_directions"
)
if coupling_directions:
direction = f"{right_grid}->{left_grid}"
direction_info = coupling_directions.get(direction)
if not direction_info:
first_provenance = (
coupling_directions.get_first_provenance()
)
location_hint = ""
if first_provenance:
yaml_file = first_provenance.get("yaml_file")
line = first_provenance.get("line")
location_hint = (
f" (for example near ``{yaml_file},line:{line}``)"
)
esm_parser.user_error(
"Missing coupling direction",
f"The ``{direction}`` does not exist in "
f"``{self.name}.coupling_directions``. You can solve "
f"this by defining it there{location_hint}."
)
transf_info = None
if "coupling_methods" in full_config[self.name]:
if interpolation in full_config[self.name]["coupling_methods"]:
Expand Down

0 comments on commit 2ec4500

Please sign in to comment.