From 03e8b4643cf90cbfd63a8bc206a07ee1cca5d1e7 Mon Sep 17 00:00:00 2001 From: Corvin-Petrut Cobarzan Date: Wed, 6 Nov 2024 13:05:39 +0000 Subject: [PATCH] Guard --- cads_adaptors/adaptors/cds.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/cads_adaptors/adaptors/cds.py b/cads_adaptors/adaptors/cds.py index 2b852281..8e06a23e 100644 --- a/cads_adaptors/adaptors/cds.py +++ b/cads_adaptors/adaptors/cds.py @@ -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: @@ -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 @@ -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