Exemple #1
0
    def __call__(
        self,
        value_provider: ContainerInterface,
        context: Optional[PropertyEvaluationContext] = None
    ) -> Optional[ValidatorState]:
        if not value_provider:
            return None

        state = ValidatorState.Unknown
        try:
            minimum = value_provider.getProperty(self._key, "minimum_value")
            maximum = value_provider.getProperty(self._key, "maximum_value")
            minimum_warning = value_provider.getProperty(
                self._key, "minimum_value_warning")
            maximum_warning = value_provider.getProperty(
                self._key, "maximum_value_warning")

            if minimum is not None and maximum is not None and minimum > maximum:
                raise ValueError(
                    "Cannot validate a state of setting {0} with minimum > maximum"
                    .format(self._key))

            if context is not None:
                value_provider = context.rootStack()

            value = value_provider.getProperty(self._key, "value")
            if value is None or value != value:
                raise ValueError(
                    "Cannot validate None, NaN or similar values in setting {0}, actual value: {1}"
                    .format(self._key, value))

            if minimum is not None and value < minimum:
                state = ValidatorState.MinimumError
            elif maximum is not None and value > maximum:
                state = ValidatorState.MaximumError
            elif minimum_warning is not None and value < minimum_warning:
                state = ValidatorState.MinimumWarning
            elif maximum_warning is not None and value > maximum_warning:
                state = ValidatorState.MaximumWarning
            else:
                state = ValidatorState.Valid
        except:
            Logger.logException(
                "w", "Could not validate setting %s, an exception was raised",
                self._key)
            state = ValidatorState.Exception

        return state
    def __call__(self,
                 value_provider: ContainerInterface,
                 context: Optional[PropertyEvaluationContext] = None) -> Any:
        if not value_provider:
            return None

        if not self._valid:
            return None

        locals = {}  # type: Dict[str, Any]
        # if there is a context, evaluate the values from the perspective of the original caller
        if context is not None:
            value_provider = context.rootStack()
        for name in self._used_values:
            value = value_provider.getProperty(name, "value", context)
            if value is None:
                continue

            locals[name] = value

        g = {}  # type: Dict[str, Any]
        g.update(globals())
        g.update(self.__operators)
        # override operators if there is any in the context
        if context is not None:
            g.update(context.context.get("override_operators", {}))

        try:
            return eval(self._compiled, g, locals)
        except Exception as e:
            Logger.logException(
                "d", "An exception occurred in inherit function %s", self)
            return 0  # Settings may be used in calculations and they need a value
Exemple #3
0
    def __call__(self, value_provider: ContainerInterface) -> Any:
        if not value_provider:
            return None

        if not self._valid:
            return None

        locals = {}  # type: Dict[str, Any]
        for name in self._settings:
            value = value_provider.getProperty(name, "value")
            if value is None:
                continue

            locals[name] = value

        g = {}  # type: Dict[str, Any]
        g.update(globals())
        g.update(self.__operators)

        try:
            return eval(self._compiled, g, locals)
        except Exception as e:
            Logger.logException(
                "d", "An exception occurred in inherit function %s", self)
            return 0  # Settings may be used in calculations and they need a value
Exemple #4
0
    def __call__(self, value_provider: ContainerInterface, context: Optional[PropertyEvaluationContext] = None) -> Any:
        if not value_provider:
            return None

        if not self._valid:
            return None

        locals = {} # type: Dict[str, Any]
        # if there is a context, evaluate the values from the perspective of the original caller
        if context is not None:
            value_provider = context.rootStack()
        for name in self._used_values:
            value = value_provider.getProperty(name, "value", context)
            if value is None:
                continue

            locals[name] = value

        g = {}  # type: Dict[str, Any]
        g.update(globals())
        g.update(self.__operators)
        # override operators if there is any in the context
        if context is not None:
            g.update(context.context.get("override_operators", {}))

        try:
            if self._compiled:
                return eval(self._compiled, g, locals)
            Logger.log("e", "An error ocurred evaluating the function {0}.".format(self))
            return 0
        except Exception as e:
            Logger.logException("d", "An exception occurred in inherit function {0}: {1}".format(self, str(e)))
            return 0  # Settings may be used in calculations and they need a value
Exemple #5
0
    def __call__(self, value_provider: ContainerInterface, context: Optional[PropertyEvaluationContext] = None) -> Optional[ValidatorState]:
        if not value_provider:
            return None

        state = ValidatorState.Unknown
        try:
            allow_empty = value_provider.getProperty(self._key, "allow_empty")  # For string only
            minimum = value_provider.getProperty(self._key, "minimum_value")
            maximum = value_provider.getProperty(self._key, "maximum_value")
            minimum_warning = value_provider.getProperty(self._key, "minimum_value_warning")
            maximum_warning = value_provider.getProperty(self._key, "maximum_value_warning")

            if minimum is not None and maximum is not None and minimum > maximum:
                raise ValueError("Cannot validate a state of setting {0} with minimum > maximum".format(self._key))

            if context is not None:
                value_provider = context.rootStack()

            value = value_provider.getProperty(self._key, "value")
            if value is None or value != value:
                raise ValueError("Cannot validate None, NaN or similar values in setting {0}, actual value: {1}".format(self._key, value))

            # "allow_empty is not None" is not necessary here because of "allow_empty is False", but it states
            # explicitly that we should not do this check when "allow_empty is None".
            if allow_empty is not None and allow_empty is False and str(value) == "":
                state = ValidatorState.Invalid
            elif minimum is not None and value < minimum:
                state = ValidatorState.MinimumError
            elif maximum is not None and value > maximum:
                state = ValidatorState.MaximumError
            elif minimum_warning is not None and value < minimum_warning:
                state = ValidatorState.MinimumWarning
            elif maximum_warning is not None and value > maximum_warning:
                state = ValidatorState.MaximumWarning
            else:
                state = ValidatorState.Valid
        except:
            Logger.logException("w", "Could not validate setting %s, an exception was raised", self._key)
            state = ValidatorState.Exception

        return state
Exemple #6
0
    def __call__(self, value_provider: ContainerInterface, context: Optional[PropertyEvaluationContext] = None) -> Optional[ValidatorState]:
        if not value_provider:
            return None

        state = ValidatorState.Unknown
        try:
            minimum = value_provider.getProperty(self._key, "minimum_value")
            maximum = value_provider.getProperty(self._key, "maximum_value")
            minimum_warning = value_provider.getProperty(self._key, "minimum_value_warning")
            maximum_warning = value_provider.getProperty(self._key, "maximum_value_warning")

            if minimum is not None and maximum is not None and minimum > maximum:
                raise ValueError("Cannot validate a state of setting {0} with minimum > maximum".format(self._key))

            if context is not None:
                value_provider = context.rootStack()

            value = value_provider.getProperty(self._key, "value")
            if value is None or value != value:
                raise ValueError("Cannot validate None, NaN or similar values in setting {0}, actual value: {1}".format(self._key, value))

            if minimum is not None and value < minimum:
                state = ValidatorState.MinimumError
            elif maximum is not None and value > maximum:
                state = ValidatorState.MaximumError
            elif minimum_warning is not None and value < minimum_warning:
                state = ValidatorState.MinimumWarning
            elif maximum_warning is not None and value > maximum_warning:
                state = ValidatorState.MaximumWarning
            else:
                state = ValidatorState.Valid
        except:
            Logger.logException("w", "Could not validate setting %s, an exception was raised", self._key)
            state = ValidatorState.Exception

        return state
Exemple #7
0
    def __call__(self,
                 value_provider: ContainerInterface,
                 context: Optional[PropertyEvaluationContext] = None) -> Any:
        """Call the actual function to calculate the value.

        :param value_provider: The container from which to get setting values in the formula.
        :param context: The context in which the call needs to be executed
        """

        if not value_provider:
            return None

        if not self._valid:
            return None

        locals = {}  # type: Dict[str, Any]
        # If there is a context, evaluate the values from the perspective of the original caller
        if context is not None:
            value_provider = context.rootStack()
        for name in self._used_values:
            value = value_provider.getProperty(name, "value", context)
            if value is None:
                continue

            locals[name] = value

        g = {}  # type: Dict[str, Any]
        g.update(globals())
        g.update(self.__operators)
        # Override operators if there is any in the context
        if context is not None:
            g.update(context.context.get("override_operators", {}))

        try:
            if self._compiled:
                return eval(self._compiled, g, locals)
            Logger.log(
                "e",
                "An error occurred evaluating the function {0}.".format(self))
            return 0
        except Exception as e:
            Logger.logException(
                "d",
                "An exception occurred in inherit function {0}: {1}".format(
                    self, str(e)))
            return 0  # Settings may be used in calculations and they need a value
Exemple #8
0
    def __call__(self, value_provider: ContainerInterface) -> Any:
        if not value_provider:
            return None

        if not self._valid:
            return None

        locals = {} # type: Dict[str, Any]
        for name in self._used_values:
            value = value_provider.getProperty(name, "value")
            if value is None:
                continue

            locals[name] = value

        g = {}  # type: Dict[str, Any]
        g.update(globals())
        g.update(self.__operators)

        try:
            return eval(self._compiled, g, locals)
        except Exception as e:
            Logger.logException("d", "An exception occurred in inherit function %s", self)
            return 0  # Settings may be used in calculations and they need a value
Exemple #9
0
    def __call__(
        self,
        value_provider: ContainerInterface,
        context: Optional[PropertyEvaluationContext] = None
    ) -> Optional[ValidatorState]:
        if not value_provider:
            return None

        state = ValidatorState.Unknown
        try:
            allow_empty = value_provider.getProperty(
                self._key, "allow_empty", context=context)  # For string only
            is_uuid = value_provider.getProperty(
                self._key, "is_uuid", context=context)  # For string only
            regex_blacklist_pattern = value_provider.getProperty(
                self._key, "regex_blacklist_pattern",
                context=context)  # For string only
            minimum = value_provider.getProperty(self._key,
                                                 "minimum_value",
                                                 context=context)
            maximum = value_provider.getProperty(self._key,
                                                 "maximum_value",
                                                 context=context)
            minimum_warning = value_provider.getProperty(
                self._key, "minimum_value_warning", context=context)
            maximum_warning = value_provider.getProperty(
                self._key, "maximum_value_warning", context=context)

            # For boolean
            boolean_warning_value = value_provider.getProperty(self._key,
                                                               "warning_value",
                                                               context=context)
            boolean_error_value = value_provider.getProperty(self._key,
                                                             "error_value",
                                                             context=context)

            if minimum is not None and maximum is not None and minimum > maximum:
                raise ValueError(
                    "Cannot validate a state of setting {0} with minimum > maximum"
                    .format(self._key))

            if context is not None:
                value_provider = context.rootStack()

            value = value_provider.getProperty(self._key,
                                               "value",
                                               context=context)
            if value is None or value != value:
                raise ValueError(
                    "Cannot validate None, NaN or similar values in setting {0}, actual value: {1}"
                    .format(self._key, value))

            setting_type = value_provider.getProperty(self._key,
                                                      "type",
                                                      context=context)

            if setting_type == "str":
                # "allow_empty is not None" is not necessary here because of "allow_empty is False", but it states
                # explicitly that we should not do this check when "allow_empty is None".
                if allow_empty is not None and allow_empty is False and str(
                        value) == "":
                    state = ValidatorState.Invalid
                    return state

                if is_uuid is not None and is_uuid is True:
                    # Try to parse the UUID string with uuid.UUID(). It will raise a ValueError if it's not valid.
                    try:
                        uuid.UUID(str(value))
                        state = ValidatorState.Valid
                    except ValueError:
                        state = ValidatorState.Invalid
                    return state

                # If "regex_blacklist_pattern" is set, it will be used to validate the value string. If the value string
                # matches the blacklist pattern, the value will be invalid.
                if regex_blacklist_pattern is not None and len(
                        regex_blacklist_pattern) > 0:
                    regex = re.compile(regex_blacklist_pattern)
                    is_valid = regex.fullmatch(str(value).lower()) is None
                    state = ValidatorState.Valid if is_valid else ValidatorState.Invalid
                    return state

                state = ValidatorState.Valid
                return state

            elif setting_type == "bool":
                state = ValidatorState.Valid
                if boolean_warning_value is not None and value == boolean_warning_value:
                    state = ValidatorState.MaximumWarning
                elif boolean_error_value is not None and value == boolean_error_value:
                    state = ValidatorState.MaximumError

            elif minimum is not None and value < minimum:
                state = ValidatorState.MinimumError
            elif maximum is not None and value > maximum:
                state = ValidatorState.MaximumError
            elif minimum_warning is not None and value < minimum_warning:
                state = ValidatorState.MinimumWarning
            elif maximum_warning is not None and value > maximum_warning:
                state = ValidatorState.MaximumWarning
            else:
                state = ValidatorState.Valid
        except:
            Logger.logException(
                "w", "Could not validate setting %s, an exception was raised",
                self._key)
            state = ValidatorState.Exception

        return state