
    ,                     v    d Z ddlmZ ddlmZ ddlmZ ddlZddlZdZdZdZ	d	 d
 d dZ
d Z G d de      Zy)z7A class that creates resource projection specification.    )absolute_import)division)unicode_literalsNzglobal.restrictionz' projection: The parent ProjectionSpec.leftc                 $    | j                  |      S N)ljustsws     <lib/googlecloudsdk/core/resource/resource_projection_spec.py<lambda>r   "   s    1771:    c                 $    | j                  |      S r   )centerr
   s     r   r   r   #   s    QXXa[r   c                 $    | j                  |      S r   )rjustr
   s     r   r   r   $   s    AGGAJr   )r   r   rightc                     i }i }| D ]T  }|s|j                   r|j                  |j                          |j                  s:|j                  |j                         V t        ||      S )aF  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                       e Zd ZdZdZdZdZ G d de      Zd!dZ	e
d	        Ze
d
        Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z e!jD                  fdZ#d  Z$y)"r   aG  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                       e Zd ZdZd Zy)ProjectionSpec._ColumnzColumn key and transform attribute for self._columns.

    Attributes:
      key: The column key.
      attribute: The column key _Attribute.
    c                      || _         || _        y r   )key	attributeselfr#   r$   s      r   __init__zProjectionSpec._Column.__init__m   s    dh dnr   N)__name__
__module____qualname____doc__r'    r   r   _Columnr!   e   s    !r   r-   Nc                 H   |xs i | _         i | _        g | _        || _        d| _        d| _        i | _        d| _        |r|j                  | _	        t        j                  |j                               | _        | j                          |rt        j                  |      ni | _        |j                  r%| j                  j!                  |j                         |j                   r&| j                   j!                  |j                          yyd| _	        d| _        |xs i | _        y)a8  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'   zProjectionSpec.__init__q   s     =bDLDODMDNDKDJDDN__dl==!1!1!34dj
mmo/6T]]7+Bdl			H,,-			H,,- 
 dldj]dlr   c                     | j                   S )z Gets the transform active level.)r7   r&   s    r   r6   zProjectionSpec.active   s     <<r   c                     | j                   S )z>Returns the projection compiler method for nested projections.)r1   r?   s    r   r=   zProjectionSpec.compiler   s     >>r   c                     | j                   |j                  _        |j                  j	                         D ]  }| j                  |        y)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   rF   zProjectionSpec._Defaults   s9     !%J&&(
nnT )r   c           	          t        |j                        D ]b  }|j                  dj                  d|z  ||j                  |   j                               | j                  |j                  |   ||dz          d y)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)sortedrD   writeformatr$   _Print)r&   rG   outlevelr#   s        r   rN   zProjectionSpec._Print   sq     joo&	ii077OOC(22 8 4 5 kk*//#&UQY7 'r   c                 "    || j                   |<   y)zsAdds name=value to the attributes.

    Args:
      name: The attribute name.
      value: The attribute value
    Nr/   )r&   namevalues      r   AddAttributezProjectionSpec.AddAttribute   s     "DOODr   c                 <    || j                   v r| j                   |= yy)zlDeletes name from the attributes if it is in the attributes.

    Args:
      name: The attribute name.
    NrR   r&   rS   s     r   DelAttributezProjectionSpec.DelAttribute   s!     t
//$
 r   c                 &    ||f| j                   |<   y)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&   rS   r#   r$   s       r   AddAliaszProjectionSpec.AddAlias   s     y)DLLr   c                 Z    | j                   j                  | j                  ||             y)zAdds key and attribute to the projection.

    Args:
      key: The parsed key to add.
      attribute: Parsed _Attribute to add.
    N)r0   appendr-   r%   s      r   AddKeyzProjectionSpec.AddKey   s!     	MMc956r   c                 8    | j                   ri | _        || _         y)zSets the projection name.

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

    Args:
      name: The projection name.
    N)r3   r/   rW   s     r   SetNamezProjectionSpec.SetName   s     zzdoDJr   c                     | j                   S )ziReturns the projection root node.

    Returns:
      The resource_projector_parser._Tree root node.
    r;   r?   s    r   r:   zProjectionSpec.GetRoot        ::r   c                     || _         y)ziSets the projection root node.

    Args:
      root: The resource_projector_parser._Tree root node.
    Nrb   )r&   roots     r   SetRootzProjectionSpec.SetRoot   s     DJr   c                     | j                   S )zReturns the projector resource_projector_parser._Tree empty node.

    Returns:
      The projector resource_projector_parser._Tree empty node.
    r2   r?   s    r   GetEmptyzProjectionSpec.GetEmpty   s     ;;r   c                     || _         y)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.
    Nrh   )r&   rH   s     r   SetEmptyzProjectionSpec.SetEmpty   s     DKr   c                     | j                   S )zReturns the projection columns.

    Returns:
      The columns in the projection, None if the entire resource is projected.
    )r0   r?   s    r   ColumnszProjectionSpec.Columns	  s     ==r   c                 ,    t        | j                        S )zReturns the number of columns in the projection.

    Returns:
      The number of columns in the projection, 0 if the entire resource is
        projected.
    )lenr0   r?   s    r   ColumnCountzProjectionSpec.ColumnCount  s     t}}r   c                     | j                   r| j                  | j                          g | _        | xj                  dz  c_        y)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;   rF   r0   r7   r?   s    r   r<   zProjectionSpec.Defaults  s0     zz
nnTZZ DMLLALr   c                     | j                   S )zReturns the short key name alias dictionary.

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

    Returns:
      The short key name alias dictionary.
    rZ   r?   s    r   AliaseszProjectionSpec.Aliases&  s     <<r   c                     | j                   S )zlReturns the projection _Attribute dictionary.

    Returns:
      The projection _Attribute dictionary.
    rR   r?   s    r   
AttributeszProjectionSpec.Attributes0  s     ??r   c                 v    | j                   D cg c]  }t        |j                  j                     ! c}S c c}w )zReturns the projection column justfication list.

    Returns:
      The ordered list of alignment functions, where each function is one of
        ljust [default], center, or rjust.
    )r0   
ALIGNMENTSr$   align)r&   cols     r   
AlignmentszProjectionSpec.Alignments8  s/     8<}}E}Js}}**+}EEEs   $6c                     | j                   D cg c]  }|j                  j                  xs d }}t        |      r|S dS c c}w )zReturns the ordered list of projection labels.

    Returns:
      The ordered list of projection label strings, None if all labels are
        empty.
     N)r0   r$   labelany)r&   ry   labelss      r   LabelszProjectionSpec.LabelsA  sE     48==A=Ccmm!!'R'=FA[6*d* Bs   !Ac                     | j                   S )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.
    )r3   r?   s    r   NamezProjectionSpec.NameK  s     ::r   c                    g }t        | j                        D ]  \  }}|j                  j                  s|j                  j                  s3|j                  |j                  j                  xs t        j                  ||j                  j                  f        t        |      D cg c]
  \  }}}||f c}}}S c c}}}w )a  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).
    )		enumerater0   r$   orderreverser]   sysmaxsizerK   )r&   orderingiry   _r   s         r   OrderzProjectionSpec.OrderU  s     HDMM*3			 5 5]]  /CKKCMM4I4IJ	L + /5X.>?.>]Q7QL.>???s   *B>c                 X    | j                   r| j                  | j                   |d       yy)znPrints the projection with indented nesting.

    Args:
      out: The output stream, sys.stdout if None.
    r   N)r;   rN   )r&   rO   s     r   PrintzProjectionSpec.Printi  s$     zz
kk$**c1% r   c                     | j                   S )zTReturns the projection tree root.

    Returns:
      The projection tree root.
    rb   r?   s    r   TreezProjectionSpec.Treer  rc   r   )NNNN)%r(   r)   r*   r+   rB   INNERPROJECTobjectr-   r'   propertyr6   r=   rF   rN   rU   rX   r[   r^   r`   r:   rf   ri   rk   rm   rp   r<   rs   ru   rz   r   r   r   r   stdoutr   r   r,   r   r   r   r   >   s     D '
%'
! 
!#>    8"" *7
F+@( jj &r   r   )r+   
__future__r   r   r   r8   r   GLOBAL_RESTRICTION_NAMEPROJECTION_ARG_DOCALIGN_DEFAULTrw   r   r   r   r,   r   r   <module>r      sO     > &  '  
 / > -0.0

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