Beispiel #1
0
    def _HandleValue(self, value):
        """Handle given value based on state of parser

    This method handles the various values that are created by the builder
    at the beginning of scope events (such as mappings and sequences) or
    when a scalar value is received.

    Method is called when handler receives a parser, MappingStart or
    SequenceStart.

    Args:
      value: Value received as scalar value or newly constructed mapping or
        sequence instance.

    Raises:
      InternalError if the building process encounters an unexpected token.
      This is an indication of an implementation error in BuilderHandler.
    """
        token, top_value = self._top

        # If the last token was a key, it means that it is necessary
        # to insert the value in to a map.
        if token == _TOKEN_KEY:
            # Fetch the key (removing from the stack)
            key = self._Pop()
            # New values at top of stack
            mapping_token, mapping = self._top
            assert _TOKEN_MAPPING == mapping_token
            # Forward to builder for assembly
            self._builder.MapTo(mapping, key, value)

        # Parent object for new value is a mapping.  It means that
        # this value that is passed in is a scalar and should
        # get placed on the stack as the key for the next value
        # from the parser.
        elif token == _TOKEN_MAPPING:
            self._Push(_TOKEN_KEY, value)

        # Parent is a sequence object.  Append value to sequence.
        elif token == _TOKEN_SEQUENCE:
            self._builder.AppendTo(top_value, value)

        # Events received at the document level are sent to the
        # builder to initialize the actual document.
        elif token == _TOKEN_DOCUMENT:
            self._builder.InitializeDocument(top_value, value)

        else:
            raise yaml_errors.InternalError('Unrecognized builder token:\n%s' %
                                            token)
Beispiel #2
0
    def GetResults(self):
        """Get results of document stream processing.

    This method can be invoked after fully parsing the entire YAML file
    to retrieve constructed contents of YAML file.  Called after EndStream.

    Returns:
      A tuple of all document objects that were parsed from YAML stream.

    Raises:
      InternalError if the builder stack is not empty by the end of parsing.
    """
        if self._stack is not None:
            raise yaml_errors.InternalError('Builder stack is not empty.')
        return tuple(self._results)