
                             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
ej                        Z G d de      Z G d de      Zy)zCSV resource printer.    )absolute_import)division)unicode_literals)resource_printer_base)resource_transform)encodingNc                   0     e Zd ZdZ fdZd ZddZ xZS )
CsvPrintera  A printer for printing CSV data.

  [Comma Separated Values](http://www.ietf.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                 P   t        t        | 
  |ddd| d| _        | j                  j                  dd      | _        | j                  j                  dd      rd nd| _        | j                  j                  d	d
      | _        | j                  j                  dd      | _	        y )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__s      /lib/googlecloudsdk/core/resource/csv_printer.pyr   zCsvPrinter.__init__/   s    	*d$dtCG/'-/ "Doo))+s;DO//--j!<$#DKoo))+s;DO**<>D    c                 h   |r| j                   s|S | j                  |v sR| j                   |v sD| j                  |v s6| j                  |v s(|d   j	                         s|d   j	                         s|S | j                   |j                  | j                   | j                   dz        z   | j                   z   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   fields     r#   _QuoteFieldzCsvPrinter._QuoteField9   s     lOOu$KK5 OOu$%!H%)"3"3"5lKKMM$++t{{Q78KK r$   c                    | j                   sd| _         d| j                  vr| j                  r| j                  }nH| j                  j	                         }|r,| j                  |      D cg c]  }|j                          }}|rm| j                  j                  | j                  j                  | j                  |      D cg c]  }| j                  |       c}      | j                  z          g }| j                  |      D ]s  }|d}nYt        |t              r| j                  j                  t!        t#        j$                  |            D 	
cg c]L  \  }	}
| j                  dj'                  t)        j*                  |	      t)        j*                  |
                  N c}
}	      }nt        |t,              rP| j                  j                  |D cg c]*  }|r$| j                  t)        j*                  |            nd, c}      }nYt        |t.              r%| j                  t1        j2                  |            }n$| j                  t)        j*                  |            }|j5                  |       v | j                  j                  | j                  j                  |      | j                  z          yc c}w c c}w c c}
}	w c c}w )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-headingN z{0}={1})r   r   _headingcolumn_attributesLabelsRemoveHiddenColumnslower_outwriter   joinr+   r   
isinstancedictr   sortedsix	iteritemsformatr   Decodelistfloatr   TransformFloatappend)r   recorddelimitlabelsxlabellinecolvalkvs              r#   
_AddRecordzCsvPrinter._AddRecordQ   sR   "   "d	T__	,====&))002&)-)A)A&)IJ)IAaggi)IFJ
))//oo""#77?$?e ""5)?$  ##$%
 D''/	c4 oo""  c 2353A i.."HOOA$68 9356 c4 oo""HKL1QThooa01B>LNc5!1@@EFxs34
kk# 0  	IIOODOO((.1A1AAB1 K$5
 Ms   *J>KAK/K
)F)__name__
__module____qualname____doc__r   r+   rK   __classcell__r"   s   @r#   r
   r
      s    "?01Cr$   r
   c                   "     e Zd ZdZ fdZ x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                 J   t        t        | 
  |i | d| _        | j                  j                  dd      | _        | j                  j                  dd      rdnd | _        | j                  j                  dd      | _        | j                  j                  d	d
      | _	        y )NTr   r   quoter   r   r   	r   r   )
r   rS   r   r   r   r   r   r   r   r   r   s      r#   r   zValuePrinter.__init__   s    	,&77 Doo))+s;DO,,Wa8#dDKoo))+t<DO**<>Dr$   rL   rM   rN   rO   r   rP   rQ   s   @r#   rS   rS      s    (? ?r$   rS   c                   "     e Zd ZdZ fdZ xZS )
GetPrinterzA 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                 0    t        t        | 
  |ddi| y )Nignore_default_transformsT)r   rY   r   r   s      r#   r   zGetPrinter.__init__   s#    	*d$d /d /'-/r$   rW   rQ   s   @r#   rY   rY      s    / /r$   rY   )rO   
__future__r   r   r   googlecloudsdk.core.resourcer   r   googlecloudsdk.core.utilr   r9   ResourcePrinterr
   rS   rY    r$   r#   <module>ra      sO      &  ' > ; - 
eC&66 eCP?: ?<	/ 	/r$   