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m
Z
 dd	lZd
d ZdddZG dd de	jZd	S )z Flattened tree resource printer.    )absolute_import)division)unicode_literals)
properties)resource_lex)resource_printer_base)resource_transformNc                    s0   g fdd  fdd| d S )a  Flattens a JSON-serializable object into a list of tuples.

  The first element of each tuple will be a key and the second element
  will be a simple value.

  For example, _Flatten({'a': ['hello', 'world'], 'b': {'x': 'bye'}})
  will produce:

    [
        ('a[0]', 'hello'),
        ('a[1]', 'world'),
        ('b.x', 'bye'),
    ]

  Args:
    obj: A JSON-serializable object.
    labels: An object mapping keys to projection labels.

  Returns:
    A list of tuples.
  c                    sJ   t jjj }|s r|  v r |   |f dS | |f dS )zAppends key/value pairs from obj into res.

    Adds projection label if defined.

    Args:
      name: Name of key.
      result: Value of key in obj.
    N)r   VALUEScoreuse_legacy_flattened_formatGetBoolappendlower)nameresult
use_legacy)labelsres K/tmp/google-cloud-sdk/lib/googlecloudsdk/core/resource/flattened_printer.pyAppendResult6   s   	z_Flatten.<locals>.AppendResultc                    s   t | tr$| rt| D ]\}}|dj||d| qdS  |g  dS t | trP| rItt| D ]\}}|dj||r?dnd|d| q2dS  |i  dS t | tr_ |t	
|  dS  ||  dS )zRecursively appends keys in path from obj into res.

    Args:
      obj: The object to flatten.
      name: The key name of the current obj.
      res: The ordered result value list.
    z{name}[{index}])r   indexz{name}{dot}{key}. )r   dotkeyN)
isinstancelist	enumerateformatdictsortedsix	iteritemsfloatr   TransformFloat)objr   r   iitemkv)r   Flattenr   r   r+   E   s&   


z_Flatten.<locals>.Flattenr   r   )r&   r   r   )r   r+   r   r   r   _Flatten   s
   r,   "\c                 C   st   ddddd}g }|r| | | D ]}|||fv r | | n||v r(|| }| | q|r5| | d|S )aD  Returns <quote>s<quote> with <escape> and <quote> in s escaped.

  s.encode('string-escape') does not work with type(s) == unicode.

  Args:
    s: The string to quote.
    quote: The outer quote character.
    escape: The enclosed escape character.

  Returns:
    <quote>s<quote> with <escape> and <quote> in s escaped.
  z\fz\nz\rz\t)
	r   )r   join)squoteescapeentitycharscr   r   r   _StringQuotec   s   


r:   c                       s2   e Zd ZdZ fddZdd Zd	ddZ  ZS )
FlattenedPrintera  Prints a flattened tree representation of JSON-serializable objects.

  A flattened tree. Each output line contains one *key*:*value* pair.

  Printer attributes:
    no-pad: Don't print space after the separator. The default adjusts the
      space to align the values into the same output column. Use *no-pad*
      for comparing resource outputs.
    separator=_SEPARATOR_: Print _SEPARATOR_ between the *key* and *value*.
      The default is ": ".

  For example:

    printer = resource_printer.Printer('flattened', out=sys.stdout)
    printer.AddRecord({'a': ['hello', 'world'], 'b': {'x': 'bye'}})

  produces:

    ---
    a[0]: hello
    a[1]: world
    b.x:  bye
  c                    s   t t| j|ddi| d S )Nretain_none_valuesF)superr;   __init__)selfargskwargs	__class__r   r   r>      s   
zFlattenedPrinter.__init__c                 C   s<   i }| j  D ]}t|j}|jj||< qt|r|S dS )zReturns an object that maps keys to projection labels.

    Returns:
      An object of keys to projection labels, None if all labels are empty.
    N)column_attributesColumnsr   
GetKeyNamer   	attributelabelany)r?   r   r9   key_namer   r   r   _LabelsByKey   s
   zFlattenedPrinter._LabelsByKeyTc                 C   s   |r| j d |  }t||}|rpd| jv}| jdd}|r*tdd |D }|D ]E\}}	| j | | j | |rJ| j d|t|   t	|	}
d|
v sc|
d	d
 
 sc|
dd 
 rgt|
}
| j |
d  q,dS dS )zImmediately prints the record as flattened a flattened tree.

    Args:
      record: A JSON-serializable object.
      delimit: Prints resource delimiters if True.
    z---
zno-pad	separatorz: c                 s   s    | ]	\}}t |V  qd S )N)len).0r   _r   r   r   	<genexpr>   s    z.FlattenedPrinter._AddRecord.<locals>.<genexpr> r0   r      N)_outwriterK   r,   
attributesgetmaxrM   r"   	text_typeisspacer:   )r?   recorddelimitr   flattened_recordpadrL   max_key_lenr   valuevalr   r   r   
_AddRecord   s(   


(zFlattenedPrinter._AddRecord)T)__name__
__module____qualname____doc__r>   rK   rb   __classcell__r   r   rB   r   r;      s
    r;   )r-   r.   )rf   
__future__r   r   r   googlecloudsdk.corer   googlecloudsdk.core.resourcer   r   r   r"   r,   r:   ResourcePrinterr;   r   r   r   r   <module>   s   
E