U
    #FZh                     @  s   d dl mZ d dlZd dl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mZ d dlmZ e	rd dlmZ d d	lmZ G d
d dZG dd deeZG dd deeZddddddZdS )    )annotationsN)dedent)IOTYPE_CHECKING)Path)	BaseCacheSeparateBodyBaseCache)CacheController)datetime)BaseFileLockc                   @  s   e Zd ZdZd$ddddd	d
dddZedddddZdddddZdddddZd%dddd
dddZ	ddd
ddd Z
ddd
d!d"d#ZdS )&_FileCacheMixinz2Shared implementation for both FileCache variants.F    Nz
str | Pathboolintztype[BaseFileLock] | NoneNone)	directoryforeverfilemodedirmode
lock_classreturnc                 C  sd   z|d krddl m} |}W n$ tk
r@   td}t|Y nX || _|| _|| _|| _|| _d S )Nr   )FileLockz
            NOTE: In order to use the FileCache you must have
            filelock installed. You can install it via pip:
              pip install cachecontrol[filecache]
            )	filelockr   ImportErrorr   r   r   r   r   r   )selfr   r   r   r   r   r   notice r   e/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/site-packages/cachecontrol/caches/file_cache.py__init__   s    z_FileCacheMixin.__init__str)xr   c                 C  s   t |   S N)hashlibsha224encode	hexdigest)r!   r   r   r   r%   6   s    z_FileCacheMixin.encode)namer   c                 C  s4   |  |}t|d d |g }tjj| jf| S )N   )r%   listospathjoinr   )r   r'   hashedpartsr   r   r   _fn:   s    
z_FileCacheMixin._fnzbytes | Nonekeyr   c              
   C  sR   |  |}z,t|d}| W  5 Q R  W S Q R X W n tk
rL   Y d S X d S )Nrb)r/   openreadFileNotFoundError)r   r1   r'   fhr   r   r   getA   s    
 z_FileCacheMixin.getbyteszint | datetime | None)r1   valueexpiresr   c                 C  s   |  |}| || d S r"   r/   _write)r   r1   r9   r:   r'   r   r   r   setJ   s    
z_FileCacheMixin.set)r+   datar   c              
   C  s   t j|}t j|| jdd | |d N tj|d\}}zt 	|| W 5 t | X t 
|| j t || W 5 Q R X dS )z:
        Safely write the data to the given path.
        T)exist_okz.lock)dirN)r*   r+   dirnamemakedirsr   r   tempfilemkstempclosewritechmodr   replace)r   r+   r>   rA   fdr'   r   r   r   r<   P   s    z_FileCacheMixin._write)r1   suffixr   c                 C  s<   |  || }| js8zt| W n tk
r6   Y nX d S r"   )r/   r   r*   remover5   )r   r1   rJ   r'   r   r   r   _deleteb   s    z_FileCacheMixin._delete)Fr   r   N)N)__name__
__module____qualname____doc__r   staticmethodr%   r/   r7   r=   r<   rL   r   r   r   r   r      s       
 r   c                   @  s    e Zd ZdZdddddZdS )	FileCachezc
    Traditional FileCache: body is stored in memory, so not suitable for large
    downloads.
    r    r   r0   c                 C  s   |  |d d S )N rL   r   r1   r   r   r   deleteq   s    zFileCache.deleteN)rM   rN   rO   rP   rV   r   r   r   r   rR   k   s   rR   c                   @  sB   e Zd ZdZdddddZdddd	d
dZdddddZdS )SeparateBodyFileCachezh
    Memory-efficient FileCache: body is stored in a separate file, reducing
    peak memory usage.
    r    zIO[bytes] | Noner0   c                 C  s6   |  |d }zt|dW S  tk
r0   Y d S X d S )N.bodyr2   )r/   r3   r5   )r   r1   r'   r   r   r   get_body{   s
    zSeparateBodyFileCache.get_bodyr8   r   )r1   bodyr   c                 C  s   |  |d }| || d S )NrX   r;   )r   r1   rZ   r'   r   r   r   set_body   s    zSeparateBodyFileCache.set_bodyc                 C  s   |  |d |  |d d S )NrS   rX   rT   rU   r   r   r   rV      s    zSeparateBodyFileCache.deleteN)rM   rN   rO   rP   rY   r[   rV   r   r   r   r   rW   u   s   rW   r    )url	filecacher   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/   )r\   r]   r1   r   r   r   url_to_file_path   s    
r_   )
__future__r   r#   r*   rC   textwrapr   typingr   r   pathlibr   Zcachecontrol.cacher   r   Zcachecontrol.controllerr	   r
   r   r   r   rR   rW   r_   r   r   r   r   <module>   s   U
