Exemple #1
0
    def validate(self, value):
        if self._argparse_value is not None:
            value = self._argparse_value
        if value is None:
            if self._default is required:
                raise ValidationError('this value is required')
            else:
                return ConfigValue(None, self._default)
        else:
            values = value.value
            if not isinstance(values, list):
                values = [values]
            validated_list = []
            if len(values) != len(self._types):
                raise ValidationError('bad array size (should be %d, found %d '
                                      'items)' % (len(self._types), len(values)))

            for i, (item, item_type) in enumerate(list(zip(values, self._types))):
                try:
                    item = item_type.validate(item)
                except ValidationError as err:
                    raise ValidationError('item #%d, %s' % (i, err),
                                          position=value.position)
                else:
                    validated_list.append(item)
            return ConfigValue(value.name, validated_list, position=value.position)
Exemple #2
0
    def validate(self, value):
        raw_addr, _, raw_port = value.partition(':')
        if not raw_addr:
            raw_addr = self._default_addr
        if not raw_port:
            if self._default_port is None:
                raise ValidationError('You must specify a port')
            else:
                raw_port = self._default_port

        try:
            addr = ipaddr.IPAddress(raw_addr, version=self._version)
        except (ValueError, ipaddr.AddressValueError) as err:
            raise ValidationError(str(err))

        try:
            port = int(raw_port)
        except ValueError:
            raise ValidationError('%r is not a port (not an integer)' %
                                  raw_port)
        if not 1 <= port <= 65535:
            raise ValidationError('%r is not a port (not in 1 - 65535 range)' %
                                  port)

        return self.Address(addr, port)
Exemple #3
0
 def validate(self, value):
     try:
         return ipaddr.IPNetwork(value, version=self._version)
     except ipaddr.AddressValueError:
         raise ValidationError('%r does not appear to be an IPv%s address' %
                               (value, self._version))
     except ValueError as err:
         raise ValidationError(str(err))
Exemple #4
0
 def validate(self, value):
     value = super(Integer, self).validate(value)
     if int(value) == value:
         value = int(value)
         if self._min is not None and value < self._min:
             raise ValidationError('%r is lower than the minimum (%d)' %
                                   (value, self._min))
         elif self._max is not None and value > self._max:
             raise ValidationError('%r is greater than the maximum (%d)' %
                                   (value, self._max))
         return value
     else:
         raise ValidationError('%r is not an integer value' % value)
Exemple #5
0
 def validate(self, value):
     value = super(RegexPattern, self).validate(value)
     # Try to compile regex object:
     try:
         value = re.compile(value, self.flags)
     except re.error:
         raise ValidationError('Bad format for regular expression')
     return value
Exemple #6
0
 def validate(self, value):
     value = super(EntryPoint, self).validate(value)
     # Get the first entry-point to match the provided value:
     entrypoint = next(
         pkg_resources.iter_entry_points(self._entrypoint, str(value)),
         None)
     if entrypoint is None:
         raise ValidationError('Unknown %s entry-point' % self._entrypoint)
     return entrypoint.name, entrypoint.load()
Exemple #7
0
 def validate(self, value):
     if self._argparse_value is not None:
         value = self._argparse_value
     if value is None:
         if self._default is required:
             raise ValidationError('this value is required')
         else:
             return ConfigValue(None, self._default)
     else:
         if isinstance(value.value, list):
             if len(value.value) == 1:
                 value.value = value.value[0]
             else:
                 raise ValidationError('%r is a list' % value.value,
                                       position=value.position)
         try:
             validated_value = self._type.validate(value.value)
         except ValidationError as err:
             raise ValidationError(str(err), position=value.position)
         return ConfigValue(value.name, validated_value, position=value.position)
Exemple #8
0
 def validate(self, value):
     if self._argparse_value is not None:
         value = self._argparse_value
     if value is None:
         if self._default is required:
             raise ValidationError('this value is required')
         else:
             return ConfigValue(None, self._default)
     else:
         if isinstance(value.value, list):
             if len(value.value) == 1:
                 value.value = value.value[0]
             else:
                 raise ValidationError('%r is a list' % value.value,
                                       position=value.position)
         if value.value in self._choices:
             return ConfigValue(value.name, self._choices[value.value],
                                position=value.position)
         else:
             choices = ', '.join(repr(x) for x in self._choices)
             raise ValidationError('bad choice (must be one of %s)' % choices)
Exemple #9
0
 def validate(self, value):
     if self._argparse_value is not None:
         value = self._argparse_value
     if value is None:
         if self._default is required:
             raise ValidationError('this value is required')
         else:
             return ConfigValue(None, self._default)
     else:
         values = value.value
         if not isinstance(values, list):
             values = [values]
         validated_list = []
         for i, item in enumerate(values):
             try:
                 item = self._type.validate(item)
             except ValidationError as err:
                 raise ValidationError('item #%d, %s' % (i, err),
                                       position=value.position)
             else:
                 validated_list.append(item)
         return ConfigValue(value.name, validated_list, position=value.position)
Exemple #10
0
 def validate(self, value):
     value = super(_BaseRegex, self).validate(value)
     match = self._regex.match(value)
     if match is None:
         raise ValidationError(self._error)
     return match
Exemple #11
0
 def validate(self, value):
     value = super(Eval, self).validate(value)
     try:
         return eval(value, self._globals, self._locals)
     except Exception as err:
         raise ValidationError('Bad expression: %s' % err)
Exemple #12
0
 def validate(self, value):
     value = super(Array, self).validate(value)
     if len(value.value) != self._size:
         raise ValidationError('bad array size (should be %d, found %d items)'
                               % (self._size, len(value.value)))
     return value
Exemple #13
0
 def validate(self, value):
     if not isinstance(value, numbers.Number):
         raise ValidationError('%r is not a number' % value)
     else:
         return value
Exemple #14
0
 def validate(self, value):
     try:
         return urllib.parse.urlparse(value)
     except ValueError as err:
         raise ValidationError(str(err))
Exemple #15
0
    def validate(self, section):
        if not isinstance(section, ConfigSection):
            raise ValidationError('Not a section')

        # Rebuild the section using schema:
        validated_section = ConfigSection(section.name, section.parent,
                                          position=section.position)
        # Validate the section's argument:
        if self.meta['args'] is None and section.args is not None:
            raise ValidationError('section %s, this section does not take '
                                  'any argument' % section.name,
                                  position=section.position)
        elif self.meta['args'] is not None:
            try:
                validated_args = self.meta['args'].validate(section.args_raw)
            except ValidationError as err:
                msg = 'section %s, arguments, %s' % (section.name, err)
                raise ValidationError(msg, position=err.position)
            else:
                validated_section.args = validated_args
        # Validate the section's children:
        for name, container in list(self.keys.items()):
            if isinstance(container, Section):
                # Validate subsections of this section:
                subsections = list(section.subsections(name))
                # Check for repeat option:
                rmin, rmax = container.meta['repeat']
                if rmax is not None and rmin > rmax:
                    raise ValidationError('section %s, rmin > rmax' % name)
                if len(subsections) < rmin:
                    raise ValidationError('section %s, section must be defined'
                                          ' at least %d times' % (name, rmin))
                if rmax is not None and len(subsections) > rmax:
                    raise ValidationError('section %s, section must be defined'
                                          ' at max %d times' % (name, rmax))
                # Do the children validation:
                args = set()  # Store the already seen args
                for subsection in subsections:
                    # Check for unique option:
                    if container.meta['unique']:
                        args_value = None if subsection.args is None else tuple(subsection.args)
                        if args_value in args:
                            msg = 'section %s, section must be unique' % name
                            raise ValidationError(msg, position=subsection.position)
                        else:
                            args.add(args_value)
                    # Container validation:
                    validated_subsection = container.validate(subsection)
                    validated_section.register(validated_subsection, name=name)
            elif isinstance(container, Container):
                # Validate all other types of containers:
                try:
                    validated_value = container.validate(section.get(name, raw=False))
                except ValidationError as err:
                    raise ValidationError('section %s, key %s, %s' % (section.name, name, err),
                                          position=err.position)
                else:
                    validated_section.register(validated_value, name=name)
        # Handle the allow_unknown meta option:
        for name, child in section.iteritems(expand_sections=True):
            if name not in self.keys:
                if self.meta['allow_unknown']:
                    validated_section.register(child, name=name)
                else:
                    msg = 'section %s, unknown key %s' % (section.name, name)
                    raise ValidationError(msg, position=child.position)
        return validated_section
Exemple #16
0
 def validate(self, value):
     try:
         return ipaddr.IPAddress(value, version=self._version)
     except (ValueError, ipaddr.AddressValueError) as err:
         raise ValidationError(str(err))
Exemple #17
0
 def validate(self, value):
     if value is not True and value is not False:
         raise ValidationError('%r is not a boolean value' % value)
     return value