o
    ,                     @   sx   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
 dd
 dZ
dd ZG dd deZdS )z7A class that creates resource projection specification.    )absolute_import)division)unicode_literalsNzglobal.restrictionz' projection: The parent ProjectionSpec.leftc                 C   
   |  |S N)ljustsw r   R/tmp/google-cloud-sdk/lib/googlecloudsdk/core/resource/resource_projection_spec.py<lambda>"      
 r   c                 C   r   r   )centerr	   r   r   r   r   #   r   c                 C   r   r   )rjustr	   r   r   r   r   $   r   )r   r   rightc                 C   sH   i }i }| D ]}|sq|j r||j  |jr||j qt||d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   defaultr   r   r   CombineDefaults'   s   r   c                   @   s  e Zd ZdZdZdZdZG dd deZd:dd	Z	e
d
d Ze
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Z e!j"fd6d7Z#d8d9 Z$dS );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                   @   s   e Zd ZdZdd ZdS )zProjectionSpec._ColumnzColumn key and transform attribute for self._columns.

    Attributes:
      key: The column key.
      attribute: The column key _Attribute.
    c                 C   s   || _ || _d S r   )key	attributeselfr   r   r   r   r   __init__m   s   
zProjectionSpec._Column.__init__N)__name__
__module____qualname____doc__r    r   r   r   r   _Columne   s    r%   Nc                 C   s   |pi | _ i | _g | _|| _d| _d| _i | _d| _|rN|j| _	t
| | _|   |r3t
|ni | _|jr@| j|j |j rL| j |j  dS dS d| _	d| _|pWi | _dS )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   compilerr   r   r   r    q   s*   
	zProjectionSpec.__init__c                 C      | j S )z Gets the transform active level.)r.   r   r   r   r   r-         zProjectionSpec.activec                 C   r5   )z>Returns the projection compiler method for nested projections.)r(   r6   r   r   r   r4      r7   zProjectionSpec.compilerc                 C   s(   | j |j_|j D ]}| | q
dS )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noder   r   r   r<      s   
zProjectionSpec._Defaultsc                 C   sN   t |jD ]}|djd| ||j| jd | |j| ||d  qdS )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)sortedr:   writeformatr   _Print)r   r=   outlevelr   r   r   r   rC      s   

zProjectionSpec._Printc                 C   s   || j |< dS )zsAdds name=value to the attributes.

    Args:
      name: The attribute name.
      value: The attribute value
    Nr&   )r   namevaluer   r   r   AddAttribute   s   zProjectionSpec.AddAttributec                 C   s   || j v r| j |= dS dS )zlDeletes name from the attributes if it is in the attributes.

    Args:
      name: The attribute name.
    NrF   r   rG   r   r   r   DelAttribute   s   
zProjectionSpec.DelAttributec                 C   s   ||f| j |< dS )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   rG   r   r   r   r   r   AddAlias   s   zProjectionSpec.AddAliasc                 C   s   | j | || dS )zAdds key and attribute to the projection.

    Args:
      key: The parsed key to add.
      attribute: Parsed _Attribute to add.
    N)r'   appendr%   r   r   r   r   AddKey   s   zProjectionSpec.AddKeyc                 C   s   | j ri | _|| _ dS )zSets the projection name.

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

    Args:
      name: The projection name.
    N)r*   r&   rJ   r   r   r   SetName   s   
zProjectionSpec.SetNamec                 C   r5   )ziReturns the projection root node.

    Returns:
      The resource_projector_parser._Tree root node.
    r2   r6   r   r   r   r1         zProjectionSpec.GetRootc                 C   
   || _ dS )ziSets the projection root node.

    Args:
      root: The resource_projector_parser._Tree root node.
    NrQ   )r   rootr   r   r   SetRoot   s   
zProjectionSpec.SetRootc                 C   r5   )zReturns the projector resource_projector_parser._Tree empty node.

    Returns:
      The projector resource_projector_parser._Tree empty node.
    r)   r6   r   r   r   GetEmpty   rR   zProjectionSpec.GetEmptyc                 C   rS   )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.
    NrV   )r   r>   r   r   r   SetEmpty   s   
zProjectionSpec.SetEmptyc                 C   r5   )zReturns the projection columns.

    Returns:
      The columns in the projection, None if the entire resource is projected.
    r'   r6   r   r   r   Columns	  rR   zProjectionSpec.Columnsc                 C   s
   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.
    )lenr'   r6   r   r   r   ColumnCount  s   
zProjectionSpec.ColumnCountc                 C   s*   | j r	| | j  g | _|  jd7  _dS )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)r2   r<   r'   r.   r6   r   r   r   r3     s   zProjectionSpec.Defaultsc                 C   r5   )zReturns the short key name alias dictionary.

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

    Returns:
      The short key name alias dictionary.
    rL   r6   r   r   r   Aliases&     zProjectionSpec.Aliasesc                 C   r5   )zlReturns the projection _Attribute dictionary.

    Returns:
      The projection _Attribute dictionary.
    rF   r6   r   r   r   
Attributes0  rR   zProjectionSpec.Attributesc                 C   s   dd | j D S )zReturns the projection column justfication list.

    Returns:
      The ordered list of alignment functions, where each function is one of
        ljust [default], center, or rjust.
    c                 S   s   g | ]}t |jj qS r   )
ALIGNMENTSr   align.0colr   r   r   
<listcomp>?      z-ProjectionSpec.Alignments.<locals>.<listcomp>rY   r6   r   r   r   
Alignments8  s   zProjectionSpec.Alignmentsc                 C   s    dd | j D }t|r|S dS )zReturns the ordered list of projection labels.

    Returns:
      The ordered list of projection label strings, None if all labels are
        empty.
    c                 S   s   g | ]}|j jp	d qS ) )r   labelrb   r   r   r   re   H  rf   z)ProjectionSpec.Labels.<locals>.<listcomp>N)r'   any)r   labelsr   r   r   LabelsA  s   zProjectionSpec.Labelsc                 C   r5   )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.
    )r*   r6   r   r   r   NameK  r^   zProjectionSpec.Namec                 C   sX   g }t | jD ]\}}|jjs|jjr"||jjptj||jjf qdd t|D S )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).
    c                 S   s   g | ]	\}}}||fqS r   r   )rc   _ireverser   r   r   re   g  s    z(ProjectionSpec.Order.<locals>.<listcomp>)		enumerater'   r   orderrp   rN   sysmaxsizer@   )r   orderingro   rd   r   r   r   OrderU  s   zProjectionSpec.Orderc                 C   s   | j r| | j |d dS dS )znPrints the projection with indented nesting.

    Args:
      out: The output stream, sys.stdout if None.
    r   N)r2   rC   )r   rD   r   r   r   Printi  s   zProjectionSpec.Printc                 C   r5   )zTReturns the projection tree root.

    Returns:
      The projection tree root.
    rQ   r6   r   r   r   Treer  rR   zProjectionSpec.Tree)NNNN)%r!   r"   r#   r$   r8   INNERPROJECTobjectr%   r    propertyr-   r4   r<   rC   rI   rK   rM   rO   rP   r1   rU   rW   rX   rZ   r\   r3   r]   r_   rg   rl   rm   rv   rs   stdoutrw   rx   r   r   r   r   r   >   sB    "



		
	
	
	

	r   )r$   
__future__r   r   r   r/   rs   GLOBAL_RESTRICTION_NAMEPROJECTION_ARG_DOCALIGN_DEFAULTr`   r   r{   r   r   r   r   r   <module>   s   