
    L-Ph8*                        d Z ddlmZ ddlZddlmZmZmZmZ  G d de	          Z
 G d de	          Z G d	 d
          Z G d d          ZdS )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)AnyCallableNoReturncastc                      e Zd ZdZdS )TempliteSyntaxErrorz*Raised when a template has a syntax error.N__name__
__module____qualname____doc__     Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/coverage/templite.pyr	   r	      s        44Dr   r	   c                      e Zd ZdZdS )TempliteValueErrorz7Raised when an expression won't evaluate in a template.Nr
   r   r   r   r   r      s        AADr   r   c                  P    e Zd ZdZdddZdd	ZddZddZdZddZ	ddZ
ddZdS )CodeBuilderzBuild source code conveniently.r   indentintreturnNonec                "    g | _         || _        d S N)codeindent_level)selfr   s     r   __init__zCodeBuilder.__init__#   s    -/	"r   strc                J    d                     d | j        D                       S )N c              3  4   K   | ]}t          |          V  d S r   )r    ).0cs     r   	<genexpr>z&CodeBuilder.__str__.<locals>.<genexpr>(   s(      11!s1vv111111r   )joinr   r   s    r   __str__zCodeBuilder.__str__'   s%    ww11ty111111r   linec                P    | j                             d| j        z  |dg           dS )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     r   add_linezCodeBuilder.add_line*   s/     		# 114>?????r   c                b    t          | j                  }| j                            |           |S )z!Add a section, a sub-CodeBuilder.)r   r   r   append)r   sections     r   add_sectionzCodeBuilder.add_section2   s-    d/00	!!!r      c                0    | xj         | j        z  c_         dS )z0Increase the current indent for following lines.Nr   INDENT_STEPr(   s    r   r   zCodeBuilder.indent:       T--r   c                0    | xj         | j        z  c_         dS )z0Decrease the current indent for following lines.Nr6   r(   s    r   dedentzCodeBuilder.dedent>   r8   r   dict[str, Any]c                b    | j         dk    sJ t          |           }i }t          ||           |S )z:Execute the code, and return a dict of globals it defines.r   )r   r    exec)r   python_sourceglobal_namespaces      r   get_globalszCodeBuilder.get_globalsB   s@      A%%%%D		+-],---r   N)r   )r   r   r   r   )r   r    )r*   r    r   r   )r   r   r   r   )r   r;   )r   r   r   r   r   r)   r/   r3   r7   r   r:   r@   r   r   r   r   r       s        ))# # # # #2 2 2 2@ @ @ @    K. . . .. . . .	  	  	  	  	  	 r   r   c                  D    e Zd ZdZddZdd
ZddZddZdd dZd!dZ	dS )"Templitea  A 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('''
            <h1>Hello {{name|upper}}!</h1>
            {% for topic in topics %}
                <p>You are interested in {{topic}}.</p>
            {% endif %}
            ''',
            {"upper": str.upper},
        )
        text = templite.render({
            "name": "Ned",
            "topics": ["Python", "Geometry", "Juggling"],
        })

    textr    contextsr;   r   r   c           
     .   i | _         |D ]}| j                             |           t                      | _        t                      | _        t                                          d                                                                            }                    d                               d                               d                               d           g d2fd}g }t          j
        d	|          }d
x}}	|D ]}
|
                    d          rd\  }}|
d         dk    }|rd}|
                    d          rD|
                    d          rH|                     |
||                                                   }                    d|z              |             |
||                                         
                                }|d         dk    rt          |          dk    r|                     d|
           |                    d                               d|                     |d                   z                                              j|d         dk    rt          |          dk    s|d         dk    r|                     d|
           |                    d           |                     |d         | j                                       d|d          d|                     |d                    d                                            2|d         d k    r|                    d            d!}	W|d                             d"          rt          |          dk    r|                     d#|
           |d         dd$         }|s|                     d%|
           |                                }||k    r|                     d&|           |d k    rd
}	                                 |                     d'|d                    1|	r)t          j        d(d)|
                                          }
n|r|
                                }
|
r"                    t-          |
                     |r|                     d*|d+                     |             | j        | j        z
  D ]}|                    d,| d-|d.                               d/                                            t/          t0          t2          t4          t6          f         t0          d0t6          f         gt4          f                                         d1                   | _        d$S )3zConstruct 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 = []zappend_result = result.appendzextend_result = result.extendzto_str = strr   r   c                     t                     dk    r                    d d         z             n>t                     dk    r+                    dd                               z              dd= dS )z%Force `buffered` to the code builder.   zappend_result(%s)r   zextend_result([%s]), N)lenr/   r'   )bufferedr   s   r   flush_outputz'Templite.__init__.<locals>.flush_output   sw    8}}!!1HQK?@@@@X""3dii6I6IIJJJr   z(?s)({{.*?}}|{%.*?%}|{#.*?#})F{)   -z{#z{{z
to_str(%s)r   ifrN   zDon't understand ifzif %s:rH   forr4   inzDon't understand forzfor c_z in    :joinedTendzDon't understand endNzToo many endszMismatched end tagzDon't understand tagz\s*\n\s*r"   zUnmatched action tagc_z = context[]zreturn ''.join(result).render_functionrA   )contextupdatesetall_vars	loop_varsr   r/   r   r3   resplit
startswith
_expr_codestripr1   rJ   _syntax_error	_variablepopr:   sublstripreprr   r   dictr    r   r@   _render_function)r   rD   rE   r]   	vars_coderL   	ops_stacktokenssquash	in_joinedtokenstartrX   exprwordsend_what
start_whatvar_namerK   r   s                     @@r   r   zTemplite.__init__x   s     	) 	)GL(((("%%%#&55 }}>???$$&&	m$$$56665666n%%% 	 	 	 	 	 	 	 	 :DAA"" ?	1 ?	1E$$ >1"
s)s* C##D)) 0M%%d++ -M??5s+;+A+A+C+CDDDOOL4$78888 !LNNN!%),2244::<<EQx4''u::?? ../DeLLL!((...hq1J1J&JKKKqU**u::??eAh$.>.> ../EuMMM!((///uQx@@@OU1XOO4??583L3LOOO   qX--!((222$(		q,,U33 Mu::?? ../EuMMM#(8ABB<( G ..FFF%.]]__
%11 ../CXNNN#x//(-II KKMMMM**+A58LLLL  +F;EKKMMBBEE +!LLNNE 1OODKK000 	F5y}EEE6 	H 	HHFHFFFFFGGGG./// $c3h#s(!34 01!
 !
r   rv   c                   d|v rb|                     d          }|                     |d                   }|dd         D ]&}|                     || j                   d| d| d}'nd|v ra|                     d          }|                     |d                   }d	                    d
 |dd         D                       }d| d	| d}n |                     || j                   d|z  }|S )z(Generate a Python expression for `expr`.|r   rH   NrZ   ().rI   c              3  4   K   | ]}t          |          V  d S r   )rl   )r$   ds     r   r&   z&Templite._expr_code.<locals>.<genexpr>  s(      77T!WW777777r   zdo_dots(zc_%s)rc   re   rh   r`   r'   )r   rv   pipesr   funcdotsargss          r   re   zTemplite._expr_code   s   $;;JJsOOE??58,,Dabb	 + +tT]333*D**4***+ D[[::c??D??47++D9977d122h77777D-d--d---DDNN4///D=Dr   msgthingr   r   c                *    t          | d|          )z6Raise a syntax error using `msg`, and showing `thing`.z: )r	   )r   r   r   s      r   rg   zTemplite._syntax_error  s    !S"5"5E"5"5666r   namevars_setset[str]c                    t          j        d|          s|                     d|           |                    |           dS )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]*$zNot a valid nameN)rb   matchrg   add)r   r   r   s      r   rh   zTemplite._variable  sH     x2D99 	914888Tr   Nr]   dict[str, Any] | Nonec                    t          | j                  }|r|                    |           |                     || j                  S )zRender this template by applying it to `context`.

        `context` is a dictionary of values to use in this rendering.

        )rm   r]   r^   rn   _do_dots)r   r]   render_contexts      r   renderzTemplite.render  sG     dl++ 	+!!'***$$^T]CCCr   valuer   c           	         |D ]v}	 t          ||          }nI# t          $ r< 	 ||         }n/# t          t          f$ r}t	          d|d|           |d}~ww xY wY nw xY wt          |          r
 |            }w|S )z(Evaluate dotted expressions at run-time.zCouldn't evaluate r   N)getattrAttributeError	TypeErrorKeyErrorr   callable)r   r   r   dotexcs        r   r   zTemplite._do_dots$  s     	  	 Cs++!   !#JEE!8,   ,<U<<s<<  E   s.   
A+AAAAAA)rD   r    rE   r;   r   r   )rv   r    r   r    )r   r    r   r   r   r   )r   r    r   r   r   r   r   )r]   r   r   r    )r   r   r   r    r   r   )
r   r   r   r   r   re   rg   rh   r   r   r   r   r   rC   rC   N   s        ( (R|
 |
 |
 |
|   $7 7 7 7
 
 
 

D 
D 
D 
D 
D     r   rC   )r   
__future__r   rb   typingr   r   r   r   
ValueErrorr	   r   r   rC   r   r   r   <module>r      s!    # " " " " " 				           
	 	 	 	 	* 	 	 	
	 	 	 	 	 	 	 	
+  +  +  +  +  +  +  + \d d d d d d d d d dr   