a
    !f                     @   s   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 ze	 W n e
yZ   eefZ	Y n0 dd ZG dd	 d	ZG d
d deeZG dd deeZdd ZdS )    N)dedent   )	BaseCacheSeparateBodyBaseCache)CacheControllerc              	   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t	fyb   Y n0 t 
| ||}zt |dW S    t |  Y n0 d S )N
O_NOFOLLOWO_BINARYwb)osO_WRONLYO_CREATO_EXCLhasattrr   r   removeIOErrorOSErroropenfdopenclose)filenamefmodeflagsfd r   _/var/www/html/python-backend/venv/lib/python3.9/site-packages/cachecontrol/caches/file_cache.py_secure_open_write   s     




r   c                   @   sV   e Zd ZdZdddZedd	 Zd
d Zdd ZdddZ	e
dddZdd ZdS )_FileCacheMixinz2Shared implementation for both FileCache variants.F    Nc           
      C   s   |d ur|d urt dzddlm} ddlm} W n" tyV   td}	t|	Y n0 |rb|}n|d u rn|}|| _|| _|| _	|| _
|| _d S )Nz/Cannot use use_dir_lock and lock_class togetherr   )LockFile)MkdirLockFilez
            NOTE: In order to use the FileCache you must have
            lockfile installed. You can install it via pip:
              pip install lockfile
            )
ValueErrorlockfiler   lockfile.mkdirlockfiler    ImportErrorr   	directoryforeverfilemodedirmode
lock_class)
selfr%   r&   r'   r(   use_dir_lockr)   r   r    noticer   r   r   __init__?   s&    
z_FileCacheMixin.__init__c                 C   s   t |   S N)hashlibsha224encode	hexdigest)xr   r   r   r1   f   s    z_FileCacheMixin.encodec                 C   s6   |  |}t|d d |g }tjj| jg|R  S )N   )r1   listr
   pathjoinr%   )r*   namehashedpartsr   r   r   _fnj   s    
z_FileCacheMixin._fnc                 C   s\   |  |}z8t|d}| W  d    W S 1 s60    Y  W n tyV   Y d S 0 d S )Nrb)r;   r   readFileNotFoundError)r*   keyr8   fhr   r   r   getq   s    
,z_FileCacheMixin.getc                 C   s   |  |}| || d S r.   r;   _write)r*   r?   valueexpiresr8   r   r   r   setz   s    
z_FileCacheMixin.set)datac              	   C   s   zt t j|| j W n ttfy0   Y n0 | |H}t|j| j	}|
| W d   n1 sl0    Y  W d   n1 s0    Y  dS )z:
        Safely write the data to the given path.
        N)r
   makedirsr6   dirnamer(   r   r   r)   r   r'   write)r*   r6   rG   lockr@   r   r   r   rC   ~   s    z_FileCacheMixin._writec                 C   s:   |  || }| js6zt| W n ty4   Y n0 d S r.   )r;   r&   r
   r   r>   )r*   r?   suffixr8   r   r   r   _delete   s    z_FileCacheMixin._delete)Fr   r   NN)N)__name__
__module____qualname____doc__r-   staticmethodr1   r;   rA   rF   bytesrC   rM   r   r   r   r   r   <   s        
'
	
r   c                   @   s   e Zd ZdZdd ZdS )	FileCachezc
    Traditional FileCache: body is stored in memory, so not suitable for large
    downloads.
    c                 C   s   |  |d d S )N rM   r*   r?   r   r   r   delete   s    zFileCache.deleteN)rN   rO   rP   rQ   rX   r   r   r   r   rT      s   rT   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	SeparateBodyFileCachezh
    Memory-efficient FileCache: body is stored in a separate file, reducing
    peak memory usage.
    c                 C   s4   |  |d }zt|dW S  ty.   Y d S 0 d S )N.bodyr<   )r;   r   r>   )r*   r?   r8   r   r   r   get_body   s
    zSeparateBodyFileCache.get_bodyc                 C   s   |  |d }| || d S )NrZ   rB   )r*   r?   bodyr8   r   r   r   set_body   s    zSeparateBodyFileCache.set_bodyc                 C   s   |  |d |  |d d S )NrU   rZ   rV   rW   r   r   r   rX      s    zSeparateBodyFileCache.deleteN)rN   rO   rP   rQ   r[   r]   rX   r   r   r   r   rY      s   rY   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;   )url	filecacher?   r   r   r   url_to_file_path   s    
ra   )r/   r
   textwrapr   cacher   r   
controllerr   r>   	NameErrorr   r   r   r   rT   rY   ra   r   r   r   r   <module>   s   )Z
