def apply(self, context: dict = None) -> bool: """ Returns true/false depending on constraint provisioning and context. :param context: Context information :return: """ constraint_check = False try: context_value = get_identifier(self.context_name, context) # Set currentTime if not specified if self.context_name == "currentTime" and not context_value: context_value = datetime.now() if context_value is not None: if self.operator in [ ConstraintOperators.IN, ConstraintOperators.NOT_IN ]: constraint_check = self.check_list_operators( context_value=context_value) elif self.operator in [ ConstraintOperators.STR_CONTAINS, ConstraintOperators.STR_ENDS_WITH, ConstraintOperators.STR_STARTS_WITH ]: constraint_check = self.check_string_operators( context_value=context_value) elif self.operator in [ ConstraintOperators.NUM_EQ, ConstraintOperators.NUM_GT, ConstraintOperators.NUM_GTE, ConstraintOperators.NUM_LT, ConstraintOperators.NUM_LTE ]: constraint_check = self.check_numeric_operators( context_value=context_value) elif self.operator in [ ConstraintOperators.DATE_AFTER, ConstraintOperators.DATE_BEFORE ]: constraint_check = self.check_date_operators( context_value=context_value) elif self.operator in [ ConstraintOperators.SEMVER_EQ, ConstraintOperators.SEMVER_GT, ConstraintOperators.SEMVER_LT ]: constraint_check = self.check_semver_operators( context_value=context_value) except Exception as excep: # pylint: disable=broad-except LOGGER.info("Could not evaluate context %s! Error: %s", self.context_name, excep) return not constraint_check if self.inverted else constraint_check
def _apply_overrides(self, context: dict) -> dict: """ Figures out if an override should be applied based on a context. Notes: - This matches only the first variant found. """ variants_with_overrides = [x for x in self.variants if 'overrides' in x.keys()] override_variant = {} # type: Dict for variant in variants_with_overrides: for override in variant['overrides']: identifier = utils.get_identifier(override['contextName'], context) if identifier in override["values"]: override_variant = variant return override_variant
def apply(self, context=None): """ Returns true/false depending on constraint provisioning and context. :param context: Context information :return: """ constraint_check = False try: value = get_identifier(self.context_name, context) if value: if self.operator.upper() == "IN": constraint_check = value in self.values elif self.operator.upper() == "NOT_IN": constraint_check = value not in self.values except Exception as excep: #pylint: disable=W0703 LOGGER.info("Could not evaluate context %s! Error: %s", self.context_name, excep) return constraint_check