o
    MfG                     @   sD   d Z ddlZddlmZ ddlmZ ddlmZ G dd deZdS )	z
Wrapper class that takes a list of template loaders as an argument and attempts
to load templates from them in order, caching the result.
    N)TemplateDoesNotExist)copy_exception   )Loaderc                       s\   e Zd Z fddZdd Zdd Zd fdd		Zd
d ZdddZdd Z	dd Z
  ZS )r   c                    s"   i | _ ||| _t | d S N)get_template_cacheget_template_loadersloaderssuper__init__)selfenginer	   	__class__ `/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/django/template/loaders/cached.pyr      s   zLoader.__init__c                 c   s*    | j D ]}t|dr| E d H  qd S )Nget_dirs)r	   hasattrr   )r   loaderr   r   r   r      s   

zLoader.get_dirsc                 C   s   |j |S r   )r   get_contents)r   originr   r   r   r      s   zLoader.get_contentsNc              
      s   |  ||}| j|}|r't|trt|tr||t|tr%t||S z	t 	||}W n tyJ } z| j
jr@t|nt| j|<  d}~ww || j|< |S )a
  
        Perform the caching that gives this loader its name. Often many of the
        templates attempted will be missing, so memory use is of concern here.
        To keep it in check, caching behavior is a little complicated when a
        template is not found. See ticket #26306 for more details.

        With template debugging disabled, cache the TemplateDoesNotExist class
        for every missing template and raise a new instance of it after
        fetching it from the cache.

        With template debugging enabled, a unique TemplateDoesNotExist object
        is cached for each missing template to preserve debug data. When
        raising an exception, Python sets __traceback__, __context__, and
        __cause__ attributes on it. Those attributes can contain references to
        all sorts of objects up the call chain and caching them creates a
        memory leak. Thus, unraised copies of the exceptions are cached and
        copies of those copies are raised after they're fetched from the cache.
        N)	cache_keyr   get
isinstancetype
issubclassr   r   r
   get_templater   debug)r   template_nameskipkeycachedtemplateer   r   r   r      s"   

zLoader.get_templatec                 c   s"    | j D ]
}||E d H  qd S r   )r	   get_template_sources)r   r   r   r   r   r   r$   C   s   
zLoader.get_template_sourcesc                    sD   d}|r fdd|D }|r|  |}ddd t |fD S )aj  
        Generate a cache key for the template name and skip.

        If skip is provided, only origins that match template_name are included
        in the cache key. This ensures each template is only parsed and cached
        once if contained in different extend chains like:

            x -> a -> a
            y -> a -> a
            z -> a -> a
         c                    s   g | ]
}|j  kr|jqS r   )r   name).0r   r   r   r   
<listcomp>V   s    z$Loader.cache_key.<locals>.<listcomp>-c                 s   s    | ]}|r|V  qd S r   r   )r'   sr   r   r   	<genexpr>Z   s    z#Loader.cache_key.<locals>.<genexpr>)generate_hashjoinstr)r   r   r   skip_prefixmatchingr   r(   r   r   G   s   
zLoader.cache_keyc                 C   s   t d|  S )N|)hashlibsha1r.   encode	hexdigest)r   valuesr   r   r   r-   \   s   zLoader.generate_hashc                 C   s   | j   dS )zEmpty the template cache.N)r   clear)r   r   r   r   reset_   s   zLoader.resetr   )__name__
__module____qualname__r   r   r   r   r$   r   r-   r9   __classcell__r   r   r   r   r      s    &
r   )	__doc__r3   django.templater   django.template.backends.djangor   baser   
BaseLoaderr   r   r   r   <module>   s    