
    ,                     v    S r SSKJr  SSKJr  SSKJr  SSKrSSKrSrSrSr	S	 S
 S S.r
S r " S S\5      rg)z7A class that creates resource projection specification.    )absolute_import)division)unicode_literalsNzglobal.restrictionz' projection: The parent ProjectionSpec.leftc                 $    U R                  U5      $ N)ljustsws     <lib/googlecloudsdk/core/resource/resource_projection_spec.py<lambda>r   "   s    1771:    c                 $    U R                  U5      $ r   )centerr
   s     r   r   r   #   s    QXXa[r   c                 $    U R                  U5      $ r   )rjustr
   s     r   r   r   $   s    AGGAJr   )r   r   rightc                     0 n0 nU  Hf  nU(       d  M  UR                   (       a  UR                  UR                   5        UR                  (       d  MK  UR                  UR                  5        Mh     [        X!S9$ )a8  Combines a list of defaults into a new defaults object.

Args:
  defaults: An ordered list of ProjectionSpec objects to combine. alias and
    symbol names from higher index objects in the list take precedence.

Returns:
  A new ProjectionSpec object that is a combination of the objects in the
  defaults list.
)symbolsaliases)r   updater   ProjectionSpec)defaultsr   r   defaults       r   CombineDefaultsr   '   s[     ''gnnW__%nnW__%  
	99r   c                      \ rS rSrSrSrSrSr " S S\5      r	S#S	 jr
\S
 5       r\S 5       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!\"RF                  4S  jr$S! r%S"r&g)$r   >   a  Creates a resource projection specification.

A resource projection is an expression string that contains a list of resource
keys with optional attributes. A projector is a method that takes a projection
specification and a resource object as input and produces a new
JSON-serializable object containing only the values corresponding to the keys
in the projection specification.

Optional projection key attributes may transform the values in the output
JSON-serializable object. Cloud SDK projection attributes are used for output
formatting.

A default or empty projection expression still produces a projector that
converts a resource to a JSON-serializable object.

This class is used by the resource projection expression parser to create a
resource projection specification from a projection expression string.

Attributes:
  aliases: Resource key alias dictionary.
  _active: The transform active level. Incremented each time Defaults() is
    called. Used to determine active transforms.
  attributes: Projection attributes dict indexed by attribute name.
  _columns: A list of (key,_Attribute) tuples used to project a resource to
    a list of columns.
  _compiler: The projection compiler method for nested projections.
  _empty: An empty projection _Tree used by Projector().
  _name: The projection name from the expression string.
  _tree: The projection _Tree root, used by
    resource_projector.Evaluate() to efficiently project each resource.
  symbols: Default and caller-defined transform function dict indexed by
    function name.
r         c                       \ rS rSrSrS rSrg)ProjectionSpec._Columne   zColumn key and transform attribute for self._columns.

Attributes:
  key: The column key.
  attribute: The column key _Attribute.
c                     Xl         X l        g r   )key	attributeselfr%   r&   s      r   __init__ProjectionSpec._Column.__init__m   s    h nr   )r&   r%   N)__name__
__module____qualname____firstlineno____doc__r)   __static_attributes__ r   r   _Columnr"   e   s    !r   r2   Nc                    U=(       d    0 U l         0 U l        / U l        X@l        SU l        SU l        0 U l        SU l        U(       a  UR                  U l	        [        R                  " UR                  5       5      U l        U R                  5         U(       a  [        R                  " U5      O0 U l        UR                  (       a%  U R                  R!                  UR                  5        UR                   (       a&  U R                   R!                  UR                   5        ggSU l	        SU l        U=(       d    0 U l        g)a   Initializes a projection.

Args:
  defaults: A list of resource_projection_spec.ProjectionSpec defaults.
  symbols: Transform function symbol table dict indexed by function name.
  aliases: Resource key alias dictionary.
  compiler: The projection compiler method for nested projections.
Nr   )r   
attributes_columns	_compiler_empty_name_snake_headings	_snake_reactive_activecopydeepcopyGetRoot_treeDefaultsr   r   )r(   r   r   r   compilers        r   r)   ProjectionSpec.__init__q   s     =bDLDODMNDKDJDDN__dl==!1!1!34dj
mmo/6T]]7+Bdl			H,,-			H,,- 
 dldj]dlr   c                     U R                   $ )z Gets the transform active level.)r<   r(   s    r   r;   ProjectionSpec.active   s     <<r   c                     U R                   $ )z>Returns the projection compiler method for nested projections.)r6   rE   s    r   rB   ProjectionSpec.compiler   s     >>r   c                     U R                   UR                  l        UR                  R	                  5        H  nU R                  U5        M     g)zDefaults() helper -- converts a projection to a default projection.

Args:
  projection: A node in the original projection _Tree.
N)DEFAULTr&   flagtreevalues	_Defaults)r(   
projectionnodes      r   rN   ProjectionSpec._Defaults   s9     !%J&&(
nnT )r   c           	          [        UR                  5       H_  nUR                  SR                  SU-  UUR                  U   R                  S95        U R                  UR                  U   X#S-   5        Ma     g)a  Print() helper -- prints projection node p and its children.

Sorted by projection tree level for diff stability.

Args:
  projection: A _Tree node in the original projection.
  out: The output stream.
  level: The nesting level counting from 1 at the root.
z{indent} {key} : {attribute}
z  )indentr%   r&   r   N)sortedrL   writeformatr&   _Print)r(   rO   outlevelr%   s        r   rW   ProjectionSpec._Print   so     joo&	ii077OOC(22 8 4 5 kk*//#&QY7 'r   c                      X R                   U'   g)zcAdds name=value to the attributes.

Args:
  name: The attribute name.
  value: The attribute value
Nr4   )r(   namevalues      r   AddAttributeProjectionSpec.AddAttribute   s     "OODr   c                 >    XR                   ;   a  U R                   U	 gg)z`Deletes name from the attributes if it is in the attributes.

Args:
  name: The attribute name.
Nr\   r(   r]   s     r   DelAttributeProjectionSpec.DelAttribute   s     
//$
 r   c                 $    X#4U R                   U'   g)zAdds name as an alias for key and attribute to the projection.

Args:
  name: The short (no dots) alias name for key.
  key: The parsed key to add.
  attribute: The attribute for key.
Nr   )r(   r]   r%   r&   s       r   AddAliasProjectionSpec.AddAlias   s     )DLLr   c                 X    U R                   R                  U R                  X5      5        g)zvAdds key and attribute to the projection.

Args:
  key: The parsed key to add.
  attribute: Parsed _Attribute to add.
N)r5   appendr2   r'   s      r   AddKeyProjectionSpec.AddKey   s     	MMc56r   c                 @    U R                   (       a  0 U l        Xl         g)zSets the projection name.

The projection name is the rightmost of the names in the expression.

Args:
  name: The projection name.
N)r8   r4   rb   s     r   SetNameProjectionSpec.SetName   s     zzdoJr   c                     U R                   $ )z]Returns the projection root node.

Returns:
  The resource_projector_parser._Tree root node.
r@   rE   s    r   r?   ProjectionSpec.GetRoot        ::r   c                     Xl         g)z]Sets the projection root node.

Args:
  root: The resource_projector_parser._Tree root node.
Nrq   )r(   roots     r   SetRootProjectionSpec.SetRoot   s	     Jr   c                     U R                   $ )zReturns the projector resource_projector_parser._Tree empty node.

Returns:
  The projector resource_projector_parser._Tree empty node.
r7   rE   s    r   GetEmptyProjectionSpec.GetEmpty   s     ;;r   c                     Xl         g)zSets the projector resource_projector_parser._Tree empty node.

The empty node is used by to apply [] empty slice projections.

Args:
  node: The projector resource_projector_parser._Tree empty node.
Nry   )r(   rP   s     r   SetEmptyProjectionSpec.SetEmpty   s	     Kr   c                     U R                   $ )zuReturns the projection columns.

Returns:
  The columns in the projection, None if the entire resource is projected.
)r5   rE   s    r   ColumnsProjectionSpec.Columns	  s     ==r   c                 ,    [        U R                  5      $ )zReturns the number of columns in the projection.

Returns:
  The number of columns in the projection, 0 if the entire resource is
    projected.
)lenr5   rE   s    r   ColumnCountProjectionSpec.ColumnCount  s     t}}r   c                     U R                   (       a  U R                  U R                   5        / U l        U =R                  S-  sl        g)zConverts the projection to a default projection.

A default projection provides defaults for attribute values and function
symbols. An explicit non-default projection value always overrides the
corresponding default value.
r   N)r@   rN   r5   r<   rE   s    r   rA   ProjectionSpec.Defaults  s0     zz
nnTZZ DMLLALr   c                     U R                   $ )zReturns the short key name alias dictionary.

This dictionary maps short (no dots) names to parsed keys.

Returns:
  The short key name alias dictionary.
rf   rE   s    r   AliasesProjectionSpec.Aliases&  s     <<r   c                     U R                   $ )z`Returns the projection _Attribute dictionary.

Returns:
  The projection _Attribute dictionary.
r\   rE   s    r   
AttributesProjectionSpec.Attributes0  s     ??r   c                 |    U R                    Vs/ s H   n[        UR                  R                     PM"     sn$ s  snf )zReturns the projection column justfication list.

Returns:
  The ordered list of alignment functions, where each function is one of
    ljust [default], center, or rjust.
)r5   
ALIGNMENTSr&   align)r(   cols     r   
AlignmentsProjectionSpec.Alignments8  s/     8<}}E}Js}}**+}EEEs   '9c                     U R                    Vs/ s H!  oR                  R                  =(       d    SPM#     nn[        U5      (       a  U$ S$ s  snf )zReturns the ordered list of projection labels.

Returns:
  The ordered list of projection label strings, None if all labels are
    empty.
 N)r5   r&   labelany)r(   r   labelss      r   LabelsProjectionSpec.LabelsA  sE     48==A=Cmm!!'R'=FA[[6*d* Bs   (Ac                     U R                   $ )zReturns the projection name.

The projection name is the rightmost of the names in the expression.

Returns:
  The projection name, None if none was specified.
)r8   rE   s    r   NameProjectionSpec.NameK  s     ::r   c                    / n[        U R                  5       H  u  p#UR                  R                  (       d  UR                  R                  (       d  M=  UR                  UR                  R                  =(       d    [        R                  X#R                  R                  45        M     [        U5       VVVs/ s H	  u  pBoRU4PM     snnn$ s  snnnf )ak  Returns the projection sort key order suitable for use by sorted().

Example:
  projection = resource_projector.Compile('...')
  order = projection.Order()
  if order:
    rows = sorted(rows, key=itemgetter(*order))

Returns:
  The list of (sort-key-index, reverse), [] if projection is None
  or if all sort order indices in the projection are None (unordered).
)		enumerater5   r&   orderreverserj   sysmaxsizerT   )r(   orderingir   _r   s         r   OrderProjectionSpec.OrderU  s     HDMM*			 5 5 5]]  /CKKMM4I4IJ	L + /5X.>?.>]Q7L.>???s   :Cc                 b    U R                   (       a  U R                  U R                   US5        gg)zbPrints the projection with indented nesting.

Args:
  out: The output stream, sys.stdout if None.
r   N)r@   rW   )r(   rX   s     r   PrintProjectionSpec.Printi  s$     zz
kk$**c1% r   c                     U R                   $ )zHReturns the projection tree root.

Returns:
  The projection tree root.
rq   rE   s    r   TreeProjectionSpec.Treer  rs   r   )r<   r5   r6   r7   r8   r9   r:   r@   r   r4   r   )NNNN)'r+   r,   r-   r.   r/   rJ   INNERPROJECTobjectr2   r)   propertyr;   rB   rN   rW   r_   rc   rg   rk   rn   r?   rv   rz   r}   r   r   rA   r   r   r   r   r   r   r   stdoutr   r   r0   r1   r   r   r   r   >   s     D '
%'
! 
!#>    8"" *7
F+@( jj &r   r   )r/   
__future__r   r   r   r=   r   GLOBAL_RESTRICTION_NAMEPROJECTION_ARG_DOCALIGN_DEFAULTr   r   r   r   r1   r   r   <module>r      sO     > &  '  
 / > -0.0

:.zV zr   