o
    ÖèFh  ã                   @  sÊ   d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZmZ d dlmZ er@d dlmZ d d	lmZ ddd„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ÚUnion)ÚPath)Ú	BaseCacheÚSeparateBodyBaseCache)ÚCacheController)Údatetime)ÚBaseFileLockÚfilenameÚstrÚfmodeÚintÚreturnú	IO[bytes]c                 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y/   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ÚOSErrorÚopenÚfdopenÚclose)r   r   ÚflagsÚfd© r"   úW/var/www/html/loop/nvenv/lib/python3.10/site-packages/cachecontrol/caches/file_cache.pyÚ_secure_open_write   s"   



þ
r$   c                   @  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Úfilemoder   ÚdirmodeÚ
lock_classútype[BaseFileLock] | Noner   Ú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]
            )	Úfilelockr1   ÚImportErrorr   r(   r*   r,   r-   r.   )Úselfr(   r*   r,   r-   r.   r1   Únoticer"   r"   r#   Ú__init__A   s    €ÿø

z_FileCacheMixin.__init__Úxr   c                 C  s   t  |  ¡ ¡ ¡ S ©N)ÚhashlibÚsha224ÚencodeÚ	hexdigest)r7   r"   r"   r#   r;   ^   s   z_FileCacheMixin.encodeÚnamec                 C  s6   |   |¡}t|d d… ƒ|g }tjj| jg|¢R Ž S )Né   )r;   Úlistr   ÚpathÚjoinr(   )r4   r=   ÚhashedÚpartsr"   r"   r#   Ú_fnb   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)rD   r   ÚreadÚFileNotFoundError)r4   rE   r=   Úfhr"   r"   r#   Úgeti   s   
(ÿÿz_FileCacheMixin.getÚvalueÚbytesÚexpiresúint | datetime | Nonec                 C  s   |   |¡}|  ||¡ d S r8   ©rD   Ú_write)r4   rE   rL   rN   r=   r"   r"   r#   Úsetr   s   
z_FileCacheMixin.setr@   Údatac              	   C  s¨   zt  t j |¡| j¡ W n	 ty   Y nw |  |d ¡�, t|| jƒ�}| 	|¡ W d  ƒ n1 s5w   Y  W d  ƒ dS W d  ƒ dS 1 sMw   Y  dS )z:
        Safely write the data to the given path.
        z.lockN)
r   Úmakedirsr@   Údirnamer-   r   r.   r$   r,   Úwrite)r4   r@   rS   rJ   r"   r"   r#   rQ   x   s   ÿÿþ"þz_FileCacheMixin._writeÚsuffixc                 C  s>   |   |¡| }| jszt |¡ W d S  ty   Y d S w d S r8   )rD   r*   r   r   rI   )r4   rE   rW   r=   r"   r"   r#   Ú_delete‡   s   ÿýz_FileCacheMixin._delete)Fr&   r'   N)r(   r)   r*   r+   r,   r   r-   r   r.   r/   r   r0   )r7   r   r   r   )r=   r   r   r   )rE   r   r   rF   r8   )rE   r   rL   rM   rN   rO   r   r0   )r@   r   rS   rM   r   r0   )rE   r   rW   r   r   r0   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r6   Ústaticmethodr;   rD   rK   rR   rQ   rX   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.
    rE   r   r   r0   c                 C  s   |   |d¡ d S )NÚ ©rX   ©r4   rE   r"   r"   r#   Údelete–   s   zFileCache.deleteN©rE   r   r   r0   )rY   rZ   r[   r\   rb   r"   r"   r"   r#   r^   �   s    r^   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.
    rE   r   r   úIO[bytes] | Nonec                 C  s0   |   |¡d }zt|dƒW S  ty   Y d S w )Nú.bodyrG   )rD   r   rI   )r4   rE   r=   r"   r"   r#   Úget_body    s   ÿzSeparateBodyFileCache.get_bodyÚbodyrM   r0   c                 C  s   |   |¡d }|  ||¡ d S )Nrf   rP   )r4   rE   rh   r=   r"   r"   r#   Úset_body§   s   zSeparateBodyFileCache.set_bodyc                 C  s   |   |d¡ |   |d¡ d S )Nr_   rf   r`   ra   r"   r"   r#   rb   «   s   zSeparateBodyFileCache.deleteN)rE   r   r   re   )rE   r   rh   rM   r   r0   rc   )rY   rZ   r[   r\   rg   ri   rb   r"   r"   r"   r#   rd   š   s
    

rd   ÚurlÚ	filecachec                 C  s   t  | ¡}| |¡S )z\Return the file cache path based on the URL.

    This does not ensure the file exists!
    )r
   Ú	cache_urlrD   )rj   rk   rE   r"   r"   r#   Úurl_to_file_path°   s   

rm   )r   r   r   r   r   r   )rj   r   rk   r^   r   r   )Ú
__future__r   r9   r   Útextwrapr   Útypingr   r   r   Úpathlibr   Úcachecontrol.cacher   r	   Úcachecontrol.controllerr
   r   r2   r   r$   r%   r^   rd   rm   r"   r"   r"   r#   Ú<module>   s    
)R
