
    x:                         S r SSKJr  SSKJr  SSKJr  SSKJr  SrSr	Sr
S	r\" \\	\
\45      r " S
 S\5      r " S S\R                   5      rg)zPyYAML event builder handler

Receives events from YAML listener and forwards them to a builder
object so that it can construct a properly structured object.
    )absolute_import)yaml_errors)yaml_listener)yamldocumentsequencemappingkeyc                   H    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rSrg)Builder/   zInterface for building documents and type from YAML events.

Implement this interface to create a new builder.  Builders are
passed to the BuilderHandler and used as a factory and assembler
for creating concrete representations of YAML files.
c                     g)a  Build new document.

The object built by this method becomes the top level entity
that the builder handler constructs.  The actual type is
determined by the sub-class of the Builder class and can essentially
be any type at all.  This method is always called when the parser
encounters the start of a new document.

Returns:
  New object instance representing concrete document which is
  returned to user via BuilderHandler.GetResults().
N selfs    0lib/googlecloudsdk/appengine/api/yaml_builder.pyBuildDocumentBuilder.BuildDocument7           c                     g)a  Initialize document with value from top level of document.

This method is called when the root document element is encountered at
the top level of a YAML document.  It should get called immediately
after BuildDocument.

Receiving the None value indicates the empty document.

Args:
  document: Document as constructed in BuildDocument.
  value: Scalar value to initialize the document with.
Nr   )r   r   values      r   InitializeDocumentBuilder.InitializeDocumentE   r   r   c                     g)ae  Build a new mapping representation.

Called when StartMapping event received.  Type of object is determined
by Builder sub-class.

Args:
  top_value: Object which will be new mappings parant.  Will be object
    returned from previous call to BuildMapping or BuildSequence.

Returns:
  Instance of new object that represents a mapping type in target model.
Nr   r   	top_values     r   BuildMappingBuilder.BuildMappingS   r   r   c                     g)a  Previously constructed mapping scope is at an end.

Called when the end of a mapping block is encountered.  Useful for
additional clean up or end of scope validation.

Args:
  top_value: Value which is parent of the mapping.
  mapping: Mapping which is at the end of its scope.
Nr   )r   r   r	   s      r   
EndMappingBuilder.EndMappinga   r   r   c                     g)ai  Build a new sequence representation.

Called when StartSequence event received.  Type of object is determined
by Builder sub-class.

Args:
  top_value: Object which will be new sequences parant.  Will be object
    returned from previous call to BuildMapping or BuildSequence.

Returns:
  Instance of new object that represents a sequence type in target model.
Nr   r   s     r   BuildSequenceBuilder.BuildSequencel   r   r   c                     g)a  Previously constructed sequence scope is at an end.

Called when the end of a sequence block is encountered.  Useful for
additional clean up or end of scope validation.

Args:
  top_value: Value which is parent of the sequence.
  sequence: Sequence which is at the end of its scope.
Nr   )r   r   r   s      r   EndSequenceBuilder.EndSequencez   r   r   c                     g)a<  Map value to a mapping representation.

Implementation is defined by sub-class of Builder.

Args:
  subject: Object that represents mapping.  Value returned from
    BuildMapping.
  key: Key used to map value to subject.  Can be any scalar value.
  value: Value which is mapped to subject. Can be any kind of value.
Nr   )r   subjectr
   r   s       r   MapToBuilder.MapTo   r   r   c                     g)zAppend value to a sequence representation.

Implementation is defined by sub-class of Builder.

Args:
  subject: Object that represents sequence.  Value returned from
    BuildSequence
  value: Value to be appended to subject.  Can be any kind of value.
Nr   )r   r*   r   s      r   AppendToBuilder.AppendTo   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r   r   r!   r$   r'   r+   r.   __static_attributes__r   r   r   r   r   /   s/    		
	r   r   c                   x    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rSrg)BuilderHandler   a1  PyYAML event handler used to build objects.

Maintains state information as it receives parse events so that object
nesting is maintained.  Uses provided builder object to construct and
assemble objects as it goes.

As it receives events from the YAML parser, it builds a stack of data
representing structural tokens.  As the scope of documents, mappings
and sequences end, those token, value pairs are popped from the top of
the stack so that the original scope can resume processing.

A special case is made for the _KEY token.  It represents a temporary
value which only occurs inside mappings.  It is immediately popped off
the stack when it's associated value is encountered in the parse stream.
It is necessary to do this because the YAML parser does not combine
key and value information in to a single event.
c                     [        U[        5      (       d  [        R                  " S5      eXl        SU l        SU l        / U l        g)zInitialization for builder handler.

Args:
  builder: Instance of Builder class.

Raises:
  ListenerConfigurationError when builder is not a Builder class.
z2Must provide builder of type yaml_listener.BuilderN)
isinstancer   r   ListenerConfigurationError_builder_stack_top_results)r   builders     r   __init__BuilderHandler.__init__   sA     gw''22<> >MDKDIDMr   c                 ^    X4U l         U R                  R                  U R                   5        g)a  Push values to stack at start of nesting.

When a new object scope is beginning, will push the token (type of scope)
along with the new objects value, the latter of which is provided through
the various build methods of the builder.

Args:
  token: Token indicating the type of scope which is being created; must
    belong to _TOKEN_VALUES.
  value: Value to associate with given token.  Construction of value is
    determined by the builder provided to this handler at construction.
N)r>   r=   appendr   tokenr   s      r   _PushBuilderHandler._Push   s#     DIKKtyy!r   c                     U R                   / :w  a  U R                   c   eU R                   R                  5       u  pU R                   (       a  U R                   S   U l        U$ SU l        U$ )zPop values from stack at end of nesting.

Called to indicate the end of a nested scope.

Returns:
  Previously pushed value at the top of the stack.
N)r=   popr>   rE   s      r   _PopBuilderHandler._Pop   s\     ;;"!888;;??$LE{{++b/di L diLr   c                 X    [        US5      (       a  UR                  b  [        S5      egg)a0  Handle anchor attached to event.

Currently will raise an error if anchor is used.  Anchors are used to
define a document wide tag to a given value (scalar, mapping or sequence).

Args:
  event: Event which may have anchor property set.

Raises:
  NotImplementedError if event attempts to use an anchor.
anchorNz%Anchors not supported in this handler)hasattrrO   NotImplementedError)r   events     r   _HandleAnchorBuilderHandler._HandleAnchor   s.     uhELL$< GHH %=r   c                    U R                   u  p#U[        :X  aG  U R                  5       nU R                   u  pV[        U:X  d   eU R                  R                  XdU5        gU[        :X  a  U R                  [        U5        gU[        :X  a  U R                  R                  X15        gU[        :X  a  U R                  R                  X15        g[        R                  " SU-  5      e)a(  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.
zUnrecognized builder token:
%sN)r>   
_TOKEN_KEYrL   _TOKEN_MAPPINGr<   r+   rG   _TOKEN_SEQUENCEr.   _TOKEN_DOCUMENTr   r   InternalError)r   r   rF   r   r
   mapping_tokenr	   s          r   _HandleValueBuilderHandler._HandleValue   s    $ yyE 
IIKc#yym},,,
mm'. 
.	 
jjU# 
/	!
mmY. 
/	!
mm&&y8 %%&G%&OPPr   c                 L    U R                   b   e/ U l         SU l        / U l        g)z?Initializes internal state of handler

Args:
  event: Ignored.
N)r=   r>   r?   r   rR   loaders      r   StreamStartBuilderHandler.StreamStart&  s)     ;;DKDIDMr   c                 P    U R                   / :X  a  U R                  b   eSU l         g)zKCleans up internal state of handler after parsing

Args:
  event: Ignored.
N)r=   r>   r_   s      r   	StreamEndBuilderHandler.StreamEnd1  s&     ;;"!222DKr   c                     U R                   / :X  d   eU R                  [        U R                  R	                  5       5        g)zOBuild new document.

Pushes new document on to stack.

Args:
  event: Ignored.
N)r=   rG   rY   r<   r   r_   s      r   DocumentStartBuilderHandler.DocumentStart:  s1     ;;"JJ ; ; =>r   c                     U R                   S   [        :X  d   eU R                  R                  U R	                  5       5        g)z*End of document.

Args:
  event: Ignored.
r   N)r>   rY   r?   rD   rL   r_   s      r   DocumentEndBuilderHandler.DocumentEndE  s2     99Q<?***MM%r   c                     [        S5      e)z.Not implemented yet.

Args:
  event: Ignored.
z(References not supported in this handler)rQ   r_   s      r   AliasBuilderHandler.AliasN  s     H
IIr   c                 :   U R                  U5        UR                  cX  U R                  S   [        :w  aA   UR	                  [
        R                  R                  UR                  UR                  5      nOUR                  nUc  UR                  nO\[
        R                  R                  UUR                  UR                  UR                  UR                  5      nUR                  U5      nU R!                  U5        g! [         a    UR                  n Nf = f)a7  Handle scalar value

Since scalars are simple values that are passed directly in by the
parser, handle like any value with no additional processing.

Of course, key values will be handles specially.  A key value is recognized
when the top token is _TOKEN_MAPPING.

Args:
  event: Event containing scalar value.
Nr   )rS   tagr>   rW   resolver   nodes
ScalarNoder   implicit
IndexErrorDEFAULT_SCALAR_TAG
start_markend_markstyleconstruct_objectr\   )r   rR   r`   rp   r   nodes         r   ScalarBuilderHandler.ScalarV  s     	uyyTYYq\^;(nnTZZ22"[[%..: IIc
{kke ZZ""3#(;;#(#3#3#(>>#(;;	0d
 %%d+ee#  ( ''(s   ?D DDc                     U R                  U5        U R                  u  p4U[        :X  a  U R                  S   u  p4U R                  R                  U5      nU R                  U5        U R                  [        U5        g)zStart of sequence scope

Create a new sequence from the builder and then handle in the context
of its parent.

Args:
  event: SequenceStartEvent generated by loader.
  loader: Loader that generated event.
N)	rS   r>   rV   r=   r<   r$   r\   rG   rX   )r   rR   r`   rF   parentr   s         r   SequenceStartBuilderHandler.SequenceStart|  sf     	uIIME 
kk"ome}}**62HhJJ)r   c                     U R                   S   [        :X  d   eU R                  5       nU R                   S   nU R                  R	                  XC5        g)z>End of sequence.

Args:
  event: Ignored
  loader: Ignored.
  r      N)r>   rX   rL   r<   r'   r   rR   r`   
end_objectr   s        r   SequenceEndBuilderHandler.SequenceEnd  sD     99Q<?***J		!IMMi4r   c                     U R                  U5        U R                  u  p4U[        :X  a  U R                  S   u  p4U R                  R                  U5      nU R                  U5        U R                  [        U5        g)zStart of mapping scope.

Create a mapping from builder and then handle in the context of its
parent.

Args:
  event: MappingStartEvent generated by loader.
  loader: Loader that generated event.
r   N)	rS   r>   rV   r=   r<   r   r\   rG   rW   )r   rR   r`   rF   r   r	   s         r   MappingStartBuilderHandler.MappingStart  sf     	uIIME 
kk"omemm((0GgJJ~w'r   c                     U R                   S   [        :X  d   eU R                  5       nU R                   S   nU R                  R	                  XC5        g)z;End of mapping

Args:
  event: Ignored.
  loader: Ignored.
r   r   N)r>   rW   rL   r<   r!   r   s        r   
MappingEndBuilderHandler.MappingEnd  sD     99Q<>)))J		!IMMY3r   c                 r    U R                   b  [        R                  " S5      e[        U R                  5      $ )aY  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.
zBuilder stack is not empty.)r=   r   rZ   tupler?   r   s    r   
GetResultsBuilderHandler.GetResults  s/     {{%%&CDDr   )r<   r?   r=   r>   N)r0   r1   r2   r3   r4   rA   rG   rL   rS   r\   ra   rd   rg   rj   rm   r|   r   r   r   r   r   r5   r   r   r   r7   r7      s\    $""""I"0Qd		?&J$L*(
5(0
4 r   r7   N)r4   
__future__r   googlecloudsdk.appengine.apir   r   ruamelr   rY   rX   rW   rV   	frozenset_TOKEN_VALUESobjectr   EventHandlerr7   r   r   r   <module>r      sl     ' 4 6  
	 kf k\q ]// q r   