
    Pe;                         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mZmZ d dlmZ d d	l
mZ d d
lmZ  e j&                  e      Z G d de      Zd Zy)    N)IOBase)ProtocolError)ReadTimeoutError)parsers)set_socket_timeout)IncompleteReadErrorr   ResponseStreamingError)ScalarTypes)XMLParseError)first_non_none_responsec                   ~    e Zd ZdZdZd Zd Zd Zd ZddZ	d	 Z
d
 Zd Zd Zd ZeZedfdZefdZd Zd Zd Zy)StreamingBodyae  Wrapper class for an http response body.

    This provides a few additional conveniences that do not exist
    in the urllib3 model:

        * Set the timeout on the socket (i.e read() timeouts)
        * Auto validation of content length, if the amount of bytes
          we read does not match the content length, an exception
          is raised.

    i   c                 .    || _         || _        d| _        y )Nr   )_raw_stream_content_length_amount_read)self
raw_streamcontent_lengths      3/usr/lib/python3/dist-packages/botocore/response.py__init__zStreamingBody.__init__5   s    %-    c                      y N r   s    r   __del__zStreamingBody.__del__:   s     	r   c                 |    	 t        | j                  |       y# t        $ r t        j	                  dd        w xY w)z&Set the timeout seconds on the socket.zbCannot access the socket object of a streaming response.  It's possible the interface has changed.T)exc_infoN)r   r   AttributeErrorloggererror)r   timeouts     r   r   z StreamingBody.set_socket_timeout@   sF    		t//9 	LL- 	   	s    ";c                 V    	 | j                   j                         S # t        $ r Y yw xY w)NF)r   readabler    r   s    r   r%   zStreamingBody.readableW   s.    	##,,.. 		s    	((Nc                 ,   	 | j                   j                  |      }| xj                  t        |      z  c_        ||s|dkD  r| j                          |S # t        $ r}t        |j                  |      d}~wt
        $ r}t        |      d}~ww xY w)zhRead at most amt bytes from the stream.

        If the amt argument is omitted, read all data.
        )endpoint_urlr"   N)r"   r   )
r   readURLLib3ReadTimeoutErrorr   urlURLLib3ProtocolErrorr	   r   len_verify_content_length)r   amtchunkes       r   r(   zStreamingBody.read]   s    
	2$$))#.E 	SZ';uq '') ' 	@"Q??# 	2(q11	2s#   A 	BA66BBBc                 6    | j                   j                         S r   )r   	readlinesr   s    r   r2   zStreamingBody.readlinesq   s    ))++r   c                 8    | j                  | j                        S )z:Return an iterator to yield 1k chunks from the raw stream.)iter_chunks_DEFAULT_CHUNK_SIZEr   s    r   __iter__zStreamingBody.__iter__t   s     8 899r   c                 T    | j                  | j                        }|r|S t               )z-Return the next 1k chunk from the raw stream.)r(   r5   StopIteration)r   current_chunks     r   __next__zStreamingBody.__next__x   s'    		$":":;  or   c                     | j                   S r   )r   r   s    r   	__enter__zStreamingBody.__enter__   s    r   c                 8    | j                   j                          y r   r   close)r   typevalue	tracebacks       r   __exit__zStreamingBody.__exit__   s     r   Fc              #      K   d}| j                  |      D ];  }||z   j                  d      }|dd D ]  }|j                  |      d     |d   }= |r|j                  |      d    yyw)zReturn an iterator to yield lines from the raw stream.

        This is achieved by reading chunk of bytes (of size chunk_size) at a
        time from the raw stream, and then yielding lines from there.
        r   TNr   )r4   
splitlines)r   
chunk_sizekeependspendingr/   lineslines          r   
iter_lineszStreamingBody.iter_lines   s      %%j1 	 Eu_006Ecr
 3ooh/223BiG		 
 $$X.q11 s   A,A.c              #   D   K   	 | j                  |      }|dk(  ry| w)z\Return an iterator to yield chunks of chunk_size bytes from the raw
        stream.
        r   N)r(   )r   rG   r9   s      r   r4   zStreamingBody.iter_chunks   s/       IIj1M#	 s    c                     | j                   M| j                  t        | j                         k7  r*t        | j                  t        | j                               y y )N)actual_bytesexpected_bytes)r   r   intr   r   s    r   r-   z$StreamingBody._verify_content_length   s\     +0A0AS  F
 1
 &!.."4#7#78 1
+r   c                 6    | j                   j                         S r   )r   tellr   s    r   rS   zStreamingBody.tell   s    $$&&r   c                 8    | j                   j                          y)z*Close the underlying http response stream.Nr>   r   s    r   r?   zStreamingBody.close   s     r   r   )__name__
__module____qualname____doc__r5   r   r   r   r%   r(   r2   r6   r:   r<   rC   nextrL   r4   r-   rS   r?   r   r   r   r   r   &   sk    
 
.(,: ! D$7% 2 &9  
'!r   r   c                 v   | j                   d   }|j                  |j                  d}|d   dk\  r|j                  |d<   nG| j                  r,t        |j                  |d   j                  d            |d<   n|j                  |d<   t        j                  |      }||j                  || j                        fS )Nprotocol)headersstatus_coder]   i,  bodyr\   zcontent-length)metadatar\   r]   contenthas_streaming_outputr   rawgetr   create_parserparseoutput_shape)operation_modelhttp_responser[   response_dictparsers        r   get_responserk      s    ''
3H (($00M ]#s* - 5 5f		-	- -}Y7;;<LM!
f !. 5 5f""8,F&,,33  r   )loggingior   urllib3.exceptionsr   r+   r   r)   botocorer   botocore.compatr   botocore.exceptionsr   r	   r
   r   botocore.hooksr   	getLoggerrU   r!   r   rk   r   r   r   <module>rt      sV      D J  .  ! ) 2 
		8	$K!F K!\r   