o
    0PjX                     @   sv   d dl mZ d dlZG dd deZG dd deZG dd deZG d	d
 d
eZdefddZG dd deZ	dS )    )ProtocolNc                   @      e Zd ZdZdS )DecodeRatioErrorzBOutput-to-input amplification ratio exceeded the configured limit.N__name__
__module____qualname____doc__ r
   r
   1/usr/lib/python3/dist-packages/httplib2/decode.pyr          r   c                   @   r   )DecodeLimitErrorz,Total output length exceeded the hard limit.Nr   r
   r
   r
   r   r   	   r   r   c                   @   sV   e Zd ZedefddZdedefddZdefddZdd
ede	defddZ
dS )DecoderProtocolreturnc                 C      d S Nr
   selfr
   r
   r   needs_input   s   zDecoderProtocol.needs_inputbc                 C   r   r   r
   r   r   r
   r
   r   decode      zDecoderProtocol.decodec                 C   r   r   r
   r   r
   r
   r   flush   r   zDecoderProtocol.flush   data
chunk_sizec                 C   sb   t  }|dkrt|}tdt||D ]}||||  }|| | q||   t|S )Nr   )	bytearraylenrangeextendr   r   bytes)r   r   r   outichunkr
   r
   r   consume_bytes   s   zDecoderProtocol.consume_bytesN)r   )r   r   r   propertyboolr   r!   r   r   intr%   r
   r
   r
   r   r      s    r   c                   @   sh   e Zd ZdZdZdZdZdZdZefde	fdd	Z
ed
efddZded
efddZd
efddZdS )ZlibDecodera#  
    Thin wrapper around zlib.Decompressor conforming to the Decoder interface.

    Note: zlib pushes all available decompressed data immediately upon receiving
    input. It never holds back output requiring `decode(b"")` to extract it.
    Thus, `needs_input` naturally remains True.
    )_decoderi      /   wbitsc                 C   s   t || _d S r   )zlibdecompressobjr*   )r   r.   r
   r
   r   __init__3   s   zZlibDecoder.__init__r   c                 C   s   | j d u r	td| j j S Nzused after flush())r*   RuntimeErroreofr   r
   r
   r   r   6   s   

zZlibDecoder.needs_inputr   c                 C   s   | j d u r	td| j |S r2   )r*   r3   
decompressr   r
   r
   r   r   <   s   
zZlibDecoder.decodec                 C   s&   | j d u r	td| j  }d | _ |S r2   )r*   r3   r   )r   resultr
   r
   r   r   A   s
   

zZlibDecoder.flushN)r   r   r   r	   	__slots__WBITS_DEFLATE
WBITS_ZLIB
WBITS_GZIPWBITS_AUTO_GZIP_ZLIBr(   r1   r&   r'   r   r!   r   r   r
   r
   r
   r   r)   #   s    r)   r   c                   C   s
   t t jS r   )r)   r8   r
   r
   r
   r   DeflateDecoderI   s   
r<   c                   @   s   e Zd ZdZ				ddededed	ed
eddfddZdddZe	de
fddZdedefddZdefddZdedefddZdS )LimitDecoder)	r*   _ratio_chunk_size_safe_limit_hard_limit_consumed_length_output_length_input_buffer_flushedd   r             
 decoderratior   
safe_limit
hard_limitr   Nc                 C   s   |dk rt d| d|dk rt d| d|dk r$t d| d|dk r0t d| d|| _|| _|| _|| _|| _d| _d| _t | _	d| _
d S )Nr   zLimitDecoder() ratio=z expected >= 0zLimitDecoder() chunk_size=zLimitDecoder() safe_limit=F)
ValueErrorr*   r>   r?   r@   rA   rB   rC   r   rD   rE   )r   rI   rJ   r   rK   rL   r
   r
   r   r1   Z   s"   
zLimitDecoder.__init__c              	   C   s   | j dkr| j| j krtd| j d| j  | jdkr$| j| jk r$d S | jdkrU| j| j| j krW| jdkr=| j| j ntd}td|dd| j d| j d	| j d S d S )
Nr   zOutput length z exceeds hard limit infzAmplification ratio z.1fz (/z) exceeds limit )rA   rC   r   r@   r>   rB   floatr   )r   actual_ratior
   r
   r   _check_limitsu   s   zLimitDecoder._check_limitsc                 C   s   | j jS r   )r*   r   r   r
   r
   r   r      s   zLimitDecoder.needs_inputr   c                 C   s    | j rtd| |}t|S )Nzdecode() called after flush())rE   r3   _pumpr!   )r   r   outputr
   r
   r   r      s   
zLimitDecoder.decodec                 C   sT   | j rtdd| _ | d}| j }|| |  jt|7  _|   t	|S )Nzflush() called more than onceT    )
rE   r3   rS   r*   r   r    rC   r   rR   r!   )r   rT   r   r
   r
   r   r      s   


zLimitDecoder.flushc                 C   s   | j | t }	 | jjs)| jd}|r)|| |  jt|7  _|   q	| j rat	| j d | j
 }| j d | j
= | j|}|  jt|7  _|r`|| |  jt|7  _|   q		 |S )NTrU   )rD   r    r   r*   r   r   rC   r   rR   r!   r?   rB   )r   r   rT   r   r$   r
   r
   r   rS      s,   

zLimitDecoder._pump)rF   r   rG   rH   )r   N)r   r   r   r7   r   rP   r(   r1   rR   r&   r'   r   r!   r   r   r   rS   r
   r
   r
   r   r=   M   s2    

r=   )
typingr   r/   	Exceptionr   r   r   r)   r<   r=   r
   r
   r
   r   <module>   s    &