U
    ï¤ùdO  ã                   @  sX  d dl mZ 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 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 ddœdd„ZG dd„ deƒZG dd„ deƒZdddddœdd„Zdd dd!œd"d#„Zdd dd$œd%d&„Zdddd'dœd(d)„Zdd d'd!œd*d+„Zdd d'd$œd,d-„Z dS ).é    )ÚannotationsN)Ú
BaseLoader)ÚEnvironment)ÚTemplate)ÚTemplateNotFoundé   )Ú_cv_app)Ú_cv_request)Úcurrent_app)Úrequest)Ústream_with_context)Úbefore_render_template)Útemplate_rendered)ÚFlask)ÚScaffoldzdict[str, t.Any]©Úreturnc                  C  sJ   t  d¡} t d¡}i }| dk	r*| j|d< |dk	rF|j|d< |j|d< |S )zSDefault template context processor.  Injects `request`,
    `session` and `g`.
    NÚgr   Úsession)r   Úgetr	   r   r   r   )ZappctxZreqctxÚrv© r   ú4/tmp/pip-unpacked-wheel-uq31yewi/flask/templating.pyÚ_default_template_ctx_processor   s    




r   c                   @  s"   e Zd ZdZdd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   út.AnyÚNone)ÚappÚoptionsr   c                 K  s,   d|kr|  ¡ |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__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r   r   r   r   r   &   s   r   c                   @  st   e Zd ZdZdddœdd„Zddd	d
œdd„Zddd	d
œdd„Zddd	d
œdd„Zdddœdd„Zddœdd„Z	dS )ÚDispatchingJinjaLoaderz\A loader that looks for templates in the application and all
    the blueprint folders.
    r   r   )r   r   c                 C  s
   || _ d S ©N)r   )r!   r   r   r   r   r    8   s    zDispatchingJinjaLoader.__init__r   Ústrz)tuple[str, str | None, t.Callable | None])ÚenvironmentÚtemplater   c                 C  s$   | j jd r|  ||¡S |  ||¡S )NZEXPLAIN_TEMPLATE_LOADING)r   ÚconfigÚ_get_source_explainedÚ_get_source_fast)r!   r)   r*   r   r   r   Ú
get_source;   s    z!DispatchingJinjaLoader.get_sourcec           	   	   C  s”   g }d }|   |¡D ]N\}}z| ||¡}|d kr4|}W n tk
rN   d }Y nX | |||f¡ q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trvÚsrcobjr   r   r/   r   r   r   r,   B   s    
z,DispatchingJinjaLoader._get_source_explainedc              	   C  sL   |   |¡D ]4\}}z| ||¡W   S  tk
r<   Y q
Y q
X q
t|ƒ‚d S r'   )r0   r.   r   )r!   r)   r*   Z_srcobjr   r   r   r   r-   Z   s    z'DispatchingJinjaLoader._get_source_fastz4t.Generator[tuple[Scaffold, BaseLoader], None, None])r*   r   c                 c  sH   | j j}|d k	r| j |fV  | j  ¡ D ]}|j}|d k	r&||fV  q&d S r'   )r   Újinja_loaderÚiter_blueprints)r!   r*   r   Ú	blueprintr   r   r   r0   d   s    z$DispatchingJinjaLoader._iter_loadersz	list[str]r   c                 C  sb   t ƒ }| jj}|d k	r$| | ¡ ¡ | j ¡ D ]*}|j}|d k	r.| ¡ D ]}| |¡ qHq.t|ƒS r'   )Úsetr   r4   ÚupdateÚlist_templatesr5   ÚaddÚlist)r!   Úresultr   r6   r*   r   r   r   r9   p   s    z%DispatchingJinjaLoader.list_templatesN)
r"   r#   r$   r%   r    r.   r,   r-   r0   r9   r   r   r   r   r&   3   s   
r&   r   r   r(   )r   r*   Úcontextr   c                 C  s@   |   |¡ tj| | j||d | |¡}tj| | j||d |S ©N©Z_async_wrapperr*   r=   )Úupdate_template_contextr   ÚsendÚensure_syncÚrenderr   )r   r*   r=   r   r   r   r   Ú_render   s    
   ÿ
   ÿrD   z%str | Template | list[str | Template]r   )Útemplate_name_or_listr=   r   c                 K  s    t  ¡ }|j | ¡}t|||ƒS )a  Render a template by name with the given context.

    :param template_name_or_list: The name of the template to render. If
        a list is given, the first name to exist will be rendered.
    :param context: The variables to make available in the template.
    )r
   Ú_get_current_objectÚ	jinja_envÚget_or_select_templaterD   ©rE   r=   r   r*   r   r   r   Úrender_template‹   s    
rJ   )Úsourcer=   r   c                 K  s    t  ¡ }|j | ¡}t|||ƒS )zÓRender a template from the given source string with the given
    context.

    :param source: The source code of the template to render.
    :param context: The variables to make available in the template.
    )r
   rF   rG   Úfrom_stringrD   ©rK   r=   r   r*   r   r   r   Úrender_template_stringš   s    rN   út.Iterator[str]c                   sJ   ˆ   ˆ¡ tjˆ ˆ jˆˆd ddœ‡ ‡‡fdd„}|ƒ }trFt|ƒ}|S )Nr?   rO   r   c                   3  s(   ˆ  ˆ¡E d H  tjˆ ˆ jˆˆd d S r>   )Úgenerater   rA   rB   r   ©r   r=   r*   r   r   rP   ®   s       ÿz_stream.<locals>.generate)r@   r   rA   rB   r   r   )r   r*   r=   rP   r   r   rQ   r   Ú_stream¦   s    
   ÿrR   c                 K  s    t  ¡ }|j | ¡}t|||ƒS )a“  Render a template by name with the given context as a stream.
    This returns an iterator of strings, which can be used as a
    streaming response from a view.

    :param template_name_or_list: The name of the template to render. If
        a list is given, the first name to exist will be rendered.
    :param context: The variables to make available in the template.

    .. versionadded:: 2.2
    )r
   rF   rG   rH   rR   rI   r   r   r   Ústream_template½   s    rS   c                 K  s    t  ¡ }|j | ¡}t|||ƒS )aZ  Render a template from the given source string with the given
    context as a stream. This returns an iterator of strings, which can
    be used as a streaming response from a view.

    :param source: The source code of the template to render.
    :param context: The variables to make available in the template.

    .. versionadded:: 2.2
    )r
   rF   rG   rL   rR   rM   r   r   r   Ústream_template_stringÐ   s    
rT   )!Ú
__future__r   ÚtypingÚtZjinja2r   r   r   r   r   Úglobalsr   r	   r
   r   Zhelpersr   Zsignalsr   r   ÚTYPE_CHECKINGr   r   Zscaffoldr   r   r&   rD   rJ   rN   rR   rS   rT   r   r   r   r   Ú<module>   s0   L