
                             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mZ ddl	m
Z
 ddl	mZ ddlZdd	lmZ  G d
 de
j                        Zy)zYAML format printer.    )absolute_import)division)unicode_literalsN)log)resource_printer_base)resource_transform)rangec                   Z     e Zd ZdZ fdZ G d dej                        Zd ZddZ	 xZ
S )YamlPrintera  Prints the YAML representations of JSON-serializable objects.

  [YAML](http://www.yaml.org), YAML ain't markup language.

  Printer attributes:
    null="string": Display string instead of `null` for null/None values.
    no-undefined: Does not display resource data items with null values.
    version=VERSION: Prints using the specified YAML version, default 1.2.

  For example:

    printer = YamlPrinter(log.out)
    printer.AddRecord({'a': ['hello', 'world'], 'b': {'x': 'bye'}})

  produces:

    ---
    a:
      - hello
      - world
    b:
      - x: bye

  Attributes:
    _yaml: Reference to the `yaml` module. Imported locally to improve startup
        performance.
  c                   	
 t        t        | 
  |ddi| ddlm
 
j                  dd      | _        d| j                  _        t        j                  | j                  _
        d| j                  _        t        j                  | j                  _        | j                  j!                  d      	| j                  j!                  d	      }|rt#        |      | j                  _        
fd
}d }	fd}d }d }| j                  j&                  j)                  t*        |       | j                  j&                  j)                  t        j,                  |       | j                  j&                  j)                  d |       | j                  j&                  j)                  t/        d       |       | j                  j&                  j)                  t0        j2                  |       y )Nretain_none_valuesTr   )yamlsafe)typpureFnullversionc                 b    j                   j                  dt        j                  |            S )Nztag:yaml.org,2002:float)nodes
ScalarNoder   TransformFloat)unused_dumperdatar   s     0lib/googlecloudsdk/core/resource/yaml_printer.py_FloatPresenterz-YamlPrinter.__init__.<locals>._FloatPresenterS   s-    ZZ""
#%7%F%Ft%LN N    c                 *    | j                  d|d      S )Ntag:yaml.org,2002:str|)stylerepresent_scalardumperr   s     r   _LiteralLinesPresenterz4YamlPrinter.__init__.<locals>._LiteralLinesPresenterW   s    $$%<d#$NNr   c                 T    dv r| j                  dd      S | j                  d      S )N)r   Nztag:yaml.org,2002:nullr   r   r!   )r$   unused_datar   s     r   _NullPresenterz,YamlPrinter.__init__.<locals>._NullPresenterZ   s3    		&&'?HH$$%<dCCr   c                 B    | j                  d|j                               S )Nztag:yaml.org,2002:map)represent_mappingitemsr#   s     r   _OrderedDictPresenterz3YamlPrinter.__init__.<locals>._OrderedDictPresenter_   s    %%&=tzz|LLr   c                     t        |      }|dk(  r| j                  g       S |dk(  r| j                  i       S | j                  |       y )Nz[]z{})reprrepresent_listrepresent_dictrepresent_undefined)r$   r   rs      r   _UndefinedPresenterz1YamlPrinter.__init__.<locals>._UndefinedPresenterb   sI    
t*a	
d$$R((	
d$$R((  &r   )superr   __init__ruamelr   YAML_yamldefault_flow_styler   STRUCTURED_INDENTATION
old_indentallow_unicoder   LOG_FILE_ENCODINGencoding
attributesgetstrr   representeradd_representerfloat_LiteralLinestypecollectionsOrderedDict)selfargskwargsr   r   r%   r(   r,   r3   r   r   	__class__s            @@r   r5   zYamlPrinter.__init__>   so   	+t%tOOO vD1DJ$)DJJ!1HHDJJ#DJJ//DJJ??v&Doo!!),Gw<djjNOD
M' 	JJ**5+:<JJ**;+D+D+ACJJ**4+>@JJ**4:+9;JJ**;+B+B+@Br   c                       e Zd ZdZy)YamlPrinter._LiteralLinesz@A yaml representer hook for literal strings containing newlines.N)__name__
__module____qualname____doc__ r   r   rE   rN   u   s    Jr   rE   c                 H   ddl m} ddl m} t        |t        j
                        rd|v rt        j                  |      S  ||      r2t        t        |            D ]  }| j                  ||         ||<    |S  ||      r |D ]  }| j                  ||         ||<    |S |S )zDig through a dict of list of primitives to help yaml output.

    Args:
      val: A dict, list, or primitive object.

    Returns:
      An updated version of val.
    r   )	dict_like)	list_like
)googlecloudsdk.core.yamlrU   rV   
isinstancesixstring_typesr   rE   r	   len_UpdateTypesForOutput)rI   valrU   rV   ikeys         r   r]   z!YamlPrinter._UpdateTypesForOutputx   s     32 #s''(TS[&&s++~SX!++CF3A j~#--c#h7C jJr   c                    | j                   }t        j                  r8t        | j                   t        j
                        rt	        j                         }| j                  |      }|| j                  _	        | j                  j                  ||       || j                   urG| j                   j                  |j                         j                  t        j                               yy)zImmediately prints the given record as YAML.

    Args:
      record: A YAML-serializable Python object.
      delimit: Prints resource delimiters if True.
    )streamN)_outrZ   PY2rY   ioStringIOBytesIOr]   r8   explicit_startdumpwritegetvaluedecoder   r=   )rI   recorddelimitrb   s       r   
_AddRecordzYamlPrinter._AddRecord   s     YYF wwJtyy"++6zz|f''/F 'DJJJJOO   TYY
iioofoo'..s/D/DEF r   )T)rO   rP   rQ   rR   r5   rZ   	text_typerE   r]   ro   __classcell__)rL   s   @r   r   r   !   s,    85BnKcmm K4Gr   r   )rR   
__future__r   r   r   rG   re   googlecloudsdk.corer   googlecloudsdk.core.resourcer   r   rZ   	six.movesr	   ResourcePrinterr   rS   r   r   <module>rw      s?      &  '  	 # > ; 
 CG'77 CGr   