3
ãde3+  ã               @   s˜   d dl Z 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 d dlmZ d dlmZ d dlmZ G d	d
„ d
eƒZdS )é    N)Úcontextmanager)Úmd5)ÚPath)Úsleep)Útime)Ú	BaseCache)ÚFileSystemSerializerc               @   s²  e Zd ZdZdZdZeƒ Zdddefe	e
e
e
ejdœdd	„Zee
d
œdd„ƒZd;eje
 eje
 ddœdd„Zeje
 e
dœdd„Ze	edœdd„Zeje	ddf d
œdd„Zed
œdd„Zeddœdd„Zed
œdd„Zdd
œd d!„Zed
œd"d#„Ze	e	d$œd%d&„Ze	ejd$œd'd(„Zd<e	ejeje
 ed)œd*d+„Zd=e	ejeje
 eed-œd.d/„Z d>e	eed0œd1d2„Z!e	ed$œd3d4„Z"ej#ejejejd5œd6d7„Z$e%e	e	ejd8œd9d:„ƒZ&dS )?Ú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
    :param hash_method: Default hashlib.md5. The hash method used to
                        generate the filename for cached results.
    z.__wz_cacheZ__wz_cache_countiô  i,  i€  )Ú	cache_dirÚ	thresholdÚdefault_timeoutÚmodeÚhash_methodc             C   sŒ   t j| |ƒ || _|| _|| _|| _ytj| jƒ W n0 tk
rd } z|j	t	j
krT‚ W Y d d }~X nX | jdkrˆ| jtt| jƒ ƒƒd� d S )Nr   )Úvalue)r   Ú__init__Ú_pathÚ
_thresholdÚ_hash_methodÚ_modeÚosÚmakedirsÚOSErrorÚerrnoÚEEXISTÚ_update_countÚlenÚlistÚ	_list_dir)Úselfr
   r   r   r   r   Úex© r    úD/var/www/agendate/envp3/lib/python3.6/site-packages/cachelib/file.pyr   *   s    
zFileSystemCache.__init__)Úreturnc             C   s   | j | jƒpdS )Nr   )ÚgetÚ_fs_count_file)r   r    r    r!   Ú_file_countC   s    zFileSystemCache._file_countN)Údeltar   r"   c             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   r&   r   Ú	new_countr    r    r!   r   G   s    
zFileSystemCache._update_count)Útimeoutr"   c             C   s*   t j| |ƒ}|dkr"ttƒ ƒ| }t|ƒS )Nr   )r   Ú_normalize_timeoutÚintr   )r   r*   r    r    r!   r+   S   s    z"FileSystemCache._normalize_timeout)Únamer"   c             C   s,   | j | jƒjtjƒd }||kp*|j| jƒS )Né   éÿÿÿÿ)Ú_get_filenamer$   Úsplitr   ÚsepÚendswithÚ_fs_transaction_suffix)r   r-   Zfshashr    r    r!   Ú_is_mgmtY   s    zFileSystemCache._is_mgmtc                s   ‡ fdd„t jˆ jƒD ƒS )z2return a list of (fully qualified) cache filenamesc             3   s(   | ] }ˆ j |ƒstjjˆ j|ƒV  qd S )N)r5   r   ÚpathÚjoinr   )Ú.0Úfn)r   r    r!   ú	<genexpr>`   s   z,FileSystemCache._list_dir.<locals>.<genexpr>)r   Úlistdirr   )r   r    )r   r!   r   ]   s    
zFileSystemCache._list_dirc             C   s   | j dko| j| j kS )Nr   )r   r%   )r   r    r    r!   Ú_over_thresholde   s    zFileSystemCache._over_threshold)Únowr"   c             C   s    xš| j ƒ D ]Ž}yN| j|dƒ�}| jj|ƒ}W d Q R X |dkrZ||k rZtj|ƒ | jdd� W q
 tk
rp   Y q
 tt	fk
r–   t
jd|dd� Y q
X q
W d S )	NÚrbr   r.   )r&   z/Exception raised while handling cache file '%s'T)Úexc_infor/   )r   Ú_safe_stream_openÚ
serializerÚloadr   Úremover   ÚFileNotFoundErrorr   ÚEOFErrorÚloggingÚwarning)r   r=   ÚfnameÚfÚexpiresr    r    r!   Ú_remove_expiredh   s    
zFileSystemCache._remove_expiredc             C   s  g }x~| j ƒ D ]r}y2| j|dƒ�}|j| jj|ƒ|fƒ W d Q R X W q tk
rX   Y q ttfk
r~   tj	d|dd� Y qX qW dd„ t
|dd„ d	�D ƒ}xh|D ]`}ytj|ƒ | jdd� W n6 tk
rÖ   Y n$ tk
rø   tj	d|dd� dS X | jƒ s¤P q¤W dS )Nr>   z/Exception raised while handling cache file '%s'T)r?   c             s   s   | ]\}}|V  qd S )Nr    )r8   Ú_rH   r    r    r!   r:   ˆ   s   z0FileSystemCache._remove_older.<locals>.<genexpr>c             S   s   | d S )Nr   r    )Úitemr    r    r!   Ú<lambda>Š   s    z/FileSystemCache._remove_older.<locals>.<lambda>)Úkeyr.   )r&   Fr/   )r   r@   ÚappendrA   rB   rD   r   rE   rF   rG   Úsortedr   rC   r   r<   )r   Zexp_fname_tuplesrH   rI   Zfname_sortedr    r    r!   Ú_remove_oldery   s<    $

zFileSystemCache._remove_olderc             C   s,   | j ƒ rtƒ }| j|ƒ | j ƒ r(| jƒ  d S )N)r<   r   rK   rR   )r   r=   r    r    r!   Ú_prunež   s
    
zFileSystemCache._prunec             C   s~   xlt | jƒ ƒD ]\\}}ytj|ƒ W q tk
r8   Y q tk
rh   tjd|dd� | j| d� dS X qW | jdd� dS )Nz/Exception raised while handling cache file '%s'T)r?   )r&   Fr   )r   )	Ú	enumerater   r   rC   rD   r   rF   rG   r   )r   ÚirH   r    r    r!   Úclear¦   s    
zFileSystemCache.clear)rO   r"   c             C   s2   t |tƒr"|jdƒ}| j|ƒjƒ }tjj| j|ƒS )Nzutf-8)	Ú
isinstanceÚstrÚencoder   Ú	hexdigestr   r6   r7   r   )r   rO   ZbkeyZ	bkey_hashr    r    r!   r0   ·   s    

zFileSystemCache._get_filenamec             C   s�   | j |ƒ}yF| j|dƒ�0}| jj|ƒ}|dks8|tƒ krD| jj|ƒS W d Q R X W n: tk
rd   Y n( ttfk
rŠ   tj	d|dd� Y nX d S )Nr>   r   z/Exception raised while handling cache file '%s'T)r?   )
r0   r@   rA   rB   r   rD   r   rE   rF   rG   )r   rO   ÚfilenamerI   Úpickle_timer    r    r!   r#   ½   s    
zFileSystemCache.get)rO   r   r*   r"   c             C   s(   | j |ƒ}tjj|ƒs$| j|||ƒS dS )NF)r0   r   r6   Úexistsr(   )r   rO   r   r*   r[   r    r    r!   ÚaddÎ   s    
zFileSystemCache.addF)rO   r   r*   r'   r"   c             C   sú   |r
d}n| j ƒ  | j|ƒ}| j|ƒ}tjj|ƒ}y~tj| j| j	d�\}}tj
|dƒ�"}	| jj||	ƒ | jj||	ƒ W d Q R X | jtj||ƒ | jtj|| jƒ t|ƒjƒ j}
W n$ tk
rÔ   tjd|dd� dS X | rî| rî| jdd	� |
dkS d S )
Nr   )ÚsuffixÚdirÚwbz/Exception raised while handling cache file '%s'T)r?   Fr.   )r&   )rS   r+   r0   r   r6   ÚisfileÚtempfileÚmkstempr4   r   ÚfdopenrA   ÚdumpÚ_run_safelyÚreplaceÚchmodr   r   ÚstatÚst_sizer   rF   rG   r   )r   rO   r   r*   r'   r[   Ú	overwriteÚfdÚtmprI   Úfsizer    r    r!   r(   Ô   s0    

zFileSystemCache.set)rO   r'   r"   c             C   sb   yt j| j|ƒƒ W n4 tk
r(   dS  tk
rH   tjddd� dS X |sZ| jdd� dS d S )NTz*Exception raised while handling cache file)r?   Fr.   )r&   r/   )r   rC   r0   rD   r   rF   rG   r   )r   rO   r'   r    r    r!   Údeleteÿ   s    zFileSystemCache.deletec             C   sŒ   | j |ƒ}yB| j|dƒ�,}| jj|ƒ}|dks8|tƒ kr<dS dS W d Q R X W n: tk
r`   dS  ttfk
r†   tj	d|dd� dS X d S )Nr>   r   TFz/Exception raised while handling cache file '%s')r?   )
r0   r@   rA   rB   r   rD   r   rE   rF   rG   )r   rO   r[   rI   r\   r    r    r!   Úhas  s    
zFileSystemCache.has)r9   ÚargsÚkwargsr"   c             O   sx   t jƒ dkrjd}d}d}d}xV||k rfy|||Ž}W n, tk
r`   t|ƒ ||7 }|d9 }Y qX P qW n
|||Ž}|S )ztOn Windows os.replace, os.chmod and open can yield
        permission errors if executed by two different processes.ÚWindowsNgü©ñÒMbP?g      $@g        é   )ÚplatformÚsystemÚPermissionErrorr   )r   r9   rr   rs   ÚoutputZ	wait_stepZmax_sleep_timeZtotal_sleep_timer    r    r!   rg      s    

zFileSystemCache._run_safely)r6   r   r"   c          
   c   s4   | j t||ƒ}|d krt‚z
|V  W d |jƒ  X d S )N)rg   Úopenr   Úclose)r   r6   r   Úfsr    r    r!   r@   7  s    
z!FileSystemCache._safe_stream_open)NN)N)NF)F)'Ú__name__Ú
__module__Ú__qualname__Ú__doc__r4   r$   r   rA   r   rX   r,   Ú_tÚAnyr   Úpropertyr%   ÚOptionalr   r+   Úboolr5   Ú	Generatorr   r<   ÚfloatrK   rR   rS   rV   r0   r#   r^   r(   rp   rq   ÚCallablerg   r   r@   r    r    r    r!   r	      s>   
%
 %r	   )r   rF   r   rv   rc   Útypingr�   Ú
contextlibr   Úhashlibr   Úpathlibr   r   r   Zcachelib.baser   Zcachelib.serializersr   r	   r    r    r    r!   Ú<module>   s   