
     #                     p   S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSKJ
r
  SS	KJr  SS
KJr   " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      rg)aV  Resource display taps.

A tapper is an object that converts an iterable into another iterable. It
applies the Tap method as each item is iterated on, and applies the Done method
just before the iteration stops. A Tapper may delete items from an iterable
and/or inject new items into an iterable. Tappers are useful for monitoring or
modifying an iterable without consuming it all at once.

For example one could always:

  items = list(iterable_items)
  for item in items:
    do_something(item)
  # pass items to the next consumer

However, if an iterable consumed a lot of resources (say a generator with a
zillion items) then the list() statement would instantiate all of the items and
strain memory. A tapper avoids this by dealing with each item as it is
generated.

There is a tapper for each of the --filter, --flatten, --limit, and --page
flags.
    )absolute_import)division)unicode_literals)log)resource_filter)resource_printer_base)resource_projector)resource_property)peek_iterablec                   *    \ rS rSrSrS rS rS rSrg)Filterer4   zyA Tapper class that filters out resources not matching an expression.

Attributes:
  _match: The resource filter method.
c                     [         R                  " XS9U l        [         R                  " U R                  5      U l        g)zConstructor.

Args:
  expression: The resource filter expression string.
  defaults: The resource format and filter default projection.
)defaultsN)r   Compile_compiled_expression
GetAllKeys_missing_keys)self
expressionr   s      +lib/googlecloudsdk/calliope/display_taps.py__init__Filterer.__init__;   s5     !0 7 7!D )33D4M4MND    c                   ^ [         R                  " U5      mU =R                  [        U4S jU R                   5       5      -  sl        [        R
                  " U5      (       a  gU R                  R                  T5      $ )zReturns True if resource matches the filter expression.

Args:
  resource: The resource to filter.

Returns:
  True if resource matches the filter expression.
c              3   d   >#    U  H%  n[         R                  " TU5      (       d  M!  Uv   M'     g 7fN)r
   ResourceContainsKey).0keyserialized_resources     r   	<genexpr>Filterer.Tap.<locals>.<genexpr>Q   s+      %C001DcJ 	%s    0	0T)r	   MakeSerializabler   setr   IsResourceMarkerr   Evaluate)r   resourcer!   s     @r   TapFilterer.TapG   so     -==hG# %%  
 --h77$$--.ABBr   c                 R    S nU R                   (       a  U" U R                   5        g g )Nc                     SR                  [        U 5       Vs/ s H"  nSR                  [        [        U5      5      PM$     sn5      n[        R
                  " SU-   5        g s  snf )Nz, .z=The following filter keys were not present in any resource : )joinsortedmapstrr   warning)missing_keysr    missing_keys_strs      r   WarnMissingKeys&Filterer.Done.<locals>.WarnMissingKeys[   sX    .4\.B
C.Bs388CSM".B
C 
kk
I Ds   )A#)r   )r   r5   s     r   DoneFilterer.DoneZ   s%     d(() r   )r   r   N	__name__
__module____qualname____firstlineno____doc__r   r)   r7   __static_attributes__ r   r   r   r   4   s    
OC&*r   r   c                   $    \ rS rSrSrS rS rSrg)	Flattenerh   aO  A Tapper class that flattens a resource key slice to separate records.

A serialized copy of the resource is modified in place. This means the same
resource object is returned for each flattened slice item. This is OK because
the downstream is not guaranteed uniqueness.

Attributes:
  _child_name: The flattened value to set is _parent_key[_child_name].
  _key: The parsed resource key of the slice to flatten.
  _parent_key: The parent of _key, None for the resource itself.
  _items: The items to flatten in the current resource.
  _resource: The serialized copy of the current resource.
c                     USS U l         U R                   (       a  U R                   S   OSU l        U R                   (       a  U R                   SS OSU l        SU l        SU l        g)zEConstructor.

Args:
  key: The resource key of the slice to flatten.
N)_key_child_name_parent_key_items	_resource)r   r    s     r   r   Flattener.__init__w   sO     ADI(,		tyy}tD)-tyy"~DDKDNr   c                    U R                   c  [        R                  " U5      U l        [        R
                  " U R                  U R                  5      U l         [        U R                   [        5      (       d(  U R                   nSU l         [        R                  " USS9$ U R                   (       d  SU l         gU R                   R                  S5      nU R                  (       a,  [        R
                  " U R                  U R                  5      nOU R                  nX#U R                  '   [        R                  " [        R                  " U R                  5      5      $ )zReturns the next slice item in resource.

Args:
  resource: The resource to flatten.

Returns:
  True if the next slice is not a list, False if there are no more items,
  or Injector(resource) which is the resource with the next slice flattened.
NTreplaceFr   )rI   r	   r$   rJ   r
   GetrF   
isinstancelistr   TapInjectorpoprH   rG   )r   r(   itemparents       r   r)   Flattener.Tap   s     {{)::8Ddn%))$..$))DdkT**{{((t<<;;dk;;??1D $$T^^T5E5EFf~~f#4$$++DNN; r   )rG   rI   rF   rH   rJ   Nr:   r;   r<   r=   r>   r   r)   r?   r@   r   r   rB   rB   h   s    r   rB   c                   $    \ rS rSrSrS rS rSrg)Limiter   zA Tapper class that filters out resources after a limit is reached.

Attributes:
  _limit: The resource count limit.
  _count: The resource count.
c                     Xl         SU l        g Nr   )_limit_count)r   limits     r   r   Limiter.__init__   s    KDKr   c                     [         R                  " U5      (       a  gU =R                  S-  sl        U R                  U R                  :*  =(       d    S$ )zReturns True if the limit has not been reached yet, None otherwise.

Args:
  resource: The resource to limit.

Returns:
  True if the limit has not been reached yet, None otherwise to stop
  iterations.
T   N)r   r&   r^   r]   r   r(   s     r   r)   Limiter.Tap   s>     --h77KK1K;;$++%--r   )r^   r]   NrW   r@   r   r   rY   rY      s    .r   rY   c                   $    \ rS rSrSrS rS rSrg)Pager   zA Tapper class that injects a PageMarker after each page of resources.

Attributes:
  _page_size: The number of resources per page.
  _count: The current page resource count.
c                     Xl         SU l        g r\   )
_page_sizer^   )r   	page_sizes     r   r   Pager.__init__   s    ODKr   c                     [         R                  " U5      (       a  gU =R                  S-  sl        U R                  U R                  :  a0  SU l        [        R
                  " [         R                  " 5       5      $ g)zInjects a PageMarker if the current page limit has been reached.

Args:
  resource: The resource to limit.

Returns:
  TapInjector(PageMarker) if the page current page limit has been reached,
  otherwise True to retain the current resource.
Trb   r   )r   r&   r^   ri   r   rR   
PageMarkerrc   s     r   r)   	Pager.Tap   s[     --h77KK1K{{T__$dk&&'<'G'G'IJJr   )r^   ri   NrW   r@   r   r   rf   rf      s    r   rf   c                   *    \ rS rSrSrS rS rS rSrg)	UriCacher   zA Tapper class that caches URIs based on the cache update op.

Attributes:
  _transform_uri: The uri() transform function.
  _update_cache_op: The non-None return value from UpdateUriCache().
  _uris: The list of changed URIs, None if it is corrupt.
c                 *    X l         Xl        / U l        g r   _transform_uri_update_cache_op_uris)r   update_cache_optransform_uris      r   r   UriCacher.__init__   s    '+DJr   c                     [         R                  " U5      (       a  gU R                  b:  U R                  USS9nU(       a  U R                  R	                  U5        gSU l        g)zAppends the URI for resource to the list of cache changes.

Sets self._uris to None if a URI could not be retrieved for any resource.

Args:
  resource: The resource from which the URI is extracted.

Returns:
  True - all resources are seen downstream.
TN	undefined)r   r&   rv   rt   appendr   r(   uris      r   r)   UriCacher.Tap   s]     --h77zzD9c	

#  
r   c                 j    U R                   b&  U R                  R                  U R                   5        g g r   )rv   ru   Update)r   s    r   r7   UriCacher.Done  s)    zz
""4::. r   rs   Nr9   r@   r   r   rp   rp      s    
*/r   rp   c                   $    \ rS rSrSrS rS rSrg)UriReplaceri  z{A Tapper class that replaces each resource item with its URI.

Attributes:
  _transform_uri: The uri() transform function.
c                     Xl         g r   rt   )r   rx   s     r   r   UriReplacer.__init__  s    'r   c                     [         R                  " U5      (       a  gU R                  USS9nU(       d  g[        R                  " USS9$ )zReplaces resource with its URI or skips the resource if it has no URI.

Args:
  resource: The resource to replace with its URI.

Returns:
  TapInjector(URI, replace=True) if the resource has a URI or False to skip
  the resource.
TNr{   FrM   )r   r&   rt   r   rR   r~   s      r   r)   UriReplacer.Tap  sF     --h77


h$

7C$$S$77r   r   NrW   r@   r   r   r   r     s    (8r   r   N)r>   
__future__r   r   r   googlecloudsdk.corer   googlecloudsdk.core.resourcer   r   r	   r
   googlecloudsdk.core.utilr   r)   r   rB   rY   rf   rp   r   r@   r   r   <module>r      s    0 '  ' # 8 > ; : 21*}   1*h9!! 9x.m .8M >%/!! %/P8-## 8r   