Ejemplo 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))

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

        return YAML(return_snippet, chunk=chunk)
Ejemplo n.º 2
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,
                        validator=self)
        except ValueError:
            raise_exception(
                "when expecting a datetime",
                "found non-datetime",
                chunk,
            )
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
 def __call__(self, chunk):
     self.validate(chunk)
     return YAML(chunk, validator=self)
Ejemplo n.º 7
0
 def empty(self, chunk):
     return YAML([], '', chunk=chunk, validator=self)
Ejemplo n.º 8
0
 def __call__(self, chunk):
     chunk.expect_scalar(self.rule_description)
     return YAML(chunk, validator=self)
Ejemplo n.º 9
0
 def __call__(self, chunk):
     self.validate(chunk)
     return YAML(chunk.strictparsed(),
                 text=None,
                 chunk=chunk,
                 validator=self)
Ejemplo n.º 10
0
 def empty(self, chunk):
     return YAML([], '', chunk=chunk)
Ejemplo n.º 11
0
 def empty(self, chunk):
     return YAML(None, '', chunk=chunk)
Ejemplo n.º 12
0
 def validate_scalar(self, chunk, value=None):
     return YAML(
         unicode(chunk.contents) if value is None else value,
         text=chunk.contents,
         chunk=chunk,
     )