
    S`                     :   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j                         Z ej"                  e      Zd Zdd	Zdd
 fdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z d Z!d Z"d fdZ#d dZ$dd fdZ%y)!    N)DockerException)parse_bytes   )StreamParseError)MULTIPLIERS	timeparsec              #   f   K   | D ](  }t        |t              s|j                  dd      }| * yw)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.
    zutf-8replaceN)
isinstancestrdecode)streamdatas     //usr/lib/python3/dist-packages/compose/utils.pystream_as_textr      s6       $$;;w	2D
s   /1c                 b    | j                  t        |            }|dk(  ry | d |dz    | |dz   d  fS )Nr   )findr   )buffer	separatorindexs      r   line_splitterr       s?    KKI'E{*519veaij111    c                     | S N )as    r   <lambda>r   '       ! r   c              #     K   |xs t         }d}t        |       D ]  }||z  }	  ||      }||\  }}|  |r	  ||       yy# t        $ r1}t        j	                  dt        |      z         t        |      d}~ww xY ww)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   	Exceptionlogerrorreprr   )r   splitterdecoderbufferedr   buffer_splititemes           r   split_bufferr-   '   s      (=HHv& D#H-L#)ND(J  	&(##   	&IIh( #1%%	&s(   9B
A B	B,A==BBc                     | j                         } 	 t        j                  |       \  }}| t        j                  j
                  j                  | |      j                         d }||fS # t        $ r Y yw xY w)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_decoder
raw_decodejsonr(   
WHITESPACEmatchend
ValueError)r   objr   rests       r   json_splitterr9   G   sq     \\^F!,,V4
Udll--33FEBFFHIJDy s   AA) )	A54A5c                 @    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-   r9   r0   r   )r   s    r   json_streamr;   T   s    
 |/B/BCCr   c                     t        j                  | ddd       }t        j                         }|j	                  |j                  d             |j                         S )NT),:c                 "    | j                         S r   )r&   xs    r   r   zjson_hash.<locals>.<lambda>]   s    TUTZTZT\ r   )	sort_keys
separatorsdefaultutf8)r2   dumpshashlibsha256updateencode	hexdigest)r7   dumphs      r   	json_hashrN   \   sD    ::cTjJ\]DAHHT[[ !;;=r   c                 $    t        | dz  dz        S )Ni ʚ;i  )int)	time_nanos    r   microseconds_from_time_nanorR   c   s    y:%,--r   c                 ,    t        | t        d   z        S )Nnano)rP   r   )time_secondss    r   nanoseconds_from_time_secondsrV   g   s    |k&1122r   c                      t        | xs d      S Nr"   r   values    r   parse_seconds_floatr[   k   s    U[b!!r   c                 <    t        | xs d      }|y t        |      S rX   )r	   rV   )rZ   parseds     r   parse_nanoseconds_intr^   o   s#    u{#F~(00r   c                 p    | j                         D ci c]  \  }}|t        ||nd       c}}S c c}}w rX   )itemsr   )source_dictkvs      r   build_string_dictrd   v   s3    ;F;L;L;NO41aAs12..OOOs   2c                 `    t        |       dk(  ry| d   dv rd| fS t        j                  |       S )Nr   )r"   r"   ).\/~r"   )lenntpath
splitdrive)paths    r   rl   rl   z   s8    
4yA~Aw''DzT""r   c                 8    	 t        |       S # t        $ r Y y w xY wr   )sdk_parse_bytesr   )ns    r   r   r      s$    q!! s   
 	c                 8    | s| S | d   dk(  r| d   dk(  r| dd S | S )Nr   "r   r   r   )ss    r   unquote_pathrt      s/    ts{qu|2wHr   c                      	 t        t        j                  d            dd } 	 t        t	        |              8# t
        $ r | cY S w xY w)N      r   )hexrandomgetrandbitsrP   truncate_idr6   )vals    r   generate_random_idr}      sJ    
&$$V,-a3	C ! 	J	s   9 AAc                 b    d| v r| | j                  d      dz   d  } t        |       dkD  r| d d S | S )Nr>   r      )r   rj   rY   s    r   r{   r{      s@    
e|ekk#&*+,
5zBSbzLr   c                     | S r   r   r@   s    r   r   r      s    A r   c              #   r   K   t               }| D ]$  } ||      }||vs|j                  |       | & yw)zHList unique elements, preserving order. Remember all elements ever seen.N)setadd)iterablekeyseenelement
unique_keys        r   unique_everseenr      s>     5D \
T!HHZ M	s   77c                 8    t        |       |kD  r| d |dz
   dz   S | S )Nrw   z...)rj   )rs   	max_charss     r   truncate_stringr      s)    
1v	)a- 5((Hr   Fc                     | S r   r   r@   s    r   r   r      r    r   c                 L    |s|s| S | D cg c]  } ||      |v r| c}S c c}w )a  This function contains the logic of choosing which services to
    attach when doing docker-compose up. It may be used both with containers
    and services, and any other entities that map to service names -
    this mapping is provided by item_to_service_name.r   )r`   service_namesattach_dependenciesitem_to_service_namer+   s        r   filter_attached_for_upr      s=     - 1$7=H 	  s   !)
)#   )&rG   json.decoderr2   loggingrk   ry   docker.errorsr   docker.utilsr   ro   errorsr   r	   r   JSONDecoderr0   	getLogger__name__r$   r   r   r-   r9   r;   rN   rR   rV   r[   r^   rd   rl   rt   r}   r{   r   r   r   r   r   r   <module>r      s         ) 7 $ "    t!g!
2 #' &@
D.3"1P# #.  FK0;r   