Пример #1
0
    def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (*any primitive type*, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to


        Raises:

            FormatError: if the instance does not conform to ``format``

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            )
Пример #2
0
    def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format),
                cause=cause,
            )
Пример #3
0
    def check(self, instance, format):
        if format not in self.checkers:
            raise FormatError("Format checker for %r format not found" %
                              (format, ))

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e

        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format),
                cause=cause,
            )
        return result
Пример #4
0
 def check(self, instance, format):
     if format == "date-time":
         if re.match(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$", instance):
             return
     elif format == "date":
         if re.match(r"^\d{4}-\d{2}-\d{2}$", instance):
             return
     elif format == "date-time-blank":
         if not instance or re.match(
                 r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$", instance):
             return
     elif format == "date-blank":
         if not instance or re.match(r"^\d{4}-\d{2}-\d{2}$", instance):
             return
     else:
         return
     raise FormatError("%r is not a %r" % (instance, format), )
Пример #5
0
 def check(self, instance, format):
     if format not in self.checkers:
         raise FormatError("Format checker for %r format not found" %
                           (format, ))
     return super(StrictFormatChecker, self).check(instance, format)