3
¥Yðiú  ã               @   s  d dl Zd dlmZ d dlmZ d dlmZ d dlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ejr†ddlmZ ddlmZ ejeejf dœdd„ZG dd„ deƒZG dd„ deƒZeededœdd„Zejeeje f ejedœdd„Zeejedœdd„ZdS )é    N)Ú
BaseLoader)ÚEnvironment)ÚTemplate)ÚTemplateNotFoundé   )Ú_app_ctx_stack)Ú_request_ctx_stack)Úbefore_render_template)Útemplate_rendered)ÚFlask)ÚScaffold)Úreturnc              C   sB   t j} tj}i }|dk	r"|j|d< | dk	r>| j|d< | j|d< |S )zSDefault template context processor.  Injects `request`,
    `session` and `g`.
    NÚgÚrequestÚsession)r   Útopr   r   r   r   )ZreqctxZappctxÚrv© r   ú1/tmp/pip-build-8om4l6ph/flask/flask/templating.pyÚ_default_template_ctx_processor   s    


r   c               @   s$   e Zd ZdZdejddœdd„ZdS )r   zÌWorks like a regular Jinja2 environment but has some additional
    knowledge of how Flask's blueprint works so that it can prepend the
    name of the blueprint to referenced templates if necessary.
    r   N)ÚappÚoptionsr   c             K   s,   d|kr|j ƒ |d< tj| f|Ž || _d S )NÚloader)Zcreate_global_jinja_loaderÚBaseEnvironmentÚ__init__r   )Úselfr   r   r   r   r   r   '   s    zEnvironment.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚtÚAnyr   r   r   r   r   r   !   s   r   c               @   sÞ   e Zd ZdZdddœdd„Zeeejeej	e ej	ej
 f dœdd	„Zeeejeej	e ej	ej
 f dœd
d„Zeeejeej	e ej	ej
 f dœdd„Zeejejdef ddf dœdd„Zeje dœdd„ZdS )ÚDispatchingJinjaLoaderz\A loader that looks for templates in the application and all
    the blueprint folders.
    r   N)r   r   c             C   s
   || _ d S )N)r   )r   r   r   r   r   r   3   s    zDispatchingJinjaLoader.__init__)ÚenvironmentÚtemplater   c             C   s$   | j jd r| j||ƒS | j||ƒS )NZEXPLAIN_TEMPLATE_LOADING)r   ÚconfigÚ_get_source_explainedÚ_get_source_fast)r   r#   r$   r   r   r   Ú
get_source6   s    z!DispatchingJinjaLoader.get_sourcec       	      C   s˜   g }d }x\| j |ƒD ]N\}}y|j||ƒ}|d kr6|}W n tk
rP   d }Y nX |j|||fƒ qW ddlm} || j||ƒ |d k	rŒ|S t|ƒ‚d S )Nr   )Ú!explain_template_loading_attempts)Ú_iter_loadersr(   r   ÚappendZdebughelpersr)   r   )	r   r#   r$   ÚattemptsZtrvZsrcobjr   r   r)   r   r   r   r&   =   s    
z,DispatchingJinjaLoader._get_source_explainedc             C   sH   x:| j |ƒD ],\}}y|j||ƒS  tk
r6   wY qX qW t|ƒ‚d S )N)r*   r(   r   )r   r#   r$   Z_srcobjr   r   r   r   r'   W   s    z'DispatchingJinjaLoader._get_source_fastr   )r$   r   c             c   sL   | j j}|d k	r| j |fV  x*| j jƒ D ]}|j}|d k	r(||fV  q(W d S )N)r   Újinja_loaderÚiter_blueprints)r   r$   r   Ú	blueprintr   r   r   r*   a   s    z$DispatchingJinjaLoader._iter_loaders)r   c             C   sj   t ƒ }| jj}|d k	r$|j|jƒ ƒ x<| jjƒ D ].}|j}|d k	r0x|jƒ D ]}|j|ƒ qLW q0W t|ƒS )N)Úsetr   r-   ÚupdateÚlist_templatesr.   ÚaddÚlist)r   Úresultr   r/   r$   r   r   r   r2   m   s    z%DispatchingJinjaLoader.list_templates)r   r   r   r   r   r   Ústrr    ÚTupleÚOptionalÚCallabler(   r&   r'   Ú	Generatorr   r*   ÚListr2   r   r   r   r   r"   .   s   (((	$
r"   r   )r$   Úcontextr   r   c             C   s.   t j|| |d� | j|ƒ}tj|| |d� |S )z)Renders the template and fires the signal)r$   r<   )r	   ÚsendÚrenderr
   )r$   r<   r   r   r   r   r   Ú_render|   s    
r?   )Útemplate_name_or_listr<   r   c             K   s*   t j}|jj|ƒ t|jjj| ƒ||jƒS )a–  Renders a template from the template folder with the given
    context.

    :param template_name_or_list: the name of the template to be
                                  rendered, or an iterable with template names
                                  the first one existing will be rendered
    :param context: the variables that should be available in the
                    context of the template.
    )r   r   r   Úupdate_template_contextr?   Ú	jinja_envZget_or_select_template)r@   r<   Úctxr   r   r   Úrender_template…   s    rD   )Úsourcer<   r   c             K   s*   t j}|jj|ƒ t|jjj| ƒ||jƒS )aF  Renders a template from the given template source string
    with the given context. Template variables will be autoescaped.

    :param source: the source code of the template to be
                   rendered
    :param context: the variables that should be available in the
                    context of the template.
    )r   r   r   rA   r?   rB   Zfrom_string)rE   r<   rC   r   r   r   Úrender_template_stringš   s    	rF   )Útypingr    Zjinja2r   r   r   r   r   Úglobalsr   r   Zsignalsr	   r
   ZTYPE_CHECKINGr   r   Zscaffoldr   ÚDictr6   r!   r   r"   Údictr?   ÚUnionr;   rD   rF   r   r   r   r   Ú<module>   s$   N
