dbfdg 3 sݨ\@sddlZddlmZddlmZmZddlmZddlm Z ddl m Z m Z ddl mZmZddlmZmZd d ZGd d d e ZGd ddeZGdddeZdS)N) literal_eval)islicechain)nodes) text_type) CodeGenerator has_safe_repr) EnvironmentTemplate)concatescapec Csntt|d}|sdSt|dkr,|d}ndjddt||D}yt|Stttfk rh|SXdS)a6Return 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. NrcSsg|] }t|qS)r).0vrr!/usr/lib/python3.6/nativetypes.py sz!native_concat..) listrlenjoinrr ValueError SyntaxError MemoryError)rheadoutrrr native_concat s  rc@seZdZdZddZdS)NativeCodeGeneratorzA code generator which avoids injecting ``to_string()`` calls around the internal code Jinja uses to render templates. cs$jr|jrdSjjtdd}tdd}tdd}dk rl|sL|rRd}qt|rffdd}qt}ndd}d}|jrjd jd }g}x|jD]} y0|dkrtj| j |j } t | stjWn"tjk r|j | wYnXy4|j j r"t| d r| j} nt| } || } Wn"tk rN|j | wYnX|rvt|dtrv|dj | q|j | gqWt|d ks|jdk r|jdk rt|d kr̈jd|jnjd|jjx|D]} t| tr2tt| } |jdkr"jd| nj| dn|jdkrLjd| n j| d} dk rjd|r~jd| d 7} j| || dkrjd| |jdk rjdqW|jdk rjjt|d krdpdng}g}xF|D]>} t| tr2|j t| jddn|j d|j | q Wjdjtt|djx|D]}j|d} dk r܈jd|rjdn"|rĈjdn|rԈjd| d 7} j||jd| dqzWjjd|r jdS) z}Same as :meth:`CodeGenerator.visit_Output`, but do not call ``to_string`` on output nodes in generated code. NZcontextfunctionFZevalcontextfunctionZenvironmentfunctioncs j|S)N) environment)x)finalizeselfrrconst_finalize7sz8NativeCodeGenerator.visit_Output..const_finalizecSs|S)Nr)r rrrr#<szif parent_template is None:T__html__rz %s.append(z %s.extend((zyield ,rzenvironment.finalize(z context, )z))%z%%z%sz % (zcontext.eval_ctx, z environment, z, r))Zhas_known_extendsZrequire_output_checkrr!getattrZ writelineindentrZ ImpossibleZas_constZeval_ctxrappendZ autoescapehasattrr$r Exception isinstancerrbufferreprrnewlinewriteZvisitZoutdentreplacer )r"ZnodeframeZfinalize_contextZ finalize_evalZ finalize_envr#Z outdent_laterZbodyZchildconstitemvalcloseformatZ argumentsZargumentr)r!r"r visit_Output's                            "             z NativeCodeGenerator.visit_OutputN)__name__ __module__ __qualname____doc__r;rrrrr"src@seZdZddZdS)NativeTemplatec OsJt||}yt|j|j|Stk r:tj}YnX|jj|dS)aBRender 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. T) dictrZroot_render_funcZ new_contextr.sysexc_inforZhandle_exception)r"argskwargsvarsrCrrrrenders  zNativeTemplate.renderN)r<r=r>rGrrrrr@sr@c@seZdZdZeZeZdS)NativeEnvironmentz=An environment that renders templates to native Python types.N)r<r=r>r?rZcode_generator_classr@Ztemplate_classrrrrrHsrH)rBZastr itertoolsrrZjinja2rZjinja2._compatrZjinja2.compilerrrZjinja2.environmentr r Z jinja2.utilsr r rrr@rHrrrrs   %