o
    !§Ñh›  ã                   @   s|   d dl Z d dlZd dlmZ ddlmZ ddlmZ ze W n e	y+   e
efZY nw dd„ ZG dd	„ d	eƒZd
d„ ZdS )é    N)Údedenté   )Ú	BaseCache)ÚCacheControllerc              	   C   s”   t j}|t jt jB O }tt dƒr|t jO }tt dƒr|t jO }zt  | ¡ W n tt	fy1   Y nw t  
| ||¡}zt  |d¡W S    t  |¡ ‚ )NÚ
O_NOFOLLOWÚO_BINARYÚwb)ÚosÚO_WRONLYÚO_CREATÚO_EXCLÚhasattrr   r   ÚremoveÚIOErrorÚOSErrorÚopenÚfdopenÚclose)ÚfilenameÚfmodeÚflagsÚfd© r   úL/usr/lib/python3/dist-packages/pip/_vendor/cachecontrol/caches/file_cache.pyÚ_secure_open_write   s"   



þ
r   c                   @   sN   e Zd Z					ddd„Zedd„ ƒZd	d
„ Zdd„ Zddd„Zdd„ Z	dS )Ú	FileCacheFé€  éÀ  Nc           
      C   sŒ   |d ur|d urt dƒ‚zddlm} ddlm} W n ty)   tdƒ}	t|	ƒ‚w |r/|}n|d u r5|}|| _|| _|| _	|| _
|| _d S )Nz/Cannot use use_dir_lock and lock_class togetherr   )ÚLockFile)ÚMkdirLockFilezª
            NOTE: In order to use the FileCache you must have
            lockfile installed. You can install it via pip:
              pip install lockfile
            )Ú
ValueErrorÚlockfiler   Úlockfile.mkdirlockfiler   ÚImportErrorr   Ú	directoryÚforeverÚfilemodeÚdirmodeÚ
lock_class)
Úselfr$   r%   r&   r'   Úuse_dir_lockr(   r   r   Únoticer   r   r   Ú__init__>   s(   
ÿø
zFileCache.__init__c                 C   s   t  |  ¡ ¡ ¡ S ©N)ÚhashlibÚsha224ÚencodeÚ	hexdigest)Úxr   r   r   r0   e   s   zFileCache.encodec                 C   s6   |   |¡}t|d d… ƒ|g }tjj| jg|¢R Ž S )Né   )r0   Úlistr	   ÚpathÚjoinr$   )r)   ÚnameÚhashedÚpartsr   r   r   Ú_fni   s   
zFileCache._fnc                 C   sZ   |   |¡}zt|dƒ�}| ¡ W  d   ƒ W S 1 sw   Y  W d S  ty,   Y d S w )NÚrb)r:   r   ÚreadÚFileNotFoundError)r)   Úkeyr7   Úfhr   r   r   Úgetp   s   
(ÿÿzFileCache.getc              	   C   s´   |   |¡}zt tj |¡| j¡ W n ttfy   Y nw |  |¡�-}t	|j| j
ƒ�}| |¡ W d   ƒ n1 s;w   Y  W d   ƒ d S W d   ƒ d S 1 sSw   Y  d S r-   )r:   r	   Úmakedirsr5   Údirnamer'   r   r   r(   r   r&   Úwrite)r)   r>   ÚvalueÚexpiresr7   Úlockr?   r   r   r   Úsety   s   
ÿÿþ"þzFileCache.setc                 C   s:   |   |¡}| jszt |¡ W d S  ty   Y d S w d S r-   )r:   r%   r	   r   r=   )r)   r>   r7   r   r   r   Údelete‡   s   
ÿýzFileCache.delete)Fr   r   NNr-   )
Ú__name__Ú
__module__Ú__qualname__r,   Ústaticmethodr0   r:   r@   rG   rH   r   r   r   r   r   <   s    
ù'

	r   c                 C   s   t  | ¡}| |¡S )z\Return the file cache path based on the URL.

    This does not ensure the file exists!
    )r   Ú	cache_urlr:   )ÚurlÚ	filecacher>   r   r   r   Úurl_to_file_path�   s   

rP   )r.   r	   Útextwrapr   Úcacher   Ú
controllerr   r=   Ú	NameErrorr   r   r   r   rP   r   r   r   r   Ú<module>   s   þ)T