
    ~+h                         d Z ddlZ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	 ddl
mZmZ ddlmZmZ ddlZdZd	 Zd
 Zd Z G d de      Z G d de      Z G d de      Zd Zy)a  Caches are used for multiple things:

    - To speed up asset building. Filter operations every step
      of the way can be cached, so that individual parts of a
      build that haven't changed can be reused.

    - Bundle definitions are cached when a bundle is built so we
      can determine whether they have changed and whether a rebuild
      is required.

This data is not all stored in the same cache necessarily. The
classes in this module provide the "environment.cache" object, but
also serve in other places.
    N)path)six)BaseHunk)Filterfreezedicts)md5_constructorpickle)FilesystemCacheMemoryCache	get_cachec                     t        |       S )a  Ensures ``data`` can be hashed().

    Mostly needs to support dict. The other special types we use
    as hash keys (Hunks, Filters) already have a proper hash() method.

    See also ``make_md5``.

    Note that we do not actually hash the data for the memory cache.
    )r   )datas    B/var/www/html/venv/lib/python3.12/site-packages/webassets/cache.pymake_hashabler      s     t    c                  ~    fdt               } |       D ]  }|j                  |        |j                         S )a  Make a md5 hash based on``data``.

    Specifically, this knows about ``Hunk`` objects, and makes sure
    the actual content is hashed.

    This is very conservative, and raises an exception if there are
    data types that it does not explicitly support. This is because
    we had in the past some debugging headaches with the cache not
    working for this very reason.

    MD5 is faster than sha, and we don't care so much about collisions.
    We care enough however not to use hash().
    c              3     K   t        | t        t        t        f      r| D ]  } |      D ]  }|   y t        | t              rDt        | j                               D ]'  } |      D ]  }|   | |         D ]  }|  ) y t        | t              r"| j                         j                  d       y t        | t              rt        |       j                  d       y t        | t        j                        r| j                  d       y t        | t        j                        r|  y t        | d      r  | j!                               D ]  }|  y | dj                  d       y t        | t"        j$                        r&t        t'        |             j                  d       y t)        dt+        |       z        w)Nzutf-8idNonezCannot MD5 type %s)
isinstancetuplelist	frozensetdictsortedkeysr   r   encodeintstrr   	text_typebinary_typehasattrr   typesFunctionTypehash
ValueErrortype)objitemdkiwalks        r   r-   zmake_md5.<locals>.walk:   sr    cE434 -d,A1W,-d$CHHJ' /a)A')c!f.AQw./ X&((*##G,,S!c(//'**S]]+**W%%S__-IS$#&&(^ [--((U//0d3i.''001DI=>>s   GG)r   update	hexdigest)r   md5r*   r-   s      @r   make_md5r1   ,   s<    ?2 
C$Z 

1==?r   c                 <    	 t        j                  |       S #  Y yxY w)z6Unpickle the string, or return ``None`` if that fails.N)r	   loads)strings    r   safe_unpickler5   Y   s     ||F##s    c                       e Zd ZdZd Zd Zy)	BaseCachea  Abstract base class.

    The cache key must be something that is supported by the Python hash()
    function. The cache value may be a string, or anything that can be pickled.

    Since the cache is used for multiple purposes, all webassets-internal code
    should always tag its keys with an id, like so:

        key = ("tag", actual_key)

    One cache instance can only be used safely with a single Environment.
    c                     t               )z4Should return the cache contents, or False.
        NotImplementedErrorselfkeys     r   getzBaseCache.geto   s     "##r   c                     t               Nr9   )r<   r=   values      r   setzBaseCache.sett   s    !##r   N)__name__
__module____qualname____doc__r>   rB    r   r   r7   r7   a   s    $
$r   r7   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   a  Caches stuff in the process memory.

    WARNING: Do NOT use this in a production environment, where you
    are likely going to have multiple processes serving the same app!

    Note that the keys are used as-is, not passed through hash() (which is
    a difference: http://stackoverflow.com/a/9022664/15677). However, the
    reason we don't is because the original value is nicer to debug.
    c                 .    || _         g | _        i | _        y r@   )capacityr   cache)r<   rJ   s     r   __init__zMemoryCache.__init__   s     	
r   c                 L    d|k(  xs d|k(  xs t        |       t        |      k(  S )zWReturn equality with the config values that instantiate
        this instance.
        FN)r   r<   others     r   __eq__zMemoryCache.__eq__   s1     ~ %u}%$x2e9$	%r   c                 b    t        t        |            }| j                  j                  |d       S r@   )r1   r   rK   r>   r;   s     r   r>   zMemoryCache.get   s&    }S)*zz~~c4((r   c                    t        t        |            }|| j                  |<   	 | j                  j	                  |       | j                  j                  |       | j                  dt        dt        | j                        | j                  z
         }| j                  t        |      d  | _        |D ]  }| j                  |=  y # t
        $ r Y w xY w)Nr   )
r1   r   rK   r   remover&   appendmaxlenrJ   )r<   r=   rA   	to_deleter)   s        r   rB   zMemoryCache.set   s    }S)*

3	IIS! 			 IIaAs499~dmm'C DE	IIc)no.	 	!D

4 	!  		s   C 	CCN)rC   rD   rE   rF   rL   rP   r>   rB   rG   r   r   r   r   x   s    
%)!r   r   c                   .    e Zd ZdZdZddZd Zd Zd Zy)	r
   z,Uses a temporary directory on the disk.
       Nc                      || _         || _        y r@   )	directorynew_file_mode)r<   r[   r\   s      r   rL   zFilesystemCache.__init__   s    "*r   c                 `    d|k(  xs( | j                   |k(  xs t        |       t        |      k(  S )zWReturn equality with the config values
        that instantiate this instance.
        T)r[   r   rN   s     r   rP   zFilesystemCache.__eq__   s6     u} %~~&%$x2e9$	%r   c                    t        j                  | j                  dt        | j                  |      z        }	 t        |d      }	 |j                         }|j                          t        |      }|t        j                  d|z         |S # t        $ r(}|j                  t        j                  k7  r Y d }~y d }~ww xY w# |j                          w xY w)N%srbz Ignoring corrupted cache file %s)r   joinr[   r1   VopenIOErrorerrnoENOENTreadcloser5   warningswarn)r<   r=   filenameferesult	unpickleds          r   r>   zFilesystemCache.get   s    99T^^THTVVS4I-IJ	Xt$A
	VVXFGGI!&)	MM<xGH  	ww%,,&	 GGIs#   B C 	B>B99B>Cc                    dt        | j                  |      z  }t        j                  | j                  |      }t        j                  d|z   | j                        \  }}	 t        j                  |d      5 }t        j                  ||       |j                          d d d        | j                   t        j                  || j                         t        j                  j                  |      rt        j                  |       t        j                   ||       y # 1 sw Y   xY w#  t        j                  |        xY w)Nr_   .)prefixdirwb)r1   rb   r   ra   r[   tempfilemkstemposfdopenr	   dumpflushr\   chmodisfileunlinkrename)r<   r=   r   r0   rk   fdtemp_filenamerl   s           r   rB   zFilesystemCache.set   s    Xdffc**99T^^S1$,,C#INN$M	2t$ D!$	
 !!-(:(:;ww~~h'		(#IImX. 	IIm$s%   "D* 8'DA>D* D'#D* *Er@   )	rC   rD   rE   rF   rb   rL   rP   r>   rB   rG   r   r   r
   r
      s"     	
A+%$r   r
   c                 H   | syt        | t              r| S t        | t              rt        | t              r |        S | du rKt	        j
                  |j                  d      }t	        j                  |      st        j                  |       n| }t        ||j                        S )z1Return a cache instance based on ``option``.
    NTz.webassets-cache)r   r7   r'   
issubclassr   ra   r[   existsrw   makedirsr
   cache_file_mode)optionctxr[   s      r   r   r      s     &)$	FD	!j&Cx~IIcmm-?@	{{9%KK	"	9c&9&9::r   )rF   rw   r   re   ru   ri   	webassetsr   webassets.merger   webassets.filterr   r   webassets.utilsr   r	   r#   __all__r   r1   r5   objectr7   r   r
   r   rG   r   r   <module>r      sl    
      $ 0 3  ;
*Z$ $.)!) )!X6i 6r;r   