U
    ]                     @   sP  d dl mZ d dl mZ d dlZd dlZd dlZd dlZd dl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 e ZeeZd
d Zdd Zd5ddZddd fddZdd Zdd Zdd Zdd Z dd Z!dd  Z"d!d" Z#d#d$ Z$d%d& Z%d'd( Zd)d* Z&d+d, Z'd-d. Z(d/d fd0d1Z)d6d3d4Z*dS )7    )absolute_import)unicode_literalsN)DockerException)parse_bytes   )StreamParseError)MULTIPLIERS	timeparsec                 C   s   t jr
| S td| S )Nutf-8)sixZPY3codecs	getwriterstream r   //usr/lib/python3/dist-packages/compose/utils.pyget_output_stream   s    r   c                 c   s,   | D ]"}t |tjs |dd}|V  qdS )zGiven a stream of bytes or text, if any of the items in the stream
    are bytes convert them to text.

    This function can be removed once docker-py returns text streams instead
    of byte streams.
    r   replaceN)
isinstancer   	text_typedecode)r   datar   r   r   stream_as_text   s    r   
c                 C   s<   |  t|}|dkrd S | d |d  | |d d  fS )Nr   )findr   r   )bufferZ	separatorindexr   r   r   line_splitter+   s    r   c                 C   s   | S Nr   )ar   r   r   <lambda>2       r"   c              
   c   s   |pt }td}t| D ].}||7 }||}|dkr8q|\}}|V  q&q|rz||V  W n< tk
r } ztdt|  t|W 5 d}~X Y nX dS )a  Given a generator which yields strings and a splitter function,
    joins all input, splits on the separator and yields each chunk.

    Unlike string.split(), each chunk includes the trailing
    separator, except for the last one if none was found on the end
    of the input.
     Nz?Compose tried decoding the following data chunk, but failed:
%s)	r   r   r   r   	Exceptionlogerrorreprr   )r   ZsplitterdecoderZbufferedr   Zbuffer_splititemer   r   r   split_buffer2   s&    

r,   c                 C   sX   |   } z4t| \}}| tjj| | d }||fW S  tk
rR   Y dS X dS )zAttempt to parse a json object from a buffer. If there is at least one
    object, return it and the rest of the buffer, otherwise return None.
    N)	stripjson_decoderZ
raw_decodejsonr)   
WHITESPACEmatchend
ValueError)r   objr   restr   r   r   json_splitterR   s    
r6   c                 C   s   t | ttjS )zGiven a stream of text, return a stream of json objects.
    This handles streams which are inconsistently buffered (some entries may
    be newline delimited, and others are not).
    )r,   r6   r.   r   r   r   r   r   json_stream_   s    r7   c                 C   s6   t j| dddd d}t }||d | S )NT),:c                 S   s   |   S r    )r(   xr   r   r   r"   h   r#   zjson_hash.<locals>.<lambda>)Z	sort_keysZ
separatorsdefaultutf8)r/   dumpshashlibZsha256updateencodeZ	hexdigest)r4   dumphr   r   r   	json_hashg   s    rD   c                 C   s   t | d d S )Ni ʚ;i  )int)Z	time_nanor   r   r   microseconds_from_time_nanon   s    rF   c                 C   s   t | td  S )NZnano)rE   r   )Ztime_secondsr   r   r   nanoseconds_from_time_secondsr   s    rG   c                 C   s   t | pdS Nr$   r	   valuer   r   r   parse_seconds_floatv   s    rK   c                 C   s    t | pd}|d krd S t|S rH   )r
   rG   )rJ   Zparsedr   r   r   parse_nanoseconds_intz   s    rL   c                 C   s   t dd |  D S )Nc                 s   s*   | ]"\}}|t |d k	r|ndfV  qd S rH   )str).0kvr   r   r   	<genexpr>   s     z$build_string_dict.<locals>.<genexpr>)dictitems)Zsource_dictr   r   r   build_string_dict   s    rT   c                 C   s.   t | dkrdS | d dkr$d| fS t| S )Nr   )r$   r$   ).\/~r$   )lenntpath
splitdrive)pathr   r   r   r[      s
    r[   c                 C   s&   z
t | W S  tk
r    Y d S X d S r    )sdk_parse_bytesr   )nr   r   r   r      s    
r   c                 C   s0   | s| S | d dkr,| d dkr,| dd S | S )Nr   "r   r   r   )sr   r   r   unquote_path   s
    ra   c                  C   sJ   t tddd } ztt|  W q W q  tk
rB   |  Y S X q d S )N      r   )hexrandomZgetrandbitsrE   truncate_idr3   )valr   r   r   generate_random_id   s    rh   c                 C   s:   d| kr| |  dd d  } t| dkr6| d d S | S )Nr9   r      )r   rY   rI   r   r   r   rf      s
    rf   c                 C   s   | S r    r   r:   r   r   r   r"      r#   c                 c   s4   t  }| D ]$}||}||kr
|| |V  q
dS )zHList unique elements, preserving order. Remember all elements ever seen.N)setadd)iterablekeyseenelementZ
unique_keyr   r   r   unique_everseen   s    
rp   #   c                 C   s$   t | |kr | d |d  d S | S )Nrc   z...)rY   )r`   Z	max_charsr   r   r   truncate_string   s    rr   )r   )rq   )+Z
__future__r   r   r   r?   Zjson.decoderr/   ZloggingrZ   re   r   Zdocker.errorsr   Zdocker.utilsr   r]   errorsr   r
   r   ZJSONDecoderr.   Z	getLogger__name__r&   r   r   r   r,   r6   r7   rD   rF   rG   rK   rL   rT   r[   ra   rh   rf   rp   rr   r   r   r   r   <module>   sD   

 

