o
                         @   s   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 de
jZdS )zYAML format printer.    )absolute_import)division)unicode_literalsN)log)resource_printer_base)resource_transform)rangec                       sD   e Zd ZdZ fddZG dd dejZdd Zdd	d
Z	  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           	         s  t t| j|ddi| ddlm jddd| _d| j_tj	| j_
d| j_tj| j_| jd | jd	}|rAt|| j_fd
d}dd } fdd}dd }dd }| jjt| | jjtj| | jjd | | jjtd | | jjtj| d S )Nretain_none_valuesTr   yamlsafe)typpureFnullversionc                    s    j dt|S )Nztag:yaml.org,2002:float)nodes
ScalarNoder   TransformFloat)unused_dumperdatar    F/tmp/google-cloud-sdk/lib/googlecloudsdk/core/resource/yaml_printer.py_FloatPresenterS   s   
z-YamlPrinter.__init__.<locals>._FloatPresenterc                 S   s   | j d|ddS )Ntag:yaml.org,2002:str|)stylerepresent_scalardumperr   r   r   r   _LiteralLinesPresenterW      z4YamlPrinter.__init__.<locals>._LiteralLinesPresenterc                    s     dv r
|  ddS |  d S )N)r   Nztag:yaml.org,2002:nullr   r   r   )r    unused_data)r   r   r   _NullPresenterZ   s   z,YamlPrinter.__init__.<locals>._NullPresenterc                 S   s   |  d| S )Nztag:yaml.org,2002:map)represent_mappingitemsr   r   r   r   _OrderedDictPresenter_   r"   z3YamlPrinter.__init__.<locals>._OrderedDictPresenterc                 S   s:   t |}|dkr| g S |dkr| i S | | d S )Nz[]z{})reprrepresent_listrepresent_dictrepresent_undefined)r    r   rr   r   r   _UndefinedPresenterb   s   

z1YamlPrinter.__init__.<locals>._UndefinedPresenter)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'   r-   	__class__)r   r   r   r/   >   s>   



zYamlPrinter.__init__c                   @   s   e Zd ZdZdS )zYamlPrinter._LiteralLinesz@A yaml representer hook for literal strings containing newlines.N)__name__
__module____qualname____doc__r   r   r   r   r?   u   s    r?   c                 C   s   ddl m} ddl m} t|tjrd|v rt|S ||r3tt	|D ]}| 
|| ||< q%|S ||rG|D ]}| 
|| ||< q9|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.yamlrL   rM   
isinstancesixstring_typesr	   r?   r   len_UpdateTypesForOutput)rC   valrL   rM   ikeyr   r   r   rT   x   s   

z!YamlPrinter._UpdateTypesForOutputTc                 C   sn   | j }tjrt| j tjrt }| |}|| j_	| jj
||d || j ur5| j | tj dS dS )zImmediately prints the given record as YAML.

    Args:
      record: A YAML-serializable Python object.
      delimit: Prints resource delimiters if True.
    )streamN)_outrQ   PY2rP   ioStringIOBytesIOrT   r2   explicit_startdumpwritegetvaluedecoder   r7   )rC   recorddelimitrX   r   r   r   
_AddRecord   s   

zYamlPrinter._AddRecord)T)rH   rI   rJ   rK   r/   rQ   	text_typer?   rT   re   __classcell__r   r   rF   r   r	   !   s    7r	   )rK   
__future__r   r   r   rA   r[   googlecloudsdk.corer   googlecloudsdk.core.resourcer   r   rQ   	six.movesr   ResourcePrinterr	   r   r   r   r   <module>   s   