def validate_value(self, value, *, ignore_required=False):
        if self.constant:
            return None

        if is_empty(value):
            if self.required and not ignore_required:
                return 'is not specified'
            return None

        value_string = self.value_to_str(value)

        if self.no_value:
            if value not in ['true', True, 'false', False]:
                return 'should be boolean, but has value ' + value_string
            return None

        if self.type == 'text':
            return None

        if self.type == 'file_upload':
            if not os.path.exists(value):
                return 'Cannot find file ' + value
            return None

        if self.type == 'int':
            if not (isinstance(value, int) or (isinstance(value, str) and string_utils.is_integer(value))):
                return 'should be integer, but has value ' + value_string

            int_value = int(value)

            if (not is_empty(self.max)) and (int_value > int(self.max)):
                return 'is greater than allowed value (' \
                       + value_string + ' > ' + str(self.max) + ')'

            if (not is_empty(self.min)) and (int_value < int(self.min)):
                return 'is lower than allowed value (' \
                       + value_string + ' < ' + str(self.min) + ')'
            return None

        allowed_values = self.values

        if self.type == 'list':
            if value not in allowed_values:
                return 'has value ' + value_string \
                       + ', but should be in [' + ','.join(allowed_values) + ']'
            return None

        if self.type == 'multiselect':
            if not isinstance(value, list):
                return 'should be a list, but was: ' + value_string + '(' + str(type(value)) + ')'
            for value_element in value:
                if value_element not in allowed_values:
                    element_str = self.value_to_str(value_element)
                    return 'has value ' + element_str \
                           + ', but should be in [' + ','.join(allowed_values) + ']'
            return None

        return None
    def validate_value(self, value, *, ignore_required=False):
        if self.constant:
            return None

        if is_empty(value):
            if self.required and not ignore_required:
                return 'is not specified'
            return None

        value_string = self.value_to_repr(value)

        if self.no_value:
            if value not in ['true', True, 'false', False]:
                return 'should be boolean, but has value ' + value_string
            return None

        if self.type == 'text':
            return None

        if self.type == 'file_upload':
            if not os.path.exists(value):
                return 'Cannot find file ' + value
            return None

        if self.type == 'int':
            if not (isinstance(value, int) or
                    (isinstance(value, str)
                     and string_utils.is_integer(value))):
                return 'should be integer, but has value ' + value_string

            int_value = int(value)

            if (not is_empty(self.max)) and (int_value > int(self.max)):
                return 'is greater than allowed value (' \
                       + value_string + ' > ' + str(self.max) + ')'

            if (not is_empty(self.min)) and (int_value < int(self.min)):
                return 'is lower than allowed value (' \
                       + value_string + ' < ' + str(self.min) + ')'
            return None

        if self.type in ('ip', 'ip4', 'ip6'):
            try:
                address = ip_address(value.strip())
                if self.type == 'ip4':
                    if not isinstance(address, IPv4Address):
                        return value_string + ' is not an IPv4 address'
                elif self.type == 'ip6':
                    if not isinstance(address, IPv6Address):
                        return value_string + ' is not an IPv6 address'
            except ValueError:
                return 'wrong IP address ' + value_string

        allowed_values = self.values

        if (self.type == 'list') or (self._is_plain_server_file()):
            if value not in allowed_values:
                return 'has value ' + value_string \
                       + ', but should be in ' + repr(allowed_values)
            return None

        if self.type == PARAM_TYPE_MULTISELECT:
            if not isinstance(value, list):
                return 'should be a list, but was: ' + value_string + '(' + str(
                    type(value)) + ')'
            for value_element in value:
                if value_element not in allowed_values:
                    element_str = self.value_to_repr(value_element)
                    return 'has value ' + element_str \
                           + ', but should be in ' + repr(allowed_values)
            return None

        if self._is_recursive_server_file():
            return self._validate_recursive_path(value, intermediate=False)

        return None
def validate_parameters(parameters, config):
    for parameter in config.get_parameters():
        if parameter.is_constant():
            continue

        name = parameter.get_name()

        if name in parameters:
            value = parameters[name]
        else:
            value = None

        if is_empty(value):
            if parameter.is_required():
                LOGGER.error('Parameter ' + name + ' is not specified')
                return False
            continue

        value_string = value_to_str(value, parameter)

        if parameter.is_no_value():
            if value not in ['true', True, 'false', False]:
                LOGGER.error('Parameter ' + name +
                             ' should be boolean, but has value ' +
                             value_string)
                return False
            continue

        if parameter.get_type() == 'text':
            continue

        if parameter.get_type() == 'int':
            if not (isinstance(value, int) or
                    (isinstance(value, str)
                     and string_utils.is_integer(value))):
                LOGGER.error('Parameter ' + name +
                             ' should be integer, but has value ' +
                             value_string)
                return False

            int_value = int(value)

            if (not is_empty(parameter.get_max())) and (int_value > int(
                    parameter.get_max())):
                LOGGER.error('Parameter ' + name +
                             ' is greater than allowed value (' +
                             value_string + ' > ' + str(parameter.get_max()) +
                             ')')
                return False

            if (not is_empty(parameter.get_min())) and (int_value < int(
                    parameter.get_min())):
                LOGGER.error('Parameter ' + name +
                             ' is lower than allowed value (' + value_string +
                             ' < ' + str(parameter.get_min()) + ')')
                return False

            continue

        if parameter.get_type() == 'list':
            if value not in parameter.get_values():
                LOGGER.error('Parameter ' + name + ' has value ' +
                             value_string + ', but should be in [' +
                             ','.join(parameter.get_values()) + ']')
                return False
            continue

    return True
def validate_parameter(parameter, parameter_values):
    if parameter.is_constant():
        return None

    value = parameter_values.get(parameter.name)
    if is_empty(value):
        if parameter.is_required():
            return 'is not specified'
        return None

    value_string = value_to_str(value, parameter)

    if parameter.is_no_value():
        if value not in ['true', True, 'false', False]:
            return 'should be boolean, but has value ' + value_string
        return None

    if parameter.type == 'text':
        return None

    if parameter.type == 'file_upload':
        if not os.path.exists(value):
            return 'Cannot find file ' + value
        return None

    if parameter.type == 'int':
        if not (isinstance(value, int) or
                (isinstance(value, str) and string_utils.is_integer(value))):
            return 'should be integer, but has value ' + value_string

        int_value = int(value)

        if (not is_empty(parameter.get_max())) and (int_value > int(
                parameter.get_max())):
            return 'is greater than allowed value (' \
                   + value_string + ' > ' + str(parameter.get_max()) + ')'

        if (not is_empty(parameter.get_min())) and (int_value < int(
                parameter.get_min())):
            return 'is lower than allowed value (' \
                   + value_string + ' < ' + str(parameter.get_min()) + ')'
        return None

    allowed_values = parameter.get_values(parameter_values)

    if parameter.type == 'list':
        if value not in allowed_values:
            return 'has value ' + value_string \
                   + ', but should be in [' + ','.join(allowed_values) + ']'
        return None

    if parameter.type == 'multiselect':
        if not isinstance(value, list):
            return 'should be a list, but was: ' + value_string + '(' + str(
                type(value)) + ')'
        for value_element in value:
            if value_element not in allowed_values:
                element_str = value_to_str(value_element, parameter)
                return 'has value ' + element_str \
                       + ', but should be in [' + ','.join(allowed_values) + ']'
        return None

    return None
Beispiel #5
0
    def validate_value(self, value, *, ignore_required=False):
        if self.constant:
            return None

        if is_empty(value):
            if self.required and not ignore_required:
                return 'is not specified'
            return None

        value_string = self.value_to_str(value)

        if self.no_value:
            if value not in ['true', True, 'false', False]:
                return 'should be boolean, but has value ' + value_string
            return None

        if self.type == 'text':
            return None

        if self.type == 'file_upload':
            if not os.path.exists(value):
                return 'Cannot find file ' + value
            return None

        if self.type == 'int':
            if not (isinstance(value, int) or (isinstance(value, str) and string_utils.is_integer(value))):
                return 'should be integer, but has value ' + value_string

            int_value = int(value)

            if (not is_empty(self.max)) and (int_value > int(self.max)):
                return 'is greater than allowed value (' \
                       + value_string + ' > ' + str(self.max) + ')'

            if (not is_empty(self.min)) and (int_value < int(self.min)):
                return 'is lower than allowed value (' \
                       + value_string + ' < ' + str(self.min) + ')'
            return None

        if self.type in ('ip', 'ip4', 'ip6'):
            try:
                address = ip_address(value.strip())
                if self.type == 'ip4':
                    if not isinstance(address, IPv4Address):
                        return value_string + ' is not an IPv4 address'
                elif self.type == 'ip6':
                    if not isinstance(address, IPv6Address):
                        return value_string + ' is not an IPv6 address'
            except ValueError:
                return 'wrong IP address ' + value_string

        allowed_values = self.values

        if (self.type == 'list') or (self._is_plain_server_file()):
            if value not in allowed_values:
                return 'has value ' + value_string \
                       + ', but should be in [' + ','.join(allowed_values) + ']'
            return None

        if self.type == PARAM_TYPE_MULTISELECT:
            if not isinstance(value, list):
                return 'should be a list, but was: ' + value_string + '(' + str(type(value)) + ')'
            for value_element in value:
                if value_element not in allowed_values:
                    element_str = self.value_to_str(value_element)
                    return 'has value ' + element_str \
                           + ', but should be in [' + ','.join(allowed_values) + ']'
            return None

        if self._is_recursive_server_file():
            return self._validate_recursive_path(value, intermediate=False)

        return None