o
    a"<f                     @   s4  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ dad0d
dZdd Zdd Zdd Zdd Zdd Zdd Zdadd Zdd Z dd Z!dd Z"d d! Z#d1d#d$Z$d1d%d&Z%d'd( Z&d)d* Z'd2d,d-Z(ed.d/ Z)dS )3    N)import_module)caches)ContentFile)	force_strsmart_bytes)SimpleLazyObject)settings) default_offline_manifest_storage)get_mod_funcc                 C   s&   t t|  }|r|d | S |S N)hashlibsha256r   	hexdigest)	plaintextlengthdigest r   G/var/www/html/kck/venv/lib/python3.10/site-packages/compressor/cache.pyget_hexdigest   s   r   c                 C   s   dt |  S )Nzdjango_compressor.%s)r   keyr   r   r   simple_cachekey   s   r   c                 C   s   dt  t| f S )Nzdjango_compressor.%s.%s)socketgethostnamer   r   r   r   r   socket_cachekey   s   r   c               
   O   sh   t d u r-zttj\}}tt||a W n tttfy, } z	tdtj|f d }~ww t | i |S )Nz)Couldn't import cache key function %s: %s)	_cachekey_funcr
   r   COMPRESS_CACHE_KEY_FUNCTIONgetattrr   AttributeErrorImportError	TypeError)argskwargsmod_name	func_nameer   r   r   get_cachekey#   s   r&   c                 C      t dt|  S )Nzmtime.%s)r&   r   )filenamer   r   r   get_mtime_cachekey0      r)   c                 C   s   t | ttjdS )N )r   replacestrr   
STATIC_URL)render_template_stringr   r   r   get_offline_hexdigest4   s
   
r0   c                 C   r'   )Nz
offline.%s)r&   r0   )sourcer   r   r   get_offline_cachekeyA   r*   r2   c                  C   sd   t d u r0tj} t| r.t| }t| 	da W d    t S 1 s'w   Y  t S i a t S )Nutf8)
_offline_manifestr   COMPRESS_OFFLINE_MANIFESTr	   existsopenjsonloadsreaddecode)r(   fpr   r   r   get_offline_manifestH   s   

r=   c                   C   s   d a d S r   )r4   r   r   r   r   flush_offline_manifestT   s   r>   c                 C   s0   t j| ddd}ttjt| t  d S )N   )indentr3   )	r8   dumpsencoder	   saver   r5   r   r>   )manifestcontentr   r   r   write_offline_manifestY   s   
rF   c                 C   s   t d| j||f S )Nztemplatetag.%s.%s.%s)r&   cachekey)
compressormodekindr   r   r   get_templatetag_cachekey_   s   rK   c                 C   sL   t jr t| }t|}|d u rtj| }t||t j |S tj| S r   )	r   COMPRESS_MTIME_DELAYr)   cachegetospathgetmtimeset)r(   r   mtimer   r   r   	get_mtimed   s   
rT      c                 C   s@   zt j| } ttt| }W n
 ty   Y d S w t||S r   )rO   rP   realpathr-   intrT   OSErrorr   )r(   r   rS   r   r   r   get_hashed_mtimeo   s   
rY   c                 C   sb   zt j| } W n
 ty   Y d S w t| d}t| |W  d    S 1 s*w   Y  d S )Nrb)rO   rP   rV   rX   r7   r   r:   )r(   r   filer   r   r   get_hashed_contentx   s   $r\   c                 C   s   t td| |f  S )Nzprecompiler.%s.%s)r   sha1r   r   )commandcontentsr   r   r   get_precompiler_cachekey   s   r`   c                 C   sJ   t | }|d u rd S |\}}}t |kr#|s#t| |dtjd d S |S )NT)	refreshedtimeout)rM   rN   time	cache_setr   COMPRESS_MINT_DELAY)r   
packed_valvalrefresh_timera   r   r   r   	cache_get   s   

ri   Fc                 C   s<   |d u rt j}|t  }|t j }|||f}t| ||S r   )r   COMPRESS_REBUILD_TIMEOUTrc   re   rM   rR   )r   rg   ra   rb   rh   real_timeoutrf   r   r   r   rd      s   

rd   c                   C   s
   t tj S r   )r   r   COMPRESS_CACHE_BACKENDr   r   r   r   <lambda>   s   
 rm   r   )rU   )FN)*r8   r   rO   r   rc   	importlibr   django.core.cacher   django.core.files.baser   django.utils.encodingr   r   django.utils.functionalr   compressor.confr   compressor.storager	   compressor.utilsr
   r   r   r   r   r&   r)   r0   r2   r4   r=   r>   rF   rK   rT   rY   r\   r`   ri   rd   rM   r   r   r   r   <module>   sB    


	
	