
                             S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSKJ
r
  SS	KrS
 rSS jr " S S\	R                  5      rg	)z Flattened tree resource printer.    )absolute_import)division)unicode_literals)
properties)resource_lex)resource_printer_base)resource_transformNc                 B   ^^^^ / mUU4S jmUU4S jmT" U ST5        T$ )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                    > [         R                  R                  R                  R	                  5       nU(       d2  T(       a+  U T;   a%  TR                  TU    R                  5       U45        gTR                  X45        g)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AppendResult_Flatten.<locals>.AppendResult6   s\     ""''CCKKMJ&TV^	jj&,$$&/0	jj$     c           
         > [        U [        5      (       a=  U (       a,  [        U 5       H  u  p4T" USR                  XS9U5        M     gT" U/ 5        g[        U [        5      (       a[  U (       aJ  [        [        R                  " U 5      5       H&  u  pVT" USR                  X(       a  SOSUS9U5        M(     gT" U0 5        g[        U [        5      (       a  T" U[        R                  " U 5      5        gT" X5        g)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   r0   _Flatten.<locals>.FlattenE   s     #t	 ~GA
$)00d0Dc
J & 	T2	C			3==-.DA
!'..DSba / 9:=? / 	T2	C		4+::3?@4r   r    )r+   r   r   r0   r   s    `@@@r   _Flattenr3      s'    , 	#!4 
#r3	*r   c                    SSSSS.n/ nU(       a  UR                  U5        U  H6  nXRU4;   a  UR                  U5        O	XS;   a  X5   nUR                  U5        M8     U(       a  UR                  U5        SR                  U5      $ )a4  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UOll6	

)a	LLO  	LL	r   c                   <   ^  \ rS rSrSrU 4S jrS rSS jrSrU =r	$ )FlattenedPrinter   a  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                 2   > [         [        U ]
  " USS0UD6  g )Nretain_none_valuesF)superrB   __init__)selfargskwargs	__class__s      r   rG   FlattenedPrinter.__init__   s$    	
D*D 5U 5-35r   c                     0 nU R                   R                  5        H;  n[        R                  " UR                  5      nUR
                  R                  X'   M=     [        U5      (       a  U$ 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)rH   r   r?   key_names       r   _LabelsByKeyFlattenedPrinter._LabelsByKey   s]     F##++-((/h**f . [[6*d*r   c                    U(       a  U R                   R                  S5        U R                  5       n[        X5      nU(       Ga,  SU R                  ;  nU R                  R                  SS5      nU(       a  [        S U 5       5      nU H  u  pU R                   R                  U5        U R                   R                  U5        U(       a*  U R                   R                  SW[        U5      -
  -  5        [        R                  " U	5      n
SU
;   d0  U
SS	 R                  5       (       d  U
S
S R                  5       (       a  [        U
5      n
U R                   R                  U
S-   5        M     gg)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   <   #    U  H  u  p[        U5      v   M     g 7f)N)len).0r    _s      r   	<genexpr>.FlattenedPrinter._AddRecord.<locals>.<genexpr>   s     B1Avs#c((1As    r6   r      N)_outwriterU   r3   
attributesgetmaxrZ   r'   	text_typeisspacer@   )rH   recorddelimitr   flattened_recordpadrX   max_key_lenr    valuevals              r   
_AddRecordFlattenedPrinter._AddRecord   s    
iioog F/DOO+c//%%k48i	B1ABB(*#					"
))//#s3x!78
9mmE"3;#a(**,,BC0@0@0B0BS!#		d
# ) r   r2   )T)
__name__
__module____qualname____firstlineno____doc__rG   rU   rp   __static_attributes____classcell__)rK   s   @r   rB   rB      s    05
+$ $r   rB   )"\)rv   
__future__r   r   r   googlecloudsdk.corer   googlecloudsdk.core.resourcer   r   r	   r'   r3   r@   ResourcePrinterrB   r2   r   r   <module>r      sC     ' &  ' * 5 > ; 
BJ8B$,<< B$r   