o
    €Ï4d  ã                   @   sâ   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlZddlmZ dZdZdZdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZG dd„ dejƒZdS )zTable format resource printer.é    )Úabsolute_import)Údivision)Úunicode_literalsN)Ú
properties)Úconsole_attr)Úresource_printer_base)Úresource_projection_spec)Úresource_transform)Úrangeé   é   é
   c                 C   sh   | du rdS t | tjƒr| S t | tjƒrt | ¡S t | tƒr#t | ¡S t	| dƒr-t 
| ¡S tj| ddS )z7Represents value as a JSON string if it's not a string.NÚ Ú__str__T)Ú	sort_keys)Ú
isinstancer   Ú	ColorizerÚsixÚstring_typesÚDecodeÚfloatr	   ÚTransformFloatÚhasattrÚ	text_typeÚjsonÚdumps©Úvalue© r   úG/tmp/google-cloud-sdk/lib/googlecloudsdk/core/resource/table_printer.pyÚ
_Stringify+   s   




r    c                 C   s   t | tjtfƒr
| S tdƒS )zFRepresents value as a number, or infinity if it is not a valid number.Úinf)r   r   Úinteger_typesr   r   r   r   r   Ú_Numify;   s   r#   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú_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                 C   s2   t j|| ¡ dd| _| | j¡t| jƒ | _d S )NF)ÚencodingÚescape)r   ÚSafeTextÚGetEncodingÚ_stringÚDisplayWidthÚlenÚ_adjust)ÚselfÚattrÚstringr   r   r   Ú__init__M   s   
ÿz_Justify.__init__c                 C   ó   | j  || j ¡S ©N)r)   Úljustr,   ©r-   Úwidthr   r   r   r3   R   ó   z_Justify.ljustc                 C   r1   r2   )r)   Úrjustr,   r4   r   r   r   r7   U   r6   z_Justify.rjustc                 C   r1   r2   )r)   Úcenterr,   r4   r   r   r   r8   X   r6   z_Justify.centerN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r3   r7   r8   r   r   r   r   r$   B   s    
r$   c                   @   s   e Zd ZdZdd„ ZdS )Ú	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                 C   s(   || _ || _|| _|| _g | _|| _d S r2   )ÚindexÚhiddenÚprinterÚoutÚrowsÚwrap)r-   r>   r?   r@   rA   rC   r   r   r   r0   h   s   
zSubFormat.__init__N)r9   r:   r;   r<   r0   r   r   r   r   r=   \   s    r=   c                       sb   e Zd ZdZ‡ fdd„Zddd„Zdd„ Z		dd
d„Zdd„ Zdd„ Z	‡ fdd„Z
dd„ Z‡  ZS )Ú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              	      sÈ  g | _ tt| ƒj|dddœ|¤Ž d}dD ]}|| jv r |} nq| js+tj|d| _| j ¡ | _	d| _
d| _g | _d| _d}d| _| jrµ| j ¡ D ]}|jjsT|jjrWd}nd| _|jjrad| _|jjrhd| _qJtj| jjd}d}| j ¡ D ]<}|jjrš| jr„| jnt ¡ }	d}
| j|jj|	| j|d	}d| _nd}	d}|jj}
| j t||jj||	|
ƒ¡ |d
7 }qxd| _ |sÂd| _d| _dS | jrÞ| jsàg | _ | jD ]}|jsÝ|j!sÝ| j  |j"¡ qÎdS dS dS )zCreates a new TablePrinter.T)Ú
by_columnsÚnon_empty_projection_requiredN)Úasciizutf-8Úwin)r%   r   F)Úsymbols)rA   r   Údefaultsr   )#Ú_rowsÚsuperrD   r0   Ú
attributesÚ_console_attrr   ÚGetConsoleAttrÚGetControlSequenceIndicatorÚ_csiÚ_page_countÚ	_optionalÚ_subformatsÚ_has_subprintersÚ
_aggregateÚcolumn_attributesÚColumnsÚ	attributeÚ	subformatr?   ÚoptionalrC   Ú_wrapr   ÚProjectionSpecrI   Ú_outÚioÚStringIOÚPrinterÚappendr=   Ú_visibler@   r>   )r-   ÚargsÚkwargsr%   ÚnameÚhas_subformatsÚcolrJ   r>   rA   rC   r@   rZ   ©Ú	__class__r   r   r0   ™   s‚   
ÿÿ
þ€ÿüÿ


€üzTablePrinter.__init__Tc                 C   s   | j  |¡ dS )z¨Adds a list of columns.

    Output delayed until Finish().

    Args:
      record: A JSON-serializable object.
      delimit: Prints resource delimiters if True.
    N)rK   rb   )r-   ÚrecordÚdelimitr   r   r   Ú
_AddRecord×   s   	zTablePrinter._AddRecordc                 C   s0   | j r|s|S g }| j D ]	}| || ¡ q|S )z%Return the visible list items in row.)rc   rb   )r-   ÚrowÚvisibler>   r   r   r   Ú_Visibleâ   s   

zTablePrinter._VisibleFc                 C   sŠ  d}d}d}|t |ƒk rH| jr0||d…  | j¡r0| j ||d… ¡}|||| … }||7 }n|t || ¡7 }||kr>n
|d7 }|t |ƒk s|s^|t ||d… ƒt ||d…  ¡ ƒ 7 }t 	d|¡}|rq| 
¡ |krq| 
¡ }n%d}	t d|¡D ]}
|
 
¡ |kr|r‹|
 ¡ |kr‹|}	 n|
 
¡ }	qy|	r–|	}|s¡|d|…  ¡ }n|d|… }||d… }|rÁ|| j ¡ krÁ|| j ¡ 7 }|| }||fS )z3Helper function to get next line of wrappable text.r   r   Nr   z\nz\s+)r+   rQ   Ú
startswithrN   ÚGetControlSequenceLenr   ÚGetCharacterDisplayWidthÚlstripÚreÚsearchÚendÚfinditerÚstartÚrstripÚGetFontCode)r-   ÚsÚ	max_widthÚinclude_all_whitespaceÚcurrent_widthÚsplitÚprefixÚ
seq_lengthÚfirst_newlineÚmax_whitespaceÚrÚ	next_lineÚremaining_valuer   r   r   Ú_GetNextLineAndRemainderë   sF   
÷
(

z%TablePrinter._GetNextLineAndRemainderc                 C   s,   g }| j r| j D ]}|jr| |j¡ q|S r2   )rT   r@   rb   r>   )r-   ÚsubsrZ   r   r   r   Ú_GetSubformatIndexes  s   
€z!TablePrinter._GetSubformatIndexesc                 C   s0   d| j vr| jr| jS | jr|  | j ¡ ¡S d S )Nú
no-heading)rM   Ú_headingrW   rp   ÚLabels©r-   r   r   r   Ú_GetVisibleLabels&  s   
zTablePrinter._GetVisibleLabelsc           :         s  ˆj sdS ˆjr5dˆ_ˆjD ]$}ˆj D ]}||j }|r$|jj|dd q|j ¡  |j ¡ r2dˆ_qdS dˆj	v }|sAdˆj	v rIˆj
 ¡ }d}n	d}ˆj	 dt¡}ˆj }ˆjr¿g }g }	ˆj ¡ D ]\}
}|	si|}||kry| d	|	|f¡ g }	|}|	 |
¡ qa|	r‰| d	|	|f¡ |D ]-\}	}|r•t|ƒ}t|	ƒD ]‰‡fd
d„t|ƒD ƒ}| ¡  dd„ |D ƒ}q™|r¸t|ƒ}q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7 W¡ rh|(rNˆj d |( ¡ d	}(|7 G¡ }8ˆj |8¡ |ˆj
 6|7¡ ˆj
 6|8¡ }(qƒ|(|ˆj
 6|7¡ 7 }(qƒ|r~ˆj |jD¡ ˆj räˆj d¡ |rg }qv|2rã|r¡ˆj |.¡ ˆj d¡ ˆj  Xd	¡}9ˆjD ]8}|jrá|j |9|j ¡ |j+ ,¡ }| -d¡dd… D ]}ˆj d'| d ¡ qÇ|j+ .d	¡ |j+ /d	¡ qªnˆj d¡ |2ryqI|r|1sˆj |.¡ ˆj d¡ t0t1ˆƒ ¡  dS )(zPrints the table.NT)ÚintermediateFzall-boxÚboxr   Úpadr   c                    s,   g | ]\}}t |ˆ  ƒt|ˆ  ƒ||f‘qS r   )r#   r    )Ú.0Úirn   )Úkeyr   r   Ú
<listcomp>h  s     ÿz'TablePrinter.Finish.<locals>.<listcomp>c                 S   s   g | ]\}}}}|‘qS r   r   )r“   Ú_rn   r   r   r   r–   k  s    Útitlez

Ú
z{0}: {1}éÿÿÿÿc                 S   s   g | ]	}d d„ |D ƒ‘qS )c                 S   ó   g | ]}t |ƒ‘qS r   ©r    ©r“   Úcellr   r   r   r–   ¶  ó    z2TablePrinter.Finish.<locals>.<listcomp>.<listcomp>r   ©r“   rn   r   r   r   r–   ¶  s    c                    ó   g | ]}ˆ   |¡‘qS r   ©rp   r    rŽ   r   r   r–   ¼  ó    r‹   c                 S   r›   r   rœ   r   r   r   r   r–   Ð  rŸ   c                 s   s    | ]}t |ƒV  qd S r2   )r+   )r“   Úxr   r   r   Ú	<genexpr>Ñ  s   € z&TablePrinter.Finish.<locals>.<genexpr>c                    r¡   r   r¢   r    rŽ   r   r   r–   æ  r£   r   Úmarginr5   c                    s   g | ]
\}}|ˆ vr|‘qS r   r   )r“   r”   Ú	col_width)rC   r   r   r–   	  s
    ÿé   z	{0}{1}{2}r   ú c                 S   s   g | ]	}|j  o
|j‘qS r   )r?   r@   )r“   rZ   r   r   r   r–   ^  s    ÿc                 S   s
   |   |¡S r2   )r3   )r|   Úwr   r   r   Ú<lambda>r  ó   
 z%TablePrinter.Finish.<locals>.<lambda>)r~   c                    s
   ˆ | ˆƒS r2   r   )r|   )Újustifyr5   r   r   r«   Œ  r¬   )r­   z    )YrK   rV   Ú_emptyrT   r>   r@   ÚPrintÚFinishÚResourcesWerePrintedrM   rN   ÚGetBoxLineCharactersÚgetÚ_TABLE_COLUMN_PADrW   ÚOrderÚinsertrb   ÚreversedÚ	enumerateÚsortÚ
Alignmentsr   ÚVALUESÚaccessibilityÚscreen_readerÚGetBoolr^   ÚwriterS   rp   rX   rY   r[   rc   r   rŠ   r
   r+   r   r   r    ÚformatrA   Úgetvaluer€   ÚtruncateÚseekrL   rD   rU   rŒ   r   Úmaxr*   r5   rC   r   ÚboolÚ
_MIN_WIDTHÚ_BOX_CHAR_LENGTHÚGetTermSizeÚsumÚminrR   ÚdrÚhÚdlÚvr$   r8   rz   ÚvrÚurÚhdÚvhÚhuÚvlÚulÚjoinÚanyr   r   rˆ   Ú_colorÚ_justifyÚRenderÚstripÚpop):r-   rZ   rn   rk   Úall_boxr‘   Útable_column_padrB   ÚgroupsÚkeysÚ	key_indexÚkey_reverseÚreverseÚ	decoratedÚalignr½   r˜   r[   ro   r”   rh   Úlabelsr‰   ÚjÚcontentÚnested_outputÚkÚlineÚheadingÚcellsÚ
col_widthsÚvisible_colsÚtable_paddingÚtable_widthÚtotal_col_widthÚnon_wrappable_widthÚavailable_widthr§   Ú	min_widthÚsepr’   Út_sepÚm_sepÚb_sepÚt_ruleÚm_ruleÚb_rulerž   ÚfirstÚhas_visible_subformatsÚrow_finishedr|   Úis_colorizerÚ
cell_valueÚ	remainderr   Ústrippedr…   ri   )r­   r•   r-   r5   rC   r   r°   /  s¨  


€

€


ÿ€ÿ



€

€ÿ

€
€

ÿÿÿÿ
€
ÿ
þ


ÿÿ
ÿ€

ÿÿ

€³€NzTablePrinter.Finishc                 C   s,   |  j d7  _ |  ¡  | j d¡ g | _dS )z)Flushes the current resource page output.r   r™   N)rR   r°   r^   r¿   rK   rŽ   r   r   r   ÚPageÀ  s   
zTablePrinter.Page)T)F)r9   r:   r;   r<   r0   rm   rp   rˆ   rŠ   r   r°   r  Ú__classcell__r   r   ri   r   rD   q   s    '
>
ý2		   rD   )r<   Ú
__future__r   r   r   r_   r   ru   Úgooglecloudsdk.corer   Úgooglecloudsdk.core.consoler   Úgooglecloudsdk.core.resourcer   r   r	   r   Ú	six.movesr
   r´   rÇ   rÆ   r    r#   Úobjectr$   r=   ÚResourcePrinterrD   r   r   r   r   Ú<module>   s,   