o
    €ÏÉ  ã                   @   sŠ   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mZ ddlm	Z	 ddl
Z
G d	d
„ d
ejƒZG dd„ deƒZG dd„ deƒZdS )zCSV resource printer.é    )Úabsolute_import)Údivision)Úunicode_literals)Úresource_printer_base)Úresource_transform)ÚencodingNc                       s2   e Zd ZdZ‡ fdd„Zdd„ Zd	dd„Z‡  ZS )
Ú
CsvPrinteraž  A printer for printing CSV data.

  [Comma Separated Values](http://www.rfc-editor.org/rfc/rfc4180.txt) with no
  keys. This format requires a projection to define the values to be printed.

  To use *\n* or *\t* as an attribute value please escape the *\* with your
  shell's escape sequence, example *separator="\\n"* for bash.

  Printer attributes:
    delimiter="string": The string printed between list value items,
      default ";".
    no-heading: Disables the initial key name heading record.
    separator="string": The string printed between values, default ",".
    terminator="string": The string printed after each record, default
      "\n" (newline).
  c                    sn   t t| ƒj|dddœ|¤Ž d| _| j dd¡| _| j dd¡r"d nd| _| j d	d
¡| _| j dd¡| _	d S )NT)Ú
by_columnsÚnon_empty_projection_requiredFÚ	delimiterú;zno-quoter   ú"Ú	separatorú,Ú
terminatorÚ
)
Úsuperr   Ú__init__Ú_heading_printedÚ
attributesÚgetÚ
_delimiterÚ_quoteÚ
_separatorÚ_terminator©ÚselfÚargsÚkwargs©Ú	__class__© úE/tmp/google-cloud-sdk/lib/googlecloudsdk/core/resource/csv_printer.pyr   /   s   ÿþzCsvPrinter.__init__c                 C   sr   |r| j s|S | j|v s)| j |v s)| j|v s)| j|v s)|d  ¡ s)|d  ¡ s)|S | j | | j | j d ¡ | j  S )a  Returns field quoted by self._quote if necessary.

    The Python 2.7 csv module does not support unicode "yet". What are they
    waiting for?

    Args:
      field: The unicode string to quote.

    Returns:
      field quoted by self._quote if necessary.
    r   éÿÿÿÿé   )r   r   r   r   ÚisspaceÚreplace)r   Úfieldr!   r!   r"   Ú_QuoteField9   s    





ÿ
ÿÿþzCsvPrinter._QuoteFieldFc                    sD  ˆ j s<dˆ _ dˆ jvr<ˆ jrˆ j}nˆ j ¡ }|r#dd„ ˆ  |¡D ƒ}|r<ˆ j ˆ j 	‡ fdd„ˆ  |¡D ƒ¡ˆ j
 ¡ g }ˆ  |¡D ]O}|du rLd}nAt|tƒrdˆ j 	‡ fdd„tt |¡ƒD ƒ¡}n)t|tƒrwˆ j 	‡ fd	d„|D ƒ¡}nt|tƒr…ˆ  t |¡¡}nˆ  t |¡¡}| |¡ qCˆ j ˆ j 	|¡ˆ j
 ¡ dS )
aD  Prints the current record as CSV.

    Printer attributes:
      noheading: bool, Disable the initial key name heading record.

    Args:
      record: A list of JSON-serializable object columns.
      delimit: bool, Print resource delimiters -- ignored.

    Raises:
      ToolException: A data value has a type error.
    Tz
no-headingc                 S   s   g | ]}|  ¡ ‘qS r!   )Úlower©Ú.0Úxr!   r!   r"   Ú
<listcomp>j   s    z)CsvPrinter._AddRecord.<locals>.<listcomp>c                    s   g | ]}ˆ   |¡‘qS r!   )r(   )r+   Úlabel©r   r!   r"   r-   m   s    ÿÿNÚ c              
      s.   g | ]\}}ˆ   d  t |¡t |¡¡¡‘qS )z{0}={1})r(   Úformatr   ÚDecode)r+   ÚkÚvr/   r!   r"   r-   w   s
    þ
ÿc                    s$   g | ]}|rˆ   t |¡¡nd ‘qS )r0   )r(   r   r2   r*   r/   r!   r"   r-   |   s   $ )r   r   Ú_headingÚcolumn_attributesÚLabelsÚRemoveHiddenColumnsÚ_outÚwriter   Újoinr   Ú
isinstanceÚdictr   ÚsortedÚsixÚ	iteritemsÚlistÚfloatr(   r   ÚTransformFloatr   r2   Úappend)r   ÚrecordÚdelimitÚlabelsÚlineÚcolÚvalr!   r/   r"   Ú
_AddRecordQ   sF   

þýÿ

þÿ
ÿ
zCsvPrinter._AddRecord)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   rK   Ú__classcell__r!   r!   r   r"   r      s
    
r   c                       ó    e Zd ZdZ‡ fdd„Z‡  ZS )ÚValuePrinteraç  A printer for printing value data.

  CSV with no heading and <TAB> separator instead of <COMMA>. Used to retrieve
  individual resource values. This format requires a projection to define the
  value(s) to be printed.

  To use *\n* or *\t* as an attribute value please escape the *\* with your
  shell's escape sequence, example *separator="\\n"* for bash.

  Printer attributes:
    delimiter="string": The string printed between list value items,
      default ";".
    quote: "..." quote values that contain delimiter, separator or terminator
      strings.
    separator="string": The string printed between values, default
      "\t" (tab).
    terminator="string": The string printed after each record, default
      "\n" (newline).
  c                    sh   t t| ƒj|i |¤Ž d| _| j dd¡| _| j dd¡rdnd | _| j dd¡| _| j d	d
¡| _	d S )NTr   r   Úquoter   r   r   ú	r   r   )
r   rR   r   r   r   r   r   r   r   r   r   r   r!   r"   r   š   s   zValuePrinter.__init__©rL   rM   rN   rO   r   rP   r!   r!   r   r"   rR   …   s    rR   c                       rQ   )Ú
GetPrinterz°A printer for printing value data with transforms disabled.

  Equivalent to the *value[no-transforms]* format. Default transforms are
  not applied to the displayed values.
  c                    s   t t| ƒj|ddi|¤Ž d S )NÚignore_default_transformsT)r   rV   r   r   r   r!   r"   r   ª   s   
ÿzGetPrinter.__init__rU   r!   r!   r   r"   rV   £   s    rV   )rO   Ú
__future__r   r   r   Úgooglecloudsdk.core.resourcer   r   Úgooglecloudsdk.core.utilr   r?   ÚResourcePrinterr   rR   rV   r!   r!   r!   r"   Ú<module>   s   h