o
    <&ßaE  ã                   @   s–   d dl 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 d dlmZ d dlmZ d d	lmZ e  e¡ZG d
d„ deƒZdd„ ZdS )é    N)Úset_socket_timeout)ÚIncompleteReadErrorÚReadTimeoutErrorÚResponseStreamingError)r   )ÚProtocolError)Úparsers)ÚScalarTypes)ÚXMLParseError)Úfirst_non_none_responsec                   @   sh   e Zd ZdZdZdd„ Zdd„ Zddd	„Zd
d„ Zdd„ Z	e	Z
ddd„Zefdd„Zdd„ Zdd„ ZdS )Ú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.

    é   c                 C   s   || _ || _d| _d S )Nr   )Ú_raw_streamÚ_content_lengthÚ_amount_read)ÚselfÚ
raw_streamÚcontent_length© r   ú3/usr/lib/python3/dist-packages/botocore/response.pyÚ__init__0   s   
zStreamingBody.__init__c                 C   s2   z	t | j|ƒ W dS  ty   tjddd� ‚ 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   Útimeoutr   r   r   r   5   s   þüz StreamingBody.set_socket_timeoutNc              
   C   sŠ   z| j  |¡}W n# ty } zt|j|d�‚d}~w ty+ } zt|d�‚d}~ww |  jt|ƒ7  _|du s?|sC|dkrC|  	¡  |S )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Úer   r   r   r   L   s   €
€ÿzStreamingBody.readc                 C   s   |   | j¡S )zCReturn an iterator to yield 1k chunks from the raw stream.
        )Úiter_chunksÚ_DEFAULT_CHUNK_SIZE©r   r   r   r   Ú__iter__`   s   zStreamingBody.__iter__c                 C   s   |   | j¡}|r
|S tƒ ‚)z6Return the next 1k chunk from the raw stream.
        )r   r&   ÚStopIteration)r   Úcurrent_chunkr   r   r   Ú__next__e   s   zStreamingBody.__next__Fc                 c   sj   � d}|   |¡D ]}||  d¡}|dd… D ]
}| |¡d V  q|d }q|r3| |¡d V  dS dS )zÓReturn 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.
        ó    TNéÿÿÿÿr   )r%   Ú
splitlines)r   Ú
chunk_sizeÚkeependsÚpendingr#   ÚlinesÚliner   r   r   Ú
iter_lineso   s   €
ÿzStreamingBody.iter_linesc                 c   s"   � 	 |   |¡}|dkrdS |V  q)z\Return an iterator to yield chunks of chunk_size bytes from the raw
        stream.
        Tr,   N)r   )r   r/   r*   r   r   r   r%   ~   s   €
üzStreamingBody.iter_chunksc                 C   s6   | j d ur| jt| j ƒkrt| jt| j ƒd�‚d S d S )N)Úactual_bytesÚexpected_bytes)r   r   Úintr   r'   r   r   r   r!   ˆ   s   
þþz$StreamingBody._verify_content_lengthc                 C   s   | j  ¡  dS )z*Close the underlying http response stream.N)r   Úcloser'   r   r   r   r8   ’   s   zStreamingBody.close)N)r   F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   r   r   r   r(   r+   Únextr4   r%   r!   r8   r   r   r   r   r   "   s    



r   c                 C   sx   | j d }|j|jdœ}|d dkr|j|d< n| jr)t|j|d  d¡ƒ|d< n|j|d< t 	|¡}|| 
|| 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Úparserr   r   r   Úget_response—   s   
þ
ÿ

ÿrN   )ÚloggingÚbotocore.compatr   Úbotocore.exceptionsr   r   r   Úurllib3.exceptionsr   r   r   Úbotocorer   r   r	   Úbotocore.hooksr
   Ú	getLoggerr9   r   Úobjectr   rN   r   r   r   r   Ú<module>   s   
u