L i+,dZddlmZddlZddlmZmZmZmZGdde Z Gdde Z Gd d Z Gd d Z y) 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)AnyCallableNoReturncastceZdZdZy)TempliteSyntaxErrorz*Raised when a template has a syntax error.N__name__ __module__ __qualname____doc__W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/coverage/templite.pyr r s4rr ceZdZdZy)TempliteValueErrorz7Raised when an expression won't evaluate in a template.Nr rrrrrsArrcNeZdZdZd d dZd dZddZddZdZddZ ddZ dd Z y ) CodeBuilderzBuild source code conveniently.c g|_||_yN)code indent_level)selfindents r__init__zCodeBuilder.__init__"s-/ "rcFdjd|jDS)Nc32K|]}t|ywr)str).0cs r z&CodeBuilder.__str__..'s1!s1v1)joinrrs r__str__zCodeBuilder.__str__&sww1tyy111rcZ|jjd|jz|dgy)zwAdd a line of source to the code. Indentation and newline will be added for you, don't provide them.   N)rextendr)rlines radd_linezCodeBuilder.add_line)s( # 1 114>?rcft|j}|jj||S)z!Add a section, a sub-CodeBuilder.)rrrappend)rsections r add_sectionzCodeBuilder.add_section1s)d//0 !rcB|xj|jz c_y)z0Increase the current indent for following lines.Nr INDENT_STEPr&s rrzCodeBuilder.indent9 T---rcB|xj|jzc_y)z0Decrease the current indent for following lines.Nr4r&s rdedentzCodeBuilder.dedent=r6rcZ|jdk(sJt|}i}t|||S)z:Execute the code, and return a dict of globals it defines.r)rr exec)r python_sourceglobal_namespaces r get_globalszCodeBuilder.get_globalsAs8  A%%%D +- ],-rN)r)rintreturnNone)r?r )r,r r?r@)r?rr?r@)r?dict[str, Any]) r r r rrr'r-r1r5rr8r=rrrrrs0)#2@ K..  rrcBeZdZdZd dZd dZd dZd dZd ddZddZ y)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 %} if-else:: {% if var %}...{% else %}...{% 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"], }) c  i|_|D]}|jj|t|_t|_t j djj}j dj dj dj dgd4fd }g}tjd|}dx}} |D]M} | jd rd \} } | d d k(}|rd } | jd r9| jdr7|j| | | j} jd| z|| | | jj}|ddk(rit|dk7r|j!d| |jdj d|j|dzj|ddk(rot|dk7r|j!d| |r|ddk7r|j!d| j#j dj|ddk(rt|dk7s|ddk7r|j!d| |jd|j%|d|jj d|dd |j|d!d"j0|dd#k(r|jd#d$} M|djd%r~t|dk7r|j!d&| |dd!d'}|s|j!d(| |j'}||k7r|j!d)||d#k(rd} ͉j#|j!d*|d| r&tj(d+d,| j} n|r| j+} | s4jt-| P|r|j!d-|d||j|jz D]}|j d.|d/|d0j d1j#t/t0t2t4t6ft0d2t6fgt4fj9d3|_y')5zConstruct 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 = strctdk(rjddzn1tdkDr#jddjzdd=y)z%Force `buffered` to the code builder.zappend_result(%s)rzextend_result([%s]), N)lenr-r%)bufferedrsr flush_outputz'Templite.__init__..flush_outputsS8}! 1HQK?@X" 3dii6IIJ rz(?s)({{.*?}}|{%.*?%}|{#.*?#})F{)-z{#z{{z to_str(%s)rifrMzDon't understand ifzif %s:rGelsezDon't understand elsezMismatched elsezelse:forr2inzDon't understand forzfor c_z in :joinedTendzDon't understand endNz Too many endszMismatched end tagzDon't understand tagz\s*\n\s*rzUnmatched action tagc_z = context[]zreturn ''.join(result).render_functionrA)contextupdatesetall_vars loop_varsrr-rr1resplit startswith _expr_codestripr/rI _syntax_errorr8 _variablepopsublstripreprrrdictr rr=_render_function)rtextcontextsr] vars_coderK ops_stacktokenssquash in_joinedtokenstartrYexprwordsend_what start_whatvar_namerJrs @@rrzTemplite.__init__|s  )G LL   ( )#&% #&5} >? $$&  m$ 56 56 n%   :DA""G 1E$" s)s*C##D)%%d+??5s+;+A+A+CDDOOL4$78!N!%,224:: ../EuM!((/uQx@ $U1XJd4??583L2MQO qX-!((2$( q,,U3u:? ../EuM#(8AB<( ..F%.]]_ %1 ../CXN#x/(-I KKM**+A58LFF;EKKMBE!LLNEOODK0OG 1R    5y} E 6 HH   H:[ AF G H ./ $ c3h#s(!34     0 1 ! rcd|vrV|jd}|j|d}|ddD]'}|j||jd|d|d})|Sd|vrK|jd}|j|d}d j d |ddD}d |d |d}|S|j||jd |z}|S) z(Generate a Python expression for `expr`.|rrGNrZ().rHc32K|]}t|ywr)rl)r!ds rr#z&Templite._expr_code.. s7T!W7r$zdo_dots(zc_%s)rcrerhr`r%)rrxpipesrfuncdotsargss rrezTemplite._expr_codes $;JJsOE??58,Dab  +tT]]3D64&* + D[::c?D??47+D997d12h77DdV2dV1-D  NN4 /D=D rc"t|d|)z6Raise a syntax error using `msg`, and showing `thing`.z: )r )rmsgthings rrgzTemplite._syntax_errors!SEE9"566rcvtjd|s|jd||j|y)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)rbmatchrgadd)rnamevars_sets rrhzTemplite._variables0xx2D9   14 8 TrNct|j}|r|j||j||jS)zRender this template by applying it to `context`. `context` is a dictionary of values to use in this rendering. )rmr]r^rn_do_dots)rr]render_contexts rrenderzTemplite.render$s:dll+   ! !' *$$^T]]CCrc |D]"} t||}t |s|}$|S#t$r3 ||}n)#ttf$r}t d|d||d}~wwxYwYRwxYw)z(Evaluate dotted expressions at run-time.zCouldn't evaluate rN)getattrAttributeError TypeErrorKeyErrorrcallable)rvaluerdotexcs rrzTemplite._do_dots0s C s+  " !#JE!8,,,UIQse< s/ * A&:A&A  AA  A&%A&)ror rprBr?r@)rxr r?r )rr rrr?r)rr rzset[str]r?r@r)r]zdict[str, Any] | Noner?r )rrrr r?r) r r r rrrergrhrrrrrrDrDMs),\D L$7  DrrD)r __future__rrbtypingrrrr ValueErrorr rrrDrrrrsG# 00 *   + + \qqr