3
ûâde  ã               @   s\   d dl Z d dlZd dlZd dlmZ ddlmZ ddlmZ ddl	m
Z
 G dd„ deƒZdS )	é    N)ÚHTTPAdapteré   )ÚCacheController)Ú	DictCache)ÚCallbackFileWrapperc                   sN   e Zd ZddhZd‡ fdd„	Zd‡ fdd„	Zd‡ fd
d„	Z‡ fdd„Z‡  ZS )ÚCacheControlAdapterÚPUTÚDELETENTc       
         sT   t t| ƒj||Ž |d kr tƒ n|| _|| _|p2d| _|p<t}	|	| j||d�| _d S )NÚGET)Úcache_etagsÚ
serializer)r
   )	Úsuperr   Ú__init__r   ÚcacheÚ	heuristicÚcacheable_methodsr   Ú
controller)
Úselfr   r   Úcontroller_classr   r   r   ÚargsÚkwÚcontroller_factory)Ú	__class__© ú?/tmp/pip-build-red6nood/pip/pip/_vendor/cachecontrol/adapter.pyr      s    
zCacheControlAdapter.__init__c                s€   |p| j }|j|krhy| jj|ƒ}W n tjk
r>   d}Y nX |rT| j||dd�S |jj| jj	|ƒƒ t
t| ƒj|f|Ž}|S )z”
        Send a request. Use the request information to see if it
        exists in the cache and cache the response if we need to and can.
        NT)Ú
from_cache)r   Úmethodr   Úcached_requestÚzlibÚerrorÚbuild_responseÚheadersÚupdateÚconditional_headersr   r   Úsend)r   Úrequestr   r   Ú	cacheableÚcached_responseÚresp)r   r   r   r$   $   s    


zCacheControlAdapter.sendFc       
         s  |p| j }| rÆ|j|krÆ| jr,| jj|ƒ}|jdkrj| jj||ƒ}||k	rPd}|jdd� |jƒ  |}n\|jdkr„| jj	||ƒ nBt
|jtj| jj	||ƒƒ|_|jrÆ|j‰ ‡ fdd„}tj||ƒ|_tt| ƒj||ƒ}|j| jkoè|j�r| jj|jƒ}	| jj|	ƒ ||_|S )z¥
        Build a response by making a request or using the cache.

        This will end up calling send and returning a potentially
        cached response
        i0  TF)Údecode_contenti-  c                s   ˆ ƒ  | j dkr| jjƒ  d S )Nr   )Ú
chunk_leftÚ_fpÚ_close)r   )Úsuper_update_chunk_lengthr   r   Ú_update_chunk_lengthn   s    
z@CacheControlAdapter.build_response.<locals>._update_chunk_length)r   r   r   ÚapplyÚstatusr   Úupdate_cached_responseÚreadÚrelease_connÚcache_responser   r+   Ú	functoolsÚpartialÚchunkedr.   ÚtypesÚ
MethodTyper   r   r    Úinvalidating_methodsÚokÚ	cache_urlÚurlr   Údeleter   )
r   r%   Úresponser   r   r&   r'   r.   r(   r<   )r   )r-   r   r    9   s:    	



z"CacheControlAdapter.build_responsec                s   | j jƒ  tt| ƒjƒ  d S )N)r   Úcloser   r   )r   )r   r   r   r@   ƒ   s    
zCacheControlAdapter.close)NTNNNN)N)FN)	Ú__name__Ú
__module__Ú__qualname__r:   r   r$   r    r@   Ú__classcell__r   r   )r   r   r      s        Ir   )r8   r5   r   Úpip._vendor.requests.adaptersr   r   r   r   r   Úfilewrapperr   r   r   r   r   r   Ú<module>   s   