
    B                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlmZ	 ddl
mZ ddlZd	Zd
ZdZd dZ ej"                  ej$                         G d de             Z G d dee      Z G d de      Z G d de      Z G d dee      Z G d de      Zd Zd Zd Z G d de      Z ej"                  ej$                         G d dej>                               Z y)!z*Base class for resource-specific printers.    )absolute_import)division)print_function)unicode_literalsN)console_attr)resource_printer_base    z{indent: <%d}{line}
c                 B    t         |z  }|j                  t        |       S )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                   ~    e Zd ZdZej
                  dd       Zej
                  d        Zej
                  d        Zy)_Markerz@Base class for a marker indicating how to format printer output.Nc                      y)aE  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   CalculateColumnWidthsz_Marker.CalculateColumnWidths3       r   c                      y)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   Printz_Marker.PrintE   r   r   c                      y)ziReturns true if this record will print non-empty output.

    Blank lines count as non-empty output.
    Nr   r   s    r   WillPrintOutputz_Marker.WillPrintOutputU   r   r   Nr   )	__name__
__module____qualname____doc__abcabstractmethodr   r#   r&   r   r   r   r   r   /   sQ    H "    r   r   c                   V     e Zd ZdZdZdZdZd fd	ZddZd Z	d Z
d	 Zd
 Zd Z xZS )TablezMarker class for a table. Fr
   c                 :    t         t        |   |       || _        y N)superr/   __init___console_attr)r   contentr   	__class__s      r   r4   zTable.__init__f   s    	%(%Dr   c           
          t        |      }| D ]4  }|j                  t        || j                  | j                  ||            }6 |S See _Marker base class.r   )ColumnWidthsMerge	separator
skip_empty)r   r   r   widthsrows        r   r   zTable.CalculateColumnWidthsj   sH    +;<F||
sDNNDOO=M$&'f  Mr   c                 `    t        |d      xs t        |d      }| xs | j                  xr |S )z4Returns true if the given row should not be printed.r   )_FollowedByEmpty_FollowedByMarkerWithNoOutputr?   )r   rA   followed_by_emptys      r   _ShouldSkipPrintingRowzTable._ShouldSkipPrintingRows   s7     	a I$A#q$I 7=t<+<=r   c                 P   |j                   |   }|dk(  r||z  }|t        |      dz
  k  r| j                  nd}| j                  }| j                  t	        j
                         }||j                  t        ||               z
  t        |      z
  }t        ||         |z   |dz  z   S )z5Generates the string value for one column in one row.r      r
   r0   )r@   lenr>   r5   caConsoleAttrDisplayWidthstr)	r   indexrA   r   r"   widthr>   r   	n_paddings	            r   _GenerateColumnValuezTable._GenerateColumnValuey   s      'Ez}e"'#c(Q,"6BI%%L!^^%l 	))#c%j/::S^K s5z?Y&)c/::r   c                     |j                  t        | j                  j                  |      j	                         |             y)zDWrites generated column values to output with the given indentation.N)writer   _COLUMN_SEPARATORjoinrstrip)r   r!   r   column_valuess       r   _WriteColumnszTable._WriteColumns   s7    
LL""''6==?	PQr   c           
          | D ]  }| j                  |      rg }t        t        |      dz
        D ]C  }t        ||   t              rt        d      |j                  | j                  ||||             E t        |d   t              r1| j                  |||       |d   j                  ||t        z   |       |j                  | j                  t        |      dz
  |||             | j                  |||        y)r:   rH   z#Markers must be in the last column.N)rF   rangerI   
isinstancer   	TypeErrorappendrQ   rX   r#   INDENT_STEP)r   r!   r   r"   rA   rW   is          r   r#   zTable.Print   s    		$	$S	)mSX\"!c!fg&?@
@%%am]K	M #
 
CGW	%6=-@Bfmk9=I%%C1c=-A	B 	6=-@! r   c                 ,     t         fd D              S )r:   c              3   B   K   | ]  }j                  |         y wr2   )rF   ).0rA   r   s     r   	<genexpr>z(Table.WillPrintOutput.<locals>.<genexpr>   s      Dt4..s33ts   anyr%   s   `r   r&   zTable.WillPrintOutput   s    DtDDDr   r2   r'   )r(   r)   r*   r+   rT   r?   r>   r4   r   rF   rQ   rX   r#   r&   __classcell__r7   s   @r   r/   r/   ]   s>    ! *)&>;$QA(Er   r/   c                       e Zd ZdZdZdZy)Labeledz3Marker class for a list of "Label: value" 2-tuples.T:N)r(   r)   r*   r+   r?   r>   r   r   r   rj   rj      s    ;*)r   rj   c                       e Zd ZdZy)Mappedz.Marker class for a list of key-value 2-tuples.N)r(   r)   r*   r+   r   r   r   rm   rm      s    6r   rm   c                   $    e Zd ZdZddZd Zd Zy)Linesz!Marker class for a list of lines.Nc                     t        |      }| D ]4  }t        |t              s|j                  |j	                  ||            }6 |S r9   )r<   r\   r   r=   r   )r   r   r   r@   r   s        r   r   zLines.CalculateColumnWidths   sH    +;<F	D'	"&&'7GI  Mr   c                     | D ]D  }t        |t              r|j                  |||       '|s*|j                  t	        ||             F y)r:   N)r\   r   r#   rS   r   )r   r!   r   r"   r   s        r   r#   zLines.Print   s=    	D'	"

6=-8'm<=	 r   c                 Z    | D ]&  }t        |t              s y|j                         s& y y)r:   TF)r\   r   r&   )r   r   s     r   r&   zLines.WillPrintOutput   s/    g& 				  r   r'   )r(   r)   r*   r+   r   r#   r&   r   r   r   ro   ro      s    )>	r   ro   c                   ,    e Zd ZdZddZddZd Zd Zy)	Sectiona  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                 @    t        |      | _        || _        d| _        y)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)ro   _lines_max_column_width_column_widths)r   linesr   s      r   r4   zSection.__init__   s     ,DK-DDr   c                 x    | j                   xs |}| j                  j                  ||      | _        t	               S )a1  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.
    )rw   rv   r   rx   r<   )r   r   r   effective_max_column_widths       r   r   zSection.CalculateColumnWidths   s:     "&!7!7!K;K++;;"M3D>r   c                     ~| j                   s| j                  |       | j                  j                  ||| j                          y)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)rx   r   rv   r#   r    s       r   r#   zSection.Print   s=     	
  } =KKfmT-@-@Ar   c                 6    | j                   j                         S )r:   )rv   r&   r%   s    r   r&   zSection.WillPrintOutput  s    ;;&&((r   r2   r'   )r(   r)   r*   r+   r4   r   r#   r&   r   r   r   rt   rt      s    
"B)r   rt   c                 &    t        | |dz   d        S )z<Returns true if all columns after the given index are empty.rH   Nre   )rA   rN   s     r   rC   rC     s    UQYZ!	!!r   c                 ~    |dz   }t        |       |kD  xr) t        | |   t              xr | |   j                          S )zHReturns true if the column after the given index is a no-output _Marker.rH   )rI   r\   r   r&   )rA   rN   
next_indexs      r   rD   rD     sF    qy*
c(Z
 0Js:$H 0*o--/
/1r   c                 `    ||k(  xs( |xr t        | |      xs t        | |dz      t              S )zFReturns true if column_index is considered the last column in the row.rH   )rC   r\   r   )rA   column_index
last_indexr?   s       r   _IsLastColumnInRowr     s?     *
$ 5=*3=5
S)*G
46r   c                   R    e Zd ZdZ	 	 	 	 	 	 d
dZed        Zd Zd Zd Z	d Z
d	 Zy)r<   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                     g | _         || _        t        |      | _        || _        || _        || _        |r,t        t        |            D ]  }| j                  ||        yy)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)	_widthsrw   rI   _separator_width_skip_empty_indent_lengthr5   r[   _ProcessColumn)r   rA   r>   r?   r   r   r   r`   s           r   r4   zColumnWidths.__init__7  sa    . DL-D	ND!D'D%D
SX!As#  r   c                     | j                   S )z3A list containing the minimum width of each column.)r   r%   s    r   r@   zColumnWidths.widthsX  s     <<r   c                 8    dj                  | j                        S )z9Returns a string representation of a ColumnWidths object.z<widths: {}>)r   r@   r%   s    r   __repr__zColumnWidths.__repr__]  s      --r   c                     |t        | j                        k(  r| j                  j                  d       t        | j                  |   |      }| j                  t        | j                  |      }|| j                  |<   y)a  Adjusts 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)rI   r   r^   maxrw   min)r   r   content_length	new_widths       r   	_SetWidthzColumnWidths._SetWidtha  sh     s4<<((
ll!DLL.?I)d,,i8i!*DLLr   c                 ,   ||   }t        |      dz
  }t        |t              rM||k(  r=| j                  |j	                  | j
                  | j                  t        z                yt        d      t        |||| j                        r| j                  |d       y| j                  }| j                  t        j                         }|j                  t!        |            | j"                  z   }|dk(  r|| j                  z  }| j                  ||       y)z=Processes a single column value when computing column widths.rH   Nz,Markers can only be used in the last column.r   )rI   r\   r   _MergeColumnWidthsr   rw   r   r_   r]   r   r   r   r5   rJ   rK   rL   rM   r   )r   rN   rA   recordr   r   rO   s          r   r   zColumnWidths._ProcessColumnu  s    ZFSAJ&'"	*	(()?)?)-)<)<{)JL	M 	FGG#uj$2B2BC
nnUA''l				#~~'''F4t7L7LLe	!$$$
nnUE"r   c                 b    t        |j                        D ]  \  }}| j                  ||        y)z/Merges another ColumnWidths into this instance.N)	enumerater@   r   )r   otherr`   rO   s       r   r   zColumnWidths._MergeColumnWidths  s'    ell+5
nnQ ,r   c                    t        |t              st        d      | j                  |j                  d}n t	        | j                  |j                        }t        |      }|j                  |        |j                  |       |S )aX  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.Nr;   )r\   r<   r]   rw   r   r   )r   r   merged_max_column_widthmergeds       r   r=   zColumnWidths.Merge  s     e\*<==%)@)@)H $ #D$:$:$)$;$;!=+BCF
d#
e$Mr   )Nr
   FNr   N)r(   r)   r*   r+   r4   propertyr@   r   r   r   r   r=   r   r   r   r<   r<   *  sN    
  $ $B  .+(#0
r   r<   c                   R     e Zd ZdZdZ fdZddZej                  d        Z	 xZ
S )CustomPrinterBaseag  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                 J    | j                   |d<   t        t        |   |i | y )Nprocess_record)	Transformr3   r   r4   )r   argskwargsr7   s      r   r4   zCustomPrinterBase.__init__  s'    #~~F	
T+T<V<r   c                    t        |t              r9|j                  | j                        }|j	                  | j
                  d|       n&|r$| j
                  j                  t        |             |r| j
                  j                  d       y y )Nr   z------
)r\   r   r   MAX_COLUMN_WIDTHr#   _outrS   r   )r   r   delimitr"   s       r   
_AddRecordzCustomPrinterBase._AddRecord  sg    &'"2243H3HImll499a/	
iioo(01
iiooj! r   c                      y)aB  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   zCustomPrinterBase.Transform  r   r   )T)r(   r)   r*   r+   r   r4   r   r,   r-   r   rg   rh   s   @r   r   r     s4     =" 
 
r   r   )r   )!r+   
__future__r   r   r   r   r,   googlecloudsdk.core.consoler   rJ   googlecloudsdk.core.resourcer   sixr_   r   r   r   add_metaclassABCMetaobjectr   listr/   rj   rm   ro   rt   rC   rD   r   r<   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   