
                             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Z G d d	ej                        Zy)
zconfig format resource printer.    )absolute_import)division)unicode_literalsN)resource_printer_base)	platformsc                   V     e Zd ZdZ fdZd Zd Zed        Zd	dZ	d	dZ
d
dZ xZS )ConfigPrinteraC  Prints a dictionary of dictionaries in config style.

  A dictionary of dictionaries in config style.

  Printer attributes:
    export: Display the dictionary as a list of system specific
      environment export commands.
    unset: Display the dictionary as a list of system specific
      environment unset commands.
  c                    t        t        | 
  |ddi| d| j                  v r?| j                  | _        t        j                  j                         rd| _	        nfd| _	        n^d| j                  v r?| j                  | _        t        j                  j                         rd| _	        nd| _	        n| j                  | _        d	| j                  v r,| j                  j                  | j                  d	   d
z          y y )Nretain_none_valuesTexportzset {name}={value}
zexport {name}={value}
unsetzset {name}=
zunset {name}
title
)superr	   __init__
attributes_PrintEnvExport
_add_itemsr   OperatingSystem	IsWindows_env_command_format_PrintEnvUnset_PrintConfig_outwrite)selfargskwargs	__class__s      2lib/googlecloudsdk/core/resource/config_printer.pyr   zConfigPrinter.__init__+   s    	-' 2$ 2*024??",,do		"	"	,	,	.#9 #< 	DOO	#++do		"	"	,	,	.#2 #3 ))do$//!
iioodoog.56 "    c                 ,   t        j                         }t        j                         }t        |      D ]G  \  }}t        j                  |      }	 t        j
                  |      }| j                  |||gz   |       I |j                         }	|j                         }
|rD|	s|
r@|
j                  d      s/|j                  dj                  dj                  |                   |	r|j                  |	       |
r|j                  |
       yy# t        $ r Y nw xY w|#|j                  dj                  |             t        |t              r"| j                  |||gz   t        |             >|j                  dj                  ||             b)	a   Prints config items in the label category.

    Args:
      out: The output stream for this category.
      label: A list of label strings.
      items: The items to list for label, either dict iteritems, an enumerated
        list, or a scalar value.
    N{name} (unset)
namez{name} = {value}
r%   value[z[{0}]
.)ioStringIOsortedsix	text_type	iteritems_PrintCategoryAttributeErrorr   format
isinstancelist	enumerategetvalue
startswithjoin)r   outlabelitemstopsubr%   r'   valuestop_contentsub_contents              r    r0   zConfigPrinter._PrintCategory@   sO    ++-C
++-Ce}e]]4 du%C$8 % ,,.K,,.K+k&<&<S&A	ii	  %12	ii	ii   			$+++67eT"C$51AB		&--4u-EFs   ,D	DDc                 >    | j                  | j                  g |       y)z{Prints config items in the root category.

    Args:
      items: The current record dict iteritems from _AddRecord().
    N)r0   r   )r   r;   s     r    r   zConfigPrinter._PrintConfigc   s     			2u-r!   c                 b    t        |t              rdt        j                  |      z   }| |z   dz   S )z.Returns a new prefix based on prefix and name.I_)r3   intr-   r.   )prefixr%   s     r    _PrefixzConfigPrinter._Prefixk   s0     $3==&&dD=3r!   c           	         t        |      D ]y  \  }}t        j                  |      }t        |t              r7| j                  t        j                  |      | j                  ||             c|/| j                  j                  dj                  ||z                t        |t              rut        |      D ]e  \  }}t        |t              sdt        j                  |      z   |i}| j                  t        j                  |      | j                  ||             g t        j                  t        j                  |            }| j                  j                  | j                  j                  ||z   |             | y)a?  Prints the environment export commands for items.

    Environment variable names have '_' instead of '.'.  Array values have
    subscripts expanded as names *_I0, *_I1, ...

    Args:
      items: The current record dict iteritems from _AddRecord().
      prefix: Parent name prefix, prepended to each item name.
    rF   Nr#   r$   rC   r&   )r,   r-   r.   r3   dictr   r/   rG   r   r   r2   r4   r5   shlexquoter   r   r;   rF   r%   r'   ivs          r    r   zConfigPrinter._PrintEnvExportr   s9    e}e]]4 d	E4	 S]]51$(LL$> 	 	@=		*11v}1EFeT"e$DAqAt$s}}Q''+A


s}}Q/&*ll64&@  B % CMM%01		0077$e 8 - 	. %r!   c           	      ^   t        |      D ]  \  }}t        j                  |      }t        |t              r7| j                  t        j                  |      | j                  ||             ct        |t              rtt        |      D ]e  \  }}t        |t              sdt        j                  |      z   |i}| j                  t        j                  |      | j                  ||             g | j                  j                  | j                  j                  ||z                ! y)zPrints the environment unset commands for items.

    Args:
      items: The current record dict iteritems from _AddRecord().
      prefix: Parent name prefix, prepended to each item name.
    rI   rC   r$   N)r,   r-   r.   r3   rJ   r   r/   rG   r4   r5   r   r   r   r2   rM   s          r    r   zConfigPrinter._PrintEnvUnset   s     e}e]]4 d	E4	 CMM%0#'<<#= 	 	?eT"e$DAqAt$s}}Q''+A


cmmA.%)\\&$%?  A % 			0077Vd]7KL %r!   c                 l    	 | j                  t        j                  |             y# t        $ r Y yw xY w)zDispatches to the specific config printer.

    Nothing is printed if the record is not a JSON-serializable dict.

    Args:
      record: A JSON-serializable dict.
      delimit: Ignored.
    N)r   r-   r/   r1   )r   recorddelimits      r    
_AddRecordzConfigPrinter._AddRecord   s.    
oocmmF+, 
s   $' 	33) )F)__name__
__module____qualname____doc__r   r0   r   staticmethodrG   r   r   rT   __classcell__)r   s   @r    r	   r	      s<    	7*!F.  .8M*r!   r	   )rY   
__future__r   r   r   r*   rK   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   r-   ResourcePrinterr	    r!   r    <module>ra      s7     & &  ' 	  > . 
P)99 Pr!   