Skip to content

Commit

Permalink
Guard
Browse files Browse the repository at this point in the history
  • Loading branch information
ecmwf-cobarzan committed Nov 6, 2024
1 parent dfff2ad commit 03e8b46
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions cads_adaptors/adaptors/cds.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ def preprocess_conditions(self, conditions):
for condition in conditions:
if "date" in condition:
condition["date"] = self.instantiate_dynamic_daterange(condition["date"], today)
self.context.add_stdout(f"----------> {condition}")

def daterange_in(self, contained, container):
def dateranges_in(self, contained, container):
container_interval = re.split("[;/]", container)
contained_intervals = self.ensure_dateranges(contained)
for contained_interval in contained_intervals:
Expand All @@ -150,11 +149,9 @@ def daterange_in(self, contained, container):
return True

def satisfy_condition(self, request: dict[str, Any], condition: dict[str, Any]):
self.context.add_stdout(f"----------> {request}")
self.context.add_stdout(f"----------> {condition}")
for key in condition:
if key == "date":
if not self.daterange_in(request[key], condition[key]):
if not self.dateranges_in(request[key], condition[key]):
return False
elif not set(ensure_list(request[key])) <= set(ensure_list(condition[key])):
return False
Expand Down Expand Up @@ -194,15 +191,17 @@ def normalise_request(self, request: Request) -> Request:
working_request = self.pre_mapping_modifications(deepcopy(request))

# Implement a request-level tagging system
self.conditional_tagging = self.config.get("conditional_tagging", None)
self.context.add_stdout(f"----------> {self.conditional_tagging} {self.config}")
if self.conditional_tagging is not None:
for tag in self.conditional_tagging:
conditions = self.conditional_tagging[tag]
self.preprocess_conditions(conditions)
if self.satisfy_conditions(request, conditions):
hidden_tag = f"__{tag}"
request[hidden_tag] = True
try:
self.conditional_tagging = self.config.get("conditional_tagging", None)
if self.conditional_tagging is not None:
for tag in self.conditional_tagging:
conditions = self.conditional_tagging[tag]
self.preprocess_conditions(conditions)
if self.satisfy_conditions(request, conditions):
hidden_tag = f"__{tag}"
request[hidden_tag] = True
except Exception as e:
self.context.add_stdout(f"An error occured while attempting conditional tagging: {e!r}")

# If specified by the adaptor, intersect the request with the constraints.
# The intersected_request is a list of requests
Expand Down

0 comments on commit 03e8b46

Please sign in to comment.