
    B                        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rSSKJr	  SSK
Jr  SSKrS	rS
rSrS S jr\R"                  " \R$                  5       " S S\5      5       r " S S\\5      r " S S\5      r " S S\5      r " S S\\5      r " S S\5      rS rS rS r " S S\5      r\R"                  " \R$                  5       " S S\R>                  5      5       r g)!z*Base class for resource-specific printers.    )absolute_import)division)print_function)unicode_literalsN)console_attr)resource_printer_base    z{indent: <%d}{line}
c                 <    [         U-  nUR                  [        U S9$ )z>Returns a formatted, indented line containing the given value.)indentline)_LINE_FORMATformat_INDENT_STRING)valueindent_lengthline_formats      7lib/googlecloudsdk/core/resource/custom_printer_base.py_GenerateLineValuer   )   s"    },+			>		>>    c                       \ rS rSrSr\R                  SS j5       r\R                  S 5       r\R                  S 5       r	Sr
g)	_Marker/   z@Base class for a marker indicating how to format printer output.Nc                     g)a  Calculates the minimum width of any table columns in the record.

Returns a ColumnWidths object that contains the minimum width of each column
in any Table markers contained in this record, including Table markers
nested within other Table markers.

Args:
  max_column_width: The maximum column width to allow.
  indent_length: The number of spaces of indentation that begin this
    record's lines.

Returns:
  A ColumnWidths object with the computed columns for this record. Will be
  empty if this record does not contain any tables.
N )selfmax_column_widthr   s      r   CalculateColumnWidths_Marker.CalculateColumnWidths3       r   c                     g)a  Prints this record to the given output.

Prints this record to the given output using this record's print format.

Args:
  output: An object with a `write` method that takes a string argument. This
    method calls output.write with one string as an argument for each line
    in this record's print format.
  indent_length: The number of spaces of indentation to print at the
    beginning of each line.
  column_widths: A ColumnWidths object containing the minimum width of each
    column in any tables contained in this record.
Nr   r   outputr   column_widthss       r   Print_Marker.PrintE   r    r   c                     g)zaReturns true if this record will print non-empty output.

Blank lines count as non-empty output.
Nr   r   s    r   WillPrintOutput_Marker.WillPrintOutputU   r    r   r   Nr   )__name__
__module____qualname____firstlineno____doc__abcabstractmethodr   r%   r)   __static_attributes__r   r   r   r   r   /   sQ    H "    r   r   c                   d   ^  \ rS rSrSrSrSrSrSU 4S jjrSS jr	S r
S	 rS
 rS rS rSrU =r$ )Table]   zMarker class for a table. Fr
   c                 8   > [         [        U ]  U5        X l        g N)superr5   __init___console_attr)r   contentr   	__class__s      r   r;   Table.__init__f   s    	%(%r   c           
          [        US9nU  H4  nUR                  [        X@R                  U R                  UU5      5      nM6     U$ See _Marker base class.r   )ColumnWidthsMerge	separator
skip_empty)r   r   r   widthsrows        r   r   Table.CalculateColumnWidthsj   sF    +;<F||
sNNDOO=M$&'f  Mr   c                     [        US5      =(       d    [        US5      nU(       + =(       d    U R                  =(       a    U$ )z4Returns true if the given row should not be printed.r   )_FollowedByEmpty_FollowedByMarkerWithNoOutputrG   )r   rI   followed_by_emptys      r   _ShouldSkipPrintingRowTable._ShouldSkipPrintingRows   s7     	a I$A#q$I 7=t<+<=r   c                 P   UR                   U   nUS:X  a  XS-  nU[        U5      S-
  :  a  U R                  OSnU R                  nU R                  c  [        R
                  " 5       nXWR                  [        X!   5      5      -
  [        U5      -
  n[        X!   5      U-   US-  -   $ )z5Generates the string value for one column in one row.r      r
   r7   )rH   lenrF   r<   caConsoleAttrDisplayWidthstr)	r   indexrI   r   r$   widthrF   r   	n_paddings	            r   _GenerateColumnValueTable._GenerateColumnValuey   s      'Eze"'#c(Q,"6BI%%L!^^%l 	))#cj/::S^K sz?Y&)c/::r   c                     UR                  [        U R                  R                  U5      R	                  5       U5      5        g)zDWrites generated column values to output with the given indentation.N)writer   _COLUMN_SEPARATORjoinrstrip)r   r#   r   column_valuess       r   _WriteColumnsTable._WriteColumns   s7    
LL""''6==?	PQr   c           
          U  GH  nU R                  U5      (       a  M  / n[        [        U5      S-
  5       HF  n[        XF   [        5      (       a  [        S5      eUR                  U R                  XdX#5      5        MH     [        US   [        5      (       a0  U R                  XU5        US   R                  X[        -   U5        M  UR                  U R                  [        U5      S-
  XBU5      5        U R                  XU5        GM
     g)rB   rR   z#Markers must be in the last column.N)rO   rangerS   
isinstancer   	TypeErrorappendr[   rc   r%   INDENT_STEP)r   r#   r   r$   rI   rb   is          r   r%   Table.Print   s    		$	$S	)	)mSX\"!cfg&&?@
@%%amK	M #
 
CGW	%	%6-@Bfk9=I%%C1c-A	B 	6-@! r   c                 .   ^  [        U 4S jT  5       5      $ )rB   c              3   P   >#    U  H  nTR                  U5      (       + v   M     g 7fr9   )rO   ).0rI   r   s     r   	<genexpr>(Table.WillPrintOutput.<locals>.<genexpr>   s"     Dt4..s333ts   #&anyr(   s   `r   r)   Table.WillPrintOutput   s    DtDDDr   )r<   r9   r+   )r,   r-   r.   r/   r0   r_   rG   rF   r;   r   rO   r[   rc   r%   r)   r3   __classcell__r>   s   @r   r5   r5   ]   sD    ! *)&>;$QA(E Er   r5   c                        \ rS rSrSrSrSrSrg)Labeled   z3Marker class for a list of "Label: value" 2-tuples.T:r   N)r,   r-   r.   r/   r0   rG   rF   r3   r   r   r   ry   ry      s    ;*)r   ry   c                       \ rS rSrSrSrg)Mapped   z.Marker class for a list of key-value 2-tuples.r   N)r,   r-   r.   r/   r0   r3   r   r   r   r}   r}      s    6r   r}   c                   .    \ rS rSrSrSS jrS rS rSrg)	Lines   z!Marker class for a list of lines.Nc                     [        US9nU  H:  n[        U[        5      (       d  M  UR                  UR	                  X5      5      nM<     U$ rA   )rD   rh   r   rE   r   )r   r   r   rH   r   s        r   r   Lines.CalculateColumnWidths   sI    +;<F	D'	"	"&&'7GI  Mr   c                     U  HO  n[        U[        5      (       a  UR                  XU5        M,  U(       d  M5  UR                  [	        XB5      5        MQ     g)rB   N)rh   r   r%   r^   r   )r   r#   r   r$   r   s        r   r%   Lines.Print   s>    	D'	"	"

6-84'<=	 r   c                 r    U  H1  n[        U[        5      (       d    gUR                  5       (       d  M1    g   g)rB   TF)rh   r   r)   )r   r   s     r   r)   Lines.WillPrintOutput   s5    g&& 					  r   r   r+   )	r,   r-   r.   r/   r0   r   r%   r)   r3   r   r   r   r   r      s    )>	r   r   c                   8    \ rS rSrSrS	S jrS
S jrS rS rSr	g)Section   a  Marker class for a section.

A section is a list of lines. Section differs from Line in that Section
introduces an alignment break into the layout and allows overriding the global
maximum column width within the section. An alignment break causes all columns
in Table markers within a Section to be aligned but columns in Table markers
outside of a specific Section marker are not aligned with the columns inside
the Section.
Nc                 >    [        U5      U l        X l        SU l        g)zInitializes a section.

Args:
  lines: A list of lines to include in the section.
  max_column_width: An optional maximum column width to use for this
    section. Overrides the global maximum column width if specified.
N)r   _lines_max_column_width_column_widths)r   linesr   s      r   r;   Section.__init__   s     ,DK-Dr   c                     U R                   =(       d    UnU R                  R                  X25      U l        [	        5       $ )a  See _Marker base class.

Args:
  max_column_width: The maximum column width to allow. Overriden by the
    instance's max_column_width, if the instance has a max_column_width
    specified.
  indent_length: See _Marker base class.

Returns:
  An empty ColumnWidths object.
)r   r   r   r   rD   )r   r   r   effective_max_column_widths       r   r   Section.CalculateColumnWidths   s8     "&!7!7!K;K++;;"3D>r   c                     AU R                   (       d  U R                  US9  U R                  R                  XU R                   5        g)a  See _Marker base class.

Args:
  output: See _Marker base class.
  indent_length: See _Marker base class.
  column_widths: Ignored by Section. Section computes its own column widths
    to align columns within the section independently from columns outside
    the section.
)r   N)r   r   r   r%   r"   s       r   r%   Section.Print   s;     	
  } =KKfT-@-@Ar   c                 6    U R                   R                  5       $ )rB   )r   r)   r(   s    r   r)   Section.WillPrintOutput  s    ;;&&((r   )r   r   r   r9   r+   )
r,   r-   r.   r/   r0   r;   r   r%   r)   r3   r   r   r   r   r      s    
"B)r   r   c                 ,    [        XS-   S 5      (       + $ )z<Returns true if all columns after the given index are empty.rR   Nrs   )rI   rX   s     r   rL   rL     s    QYZ!	!!r   c                     US-   n[        U 5      U:  =(       a0    [        X   [        5      =(       a    X   R                  5       (       + $ )zHReturns true if the column after the given index is a no-output _Marker.rR   )rS   rh   r   r)   )rI   rX   
next_indexs      r   rM   rM     sB    qy*
c(Z
 0Js$H 0o--/
/1r   c                 x    X:H  =(       d0    U=(       a    [        X5      =(       d    [        XS-      [        5      $ )zFReturns true if column_index is considered the last column in the row.rR   )rL   rh   r   )rI   column_index
last_indexrG   s       r   _IsLastColumnInRowr     s9     
$ 5=*3=5
S)*G
46r   c                   \    \ rS rSrSr      SS jr\S 5       rS rS r	S r
S	 rS
 rSrg)rD   i*  a  Computes and stores column widths for a table and any nested tables.

A nested table is a table defined in the last column of a row in another
table. ColumnWidths includes any nested tables when computing column widths
so that the width of each column will be based on the contents of that column
in the parent table and all nested tables.

Attributes:
  widths: A list containing the computed minimum width of each column in the
    table and any nested tables.
Nc                     / U l         X@l        [        U5      U l        X0l        XPl        X`l        U(       a-  [        [        U5      5       H  nU R                  Xq5        M     gg)a  Computes the width of each column in row and in any nested tables.

Args:
  row: An optional list containing the columns in a table row. Any marker
    classes nested within the row must be in the last column of the row.
  separator: An optional separator string to place between columns.
  skip_empty: A boolean indicating whether columns followed only by empty
    columns should be skipped.
  max_column_width: An optional maximum column width.
  indent_length: The number of indent spaces that precede `row`. Added to
    the width of the first column in `row`.
  console_attr: The console attribute for width calculation

Returns:
  A ColumnWidths object containing the computed column widths.
N)	_widthsr   rS   _separator_width_skip_empty_indent_lengthr<   rg   _ProcessColumn)r   rI   rF   rG   r   r   r   rl   s           r   r;   ColumnWidths.__init__7  sW    . DL-	ND!'%
SX!A#  r   c                     U R                   $ )z3A list containing the minimum width of each column.)r   r(   s    r   rH   ColumnWidths.widthsX  s     <<r   c                 8    SR                  U R                  5      $ )z9Returns a string representation of a ColumnWidths object.z<widths: {}>)r   rH   r(   s    r   __repr__ColumnWidths.__repr__]  s      --r   c                     U[        U R                  5      :X  a  U R                  R                  S5        [        U R                  U   U5      nU R                  b  [        U R                  U5      nX0R                  U'   g)zAdjusts widths to account for the length of new column content.

Args:
  column_index: The column index to potentially update. Must be between 0
    and len(widths).
  content_length: The column content's length to consider when updating
    widths.
r   N)rS   r   rj   maxr   min)r   r   content_length	new_widths       r   	_SetWidthColumnWidths._SetWidtha  sf     s4<<((
ll!DLL.?I)d,,i8i!*LLr   c                 <   X!   n[        U5      S-
  n[        U[        5      (       aM  X:X  a=  U R                  UR	                  U R
                  U R                  [        -   5      5        g[        S5      e[        X!X@R                  5      (       a  U R                  US5        gU R                  nU R                  c  [        R                  " 5       nUR                  [!        U5      5      U R"                  -   nUS:X  a  X`R                  -  nU R                  X5        g)z=Processes a single column value when computing column widths.rR   Nz,Markers can only be used in the last column.r   )rS   rh   r   _MergeColumnWidthsr   r   r   rk   ri   r   r   r   r<   rT   rU   rV   rW   r   )r   rX   rI   recordr   r   rY   s          r   r   ColumnWidths._ProcessColumnu  s    ZFSAJ&'""		(()?)?)-)<)<{)JL	M 	FGG#j2B2BCC
nnUA''l				#~~'''F4t7L7LLe	!$$$
nnU"r   c                 b    [        UR                  5       H  u  p#U R                  X#5        M     g)z/Merges another ColumnWidths into this instance.N)	enumeraterH   r   )r   otherrl   rY   s       r   r   ColumnWidths._MergeColumnWidths  s#    ell+
nnQ ,r   c                    [        U[        5      (       d  [        S5      eU R                  b  UR                  c  SnO [	        U R                  UR                  5      n[        US9nUR                  U 5        UR                  U5        U$ )a0  Merges this object and another ColumnWidths into a new ColumnWidths.

Combines the computed column widths for self and other into a new
ColumnWidths. Uses the larger maximum column width between the two
ColumnWidths objects for the merged ColumnWidths. If one or both
ColumnWidths objects have unlimited max column width (max_column_width is
None), sets the merged ColumnWidths max column width to unlimited (None).

Args:
  other: A ColumnWidths object to merge with this instance.

Returns:
  A new ColumnWidths object containing the combined column widths.
z$other must be a ColumnWidths object.NrC   )rh   rD   ri   r   r   r   )r   r   merged_max_column_widthmergeds       r   rE   ColumnWidths.Merge  s     e\**<==%)@)@)H $ #D$:$:$)$;$;!=+BCF
d#
e$Mr   )r<   r   r   r   r   r   )Nr
   FNr   N)r,   r-   r.   r/   r0   r;   propertyrH   r   r   r   r   rE   r3   r   r   r   rD   rD   *  sN    
  $ $B  .+(#0
r   rD   c                   ^   ^  \ rS rSrSrSrU 4S jrSS jr\R                  S 5       r
SrU =r$ )	CustomPrinterBasei  a[  Base to extend to custom-format a resource.

Instead of using a format string, uses the "Transform" method to build a
structure of marker classes that represent how to print out the resource
in a structured way, and then prints it out in that way.

A string prints out as a string; the marker classes above print out as an
indented aligned table.
   c                 L   > U R                   US'   [        [        U ]  " U0 UD6  g )Nprocess_record)	Transformr:   r   r;   )r   argskwargsr>   s      r   r;   CustomPrinterBase.__init__  s'    #~~F	
T+T<V<r   c                 <   [        U[        5      (       a9  UR                  U R                  5      nUR	                  U R
                  SU5        O+U(       a$  U R
                  R                  [        U5      5        U(       a  U R
                  R                  S5        g g )Nr   z------
)rh   r   r   MAX_COLUMN_WIDTHr%   _outr^   r   )r   r   delimitr$   s       r   
_AddRecordCustomPrinterBase._AddRecord  sj    &'""2243H3HImll499a/	
iioo(01
iiooj! r   c                     g)a&  Override to describe the format of the record.

Takes in the raw record, returns a structure of "marker classes" (above in
this file) that will describe how to print it.

Args:
  record: The record to transform
Returns:
  A structure of "marker classes" that describes how to print the record.
Nr   )r   r   s     r   r   CustomPrinterBase.Transform  r    r   r   )T)r,   r-   r.   r/   r0   r   r;   r   r1   r2   r   r3   rv   rw   s   @r   r   r     s4     =" 
 
r   r   )r   )!r0   
__future__r   r   r   r   r1   googlecloudsdk.core.consoler   rT   googlecloudsdk.core.resourcer   sixrk   r   r   r   add_metaclassABCMetaobjectr   listr5   ry   r}   r   r   rL   rM   r   rD   ResourcePrinterr   r   r   r   <module>r      s    1 &  % ' 
 : > 
   '? 3;;*f *  *ZJED' JEZe 7U 7D' @9)g 9)x"
1
6B6 BJ 3;;%-== %  %r   