o
    zqf                     @  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 d dl	m
Z
mZ d dlmZ er8d 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)	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    i/root/parts/websockify/install/lib/python3.10/site-packages/pip/_vendor/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	d/d0d!d"Z	d1d%d&Z
d2d(d)ZdS )3_FileCacheMixinz2Shared implementation for both FileCache variants.F    N	directoryr   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 filelock
            )	filelockr.   ImportErrorr   r&   r'   r)   r*   r+   )selfr&   r'   r)   r*   r+   r.   noticer    r    r!   __init__@   s    

z_FileCacheMixin.__init__xc                 C  s   t |   S N)hashlibsha224encode	hexdigest)r4   r    r    r!   r8   ]   s   z_FileCacheMixin.encodenamec                 C  s6   |  |}t|d d |g }tjj| jg|R  S )N   )r8   listr   pathjoinr&   )r1   r:   hashedpartsr    r    r!   _fna   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)rA   r   readFileNotFoundError)r1   rB   r:   fhr    r    r!   geth   s   
(z_FileCacheMixin.getvaluebytesexpiresint | datetime | Nonec                 C  s   |  |}| || d S r5   rA   _write)r1   rB   rI   rK   r:   r    r    r!   setq   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)r1   r=   rP   rG   r    r    r!   rN   w   s   "z_FileCacheMixin._writesuffixc                 C  s>   |  || }| jszt| W d S  ty   Y d S w d S r5   )rA   r'   r   r   rF   )r1   rB   rT   r:   r    r    r!   _delete   s   z_FileCacheMixin._delete)Fr$   r%   N)r&   r   r'   r(   r)   r   r*   r   r+   r,   r   r-   )r4   r   r   r   )r:   r   r   r   )rB   r   r   rC   r5   )rB   r   rI   rJ   rK   rL   r   r-   )r=   r   rP   rJ   r   r-   )rB   r   rT   r   r   r-   )__name__
__module____qualname____doc__r3   staticmethodr8   rA   rH   rO   rN   rU   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.
    rB   r   r   r-   c                 C  s   |  |d d S )N rU   r1   rB   r    r    r!   delete   s   zFileCache.deleteNrB   r   r   r-   )rV   rW   rX   rY   r_   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.
    rB   r   r   IO[bytes] | Nonec                 C  s0   |  |d }zt|dW S  ty   Y d S w )N.bodyrD   )rA   r   rF   )r1   rB   r:   r    r    r!   get_body   s   zSeparateBodyFileCache.get_bodybodyrJ   r-   c                 C  s   |  |d }| || d S )Nrc   rM   )r1   rB   re   r:   r    r    r!   set_body   s   zSeparateBodyFileCache.set_bodyc                 C  s   |  |d |  |d d S )Nr\   rc   r]   r^   r    r    r!   r_      s   zSeparateBodyFileCache.deleteN)rB   r   r   rb   )rB   r   re   rJ   r   r-   r`   )rV   rW   rX   rY   rd   rf   r_   r    r    r    r!   ra      s
    

ra   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_urlrA   )rg   rh   rB   r    r    r!   url_to_file_path   s   

rj   )r   r   r   r   r   r   )rg   r   rh   r[   r   r   )
__future__r   r6   r   textwrapr   typingr   r   pip._vendor.cachecontrol.cacher   r   #pip._vendor.cachecontrol.controllerr   r	   r/   r
   r"   r#   r[   ra   rj   r    r    r    r!   <module>   s   
)R
