o
    /=jý  ã                   @  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	rBd 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„ZdS )é    )ÚannotationsN)Údedent)ÚIOÚTYPE_CHECKING)ÚPath)Ú	BaseCacheÚSeparateBodyBaseCache)ÚCacheController)Údatetime)ÚBaseFileLockc                   @  sh   e Zd ZdZ				d+d,dd„Zed-dd„ƒZd.dd„Zd/dd„Z	d0d1d"d#„Z	d2d&d'„Z
d3d)d*„ZdS )4Ú_FileCacheMixinz2Shared implementation for both FileCache variants.Fé€  éÀ  NÚ	directoryú
str | PathÚforeverÚboolÚfilemodeÚintÚdirmodeÚ
lock_classútype[BaseFileLock] | NoneÚreturnÚNonec                 C  s^   z|d u rddl m} |}W n ty   tdƒ}t|ƒ‚w || _|| _|| _|| _|| _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   úhC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\cachecontrol/caches/file_cache.pyÚ__init__   s    €ÿø

z_FileCacheMixin.__init__ÚxÚstrc                 C  s   t  |  ¡ ¡ ¡ S ©N)ÚhashlibÚsha224ÚencodeÚ	hexdigest)r"   r   r   r    r'   6   s   z_FileCacheMixin.encodeÚnamec                 C  s6   |   |¡}t|d d… ƒ|g }tjj| jg|¢R Ž S )Né   )r'   ÚlistÚosÚpathÚjoinr   )r   r)   ÚhashedÚpartsr   r   r    Ú_fn:   s   
z_FileCacheMixin._fnÚkeyúbytes | Nonec                 C  sZ   |   |¡}zt|dƒ�}| ¡ W  d   ƒ W S 1 sw   Y  W d S  ty,   Y d S w )NÚrb)r1   ÚopenÚreadÚFileNotFoundError)r   r2   r)   Úfhr   r   r    ÚgetA   s   
(ÿÿz_FileCacheMixin.getÚvalueÚbytesÚexpiresúint | datetime | Nonec                 C  s   |   |¡}|  ||¡ d S r$   ©r1   Ú_write)r   r2   r:   r<   r)   r   r   r    ÚsetJ   s   
z_FileCacheMixin.setr-   Údatac              
   C  s¤   t j |¡}t j|| jdd� |  |d ¡�2 tj|d�\}}zt  ||¡ W t  	|¡ nt  	|¡ w t  
|| j¡ t  ||¡ W d  ƒ dS 1 sKw   Y  dS )z:
        Safely write the data to the given path.
        T)Úexist_okz.lock)ÚdirN)r,   r-   ÚdirnameÚmakedirsr   r   ÚtempfileÚmkstempÚwriteÚcloseÚchmodr   Úreplace)r   r-   rA   rD   Úfdr)   r   r   r    r?   P   s   "øz_FileCacheMixin._writeÚsuffixc                 C  s>   |   |¡| }| jszt |¡ W d S  ty   Y d S w d S r$   )r1   r   r,   Úremover7   )r   r2   rM   r)   r   r   r    Ú_deleteb   s   ÿýz_FileCacheMixin._delete)Fr   r   N)r   r   r   r   r   r   r   r   r   r   r   r   )r"   r#   r   r#   )r)   r#   r   r#   )r2   r#   r   r3   r$   )r2   r#   r:   r;   r<   r=   r   r   )r-   r#   rA   r;   r   r   )r2   r#   rM   r#   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   Ústaticmethodr'   r1   r9   r@   r?   rO   r   r   r   r    r      s    ú


ÿ
r   c                   @  s   e Zd ZdZd	dd„ZdS )
Ú	FileCachezc
    Traditional FileCache: body is stored in memory, so not suitable for large
    downloads.
    r2   r#   r   r   c                 C  s   |   |d¡ d S )NÚ ©rO   ©r   r2   r   r   r    Údeleteq   s   zFileCache.deleteN©r2   r#   r   r   )rP   rQ   rR   rS   rY   r   r   r   r    rU   k   s    rU   c                   @  s.   e Zd ZdZddd„Zddd„Zddd„ZdS )ÚSeparateBodyFileCachezh
    Memory-efficient FileCache: body is stored in a separate file, reducing
    peak memory usage.
    r2   r#   r   úIO[bytes] | Nonec                 C  s0   |   |¡d }zt|dƒW S  ty   Y d S w )Nú.bodyr4   )r1   r5   r7   )r   r2   r)   r   r   r    Úget_body{   s   ÿzSeparateBodyFileCache.get_bodyÚbodyr;   r   c                 C  s   |   |¡d }|  ||¡ d S )Nr]   r>   )r   r2   r_   r)   r   r   r    Úset_body‚   s   zSeparateBodyFileCache.set_bodyc                 C  s   |   |d¡ |   |d¡ d S )NrV   r]   rW   rX   r   r   r    rY   †   s   zSeparateBodyFileCache.deleteN)r2   r#   r   r\   )r2   r#   r_   r;   r   r   rZ   )rP   rQ   rR   rS   r^   r`   rY   r   r   r   r    r[   u   s
    

r[   Úurlr#   Ú	filecacher   c                 C  s   t  | ¡}| |¡S )z\Return the file cache path based on the URL.

    This does not ensure the file exists!
    N)r	   Ú	cache_urlr1   )ra   rb   r2   r   r   r    Úurl_to_file_path‹   s   

rd   )ra   r#   rb   rU   r   r#   )Ú
__future__r   r%   r,   rF   Útextwrapr   Útypingr   r   Úpathlibr   Zcachecontrol.cacher   r   Zcachecontrol.controllerr	   r
   r   r   r   rU   r[   rd   r   r   r   r    Ú<module>   s    U
