Exemplo n.º 1
0
    def validate(self, chunk):
        return_snippet = chunk.contents

        if type(chunk.contents) != CommentedMap:
            raise_exception(
                "when expecting a mapping",
                "found non-mapping",
                chunk,
            )
        else:
            for key, value in chunk.contents.items():
                if key not in self._validator_dict.keys():
                    raise_exception(
                        u"while parsing a mapping",
                        u"unexpected key not in schema '{0}'".format(
                            unicode(key)), chunk.key(key))

                validator = self._validator_dict[key]
                del return_snippet[key]
                return_snippet[YAML(key,
                                    chunk=chunk.key(key),
                                    validator=validator)] = validator(
                                        chunk.val(key))

        return YAML(return_snippet, chunk=chunk, validator=self)
Exemplo n.º 2
0
    def validate(self, chunk):
        return_snippet = chunk.contents

        if not isinstance(return_snippet, CommentedSeq):
            raise_exception(
                "when expecting a sequence of {0} elements".format(
                    len(self._validators)),
                "found non-sequence",
                chunk,
            )
        else:
            if len(self._validators) != len(chunk.contents):
                raise_exception(
                    "when expecting a sequence of {0} elements".format(
                        len(self._validators)),
                    "found a sequence of {0} elements".format(
                        len(chunk.contents)),
                    chunk,
                )
            for i, item_and_val in enumerate(
                    zip(chunk.contents, self._validators)):
                item, validator = item_and_val
                return_snippet[i] = validator(chunk.index(i))

        return YAML(return_snippet, chunk=chunk)
Exemplo n.º 3
0
    def validate(self, document, location=None):
        if location is None:
            location = YAMLLocation()
            document = copy.deepcopy(document)
        return_snippet = {}

        if type(location.get(document)) != CommentedMap:
            raise_exception(
                "when expecting a mapping",
                "found non-mapping",
                document, location=location,
            )
        else:
            for key, value in location.get(document).items():
                if key not in self._validator_dict.keys():
                    raise_exception(
                        "while parsing a mapping",
                        "unexpected key not in schema '{0}'".format(key),
                        document, location=location.key(key)
                    )

                return_snippet[key] = self._validator_dict[key](
                    document, location.val(key)
                )

        return return_snippet
Exemplo n.º 4
0
    def validate(self, document, location=None):
        if location is None:
            location = YAMLLocation()
            document = copy.deepcopy(document)
        return_snippet = {}

        if type(location.get(document)) != CommentedMap:
            raise_exception(
                "when expecting a mapping",
                "found non-mapping",
                document, location=location,
            )
        else:
            for key, value in location.get(document).items():
                if key not in self._validator_dict.keys():
                    raise_exception(
                        "while parsing a mapping",
                        "unexpected key not in schema '{0}'".format(key),
                        document, location=location.key(key)
                    )

                return_snippet[key] = self._validator_dict[key](
                    document, location=location.val(key)
                )

        return return_snippet
Exemplo n.º 5
0
    def validate(self, document, location=None):
        if location is None:
            location = YAMLLocation()
            document = copy.deepcopy(document)

        return_snippet = []

        if type(location.get(document)) != CommentedSeq:
            raise_exception(
                "when expecting a unique sequence",
                "found non-sequence",
                document, location=location,
            )
        else:
            existing_items = set()

            for i, item in enumerate(location.get(document)):
                if item in existing_items:
                    raise_exception(
                        "while parsing a sequence",
                        "duplicate found",
                        document, location=location
                    )
                else:
                    existing_items.add(item)
                    return_snippet.append(self._validator(document, location=location.index(i)))

        return return_snippet
Exemplo n.º 6
0
    def validate(self, document, location=None):
        if location is None:
            location = YAMLLocation()
            document = copy.deepcopy(document)

        return_snippet = []

        if type(location.get(document)) != CommentedSeq:
            raise_exception(
                "when expecting a unique sequence",
                "found non-sequence",
                document, location=location,
            )
        else:
            existing_items = set()

            for i, item in enumerate(location.get(document)):
                if item in existing_items:
                    raise_exception(
                        "while parsing a sequence",
                        "duplicate found",
                        document, location=location
                    )
                else:
                    existing_items.add(item)
                    return_snippet.append(self._validator(document, location=location.index(i)))

        return return_snippet
Exemplo n.º 7
0
 def validate_scalar(self, document, location, value=None):
     val = str(location.get(document)) if value is None else value
     if re.compile(r"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$").match(str(val)) is None:
         raise_exception(
             "when expecting a decimal",
             "found non-decimal",
             document, location=location,
         )
     else:
         return decimal.Decimal(val)
Exemplo n.º 8
0
 def validate_scalar(self, chunk, value=None):
     val = unicode(chunk.contents) if value is None else value
     if not utils.is_decimal(val):
         raise_exception(
             "when expecting a decimal",
             "found non-decimal",
             chunk,
         )
     else:
         return YAML(decimal.Decimal(val), val, chunk=chunk, validator=self)
Exemplo n.º 9
0
 def validate(self, document, location=None):
     val = str(location.get(document))
     if val not in self._restricted_to:
         raise_exception(
             "when expecting one of: {0}".format(", ".join(self._restricted_to)),
             "found '{0}'".format(val),
             document, location=location,
         )
     else:
         return val
Exemplo n.º 10
0
 def validate_scalar(self, document, location):
     val = str(location.get(document))
     if val != "":
         raise_exception(
             "when expecting an empty value",
             "found non-empty value",
             document, location=location,
         )
     else:
         return self.empty()
Exemplo n.º 11
0
 def validate_scalar(self, chunk, value):
     val = unicode(chunk.contents) if value is None else value
     if val != "":
         raise_exception(
             "when expecting an empty value",
             "found non-empty value",
             chunk,
         )
     else:
         return self.empty(chunk)
Exemplo n.º 12
0
 def validate_scalar(self, document, location, value=None):
     val = str(location.get(document)) if value is None else value
     if re.compile("^[-+]?\d+$").match(val) is None:
         raise_exception(
                 "when expecting an integer",
                 "found non-integer",
                 document, location=location,
             )
     else:
         return int(val)
Exemplo n.º 13
0
 def validate_scalar(self, document, location):
     val = str(location.get(document))
     if re.compile(r"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$").match(str(val)) is None:
         raise_exception(
             "when expecting a decimal",
             "found non-decimal",
             document, location=location,
         )
     else:
         return decimal.Decimal(val)
Exemplo n.º 14
0
 def validate_scalar(self, document, location):
     val = str(location.get(document))
     if re.compile("^[-+]?\d+$").match(val) is None:
         raise_exception(
                 "when expecting an integer",
                 "found non-integer",
                 document, location=location,
             )
     else:
         return int(val)
Exemplo n.º 15
0
 def validate_scalar(self, chunk, value):
     val = unicode(chunk.contents) if value is None else value
     if val not in self._restricted_to:
         raise_exception(
             "when expecting one of: {0}".format(", ".join(self._restricted_to)),
             "found '{0}'".format(val),
             chunk,
         )
     else:
         return YAML(val, chunk=chunk)
Exemplo n.º 16
0
 def validate_scalar(self, chunk, value=None):
     val = unicode(chunk.contents) if value is None else value
     if not utils.is_integer(val):
         raise_exception(
                 "when expecting an integer",
                 "found non-integer",
                 chunk,
             )
     else:
         return YAML(int(val), val, chunk=chunk)
Exemplo n.º 17
0
 def validate_scalar(self, document, location, value):
     val = str(location.get(document)) if value is None else value
     if val not in self._restricted_to:
         raise_exception(
             "when expecting one of: {0}".format(", ".join(self._restricted_to)),
             "found '{0}'".format(val),
             document, location=location,
         )
     else:
         return val
Exemplo n.º 18
0
 def validate_scalar(self, document, location, value):
     val = str(location.get(document)) if value is None else value
     if val != "":
         raise_exception(
             "when expecting an empty value",
             "found non-empty value",
             document, location=location,
         )
     else:
         return self.empty()
Exemplo n.º 19
0
    def validate(self, chunk):
        val = chunk.contents

        if type(val) == CommentedSeq or type(val) == CommentedMap:
            raise_exception(
                "when expecting a {0}".format(self.__class__.__name__.lower()),
                "found mapping/sequence",
                chunk,
            )
        else:
            return self.validate_scalar(chunk, value=None)
Exemplo n.º 20
0
    def validate_scalar(self, chunk, value=None):
        val = unicode(chunk.contents) if value is None else value

        try:
            return YAML(dateutil.parser.parse(val), val, chunk=chunk)
        except ValueError:
            raise_exception(
                "when expecting a datetime",
                "found non-datetime",
                chunk,
            )
Exemplo n.º 21
0
    def validate(self, document, location=None):
        val = location.get(document)

        if type(val) == CommentedSeq or type(val) == CommentedMap:
            raise_exception(
                "when expecting a {0}".format(self.__class__.__name__.lower()),
                "found mapping/sequence",
                document, location=location,
            )
        else:
            return self.validate_scalar(document, location, value=None)
Exemplo n.º 22
0
    def validate_scalar(self, document, location, value=None):
        val = str(location.get(document)) if value is None else value

        try:
            return dateutil.parser.parse(val)
        except ValueError:
            raise_exception(
                "when expecting a datetime",
                "found non-datetime",
                document, location=location,
            )
Exemplo n.º 23
0
    def validate(self, document, location=None):
        val = location.get(document)

        if type(val) == CommentedSeq or type(val) == CommentedMap:
            raise_exception(
                "when expecting a {0}".format(self.__class__.__name__.lower()),
                "found mapping/sequence",
                document, location=location,
            )
        else:
            return self.validate_scalar(document, location=location)
Exemplo n.º 24
0
    def validate_scalar(self, document, location):
        val = str(location.get(document))

        try:
            return dateutil.parser.parse(val)
        except ValueError:
            raise_exception(
                "when expecting a datetime",
                "found non-datetime",
                document, location=location,
            )
Exemplo n.º 25
0
 def validate_scalar(self, chunk, value=None):
     if re.compile(self._regex).match(chunk.contents) is None:
         raise_exception(
             self._matching_message,
             "found non-matching string",
             chunk,
         )
     return YAML(
         unicode(chunk.contents) if value is None else value,
         text=chunk.contents,
         chunk=chunk
     )
Exemplo n.º 26
0
 def validate_scalar(self, chunk, value=None):
     val = unicode(chunk.contents) if value is None else value
     if unicode(val).lower() not in constants.BOOL_VALUES:
         raise_exception(
             """when expecting a boolean value (one of "{0}")""".format(
                 '", "'.join(constants.BOOL_VALUES)),
             "found non-boolean",
             chunk,
         )
     else:
         if val.lower() in constants.TRUE_VALUES:
             return YAML(True, val, chunk=chunk, validator=self)
         else:
             return YAML(False, val, chunk=chunk, validator=self)
Exemplo n.º 27
0
    def validate(self, chunk):
        return_snippet = chunk.contents

        if not isinstance(return_snippet, CommentedSeq):
            raise_exception(
                "when expecting a sequence",
                "found non-sequence",
                chunk,
            )
        else:
            for i, item in enumerate(chunk.contents):
                return_snippet[i] = self._validator(chunk.index(i))

        return YAML(return_snippet, chunk=chunk)
Exemplo n.º 28
0
 def validate_scalar(self, document, location, value=None):
     val = str(location.get(document)) if value is None else value
     if str(val).lower() not in BOOL_VALUES:
         raise_exception(
             """when expecting a boolean value (one of "{0}")""".format(
                 '", "'.join(BOOL_VALUES)
             ),
             "found non-boolean",
             document, location=location,
         )
     else:
         if val in TRUE_VALUES:
             return True
         else:
             return False
Exemplo n.º 29
0
 def validate_scalar(self, document, location):
     val = str(location.get(document))
     if str(val).lower() not in BOOL_VALUES:
         raise_exception(
             """when expecting a boolean value (one of "{0}")""".format(
                 '", "'.join(BOOL_VALUES)
             ),
             "found non-boolean",
             document, location=location,
         )
     else:
         if val in TRUE_VALUES:
             return True
         else:
             return False
Exemplo n.º 30
0
    def validate(self, document, location=None):
        if location is None:
            location = YAMLLocation()
            document = copy.deepcopy(document)
        return_snippet = []

        if type(location.get(document)) != CommentedSeq:
            raise_exception(
                "when expecting a sequence",
                "found non-sequence",
                document, location=location,
            )
        else:
            for i, item in enumerate(location.get(document)):
                return_snippet.append(self._validator(document, location=location.index(i)))

        return return_snippet
Exemplo n.º 31
0
    def validate(self, document, location=None):
        if location is None:
            location = YAMLLocation()
            document = copy.deepcopy(document)
        return_snippet = []

        if type(location.get(document)) != CommentedSeq:
            raise_exception(
                "when expecting a sequence",
                "found non-sequence",
                document, location=location,
            )
        else:
            for i, item in enumerate(location.get(document)):
                return_snippet.append(self._validator(document, location=location.index(i)))

        return return_snippet
Exemplo n.º 32
0
    def validate(self, document, location=None):
        if location is None:
            location = YAMLLocation()
            document = copy.deepcopy(document)
        return_snippet = {}

        if type(location.get(document)) != CommentedMap:
            raise_exception(
                "when expecting a mapping",
                "found non-mapping",
                document, location=location,
            )
        else:
            for key, value in location.get(document).items():
                valid_key = self._key_validator(document, location.key(key))
                valid_val = self._value_validator(document, location.val(key))
                return_snippet[valid_key] = valid_val

        return return_snippet
Exemplo n.º 33
0
    def validate(self, document, location=None):
        if location is None:
            location = YAMLLocation()
            document = copy.deepcopy(document)
        return_snippet = {}

        if type(location.get(document)) != CommentedMap:
            raise_exception(
                "when expecting a mapping",
                "found non-mapping",
                document, location=location,
            )
        else:
            for key, value in location.get(document).items():
                valid_key = self._key_validator(document, location.key(key))
                valid_val = self._value_validator(document, location.val(key))
                return_snippet[valid_key] = valid_val

        return return_snippet
Exemplo n.º 34
0
    def validate(self, chunk):
        return_snippet = chunk.contents

        if not isinstance(return_snippet, CommentedMap):
            raise_exception(
                "when expecting a mapping",
                "found non-mapping",
                chunk,
            )
        else:
            for key, value in chunk.contents.items():
                valid_key = self._key_validator(chunk.key(key))
                valid_val = self._value_validator(chunk.val(key))
                return_snippet[valid_key] = valid_val

                del return_snippet[valid_key]
                return_snippet[valid_key] = self._value_validator(
                    chunk.val(key))

        return YAML(return_snippet, chunk=chunk)
Exemplo n.º 35
0
    def validate(self, chunk):
        return_snippet = chunk.contents

        if type(chunk.contents) != CommentedSeq:
            raise_exception(
                "when expecting a unique sequence",
                "found non-sequence",
                chunk,
            )
        else:
            existing_items = set()

            for i, item in enumerate(chunk.contents):
                if item in existing_items:
                    raise_exception("while parsing a sequence",
                                    "duplicate found", chunk)
                else:
                    existing_items.add(item)
                    return_snippet[i] = self._validator(chunk.index(i))

        return YAML(return_snippet, chunk=chunk)