3
(h                 @   s|   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
mZ ddlmZ ddlmZ dd	lmZ G d
d deZdS )    )ImproperlyConfigured)	lru_cachesix)cached_property)import_string   )ContextTemplate)_builtin_context_processors)TemplateDoesNotExist)import_libraryc            
   @   s   e Zd ZdddgZd#d	d
Zeej dd Zedd Z	dd Z
dd Zedd Zdd Zdd Zd$ddZdd Zdd Zd%dd Zd!d" ZdS )&Enginezdjango.template.defaulttagszdjango.template.defaultfilterszdjango.template.loader_tagsNF utf-8Tc             C   s   |d krg }|d krg }|d kr6dg}|rB|dg7 }n|rBt d|d krNi }|	d krZg }	|| _|| _|
| _|| _|| _|| _|| _|| _|| _	| j
|| _| j|	 | _| j| j| _d S )Nz)django.template.loaders.filesystem.Loaderz.django.template.loaders.app_directories.Loaderz1app_dirs must not be set when loaders is defined.)r   dirsapp_dirs
autoescapecontext_processorsdebugloadersstring_if_invalidfile_charset	librariesget_template_librariesZtemplate_librariesdefault_builtinsbuiltinsget_template_builtinsZtemplate_builtins)selfr   r   r   r   r   r   r   r   r   r    r   :/tmp/pip-install-q3hcpn_q/Django/django/template/engine.py__init__   s6    zEngine.__init__c                 sf   ddl m}  ddlm   fdd| j D }t|dkrD|d jS t|dkrZtdntdd	S )
a  
        When only one DjangoTemplates backend is configured, returns it.

        Raises ImproperlyConfigured otherwise.

        This is required for preserving historical APIs that rely on a
        globally available, implicitly configured engine such as:

        >>> from django.template import Context, Template
        >>> template = Template("Hello {{ name }}!")
        >>> context = Context({'name': "world"})
        >>> template.render(context)
        'Hello world!'
        r   )engines)DjangoTemplatesc                s   g | ]}t | r|qS r   )
isinstance).0engine)r"   r   r   
<listcomp>J   s    z&Engine.get_default.<locals>.<listcomp>r   z)No DjangoTemplates backend is configured.zPSeveral DjangoTemplates backends are configured. You must select one explicitly.N)Zdjango.templater!   Zdjango.template.backends.djangor"   alllenr%   r   )r!   Zdjango_enginesr   )r"   r   get_default4   s    
zEngine.get_defaultc             C   s$   t }|t| j7 }tdd |D S )Nc             s   s   | ]}t |V  qd S )N)r   )r$   pathr   r   r   	<genexpr>[   s    z5Engine.template_context_processors.<locals>.<genexpr>)r
   tupler   )r   r   r   r   r   template_context_processorsW   s    z"Engine.template_context_processorsc             C   s   dd |D S )Nc             S   s   g | ]}t |qS r   )r   )r$   xr   r   r   r&   ^   s    z0Engine.get_template_builtins.<locals>.<listcomp>r   )r   r   r   r   r   r   ]   s    zEngine.get_template_builtinsc             C   s*   i }x |j  D ]\}}t|||< qW |S )N)itemsr   )r   r   Zloadednamer*   r   r   r   r   `   s    zEngine.get_template_librariesc             C   s   | j | jS )N)get_template_loadersr   )r   r   r   r   template_loadersf   s    zEngine.template_loadersc             C   s2   g }x(|D ] }| j |}|d k	r
|j| q
W |S )N)find_template_loaderappend)r   r2   r   Ztemplate_loaderloaderr   r   r   r1   j   s    

zEngine.get_template_loadersc             C   s^   t |ttfr(t|dd  }|d }ng }t |tjrNt|}|| f| S td| d S )Nr   r   z3Invalid value in template loaders configuration: %r)r#   r,   listr   string_typesr   r   )r   r5   argsloader_classr   r   r   r3   r   s    
zEngine.find_template_loaderc             C   s   g }x| j D ]v}|jrby|j|||d}||jfS  tk
r^ } z|j|j W Y d d }~X qX qy
|||S  tk
r   Y qX qW t||dd S )N)Ztemplate_dirsskip)tried)r2   Zsupports_recursionget_templateoriginr   extendr;   )r   r0   r   r:   r;   r5   templateer   r   r   find_template   s    
 

zEngine.find_templatec             C   s   t || dS )z
        Returns a compiled Template object for the given template code,
        handling template inheritance recursively.
        )r%   )r	   )r   Ztemplate_coder   r   r   from_string   s    zEngine.from_stringc             C   s,   | j |\}}t|ds(t|||| d}|S )z
        Returns a compiled Template object for the given template name,
        handling template inheritance recursively.
        render)r%   )rA   hasattrr	   )r   template_namer?   r=   r   r   r   r<      s    
zEngine.get_templatec             C   sJ   t |ttfr| j|}n
| j|}t |tr8|j|S |jt|S dS )z
        Render the template specified by template_name with the given context.
        For use in Django's test suite.
        N)r#   r6   r,   select_templater<   r   rC   )r   rE   contexttr   r   r   render_to_string   s    


zEngine.render_to_stringc             C   s~   |st dg }xZ|D ]R}y
| j|S  t k
rf } z&|jd |krT|j|jd  wW Y dd}~X qX qW t dj|dS )zW
        Given a list of template names, returns the first that can be loaded.
        zNo template names providedr   Nz, )r   r<   r8   r4   join)r   Ztemplate_name_list	not_foundrE   excr   r   r   rF      s    

zEngine.select_template)
NFNFNr   r   NNT)NN)N)__name__
__module____qualname__r   r    staticmethodr   r)   r   r-   r   r   r2   r1   r3   rA   rB   r<   rI   rF   r   r   r   r   r      s&     
"

r   N)Zdjango.core.exceptionsr   Zdjango.utilsr   r   Zdjango.utils.functionalr   Zdjango.utils.module_loadingr   baser   r	   rG   r
   
exceptionsr   Zlibraryr   objectr   r   r   r   r   <module>   s   