î

_ÝV€  ã               @   s”   d  Z  d d l Z d d l Z d d l m Z m Z m Z d d l m Z d d l	 m
 Z
 d d l m Z d d l m Z Gd	 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)ÚOriginÚTemplateÚTemplateDoesNotExist)ÚRemovedInDjango20Warning)Úforce_bytes)Úfunc_supports_parameteré   )ÚLoaderc                   s¬   e  Z d  Z ‡  f d d †  Z d d „  Z d d ‡  f d d † Z d d d	 „ Z d d
 d „ Z d d „  Z e	 d d „  ƒ Z
 d d d „ Z d d d „ Z d d „  Z ‡  S)r	   c                sG   i  |  _  i  |  _ i  |  _ | j | ƒ |  _ t t |  ƒ j | ƒ d  S)N)Útemplate_cacheÚfind_template_cacheÚget_template_cacheZget_template_loadersÚloadersÚsuperr	   Ú__init__)ÚselfÚenginer   )Ú	__class__© ú@/tmp/pip-build-ghmbqnp_/Django/django/template/loaders/cached.pyr      s
    			zLoader.__init__c             C   s   | j  j | ƒ S)N)ÚloaderÚget_contents)r   Úoriginr   r   r   r      s    zLoader.get_contentsNc                s´   |  j  | | | ƒ } |  j j | ƒ } | rI t | t ƒ rE | ‚ n  | Sy" t t |  ƒ j | | | ƒ } Wn5 t k
 r¢ } z | |  j | <‚  WYd  d  } ~ Xn X| |  j | <| S)N)Ú	cache_keyr   ÚgetÚ
isinstancer   r   r	   Úget_template)r   Útemplate_nameÚtemplate_dirsÚskipÚkeyÚcachedÚtemplateÚe)r   r   r   r      s    	zLoader.get_templatec             c   sb   x[ |  j  D]P } | g } t | j d ƒ r; | j | ƒ n  x | j | Œ  D] } | VqK Wq
 Wd  S)Nr   )r   r   Úget_template_sourcesÚappend)r   r   r   r   Úargsr   r   r   r   r#   1   s    	zLoader.get_template_sourcesc                sx   d } d } | rF ‡  f d d †  | Dƒ } | rF |  j  | ƒ } qF n  | r^ |  j  | ƒ } n  d ˆ  | | f j d ƒ S)aq  
        Generate a cache key for the template name, dirs, 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 Sr   )r   Úname)Ú.0r   )r   r   r   ú
<listcomp>K   s   	 z$Loader.cache_key.<locals>.<listcomp>z%s-%s-%sú-)Úgenerate_hashÚstrip)r   r   r   r   Zdirs_prefixZskip_prefixZmatchingr   )r   r   r   ;   s    zLoader.cache_keyc             C   s"   t  j t d j | ƒ ƒ ƒ j ƒ  S)Nú|)ÚhashlibÚsha1r   ÚjoinÚ	hexdigest)r   Úvaluesr   r   r   r+   T   s    zLoader.generate_hashc             C   s   t  d d „  |  j Dƒ ƒ S)z™
        RemovedInDjango20Warning: This is an internal property used by the
        ExtendsNode during the deprecation of non-recursive loaders.
        c             s   s   |  ] } t  | d  ƒ Vq d S)r   N)Úhasattr)r(   r   r   r   r   ú	<genexpr>]   s    z,Loader.supports_recursion.<locals>.<genexpr>)Úallr   )r   r   r   r   Úsupports_recursionW   s    zLoader.supports_recursionc       	      C   sÜ   |  j  | | ƒ } y |  j | } Wn‚ t k
 r§ d } xg |  j D]\ } y | | | ƒ \ } } Wn t k
 rv YqC Xt d | d | d | ƒ } | | f } PqC WYn X| |  j | <| r¿ | St |  j | <t | ƒ ‚ d S)z
        RemovedInDjango20Warning: An internal method to lookup the template
        name in all the configured loaders.
        Nr'   r   r   )r   r   ÚKeyErrorr   r   r   r
   )	r   r'   Údirsr   Úresultr   r!   Zdisplay_namer   r   r   r   Úfind_template_   s*    	
zLoader.find_templatec             C   sä   t  j d t ƒ |  j | | ƒ } |  j j | ƒ } | t k rI t ‚ n | d  k rÙ |  j | | ƒ \ } } t | d ƒ sÃ y t	 | | | |  j
 ƒ } WqÃ t k
 r¿ | | f |  j | <YqÃ Xn  | d  f |  j | <n  |  j | S)NzEThe load_template() method is deprecated. Use get_template() instead.Úrender)ÚwarningsÚwarnr   r   r
   r   r   r:   r3   r   r   )r   r   r   r   Ztemplate_tupler!   r   r   r   r   Úload_template}   s     	zLoader.load_templatec             C   s+   |  j  j ƒ  |  j j ƒ  |  j j ƒ  d S)zEmpty the template cache.N)r
   Úclearr   r   )r   r   r   r   Úreset•   s    zLoader.reset)Ú__name__Ú
__module__Ú__qualname__r   r   r   r#   r   r+   Úpropertyr6   r:   r>   r@   r   r   )r   r   r	      s   
r	   )Ú__doc__r.   r<   Zdjango.templater   r   r   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   Zdjango.utils.inspectr   Úbaser	   Z
BaseLoaderr   r   r   r   Ú<module>   s   