U
    yca                     @   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)	Generator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-0we0fmvo/pip/pip/_internal/network/cache.pyis_from_cache   s    r   )NNN)r   c                   c   s$   z
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                       sv   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	dee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   || _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 |}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   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   r&   r!   fr   r   r   get2   s    
zSafeFileCache.get)r&   valueexpiresr   c              
   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&   r,   r-   r!   r*   r   r   r   set8   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   deleteB   s    
zSafeFileCache.delete)N)__name__
__module____qualname____doc__strr   r%   r   bytesr+   intr1   r3   __classcell__r   r   r   r   r      s   
r   )r7   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   
