
                             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
 ZddZ G d de	j                        Zy	)z Flattened tree resource printer.    )absolute_import)division)unicode_literals)
properties)resource_lex)resource_printer_base)resource_transformNc                 >    g fdf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                     t         j                  j                  j                  j	                         }|s+r)| v r%j                  |    j                         |f       yj                  | |f       y)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ress      5lib/googlecloudsdk/core/resource/flattened_printer.pyAppendResultz_Flatten.<locals>.AppendResult6   s^     ""''CCKKMJ&TV^	jj&,$$&/0	jj$     c           	         t        | t              r;| r/t        |       D ]   \  }} |dj                  ||      |       " y |g        yt        | t              rS| rGt        t        j                  |             D ]%  \  }} |dj                  ||rdnd|      |       ' y |i        yt        | t              r |t        j                  |              y ||        y)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vr   Flattens	          r   r/   z_Flatten.<locals>.FlattenE   s     #t	 ~GAt
$)00d!0Dc
J & 	T2	C		3==-.DAq
!'..DSba / 9:=? / 	T2	C	4+::3?@4r   r    )r*   r   r   r/   r   s    `@@@r   _Flattenr1      s'    , 	#!4 
#r3	*r   c                     ddddd}g }|r|j                  |       | D ]4  }|||fv r|j                  |       n	||v r||   }|j                  |       6 |r|j                  |       dj                  |      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cs         r   _StringQuoter>   c   s     uE?&
%
	LLaVUOll6	
f
)a	LLO  	LL	r   c                   0     e Zd ZdZ fdZd ZddZ x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                 0    t        t        | 
  |ddi| y )Nretain_none_valuesF)superr@   __init__)selfargskwargs	__class__s      r   rD   zFlattenedPrinter.__init__   s$    	
D*D 5U 5-35r   c                     i }| j                   j                         D ]:  }t        j                  |j                        }|j
                  j                  ||<   < 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)rE   r   r=   key_names       r   _LabelsByKeyzFlattenedPrinter._LabelsByKey   s]     F##++-((/h**fX . [6*d*r   c                    |r| j                   j                  d       | j                         }t        ||      }|rd| j                  v}| j                  j                  dd      }|rt        d |D              }|D ]  \  }}	| j                   j                  |       | j                   j                  |       |r*| j                   j                  dt        |      z
  z         t        j                  |	      }
d|
v s&|
dd	 j                         s|
d
d j                         rt        |
      }
| j                   j                  |
dz           yy)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              3   8   K   | ]  \  }}t        |        y w)N)len).0r   _s      r   	<genexpr>z.FlattenedPrinter._AddRecord.<locals>.<genexpr>   s     B1AvsA#c(1As    r4   r      N)_outwriterQ   r1   
attributesgetmaxrU   r&   	text_typeisspacer>   )rE   recorddelimitr   flattened_recordpadrS   max_key_lenr   valuevals              r   
_AddRecordzFlattenedPrinter._AddRecord   s    
iioog F/DOO+c//%%k48i	B1ABB(*#u					"
))//#s3x!78
9mmE"3;#a(**,BC0@0@0BS!#		d
# ) r   )T)__name__
__module____qualname____doc__rD   rQ   rj   __classcell__)rH   s   @r   r@   r@      s    05
+$r   r@   )"\)rn   
__future__r   r   r   googlecloudsdk.corer   googlecloudsdk.core.resourcer   r   r	   r&   r1   r>   ResourcePrinterr@   r0   r   r   <module>rv      sC     ' &  ' * 5 > ; 
BJ8B$,<< B$r   