o
    Fa                     @   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 ddlmZ ddlmZ dejej dejej fddZG dd deZG dd deZG dd deZee_dS )    N)literal_eval)parse)chain)islice   )nodes)CodeGenerator)Frame)has_safe_repr)Environment)Templatevaluesreturnc              
   C   s   t t| d}|sdS t|dkr|d }t|ts|S nddd t|| D }z	tt|dd	W S  t	t
tfyA   | Y S w )
an  Return a native Python type from the list of compiled nodes. If
    the result is a single node, its value is returned. Otherwise, the
    nodes are concatenated as strings. If the result can be parsed with
    :func:`ast.literal_eval`, the parsed value is returned. Otherwise,
    the string is returned.

    :param values: Iterable of outputs to concatenate.
       Nr   r    c                 S      g | ]}t |qS  str.0vr   r   4/usr/lib/python3/dist-packages/jinja2/nativetypes.py
<listcomp>"       z!native_concat.<locals>.<listcomp>eval)mode)listr   len
isinstancer   joinr   r   r   
ValueErrorSyntaxErrorMemoryError)r   headrawr   r   r   native_concat   s    	

r&   c                   @   s   e Zd ZdZedejdejfddZdejej de	fddZ
d	ejd
edejdejfddZd	ejd
edejddfddZd	ejd
edejddfddZdS )NativeCodeGeneratorzaA code generator which renders Python types by not adding
    ``str()`` around output nodes.
    valuer   c                 C   s   | S Nr   )r(   r   r   r   _default_finalize4   s   z%NativeCodeGenerator._default_finalizegroupc                 C   s   t ddd |D S )Nr   c                 S   r   r   r   r   r   r   r   r   9   r   z:NativeCodeGenerator._output_const_repr.<locals>.<listcomp>)reprr    )selfr+   r   r   r   _output_const_repr8   s   z&NativeCodeGenerator._output_const_reprnodeframefinalizec                 C   s6   | |j}t|st t|tjr|S ||S r)   )as_consteval_ctxr
   r   
Impossibler   TemplateDataconst)r-   r/   r0   r1   r6   r   r   r   _output_child_to_const;   s   
z*NativeCodeGenerator._output_child_to_constNc                 C   s   |j d ur| |j  d S d S r)   srcwriter-   r/   r0   r1   r   r   r   _output_child_preH   s   
z%NativeCodeGenerator._output_child_prec                 C   s   |j d ur| d d S d S )N)r8   r;   r   r   r   _output_child_postN   s   
z&NativeCodeGenerator._output_child_post)__name__
__module____qualname____doc__staticmethodtAnyr*   Iterabler   r.   r   Exprr	   r   _FinalizeInfor7   r<   r>   r   r   r   r   r'   /   s@    

r'   c                   @   s   e Zd ZdZeZdS )NativeEnvironmentz=An environment that renders templates to native Python types.N)r?   r@   rA   rB   r'   code_generator_classr   r   r   r   rI   U   s    rI   c                   @   sH   e Zd ZeZdejdejdejfddZdejdejdejfddZdS )	NativeTemplateargskwargsr   c                 O   sB   |  t|i |}zt| |W S  ty    | j  Y S w )aB  Render the template to produce a native Python type. If the
        result is a single node, its value is returned. Otherwise, the
        nodes are concatenated as strings. If the result can be parsed
        with :func:`ast.literal_eval`, the parsed value is returned.
        Otherwise, the string is returned.
        )new_contextdictr&   root_render_func	Exceptionenvironmenthandle_exceptionr-   rL   rM   ctxr   r   r   render^   s   zNativeTemplate.renderc                    sd   | j js	td| t|i |}ztdd | |2 I d H W S  ty1   | j   Y S w )Nz8The environment was not created with async mode enabled.c                    s   g | z3 d H W }|q6 S r)   r   )r   nr   r   r   r   v   s    z/NativeTemplate.render_async.<locals>.<listcomp>)	rR   is_asyncRuntimeErrorrN   rO   r&   rP   rQ   rS   rT   r   r   r   render_asyncl   s   zNativeTemplate.render_asyncN)	r?   r@   rA   rI   environment_classrD   rE   rV   rZ   r   r   r   r   rK   [   s     rK   )typingrD   astr   r   	itertoolsr   r   r   r   compilerr   r	   r
   rR   r   r   rF   rE   Optionalr&   r'   rI   rK   template_classr   r   r   r   <module>   s     " &
!