slbdZddlmZddlZddlmZmZmZmZm Z m Z m Z m Z m Z GddeZGddeZGd d ZGd d ZdS) zA simple Python template renderer, for a nano-subset of Django syntax. For a detailed discussion of this code, see this chapter from 500 Lines: http://aosabook.org/en/500L/a-template-engine.html ) annotationsN) AnyCallableDictListNoReturnOptionalSetUnioncastceZdZdZdS)TempliteSyntaxErrorz*Raised when a template has a syntax error.N__name__ __module__ __qualname____doc__/srv/buildsys-work-dir/castor/build_node/builder-2/WGSG1/unpkd_srcs/cloudlinux-venv-1.0.6/venv/lib/python3.11/site-packages/coverage/templite.pyrrs44DrrceZdZdZdS)TempliteValueErrorz7Raised when an expression won't evaluate in a template.NrrrrrrsAADrrcPeZdZdZdddZdd Zdd Zdd Zd ZddZ ddZ ddZ dS) CodeBuilderzBuild source code conveniently.rindentintreturnNonec"g|_||_dSN)code indent_level)selfrs r__init__zCodeBuilder.__init__#s35 "rstrcJdd|jDS)Nc34K|]}t|VdSr )r%).0cs r z&CodeBuilder.__str__..(s(11!s1vv111111r)joinr!r#s r__str__zCodeBuilder.__str__'s%ww11ty111111rlinecP|jd|jz|dgdS)zwAdd a line of source to the code. Indentation and newline will be added for you, don't provide them.   N)r!extendr")r#r/s radd_linezCodeBuilder.add_line*s/ # 114>?????rcbt|j}|j||S)z!Add a section, a sub-CodeBuilder.)rr"r!append)r#sections r add_sectionzCodeBuilder.add_section2s-d/00 !!!rc0|xj|jz c_dS)z0Increase the current indent for following lines.Nr" INDENT_STEPr-s rrzCodeBuilder.indent: T--rc0|xj|jzc_dS)z0Decrease the current indent for following lines.Nr;r-s rdedentzCodeBuilder.dedent>r=rDict[str, Any]cb|jdksJt|}i}t|||S)z:Execute the code, and return a dict of globals it defines.r)r"r%exec)r# python_sourceglobal_namespaces r get_globalszCodeBuilder.get_globalsBs@ A%%%%D +- ],---rN)r)rrrr)rr%)r/r%rr)rrrr)rr@) rrrrr$r.r4r8r<rr?rErrrrr s))#####2222@@@@ K........       rrcDeZdZdZddZdd ZddZddZdd dZd!dZ dS)"TempliteaA simple template renderer, for a nano-subset of Django syntax. Supported constructs are extended variable access:: {{var.modifier.modifier|filter|filter}} loops:: {% for var in list %}...{% endfor %} and ifs:: {% if var %}...{% endif %} Comments are within curly-hash markers:: {# This will be ignored #} Lines between `{% joined %}` and `{% endjoined %}` will have lines stripped and joined. Be careful, this could join words together! Any of these constructs can have a hyphen at the end (`-}}`, `-%}`, `-#}`), which will collapse the white space following the tag. Construct a Templite with the template text, then use `render` against a dictionary context to create a finished string:: templite = Templite('''

Hello {{name|upper}}!

{% for topic in topics %}

You are interested in {{topic}}.

{% endif %} ''', {"upper": str.upper}, ) text = templite.render({ "name": "Ned", "topics": ["Python", "Geometry", "Juggling"], }) textr%contextsr@rrc H i|_|D]}|j|t|_t|_t d}ddddgd0fd }g}tj d |}d x}} |D]} | d r%d \} } | d dk}|rd } | drD| drH| | | |  } d| z|| | |  }|ddkrt|dkr|d| |dd| |dzj|ddkrt|dks |ddkr|d| |d||d|jd|d| |d?|ddkr|dd} d|d d rt|dkr|d!| |ddd"}|s|d#| |}||kr|d$||dkrd }   |d%|d>| r)tjd&d'| } n|r| } | r"t/| |r|d(|d)||j|jz D]}|d*|d+|d,d-t1t2t4t6t8ft2d.t8fgt6fd/|_d"S)1zConstruct a Templite with the given `text`. `contexts` are dictionaries of values to use for future renderings. These are good for filters and global values. z&def render_function(context, do_dots):z result = []zappend_result = result.appendzextend_result = result.extendz to_str = strrrctdkrddzn>tdkr+ddzdd=dS)z%Force `buffered` to the code builder.zappend_result(%s)rzextend_result([%s]), N)lenr4r,)bufferedr!sr flush_outputz'Templite.__init__..flush_outputsw8}}!! 1HQK?@@@@X"" 3dii6I6IIJJJ rz(?s)({{.*?}}|{%.*?%}|{#.*?#})F{)-z{#z{{z to_str(%s)rifrSzDon't understand ifzif %s:rMforr9inzDon't understand forzfor c_{} in {}:joinedTendzDon't understand endNz Too many endszMismatched end tagzDon't understand tagz\s*\n\s*r'zUnmatched action tagc_z = context[]zreturn ''.join(result).render_functionrF)contextupdatesetall_vars loop_varsrr4rr8resplit startswith _expr_codestripr6rO _syntax_error _variableformatpopr?sublstripreprr rrr%rrE_render_function)r#rIrJra vars_coderQ ops_stacktokenssquash in_joinedtokenstartr\exprwordsend_what start_whatvar_namerPr!s @@rr$zTemplite.__init__xs  ) )G L   ( ( ( ("%%% #&55}} >??? $$&&  m$$$ 5666 5666 n%%%         :DAA""B 1B 1E$$A 1" s)s*C##D))3M%%d++0M??5s+;+A+A+C+CDDDOOL4$78888!LNNN!%),2244::<.> ../EuMMM!((///uQx@@@ -44 %a $a 9 9  qX--!((222$( q,,U33Mu::?? ../EuMMM#(8ABB<(G ..FFF%.]]__ %11 ../CXNNN#x//(-II KKMMMM**+A58LLLL+F;EKKMMBBEE+!LLNNE1OODKK000  F   5y} E E E  6 H HH   FHFFFFF G G G G ./// $ c3h#s(!34      0 1 ! ! rrzcd|vrb|d}||d}|ddD]&}|||jd|d|d}'nd|vra|d}||d}d d |ddD}d |d |d}n |||jd |z}|S) z(Generate a Python expression for `expr`.|rrMNr^().rNc34K|]}t|VdSr )rq)r)ds rr+z&Templite._expr_code..s(77T!WW777777rzdo_dots(zc_%s)rgrirlrdr,)r#rzpipesr!funcdotsargss rrizTemplite._expr_codes $;;JJsOOE??58,,Dabb  + +tT]333*D**4*** +D[[::c??D??47++D9977d122h77777D-d--d---DD NN4 / / /D=D rmsgthingrrc*t|d|)z6Raise a syntax error using `msg`, and showing `thing`.z: )r)r#rrs rrkzTemplite._syntax_error s!S"5"5E"5"5666rnamevars_setSet[str]ctjd|s|d|||dS)zTrack that `name` is used as a variable. Adds the name to `vars_set`, a set of variable names. Raises an syntax error if `name` is not a valid name. z[_a-zA-Z][_a-zA-Z0-9]*$zNot a valid nameN)rfmatchrkadd)r#rrs rrlzTemplite._variablesHx2D99 9   14 8 8 8 TrNraOptional[Dict[str, Any]]ct|j}|r|||||jS)zRender this template by applying it to `context`. `context` is a dictionary of values to use in this rendering. )dictrarbrr_do_dots)r#rarender_contexts rrenderzTemplite.rendersGdl++  +  ! !' * * *$$^T]CCCrvaluerc |D]v} t||}nI#t$r< ||}n/#ttf$r}t d|d||d}~wwxYwYnwxYwt |r |}w|S)z(Evaluate dotted expressions at run-time.zCouldn't evaluate rN)getattrAttributeError TypeErrorKeyErrorrcallable)r#rrdotexcs rrzTemplite._do_dots's C s++!   !#JEE!8,,rsS#"""""       *           + + + + + + + + \ggggggggggr