o
                         @   sn   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	 d	ejZdS )
zconfig format resource printer.    )absolute_import)division)unicode_literalsN)resource_printer_base)	platformsc                       sZ   e Zd ZdZ fddZdd Zdd Zedd	 ZdddZ	dddZ
dddZ  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                    s   t t| j|ddi| d| jv r#| j| _tj rd| _	nd| _	nd| jv r9| j
| _tj r5d| _	nd| _	n| j| _d	| jv rO| j| jd	 d
  d S d S )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__ H/tmp/google-cloud-sdk/lib/googlecloudsdk/core/resource/config_printer.pyr   +   s"   




zConfigPrinter.__init__c              	   C   s  t  }t  }t|D ]N\}}t|}zt|}| |||g | W q ty/   Y nw |du r>|dj	|d qt
|trP| |||g t| q|dj	||d q| }	| }
|ry|	sn|
ry|
dsy|d	d| |	r||	 |
r||
 dS dS )	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_contentr   r   r   r-   @   s:   	



zConfigPrinter._PrintCategoryc                 C   s   |  | jg | dS )z{Prints config items in the root category.

    Args:
      items: The current record dict iteritems from _AddRecord().
    N)r-   r   )r   r8   r   r   r   r   c   s   zConfigPrinter._PrintConfigc                 C   s$   t |trdt| }| | d S )z.Returns a new prefix based on prefix and name.I_)r0   intr*   r+   )prefixr"   r   r   r   _Prefixk   s   
zConfigPrinter._Prefix c                 C   s   t |D ]o\}}t|}t|tr!| jt|| ||d q|du r2| j	dj
|| d qt|tr]t|D ] \}}t|tsMdt| |i}| jt|| ||d q;qtt|}| j	| jj
|| |d qdS )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.
    rA   Nr    r!   r>   r#   )r)   r*   r+   r0   dictr   r,   rB   r   r   r/   r1   r2   shlexquoter   r   r8   rA   r"   r$   ivr   r   r   r   r   s*   







zConfigPrinter._PrintEnvExportc                 C   s   t |D ]U\}}t|}t|tr!| jt|| ||d qt|trLt	|D ] \}}t|ts<dt| |i}| jt|| ||d q*q| j
| jj|| d qdS )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.
    rD   r>   r!   N)r)   r*   r+   r0   rE   r   r,   rB   r1   r2   r   r   r   r/   rH   r   r   r   r      s    





zConfigPrinter._PrintEnvUnsetFc                 C   s,   z|  t| W dS  ty   Y dS 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,   r.   )r   recorddelimitr   r   r   
_AddRecord   s
   	zConfigPrinter._AddRecord)rC   )F)__name__
__module____qualname____doc__r   r-   r   staticmethodrB   r   r   rM   __classcell__r   r   r   r   r      s    #


r   )rQ   
__future__r   r   r   r'   rF   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   r*   ResourcePrinterr   r   r   r   r   <module>   s   