
    4d                        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dlZddlm	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ZdZdZd Zd Z G d de      Z G d de      Z G d dej6                        Zy)zTable format resource printer.    )absolute_import)division)unicode_literalsN)
properties)console_attr)resource_printer_base)resource_projection_spec)resource_transform)range      
   c                 X   | yt        | t        j                        r| S t        | t        j                        rt        j
                  |       S t        | t              rt        j                  |       S t        | d      rt        j                  |       S t        j                  | d      S )z7Represents value as a JSON string if it's not a string. __str__T)	sort_keys)
isinstancer   	Colorizersixstring_typesDecodefloatr
   TransformFloathasattr	text_typejsondumpsvalues    1lib/googlecloudsdk/core/resource/table_printer.py
_Stringifyr!   +   s    
]%//0L%))*u%%%,,U33ui ==::et,,    c                 \    t        | t        j                  t        f      r| S t        d      S )zFRepresents value as a number, or infinity if it is not a valid number.inf)r   r   integer_typesr   r   s    r    _Numifyr&   ;   s&    ))512L	ur"   c                   (    e Zd ZdZd Zd Zd Zd Zy)_Justifya  Represents a string object for justification using display width.

  Attributes:
    _adjust: The justification width adjustment. The builtin justification
      functions use len() which counts characters, but some character encodings
      require console_attr.DisplayWidth() which returns the encoded character
      display width.
    _string: The output encoded string to justify.
  c                     t        j                  ||j                         d      | _        |j	                  | j                        t        | j                        z
  | _        y )NF)encodingescape)r   SafeTextGetEncoding_stringDisplayWidthlen_adjust)selfattrstrings      r    __init__z_Justify.__init__M   sH    (())+E;DL$$T\\2S5FFDLr"   c                 R    | j                   j                  || j                  z
        S N)r.   ljustr1   r2   widths     r    r8   z_Justify.ljustR        <<edll233r"   c                 R    | j                   j                  || j                  z
        S r7   )r.   rjustr1   r9   s     r    r=   z_Justify.rjustU   r;   r"   c                 R    | j                   j                  || j                  z
        S r7   )r.   centerr1   r9   s     r    r?   z_Justify.centerX   s     <<ut||344r"   N)__name__
__module____qualname____doc__r5   r8   r=   r?    r"   r    r(   r(   B   s    G
445r"   r(   c                       e Zd ZdZd Zy)	SubFormataE  A sub format object.

  Attributes:
    index: The parent column index.
    hidden: Column is projected but not displayed.
    printer: The nested printer object.
    out: The nested printer output stream.
    rows: The nested format aggregate rows if the parent has no columns.
    wrap: If column text should be wrapped.
  c                 X    || _         || _        || _        || _        g | _        || _        y r7   )indexhiddenprinteroutrowswrap)r2   rH   rI   rJ   rK   rM   s         r    r5   zSubFormat.__init__h   s,    DJDKDLDHDIDIr"   N)r@   rA   rB   rC   r5   rD   r"   r    rF   rF   \   s    	r"   rF   c                   V     e Zd ZdZ fdZd
dZd Z	 ddZd Zd Z	 fdZ
d	 Z xZS )TablePrinteraQ  A printer for printing human-readable tables.

  Aligned left-adjusted columns with optional title, column headings and
  sorting. This format requires a projection to define the table columns. The
  default column headings are the disambiguated right hand components of the
  column keys in ANGRY_SNAKE_CASE. For example, the projection keys
  (first.name, last.name) produce the default column heading
  ('NAME', 'LAST_NAME').

  If *--page-size*=_N_ is specified then output is grouped into tables with
  at most _N_ rows. Headings, alignment and sorting are done per-page. The
  title, if any, is printed before the first table.

  If screen reader option is True, you may observe flattened list output instead
  of a table with columns. Please refer to $ gcloud topic accessibility to turn
  it off.

  Printer attributes:
    all-box: Prints a box around the entire table and each cell, including the
      title if any.
    box: Prints a box around the entire table and the title cells if any.
    format=_FORMAT-STRING_: Prints the key data indented by 4 spaces using
      _FORMAT-STRING_ which can reference any of the supported formats.
    no-heading: Disables the column headings.
    margin=N: Right hand side padding when one or more columns are wrapped.
    pad=N: Sets the column horizontal pad to _N_ spaces. The default is 1 for
      box, 2 otherwise.
    title=_TITLE_: Prints a centered _TITLE_ at the top of the table, within
      the table box if *box* is enabled.

  Attributes:
    _optional: True if at least one column is optional. An optional column is
      not displayed if it contains no data.
    _page_count: The output page count, incremented before each page.
    _rows: The list of all resource columns indexed by row.
    _visible: Ordered list of visible column indexes.
    _wrap: True if at least one column can be text wrapped.
  c           
         g | _         t        t        |   |ddd| d}dD ]  }|| j                  v s|} n | j
                  st        j                  |      | _        | j
                  j                         | _	        d| _
        d| _        g | _        d| _        d}d| _        | j                  r| j                  j!                         D ]s  }|j"                  j$                  s|j"                  j&                  rd}nd| _        |j"                  j(                  rd| _        |j"                  j*                  smd| _        u t/        j0                  | j                  j2                        }d}| j                  j!                         D ]  }|j"                  j$                  ri| j                  r| j4                  nt7        j8                         }	d}
| j;                  |j"                  j$                  |	| j
                  |	      }d| _        nd}	d}|j"                  j*                  }
| j                  j=                  t?        ||j"                  j&                  ||	|
             |d
z  } d| _         |sd| _        d| _        y| j                  re| j                  sXg | _         | j                  D ]A  }|j&                  r|jB                  r| j@                  j=                  |jD                         C yyy)zCreates a new TablePrinter.T)
by_columnsnon_empty_projection_requiredN)asciizutf-8win)r*   r   F)symbols)rK   r   defaultsr   )#_rowssuperrO   r5   
attributes_console_attrr   GetConsoleAttrGetControlSequenceIndicator_csi_page_count	_optional_subformats_has_subprinters
_aggregatecolumn_attributesColumns	attribute	subformatrI   optionalrM   _wrapr	   ProjectionSpecrU   _outioStringIOPrinterappendrF   _visiblerJ   rH   )r2   argskwargsr*   namehas_subformatscolrV   rH   rK   rM   rJ   rf   	__class__s                r    r5   zTablePrinter.__init__   s   DJ	,&	$dNFLNH)		  * '66Id"">>@DID DND!DNDO''//1#==""cmm&:&:.!$/==!!$.==$* 2 *88((002he''//1#==""!__		"++-#$LLmm%%--	 ! !'
 #'$
#'##$eS]]117CF	H
# 2$ DMddo			$//dm'')	(9(9
--

y
/ ( #2	r"   c                 :    | j                   j                  |       y)zAdds a list of columns.

    Output delayed until Finish().

    Args:
      record: A JSON-serializable object.
      delimit: Prints resource delimiters if True.
    N)rW   rn   )r2   recorddelimits      r    
_AddRecordzTablePrinter._AddRecord   s     	JJfr"   c                 t    | j                   r|s|S g }| j                   D ]  }|j                  ||           |S )z%Return the visible list items in row.)ro   rn   )r2   rowvisiblerH   s       r    _VisiblezTablePrinter._Visible   s8    ==jGnnSZ  Nr"   c                    d}d}d}|t        |      k  r| j                  rJ||d j                  | j                        r,| j                  j	                  ||d       }||||z    }||z  }n&|t        j                  ||         z  }||kD  rn|dz  }|t        |      k  r|s.|t        ||d       t        ||d j                               z
  z  }t        j                  d|      }|r$|j                         |k  r|j                         }n]d}	t        j                  d|      D ]>  }
|
j                         |kD  r|r|
j                         |k  r|}	 n|
j                         }	@ |	r|	}|s|d| j                         }n|d| }||d }|r?|| j                  j                         k7  r"|| j                  j                         z  }||z   }||fS )z3Helper function to get next line of wrappable text.r   r   Nr   z\nz\s+)r0   r]   
startswithrZ   GetControlSequenceLenr   GetCharacterDisplayWidthlstripresearchendfinditerstartrstripGetFontCode)r2   s	max_widthinclude_all_whitespacecurrent_widthsplitprefix
seq_lengthfirst_newlinemax_whitespacer	next_lineremaining_values                r    _GetNextLineAndRemainderz%TablePrinter._GetNextLineAndRemainder   s    MEF
#a&.	qy++DII6''==aiH
5+,>>qxHH9$

 #a&. "s1UV9~AefI$4$4$6 777e IIeQ'M**,5!e n{{61%!557U?#	U(:"N
 & 
!FU)""$iFU)iiO&D..::<<4%%1133i0oo%%r"   c                     g }| j                   r9| j                   D ]*  }|j                  s|j                  |j                         , |S r7   )r`   rJ   rn   rH   )r2   subsrf   s      r    _GetSubformatIndexesz!TablePrinter._GetSubformatIndexes  s@    D'')
++ioo
& ( Kr"   c                     d| j                   vrM| j                  r| j                  S | j                  r)| j                  | j                  j	                               S y )N
no-heading)rY   _headingrc   r}   Labelsr2   s    r    _GetVisibleLabelszTablePrinter._GetVisibleLabels&  sI    4??*	}}!!}}T33::<==r"   c                 
*  => | j                   sy| j                  rd| _        | j                  D ]~  }| j                   D ]1  }||j                     }|s|j
                  j                  |d       3 |j
                  j                          |j
                  j                         sxd| _         yd| j                  v }|sd| j                  v r| j                  j                         }d}n"d}| j                  j                  dt              }| j                   }| j                  rg }g }	| j                  j                         D ]7  \  }
}|	s|}|k7  r|j!                  d	|	|f       g }	|}|	j#                  |
       9 |	r|j!                  d	|	f       |D ]  \  }	}|rt%        |      }t%        |	      D ]a  }t'        |      D cg c]#  \  }}t)        ||         t+        ||         ||f% }}}|j-                          |D cg c]	  \  }}}}| }}}c |st%        |      } | j                  j/                         }nd}t0        j2                  j4                  j6                  j9                         }|r;| j                  j                  d
      }|6| j:                  j=                  |       | j:                  j=                  d       | j>                  r{d}g }t'        | jA                  | j                  jC                                     D ]/  \  }}|jD                  jF                  s|j#                  |       .d}1 |r|sd| _        y|| _$        | jK                         }| jM                         }t'        |      D ]  \  }}|r| j:                  j=                  d       tO        tQ        |            D ]  }| jH                  || jH                  vr||v r#tS        jT                  t+        ||               }|rB|tQ        |      k  r4||   r/| j:                  j=                  djW                  ||   |             n| j:                  j=                  |       | j:                  j=                  d        | j                  s| j                  D ]  }|j
                  s|j
                  j                  ||j                            |jX                  j[                         }t'        |j]                  d      dd       D ]@  \  }}|s| j:                  j=                  d       | j:                  j=                  |dz          B |jX                  j_                  d	       |jX                  ja                  d	       | j:                  j=                  d         g | _         tb        td        |           y|D cg c]  }|D cg c]  }t+        |       c} }}}| jf                  sg | _         | jH                  r/|D cg c]  }| jA                  |       }}| jA                  |      }g }d| j                  vr| jh                  r| jh                  }n8| j                  r*| jA                  | j                  jk                               }nd}|r| j                  rdg } | j                  D ]O  }|j
                  r|j                  tQ        |      k  s)| j#                  t+        ||j                                  Q | g}n|D cg c]  }t+        |       c}g}d	gtm        d ||z   D              z  }!|D ]@  }t'        |      D ]0  \  }}tm        |!|   | j                  jo                  |            |!|<   2 B | j>                  rd}g }t'        | jA                  | j                  jC                                     D ]4  \  }}|jD                  jF                  r|!|   r|j#                  |       3d}6 |rj|sd| _        y|| _$        |D cg c]  }| jA                  |       }}| jA                  |      }|r| jA                  |d	         gng }| jA                  |!      }!|rAt'        |d	         D ]0  \  }}tm        |!|   | j                  jo                  |            |!|<   2 | j                  rxt'        | j                  jC                               D ]R  \  }}|jD                  jp                  s|!|   |jD                  jp                  k  s:|jD                  jp                  |!|<   T i }"t'        | jA                  | j                  jC                                     D ]c  \  }}|jD                  jr                  stu        |jD                  jr                  tv              r
tx        |"|<   K|jD                  jr                  |"|<   e |"r/tQ        | jA                  | j                  jC                                     }#|#dz
  |z  }$|rtz        |#dz   z  |#|z  dz  z   }$|$| j                  j                  dd	      z  }$| j                  j                  d| j                  j}                         d	         }%|%|$z
  }&|&t        |!      k  rut        t'        |!      D 'cg c]  \  }}'||"vr|' c}'}      }(|&|(z
  })t'        |!      D ]7  \  }}'||"v st        |"|   |!|         }*tm        |)tQ        |"      z  |*      |!|<   9 | j                  dk  r| j                  j                  d
      nd}||r|j                  }d	>d}+tO        tQ        |!            D ]&  }>|!|   z  >|r|j                  |!|   |+z   z  z  }d}+( >| j                  jo                  |      k  r|"s| j                  jo                  |      tQ        |!      z   dz
  tQ        |!      z  },>tQ        |!      |,z  z  >|r|j                  tQ        |!      z  |,z  z  }tO        tQ        |!            D ]  }|!|xx   |,z  cc<    |r>dtQ        |!      z  dz
  z  >|j                  z  }| j:                  j=                  |       | j:                  j=                  d       djW                  |j                  t        | j                  |      j                  >      |j                        }nG>|tQ        |!      dz
  z  z  >t        | j                  |      j                  >      j                         }| j:                  j=                  |       | j:                  j=                  d       |r|r|j                  n|j                  }-|j                  }.|j                  }/d}0d}1d}2tO        tQ        |!            D ]S  }|j                  |!|   dz   z  }|0|-|z   z  }0|j                  }-|1|.|z   z  }1|j                  }.|2|/|z   z  }2|j                  }/U |0|r|j                  n|j                  z  }0|1|j                  z  }1|2|j                  z  }2| j:                  j=                  |0       | j:                  j=                  d       |rg }|d	   }g }tO        tQ        |            D ]C  }|j#                  |j                         |j#                  ||   j                  |!|                E |j#                  |j                         | j:                  j=                  dj                  |             | j:                  j=                  d       | j:                  j=                  |1       | j:                  j=                  d       d}3|xrJ | j                  xr< t        | j                  D cg c]  }|j                   xr |j
                   c}      }4||z   D ]  }|3rd}3ns|rq|4r7| j:                  j=                  0       | j:                  j=                  d       n8|r6| j:                  j=                  1       | j:                  j=                  d       d}5|5rd	},d}5tO        tQ        |            D ]  }|!|   >|r(| j:                  j=                  |j                  dz          |r||   nd =||   }6tu        |6t        j                        }7| j                  jo                  |6      >kD  sdtS        jT                  |6      v r| j                  tS        jT                  |6      >|7      \  }8}9|7rZt        j                  |8|6j                  |6j                        }t        j                  |9|6j                  |6j                        ||<   n|8}|9||<   |9r
d}5n|6}d||<   |7ri|,r | j:                  j=                  d|,z         d	},|j                  | j:                  =>fd       |r | j:                  j=                  d|z         |}, =t        | j                  |      >      }:|r;| j:                  j=                  |:       | j:                  j=                  d|z         |:j                         r|,r | j:                  j=                  d|,z         d	},|:j                         };| j:                  j=                  |;       || j                  jo                  |:      z   | j                  jo                  |;      z
  },|,|| j                  jo                  |:      z   z  }, |r%| j:                  j=                  |j                         | j                   rH| j:                  j=                  d       |rg }|5r?|r6| j:                  j=                  2       | j:                  j=                  d       | j                   j                  d	      }<| j                  D ]  }|j
                  s|j
                  j                  |<|j                            |jX                  j[                         }|j]                  d      dd D ]#  }| j:                  j=                  d|z   dz          % |jX                  j_                  d	       |jX                  ja                  d	        n| j:                  j=                  d       |5s_ |r8|4s6| j:                  j=                  2       | j:                  j=                  d       tb        td        |           yc c}}w c c}}w c c}w c c}}w c c}w c c}w c c}w c c}'}w c c}w )zPrints the table.NT)intermediateFzall-boxboxr   padr   titlez


z{0}: {1}r   c              3   2   K   | ]  }t        |        y wr7   )r0   ).0xs     r    	<genexpr>z&TablePrinter.Finish.<locals>.<genexpr>  s     :>a3q6>s   r   marginr:      z	{0}{1}{2}r    c                 $    | j                  |      S r7   )r8   )r   ws     r    <lambda>z%TablePrinter.Finish.<locals>.<lambda>r  s    
r"   )r   c                      |       S r7   rD   )r   justifyr:   s    r    r   z%TablePrinter.Finish.<locals>.<lambda>  s    WQ5Fr"   )r   z    )[rW   rb   _emptyr`   rH   rJ   PrintFinishResourcesWerePrintedrY   rZ   GetBoxLineCharactersget_TABLE_COLUMN_PADrc   Orderinsertrn   reversed	enumerater&   r!   sort
Alignmentsr   VALUESaccessibilityscreen_readerGetBoolrj   writer_   r}   rd   re   rg   ro   r   r   r   r0   r   r   formatrK   getvaluer   truncateseekrX   rO   ra   r   r   maxr/   r:   rM   r   bool
_MIN_WIDTH_BOX_CHAR_LENGTHGetTermSizesumminr^   drhdlvr(   r?   r   vrurhdvhhuvluljoinanyrI   r   r   r   _color_justifyRenderstrippop)@r2   rf   r{   rw   all_boxr   table_column_padrL   groupskeys	key_indexkey_reversereversekeyi	decorated_alignr   r   rg   r|   rt   labelsr   jcontentnested_outputklinecellheadingcells
col_widthsrM   visible_colstable_paddingtable_widthtotal_col_width	col_widthnon_wrappable_widthavailable_width	min_widthsepr   t_sepm_sepb_sept_rulem_ruleb_rulefirsthas_visible_subformatsrow_finishedr   is_colorizer
cell_value	remainderr   strippedr   r   r:   ru   s@                                                                @@r    r   zTablePrinter.Finish/  s   :: dk'')::Cy'&##F#>  	  "113$+ (  4??*G%4??*335cc,,U4EF ::D
 fd$($:$:$@$@$B
 )['k!
--D'?
+$'I %C 
a$)!-$
 $$D>C%.t_6%461c  C):c#h+?CH%4  6
..
*34),!Q3#)$4	 "
 $$ " $$//1ee %%33AAIIKMoo!!'*e						 
MM$0088:;=FAs''NN1H= DK!$-%%'f&&(d dO&!S
))//$
s3xA]]&1DMM+A$YMM*SV"45'CKF1IIIOOJ--fQiABIIOOG$
))//$
 ! ++i  %%c)//&:;'mm446m&}':':4'@"'EF'!T))//$'		t, G
 mm$$Q'mm  #iiood# ,# $< djL$&( ;??$3#.#$Z#.$D?  dj }},01DSdmmC Dd1mmE"e G4??*	!!t55<<>?	%++i$$3v;)Fll:fY__&=>? , G'39:64j&6:;'s:4'>:::JcN&!SJqM4+=+=+J+J3+OP
1 #  ~~hgdmmD,B,B,J,J,LMN&!S}}%%A
..
(	 O
 
$+
.23dsc"d3e$184==,-b]]:.
gaj)&!SJqM4+=+=+J+J3+OP
1 *d44<<>?&!S==:a=3==3F3F#F----*Q- @ DDMM$*@*@*H*H*JKL3			cmm(($/$q'MM&&$q' M t'='='E'E'GHIl#a'+;;m	q 01++a/0 	 t**8Q77mOO''(,(:(:(F(F(H(KMk#m3o	3z?	*!,5j,A#
,A.1i} ,A#
  *,??%j1LAy$YDGZ]3I3t9 <iHJqM 2 -1,<,<,ADOO(tE	vvecS_%!A
#%%:a=3./
/$	 &
 
##007	7""//6ZH1L:Z3&&
#%%#j/)C/
/$s:'A
Q-3
- (	S_$q((				!!EET''/66u=suuF 	!S_q%899**E299%@GGI
iiood
iiood cff366effeffefffS_%!uu
1)*%$,%$,%$, & %SVV+fff
iioof
iiood	ajs3xA
++cee

++c!fmmJqM2
3 ! 	CEE		'						
 E  -T%5%5 -#**	,*Y 

49#4#4
4*	,;- ~	!
))//&
!
))//$

))//&
!
))//$
ls3xAQ-%IIOOCEECK( %E!H+B'!f!#A|'='=>,  --a058cmmA&&$($A$Aa % %B %N!J	!++J!**Md#--i1::Nc!f  d c!f"lDCFiioocCi(cKK		+FKGiiooc$445$cHT%7%7>FEiiooe$iiooc$445		c	*hiiooh'"T%7%7%D%DU%KK$$11(;<  %(:(:(G(G(NNNce !f 
))//#%%
 ::
))//$
Giioof%iiood#

q!A!--	""!!'')//(:; ) 6 6 8)//5cr:D))//&4-$"67 ; &&q)""1% . ))//$
[  r #					,$&m
6 5V /? 2( ;, 4F#
j	,sN   (AS
AS
<	AS%AS AS%AS+AS0AS5AS:
6"AT S AS%c                     | xj                   dz  c_         | j                          | j                  j                  d       g | _        y)z)Flushes the current resource page output.r   r   N)r^   r   rj   r   rW   r   s    r    PagezTablePrinter.Page  s3    KKMIIOODDJr"   )T)F)r@   rA   rB   rC   r5   ry   r}   r   r   r   r   r  __classcell__)ru   s   @r    rO   rO   q   s:    %N<0|	 7<0&dO'br"   rO   )rC   
__future__r   r   r   rk   r   r   googlecloudsdk.corer   googlecloudsdk.core.consoler   googlecloudsdk.core.resourcer   r	   r
   r   	six.movesr   r   r   r   r!   r&   objectr(   rF   ResourcePrinterrO   rD   r"   r    <module>r     s}    % &  ' 	  	 * 4 > A ; 
     
- 5v 54 *T	(88 T	r"   