U
    «ÃT_Z	  ã                   @   sœ   d 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	 ddl
mZmZ ddlmZ dd	lmZ ertdd
lmZ dd„ Zedd„ ƒZG dd„ deƒZdS )zHTTP cache implementation.
é    N)Úcontextmanager)Ú	BaseCache)Ú	FileCache)ÚResponse)Úadjacent_tmp_fileÚreplace)Ú
ensure_dir)ÚMYPY_CHECK_RUNNING)ÚOptionalc                 C   s   t | ddƒS )NÚ
from_cacheF)Úgetattr)Úresponse© r   ú?/tmp/pip-unpacked-wheel-8u7dkzi1/pip/_internal/network/cache.pyÚis_from_cache   s    r   c                	   c   s(   z
dV  W n t tfk
r"   Y nX dS )zvIf we can't access the cache then we can just skip caching and process
    requests as if caching wasn't enabled.
    N)ÚOSErrorÚIOErrorr   r   r   r   Úsuppressed_cache_errors   s    
r   c                       s@   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )ÚSafeFileCachezw
    A file based cache which is safe to use even when the target directory may
    not be accessible or writable.
    c                    s(   |d k	st dƒ‚tt| ƒ ¡  || _d S )Nz!Cache directory must not be None.)ÚAssertionErrorÚsuperr   Ú__init__Ú	directory)Úselfr   ©Ú	__class__r   r   r   ,   s    zSafeFileCache.__init__c                 C   s4   t  |¡}t|d d… ƒ|g }tjj| jf|žŽ S )Né   )r   ÚencodeÚlistÚosÚpathÚjoinr   )r   ÚnameÚhashedÚpartsr   r   r   Ú_get_cache_path2   s    
zSafeFileCache._get_cache_pathc                 C   sR   |   |¡}tƒ �8 t|dƒ�"}| ¡ W  5 Q R £ W  5 Q R £ S Q R X W 5 Q R X d S )NÚrb)r%   r   ÚopenÚread)r   Úkeyr    Úfr   r   r   Úget;   s    
zSafeFileCache.getc              
   C   sZ   |   |¡}tƒ �@ ttj |¡ƒ t|ƒ�}| |¡ W 5 Q R X t|j	|ƒ W 5 Q R X d S ©N)
r%   r   r   r   r    Údirnamer   Úwriter   r"   )r   r)   Úvaluer    r*   r   r   r   ÚsetB   s    

zSafeFileCache.setc              	   C   s*   |   |¡}tƒ � t |¡ W 5 Q R X d S r,   )r%   r   r   Úremove)r   r)   r    r   r   r   ÚdeleteM   s    
zSafeFileCache.delete)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r+   r0   r2   Ú__classcell__r   r   r   r   r   &   s   	r   )r6   r   Ú
contextlibr   Úpip._vendor.cachecontrol.cacher   Úpip._vendor.cachecontrol.cachesr   Úpip._vendor.requests.modelsr   Úpip._internal.utils.filesystemr   r   Úpip._internal.utils.miscr   Úpip._internal.utils.typingr	   Útypingr
   r   r   r   r   r   r   r   Ú<module>   s   

