
                         n    S SK r S SKr SSKJr  \ R                  " 5       rS rS rS rS
S jr	SS 4S	 jr
g)    N   )StreamParseErrorc              #   t   #    U  H.  n[        U[        5      (       d  UR                  SS5      nUv   M0     g7f)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datas     +lib/third_party/docker/utils/json_stream.pystream_as_textr   
   s3      $$$;;w	2D
 s   68c                     U R                  5       n  [        R                  U 5      u  pU [        R                  R
                  R                  X5      R                  5       S nX4$ ! [         a     gf = f)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jsondecoder
WHITESPACEmatchend
ValueError)bufferobjindexrests       r   json_splitterr      sk     \\^F!,,V4
dll--33FBFFHIJy s   AA& &
A32A3c                 @    [        U [        [        R                  5      $ )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
   s    r   json_streamr   $   s    
 |/B/BCC    c                 b    U R                  [        U5      5      nUS:X  a  g U S US-    XS-   S  4$ )N   )findr   )r   	separatorr   s      r   line_splitterr&   ,   s=    KKI'E{*519vaij111r    c                     U $ )N )as    r   <lambda>r*   3   s    !r    c              #      #    U=(       d    [         nSn[        U 5       H  nX4-  n U" U5      nUc  M  Uu  pcUv   M     U(       a   U" U5      v   gg! [         a  n[        U5      eSnAff = f7f)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.
 N)r&   r   	Exceptionr   )r
   splitterr   bufferedr   buffer_splititemes           r   r   r   3   s      (=HHv&#H-L#)NDJ  ' 	&(##   	&"1%%	&s*   AA1
A A1
A.A))A..A1)
)r   json.decodererrorsr   JSONDecoderr   r   r   r   r&   r   r(   r    r   <module>r7      s?      % !

D2 #' &r    