Exemplo n.º 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 token == _TOKEN_KEY:

      key = self._Pop()

      mapping_token, mapping = self._top
      assert _TOKEN_MAPPING == mapping_token

      self._builder.MapTo(mapping, key, value)





    elif token == _TOKEN_MAPPING:
      self._Push(_TOKEN_KEY, value)


    elif token == _TOKEN_SEQUENCE:
      self._builder.AppendTo(top_value, value)



    elif token == _TOKEN_DOCUMENT:
      self._builder.InitializeDocument(top_value, value)

    else:
      raise yaml_errors.InternalError('Unrecognized builder token:\n%s' % token)
    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)