U
    ]                     @   sJ  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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 Zdd Zdd Zdd Zejeje ejeje ejeje ejeje ejej e ejej!e ejej"e dddZ#dd Z$d ddZ%dd Z&d!ddZ'dS )"    )absolute_import)unicode_literalsN)types)COMPOSEFILE_V1)COMPOSEFILE_V2_1)COMPOSEFILE_V2_3)COMPOSEFILE_V3_0)COMPOSEFILE_V3_2)COMPOSEFILE_V3_4)COMPOSEFILE_V3_5c                 C   s   t jr| jn| j}|| S N)sixPY3represent_strrepresent_unicodereprdumperdataZrepresenter r   :/usr/lib/python3/dist-packages/compose/config/serialize.pyserialize_config_type   s    r   c                 C   s   |  | S r   )Zrepresent_dictr   r   r   r   r   r   serialize_dict_type   s    r   c                 C   sL   t jr| jn| j}t|t jr(|d}| dkrD| jd|ddS ||S )z6 Ensure boolean-like strings are quoted in the output zutf-8)ynZyesZnoZonZofftrueZfalseztag:yaml.org,2002:str")Zstyle)	r   r   r   r   
isinstanceZbinary_typedecodelowerZrepresent_scalarr   r   r   r   serialize_string   s    
r!   c                 C   s   | dd}t| |S )zN Ensure boolean-like strings are quoted in the output and escape $ characters $z$$)replacer!   r   r   r   r   serialize_string_escape_dollar(   s    r$   c                    s   d j tkrttnt j i} fdd jD }dd |D |d< dD ]}t |}|s`qL| ||< ||  D ]x\}}d|kr|d= d	|kr؈ j tk s j tkr j t	|k r|d	= nd
|krt
|d
 |d
< d|krx j tk rx|d= qxqL|S )Nversionc                    s*   g | ]"}t | jr |d   ndqS )nameN)denormalize_service_dictr%   .0service_dictconfigimage_digestsr   r   
<listcomp>9   s   z&denormalize_config.<locals>.<listcomp>c                 S   s   i | ]}| d |qS )r&   )popr(   r   r   r   
<dictcomp>@   s    z&denormalize_config.<locals>.<dictcomp>services)ZnetworksvolumesZsecretsZconfigsZexternal_namer&   ZexternalZ
attachable)r%   V1strV2_1r1   getattrcopyitemsV3_0v3_introduced_name_keyboolV3_2)r,   r-   resultZdenormalized_serviceskeyZconfig_dictr&   confr   r+   r   denormalize_config7   s6     



r@   c                 C   s   | dkrt S tS )Nr2   )V3_4V3_5)r>   r   r   r   r:   \   s    r:   Tc                 C   s\   |r$t jtt t jtjt nt jtt t jtjt t jt	| |dddddS )NF   P   T)Zdefault_flow_styleindentwidthZallow_unicode)
yaml
SafeDumperadd_representerr4   r$   r   Z	text_typer!   Z	safe_dumpr@   )r,   r-   Zescape_dollarr   r   r   serialize_configb   s    rJ   c                 C   sd   | df}dddddg}|D ]>}| |d  }|t | |d  krT|} t | |d f}q qZqd	j| S )
Nns)     @@us)rL   Zms)rL   s)      N@m)rO   hr      z{0}{1})intformat)valuer=   tableZstageZtmpr   r   r   serialize_ns_time_valuer   s    rW   c                    sV  |   } |r|| d< d| kr.t| d | d<  tkrFd| krFd| d< d| kr| tk s^ tkr|tdd | d  D | d< d| krd	| d krt| d d	 | d d	< d
| d krt| d d
 | d d
< d| d krt| d d | d d< d| kr fdd| d D | d< d| krR t	k s< tkrR t
k rRdd | d D | d< | S )NZimageZrestartZnetwork_modeZbridgeZ
depends_onc                 S   s   g | ]}|qS r   r   )r)   Zsvcr   r   r   r.      s    z,denormalize_service_dict.<locals>.<listcomp>ZhealthcheckZintervalZtimeoutZstart_periodZportsc                    s&   g | ]}|j s tk r| n|qS r   )Zexternal_ipr<   legacy_repr)r)   pr%   r   r   r.      s   r2   c                 S   s$   g | ]}t |tjr| n|qS r   )r   r   	MountSpecrX   )r)   vr   r   r   r.      s    )r7   r   Zserialize_restart_specr3   r5   r9   sortedkeysrW   V2_3r<   )r*   r%   Zimage_digestr   rZ   r   r'      sF    






(
r'   )N)NT)N)(Z
__future__r   r   r   rG   Zcompose.configr   Zcompose.constr   r3   r   r5   r   r_   r   r9   r	   r<   r
   rA   r   rB   r   r   r!   r$   rH   rI   r[   ZVolumeFromSpecZ
VolumeSpecZSecurityOptZServiceSecretZServiceConfigZServicePortr@   r:   rJ   rW   r'   r   r   r   r   <module>   s6   
%
