o
    2.a                     @   s\   d dl Z d dlZ ddlmZ e  Zdd Zdd Zdd	 ZdddZ	ddd fddZ
dS )    N   )StreamParseErrorc                 c   s,    | D ]}t |ts|dd}|V  qdS )z
    Given 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 we return text streams
    instead of byte streams.
    zutf-8replaceN)
isinstancestrdecode)streamdata r
   :/usr/lib/python3/dist-packages/docker/utils/json_stream.pystream_as_text
   s   
r   c                 C   sR   |   } zt| \}}| tjj| | d }||fW S  ty(   Y dS 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_decoderZ
raw_decodejsondecoder
WHITESPACEmatchend
ValueError)bufferobjindexrestr
   r
   r   json_splitter   s   
r   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).
    )split_bufferr   r   r   )r   r
   r
   r   json_stream$   s   r   
c                 C   s:   |  t|}|dkrd S | d |d  | |d d  fS )N   )findr   )r   Z	separatorr   r
   r
   r   line_splitter,   s    r    c                 C   s   | S )Nr
   )ar
   r
   r   <lambda>3   s    r"   c              
   c   s    |pt }d}t| D ]}||7 }	 ||}|du rn|\}}|V  qq|r?z||V  W dS  ty> } zt|d}~ww 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.
     TN)r    r   	Exceptionr   )r   Zsplitterr   Zbufferedr	   Zbuffer_splititemer
   r
   r   r   3   s*   r   )r   )r   Zjson.decodererrorsr   ZJSONDecoderr   r   r   r   r    r   r
   r
   r
   r   <module>   s    
