3
ûâde4  ã               @   s¢   d Z ddlZddlmZ ddlmZ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 eed
œdd„Zeed dœdd„ƒZG dd„ deƒZdS )zHTTP cache implementation.
é    N)Úcontextmanager)ÚIteratorÚOptional)Ú	BaseCache)Ú	FileCache)ÚResponse)Úadjacent_tmp_fileÚreplace)Ú
ensure_dir)ÚresponseÚreturnc             C   s   t | ddƒS )NÚ
from_cacheF)Úgetattr)r   © r   ú:/tmp/pip-build-red6nood/pip/pip/_internal/network/cache.pyÚis_from_cache   s    r   )r   c               c   s$   y
dV  W n t 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)ÚOSErrorr   r   r   r   Úsuppressed_cache_errors   s    
r   c                   sn   e Zd ZdZeddœ‡ fdd„Zeedœdd„Zeee d	œd
d„Z	eeddœdd„Z
edd	œ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.
    N)Ú	directoryr   c                s$   |d k	st dƒ‚tƒ jƒ  || _d S )Nz!Cache directory must not be None.)ÚAssertionErrorÚsuperÚ__init__r   )Úselfr   )Ú	__class__r   r   r   %   s    
zSafeFileCache.__init__)Únamer   c             C   s4   t j|ƒ}t|d d… ƒ|g }tjj| jf|žŽ S )Né   )r   ÚencodeÚlistÚosÚpathÚjoinr   )r   r   ÚhashedÚpartsr   r   r   Ú_get_cache_path*   s    
zSafeFileCache._get_cache_path)Úkeyr   c             C   s:   | j |ƒ}tƒ �  t|dƒ�
}|jƒ S Q R X W d Q R X d S )NÚrb)r$   r   ÚopenÚread)r   r%   r    Úfr   r   r   Úget2   s    
zSafeFileCache.get)r%   Úvaluer   c             C   sZ   | j |ƒ}tƒ �@ ttjj|ƒƒ t|ƒ�}|j|ƒ W d Q R X t|j	|ƒ W d Q R X d S )N)
r$   r   r
   r   r    Údirnamer   Úwriter	   r   )r   r%   r+   r    r)   r   r   r   Úset8   s    

zSafeFileCache.setc          	   C   s*   | j |ƒ}tƒ � tj|ƒ W d Q R X d S )N)r$   r   r   Úremove)r   r%   r    r   r   r   ÚdeleteB   s    
zSafeFileCache.delete)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   r$   r   Úbytesr*   r.   r0   Ú__classcell__r   r   )r   r   r      s   
r   )r4   r   Ú
contextlibr   Útypingr   r   Zpip._vendor.cachecontrol.cacher   Úpip._vendor.cachecontrol.cachesr   Zpip._vendor.requests.modelsr   Zpip._internal.utils.filesystemr   r	   Zpip._internal.utils.miscr
   Úboolr   r   r   r   r   r   r   Ú<module>   s   
