Ejemplo n.º 1
0
    def __setitem__(self, index, value):
        existing_validator = self._value[index].validator

        if isinstance(value, YAML):
            new_value = existing_validator(value._chunk)
        else:
            new_value = existing_validator(YAMLChunk(ruamel_structure(value)))

        # First validate against forked document
        proposed_chunk = self._chunk.fork()
        proposed_chunk.contents[index] = new_value.as_marked_up()
        proposed_chunk.strictparsed()[index] = deepcopy(
            new_value.as_marked_up())

        if self.is_mapping():
            updated_value = existing_validator(proposed_chunk.val(index))
            updated_value._chunk.make_child_of(self._chunk.val(index))
        else:
            updated_value = existing_validator(proposed_chunk.index(index))
            updated_value._chunk.make_child_of(self._chunk.index(index))

        # If validation succeeds, update for real
        marked_up = new_value.as_marked_up()

        # So that the nicer x: | style of text is used instead of
        # x: "text\nacross\nlines"
        if isinstance(marked_up, (str, unicode)):
            if u"\n" in marked_up:
                marked_up = PreservedScalarString(marked_up)

        self._chunk.contents[index] = marked_up
        self._value[YAML(index) if self.is_mapping() else index] = new_value
Ejemplo n.º 2
0
def as_document(data, schema=None, label=u"<unicode string>"):
    """
    Translate dicts/lists and scalar (string/bool/float/int/etc.) values into a
    YAML object which can be dumped out.
    """
    if schema is None:
        schema = Any()

    return schema(YAMLChunk(utils.ruamel_structure(data), label=label))