
    SF                     <   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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r " S S\R0                  5      r " S S\5      r " S S\5      r " S S\5      rg)aH  Resource display for all calliope commands.

The print_format string passed to resource_printer.Print() is determined in this
order:
 (1) Display disabled and resources not consumed if user output is disabled.
 (2) The explicit --format flag format string.
 (3) The explicit Display() method.
 (4) The DisplayInfo format from the parser.
 (5) Otherwise no output but the resources are consumed.

Format expressions are left-to-right composable. Each format expression is a
string tuple

  < NAME [ATTRIBUTE...] (PROJECTION...) >

where only one of the three elements need be present.
    )absolute_import)division)unicode_literals)display_taps)
exceptions)log)module_util)
properties)cache_update_ops)resource_lex)resource_printer)resource_projection_spec)resource_property)resource_reference)resource_transform)peek_iterableNc                       \ rS rSrSrSrg)Error5   zBase exception for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       &lib/googlecloudsdk/calliope/display.pyr   r   5   s    'r   r   c                       \ rS rSrSrSrg)CommandNeedsAddCacheUpdater9   z(Command needs an AddCacheUpdater() call.r   Nr   r   r   r   r!   r!   9   s    0r   r!   c                       \ rS rSrSrSrg)"CommandShouldntHaveAddCacheUpdater=   z4Command has an AddCacheUpdater() call and shouldn't.r   Nr   r   r   r   r$   r$   =   s    <r   r$   c                       \ rS rSrSrSrSS j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Srg)	DisplayerA   a  Implements the resource display method.

Dispatches the global flags args by constructing a format string and letting
resource_printer.Print() do the heavy lifting.

Attributes:
  _args: The argparse.Namespace given to command.Run().
  _command: The Command object that generated the resources to display.
  _defaults: The resource format and filter default projection.
  _format: The printer format string.
  _info: The resource info or None if not registered.
  _printer: The printer object.
  _printer_is_initialized: True if self._printer has been initialized.
  _resources: The resources to display, returned by command.Run().
  _transform_uri: A transform function that returns the URI for a resource.
)asyncfilterlimitNc                    X l         SU l        Xl        SU l        SU l        SU l        SU l        SU l        SU l        SU l	        X0l
        U(       d  UR                  5       nU(       a}  UR                  U l        [        R                  " U R                  UR                  UR                   S9U l        UR"                  U l        UR$                  U l        UR(                  U l        U R                  R*                  R-                  S[.        R0                  5      U l        UU R                  R*                  [.        R4                  " S5      '   g)aK  Constructor.

Args:
  command: The Command object.
  args: The argparse.Namespace given to the command.Run().
  resources: The resources to display, returned by command.Run(). May be
    omitted if only GetFormat() will be called.
  display_info: The DisplayInfo object reaped from parser.AddDisplayInfo()
    in the command path.
NF)defaultssymbolsaliasesuriconditionals)_args_cache_updater_command	_defaults_default_format_used_format_filter_info_printer_printer_is_initialized
_resourcesGetDisplayInfocache_updaterr   ProjectionSpec
transformsr/   formatflatten_flattenr*   r.   getr   TransformUri_transform_uriGetTypeDataName)selfcommandargs	resourcesdisplay_infos        r   __init__Displayer.__init__V   s    JDMDN %DDLDLDJDM#(D O((*l(66d/>>>>))&&dn
 "((dl"**dm!((dl..0044!..D
 	 	NN**>:r   c                 j    US:X  a  [        U R                  SS5      $ [        U R                  US5      $ )zReturns the value of flag_name in args, None if it is unknown or unset.

Args:
  flag_name: The flag name string sans leading '--'.

Returns:
  The flag value or None if it is unknown or unset.
r)   async_N)getattrr2   )rH   	flag_names     r   _GetFlagDisplayer._GetFlag   s4     G TZZ4004::y$//r   c           
      
   SSK Jn  U R                  [        R                  :X  a  gU R                  (       d  [        U R                  UR                  UR                  UR                  UR                  45      (       d  gS[        R                  R                  R                  R                  5       ;   aO  [!        SR#                  SR%                  U R&                  R)                  5       R+                  5       5      5      5      eg[-        U R.                   Vs/ s H  o R1                  U5      PM     sn5      (       a  g[        U R                  UR                  UR                  45      (       a"  [        R2                  " U R                  5      nGO'[        U R                  UR                  5      (       a!  [        R4                  " U R                  5      nO[        U R                  UR                  5      (       a!  [        R6                  " U R                  5      nO[9        SR#                  [:        R<                  " U R                  5      [:        R<                  " U R&                  R)                  5       5      SR%                  U R&                  R)                  5       R+                  5       5      5      5      e[>        R@                  " X0RB                  5      n[D        RF                  " U RH                  U5      U l$        gs  snf )z@Taps a resource Uri cache updater into self.resources if needed.r   )baseNAddCacheUpdaterz8`{}` needs a parser.display_info.AddCacheUpdater() call. z.Cache updater [{}] not expected for [{}] `{}`.)%googlecloudsdk.callioperV   r3   r   NoCacheUpdater
isinstancer4   CreateCommandDeleteCommandListCommandRestoreCommandr
   VALUEScorelintGetr!   rA   joinr2   _GetCommandGetPathany_CORRUPT_FLAGSrS   AddToCacheOpDeleteFromCacheOpReplaceCacheOpr$   r	   GetModulePathr   	UriCacherrF   r   Tapperr<   )rH   rV   flagcache_update_optaps        r   _AddUriCacheTapDisplayer._AddUriCacheTap   s1    -.===
--    !!	  		j//4499==?	?)FMM//199;<
 	

 
D,?,?@,?DMM$,?@AA $--$"4"4d6I6I!JKK(55d6I6IJo	DMM4#5#5	6	6(::4;N;NOo	DMM4#3#3	4	4(778K8KLo.
:
A
A''(;(;<''

(>(>(@Ahhtzz--/779:  
 
 2E2E
FC#**4??C@DO) As   L c                 X   U R                  S5      (       d  g/ nU R                  R                   Hv  nUR                  S5      (       a  UR	                  S5      nSnOSnUR                  SS5      nUR                  [        R                  " U5      R                  5       U45        Mx     U$ )zReturns the list of --sort-by [(key, reverse)] tuples.

Returns:
  The list of --sort-by [(key, reverse)] tuples, None if --sort-by was not
  specified. The keys are ordered from highest to lowest precedence.
sort_byN~TFz[]z[0])
rS   r2   ru   
startswithlstripreplaceappendr   LexerKey)rH   keysnamereverses       r   _GetSortKeysDisplayer._GetSortKeys   s     ==##D

""			{{3\\$&d
kk<%%d+//17;< # Kr   c                 L   ^^ S m[        U R                  UU4S jUS9U l        g)z_AddSortByTap helper that sorts the resources by keys.

Args:
  keys: The ordered list of parsed resource keys from highest to lowest
    precedence.
  reverse: Sort by the keys in descending order if True, otherwise
    ascending.
c                     [         R                  " XS5      n SU:  d   eU$ ! [        [        4 a    [        R
                  " U5      s $ f = f)z>Returns the value for key in r that can be compared with None.N)r   rc   AssertionError	TypeErrorsix	text_type)rkeyvalues      r   _GetKey)Displayer._SortResources.<locals>._GetKey   sM    ##AD1e$e||i( $}}U##$s   	# &AAc                 @   > T Vs/ s H  nT" X5      PM     sn$ s  snf )Nr   )r   kr   r}   s     r   <lambda>*Displayer._SortResources.<locals>.<lambda>   s    d3dwq}d33s   )r   r   N)sortedr<   )rH   r}   r   r   s    ` @r   _SortResourcesDisplayer._SortResources   s#    	$ 3DOr   c                    [         R                  " U R                  5      (       d  gU R                  5       nU(       d  gSU R                  l        / n/ nSnU H<  u  pVU(       d  UnOXF:w  a  UR                  SX445        / nUnUR                  U5        M>     U(       a  UR                  SX445        U H  u  pvU R                  Xv5        M     g)z-Sorts the resources using the --sort-by keys.NFr   )	r   
IsListLiker<   r   r2   ru   insertrz   r   )rH   	sort_keysgroups
group_keysgroup_reverser   r   r}   s           r   _AddSortByTapDisplayer._AddSortByTap   s    ''88!!#IDJJ FJM!#a*45
 " mmA
23  
$(  r   c                     U R                  5       nU(       d  g[        R                  " XR                  5      n[        R
                  " U R                  U5      U l        g)z5Taps a resource filter into self.resources if needed.N)
_GetFilterr   Filtererr5   r   rn   r<   )rH   
expressionrq   s      r   _AddFilterTapDisplayer._AddFilterTap  s@    "J



NN
;C#**4??C@DOr   c                   ^  U 4S jnT R                  5       nU(       d  gU He  n/ nSn[        R                  " U5      R                  5        H%  nUc  SnU" U5        M  SnUR	                  U5        M'     U(       a  M]  U" U5        Mg     g)zCTaps one or more resource flatteners into self.resources if needed.c                 ~   > [         R                  " U 5      n[        R                  " TR                  U5      Tl        g)z&Helper to add one flattened slice tap.N)r   	Flattenerr   rn   r<   )r   rq   rH   s     r   _Slice(Displayer._AddFlattenTap.<locals>._Slice  s-    ""3'c &,,T__cBdor   NFT)_GetFlattenr   r{   r|   rz   )rH   r   r}   r   flattened_keyslicedr   s   `      r   _AddFlattenTapDisplayer._AddFlattenTap  s    C Dmf!!#&**,!9&

&


q
! - V} r   c                     U R                  S5      nUb  US:  a  g[        R                  " U5      n[        R                  " U R
                  U5      U l        g)z4Taps a resource limit into self.resources if needed.r+   Nr   )rS   r   Limiterr   rn   r<   )rH   r+   rq   s      r   _AddLimitTapDisplayer._AddLimitTap6  sF    MM'"E}	


u
%C#**4??C@DOr   c                     U R                  S5      nUb  US::  a  g[        R                  " U5      n[        R                  " U R
                  U5      U l        g)z4Taps a resource pager into self.resources if needed.	page_sizeNr   )rS   r   Pagerr   rn   r<   )rH   r   rq   s      r   _AddPageTapDisplayer._AddPageTap>  sG    k*IIN


Y
'C#**4??C@DOr   c                     U R                  S5      SLa  g[        R                  " U R                  5      n[        R
                  " U R                  U5      U l        g)z;Taps a resource Uri replacer into self.resources if needed.r0   TN)rS   r   UriReplacerrF   r   rn   r<   )rH   rq   s     r   _AddUriReplaceTapDisplayer._AddUriReplaceTapF  sF    
 }}U4'

"
"4#6#6
7C#**4??C@DOr   c                 ^   U R                   (       d  U R                  $ U R                   R                  5       nU(       d'  U R                   R                  (       d  U R                  $ [        R
                  " [        R                  " U R                   R                  U R                  /5      US9$ )z6Returns the default symbols for --filter and --format.)r-   r.   )r9   r5   GetTransformsr-   r   r?   CombineDefaults)rH   r.   s     r   _GetResourceInfoDefaults"Displayer._GetResourceInfoDefaultsQ  sz    ::^^jj&&(G4::..^^#22)99ZZ  $..1
 	 r   c                 @    U R                   R                  =(       d    S$ )zrDetermines the explicit format.

Returns:
  format: The format string, '' if there is no explicit format, or None
 )r2   rA   rH   s    r   _GetExplicitFormatDisplayer._GetExplicitFormat_  s     ::""r   c                 R    [        U R                  S5      (       a  gU R                  $ )zlDetermines the default format.

Returns:
  format: The format string, '' if there is an explicit Display().
Displayr   )hasattrr4   r7   r   s    r   _GetDefaultFormatDisplayer._GetDefaultFormatg  s"     t}}i((<<r   c                     U R                  S5      nUcR  U R                  (       a5  [        R                  " S[        R
                  " U R                  5      5        U R                  $ U$ )Nr*   zDisplay filter: "%s")rS   r8   r   infor   r   )rH   flag_filters     r   r   Displayer._GetFilterq  sI    --)K	't||)DE\\r   c                 F    U R                  S5      nUc  U R                  $ U$ )NrB   )rS   rC   )rH   flag_flattens     r   r   Displayer._GetFlattenz  s&    ==+L]]r   c           	         U R                  5       nU R                  5       nU(       d  U R                  S5      SL a  gSU l        UnOU(       a  US-   U-   nU(       d  U$ U R	                  5       nU(       d  U$ / n[        USS9 HF  u  nu  pgU(       a  SOSnUR                  S	R                  [        R                  " U5      XXS
95        MH     USR                  SR                  U5      S9-  nU$ )zNDetermines the display format.

Returns:
  format: The display format string.
r0   Tzvalue(.)rX      )startz:reverser   z{name}:sort={order}{attr})r~   orderattrz:({orders}),)orders)r   r   rS   r6   r   	enumeraterz   rA   r   
GetKeyNamerd   )	rH   default_fmtfmtr   r   r   r   r   r   s	            r   	GetFormatDisplayer.GetFormat  s     ((*K

!
!
#C 
u		%"&dc	" ##cj!!#Ij F!*9A!>~"Zdmm
%
,
,**3/u -  "? =sxx'788CJr   c                    U R                   (       a  gSU l         U R                  5       U l        U R                  5       U l        U R                  (       ak  [
        R                  " U R                  U R                  [        R                  S9U l	        U R                  (       a  U R                  R                  U l        ggg)z2Initializes the printer and associated attributes.NT)r-   out)r;   r   r7   r   r5   r   Printerr   r   r:   column_attributesr   s    r   _InitPrinterDisplayer._InitPrinter  s     ###'D  >>#DL 224DN ||&..
,,SWWdm 
88 
	 r   c                     U R                  5         [        R                  " U R                  5       U R                  U R
                  S9$ )z7Returns the set of key names referenced by the command.)filter_stringprinterr-   )r   r   GetReferencedKeyNamesr   r:   r5   r   s    r   r   Displayer.GetReferencedKeyNames  s9    33oo' r   c                     U R                  5         U R                  5         U R                  5         U R                  5         U R	                  5         U R                  5         U R                  5         g)a  Adds each of the standard display taps, if needed.

The taps must be included in this order in order to generate the correct
results. For example, limiting should not happen until after filtering is
complete, and pagination should only happen on the fully trimmed results.
N)rr   r   r   r   r   r   r   r   s    r   _AddDisplayTapsDisplayer._AddDisplayTaps  sX     	r   c                    [         R                  " 5       (       d\  [         R                  " S5        [        R                  " U R
                  5      (       a  [        U R
                  5      $ U R
                  $ U R                  5         U R                  5         SnU R                  (       aa  [         R                  " SU R                  5        U R                  R                  U R
                  5        U R                  R                  5       nOa[        U R                  S5      (       aF  [         R                  " S5        U R                  R                  U R                   U R
                  5        [         R"                  R%                  5         U R                   R'                  S5      (       d  U R                  R)                  U5        U R
                  $ )zThe default display method.zDisplay disabled.TzDisplay format: "%s"r   zExplicit Display.rA   )r   IsUserOutputEnabledr   r   r   r<   listr   r   r:   r7   PrintResourcesWerePrintedr   r4   r   r2   r   flushIsSpecifiedEpilog)rH   resources_were_displayeds     r   r   Displayer.Display  s0    ""$$	hh"#
 
	%	%doo	6	6DOO$$__ 	#}}	hh%t||4
mm$//*!%!C!C!E			*	*	hh"#
mmDJJ8 GGMMO ::!!(++
mm34??r   )r2   r3   r4   r6   r5   r8   rC   r7   r9   r:   r;   r<   rF   )NN)r   r   r   r   r   rh   rM   rS   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r'   r'   A   s    $ 0.(T01Af,6)@A6AA	A#5n9*$r   r'   )r   
__future__r   r   r   rY   r   googlecloudsdk.corer   r   r	   r
   googlecloudsdk.core.cacher   googlecloudsdk.core.resourcer   r   r   r   r   r   googlecloudsdk.core.utilr   r   r   r!   r$   objectr'   r   r   r   <module>r      sw   $ '  ' 0 * # + * 6 5 9 A : ; ; 2 
(J (1% 1= =H Hr   