U
    9a!                     @   sl   d dl Z d dlZ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)md5)Path)time)	BaseCachec                   @   s   e Zd ZdZdZdZd+ddZed	d
 Zd,ddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd-d"d#Zd.d%d&Zd/d'd(Zd)d* ZdS )0FileSystemCachea+  A cache that stores the items on the file system.  This cache depends
    on being the only user of the `cache_dir`.  Make absolutely sure that
    nobody but this cache stores files there or otherwise the cache will
    randomly delete files therein.

    :param cache_dir: the directory where cache files are stored.
    :param threshold: the maximum number of items the cache stores before
                      it starts deleting some. A threshold value of 0
                      indicates no threshold.
    :param default_timeout: the default timeout that is used if no timeout is
                            specified on :meth:`~BaseCache.set`. A timeout of
                            0 indicates that the cache never expires.
    :param mode: the file mode wanted for the cache files, default 0600
    z.__wz_cacheZ__wz_cache_count  ,    c              
   C   s   t | | || _|| _|| _zt| j W n0 tk
r^ } z|jtj	krN W 5 d }~X Y nX | jdkr| j
tt|  d d S )Nr   value)r   __init___path
_threshold_modeosmakedirsOSErrorerrnoEEXIST_update_countlenlist	_list_dir)self	cache_dir	thresholdZdefault_timeoutmodeex r   1/tmp/pip-unpacked-wheel-bx08cqbu/cachelib/file.pyr   "   s    
zFileSystemCache.__init__c                 C   s   |  | jpdS Nr   )get_fs_count_filer   r   r   r   _file_count3   s    zFileSystemCache._file_countNc                 C   s<   | j dkrd S |r| j| }n|p$d}| j| j|dd d S )Nr   T)mgmt_element)r   r$   setr"   )r   deltar   Z	new_countr   r   r   r   7   s    
zFileSystemCache._update_countc                 C   s&   t | |}|dkrt | }t|S r    )r   _normalize_timeoutr   int)r   timeoutr   r   r   r(   A   s    
z"FileSystemCache._normalize_timeoutc                 C   s,   |  | jtjd }||kp*|| jS )N)_get_filenamer"   splitr   sependswith_fs_transaction_suffix)r   nameZfshashr   r   r   _is_mgmtG   s    zFileSystemCache._is_mgmtc                    s    fddt  jD S )z2return a list of (fully qualified) cache filenamesc                 3   s(   | ] }  |stj j|V  qd S N)r2   r   pathjoinr   ).0fnr#   r   r   	<genexpr>M   s   
z,FileSystemCache._list_dir.<locals>.<genexpr>)r   listdirr   r#   r   r#   r   r   K   s    

zFileSystemCache._list_dirc                 C   s   | j dko| j| j kS r    )r   r$   r#   r   r   r   _over_thresholdS   s    zFileSystemCache._over_thresholdc              
   C   s   |   D ]x}zJt|d}t|}W 5 Q R X |dkrT||k rTt| | jdd W q ttfk
r~   t	j
d|dd Y qX qd S )Nrbr   r+   r'   /Exception raised while handling cache file '%s'Texc_info)r   openpickleloadr   remover   r   EOFErrorloggingwarning)r   nowfnamefexpiresr   r   r   _remove_expiredV   s    
zFileSystemCache._remove_expiredc              
   C   s   g }|   D ]\}z.t|d}|t||f W 5 Q R X W q ttfk
rf   tjd|dd Y qX qdd t	|dd d	D }|D ]T}zt
| | jd
d W n( tk
r   tjd|dd Y  dS X |  s qqd S )Nr;   r=   Tr>   c                 s   s   | ]\}}|V  qd S r3   r   )r6   _rH   r   r   r   r8   q   s    z0FileSystemCache._remove_older.<locals>.<genexpr>c                 S   s   | d S r    r   )itemr   r   r   <lambda>r       z/FileSystemCache._remove_older.<locals>.<lambda>)keyr+   r<   F)r   r@   appendrA   rB   r   rD   rE   rF   sortedr   rC   r   r:   )r   Zexp_fname_tuplesrH   rI   Zfname_sortedr   r   r   _remove_oldere   s6    "

zFileSystemCache._remove_olderc                 C   s,   |   rt }| | |   r(|   d S r3   )r:   r   rK   rS   )r   rG   r   r   r   _prune   s
    
zFileSystemCache._prunec              	   C   sl   t |  D ]N\}}zt| W q tk
rX   tjd|dd | j| d Y  dS X q| jdd dS )Nr=   Tr>   r<   Fr   r
   )	enumerater   r   rC   r   rE   rF   r   )r   irH   r   r   r   clear   s    zFileSystemCache.clearc                 C   s0   t |tr |d}t| }tj| j|S )Nzutf-8)	
isinstancestrencoder   	hexdigestr   r4   r5   r   )r   rP   Zkey_hashr   r   r   r,      s    

zFileSystemCache._get_filenamec              
   C   s   |  |}z^t|dJ}t|}|dks4|t krLt|W  5 Q R  W S W 5 Q R  W d S W 5 Q R X W n. tttjfk
r   tj	d|dd Y d S X d S )Nr;   r   r=   Tr>   )
r,   r@   rA   rB   r   r   rD   PickleErrorrE   rF   r   rP   filenamerI   Zpickle_timer   r   r   r!      s    

zFileSystemCache.getc                 C   s(   |  |}tj|s$| |||S dS )NF)r,   r   r4   existsr&   )r   rP   r   r*   r^   r   r   r   add   s    
zFileSystemCache.addFc              	   C   s   |r
d}n|    | |}| |}tj|}zxtj| j| j	d\}}t
|d$}	t||	d t||	tj W 5 Q R X t|| t|| j t| j}
W n& tk
r   tjd|dd Y dS X |s|s| jdd	 |
dkS d S )
Nr   )suffixdirwb   r=   Tr>   Fr<   )rT   r(   r,   r   r4   isfiletempfilemkstempr0   r   fdopenrA   dumpHIGHEST_PROTOCOLreplacechmodr   r   statst_sizer   rE   rF   r   )r   rP   r   r*   r%   r^   	overwritefdtmprI   fsizer   r   r   r&      s6    

 
zFileSystemCache.setc                 C   sR   zt | | W n$ tk
r8   tjddd Y dS X |sJ| jdd dS d S )Nz*Exception raised while handling cache fileTr>   Fr+   r<   )r   rC   r,   r   rE   rF   r   )r   rP   r%   r   r   r   delete   s    zFileSystemCache.deletec              
   C   s   |  |}zVt|dB}t|}|dks4|t krDW 5 Q R  W dS W 5 Q R  W dS W 5 Q R X W nB tk
rv   Y dS  tttjfk
r   t	j
d|dd Y dS X d S )Nr;   r   TFr=   r>   )r,   r@   rA   rB   r   FileNotFoundErrorr   rD   r\   rE   rF   r]   r   r   r   has   s     

zFileSystemCache.has)r   r   r	   )NN)N)NF)F)__name__
__module____qualname____doc__r0   r"   r   propertyr$   r   r(   r2   r   r:   rK   rS   rT   rW   r,   r!   r`   r&   rs   ru   r   r   r   r   r      s*   





#
r   )r   rE   r   rA   rf   hashlibr   pathlibr   r   Zcachelib.baser   r   r   r   r   r   <module>   s   