
    w:i^                        d Z ddlmZ ddlZddlmZmZmZmZm	Z	m
Z
 ddlmZmZmZmZmZmZ ddlmZ g dZdZ G d	 d
e          Z G d de          Z G d de          Z G d de          Z G d de          Zd ZddZddZ G d de           Z! ee!           G d de                      Z" G d de"          Z#e#j$        Z$e#j%        Z%e#j&        Z&e#j'        Z'dS )zBasic templating functionality.    )dequeN)add_metaclassnumeric_typesstring_types	text_typeStringIOBytesIO)AttrsStreamStreamEventKindSTARTTEXT_ensure)
ParseError)ContextDirectiveFactoryTemplateTemplateErrorTemplateRuntimeErrorTemplateSyntaxErrorBadDirectiveErrorzrestructuredtext enc                       e Zd ZdZddZdS )r   z?Base exception class for errors related to template processing.Nc                     |d}|| _         |dk    s|dk    rd| j         ||fz  }t                              | |           || _        || _        || _        dS )a9  Create the exception.
        
        :param message: the error message
        :param filename: the filename of the template
        :param lineno: the number of line in the template at which the error
                       occurred
        :param offset: the column number at which the error occurred
        Nz<string>r   z%s (%s, line %d))msg	Exception__init__filenamelinenooffsetselfmessager   r   r    s        L/var/www/html/trac/venv/lib/python3.11/site-packages/genshi/template/base.pyr   zTemplateError.__init__   sk     !Hz!!Vq[[(DHh+GGG4)))     Nr   r   __name__
__module____qualname____doc__r    r%   r$   r   r      s.        II     r%   r   c                       e Zd ZdZddZdS )r   z|Exception raised when an expression in a template causes a Python syntax
    error, or the template is not well-formed.
    Nr   c                     t          |t                    r2|j        +t          |                              d|j        z  d          }t
                              | |||           dS )a8  Create the exception
        
        :param message: the error message
        :param filename: the filename of the template
        :param lineno: the number of line in the template at which the error
                       occurred
        :param offset: the column number at which the error occurred
        Nz
 (line %d) )
isinstanceSyntaxErrorr   strreplacer   r   r!   s        r$   r   zTemplateSyntaxError.__init__8   s`     g{++ 	N0J'll**<'.+H"MMGtWh?????r%   r&   r'   r,   r%   r$   r   r   3   s8         @ @ @ @ @ @r%   r   c                       e Zd ZdZddZdS )r   zException raised when an unknown directive is encountered when parsing
    a template.
    
    An unknown directive is any attribute using the namespace for directives,
    with a local name that doesn't match any registered directive.
    Nr   c                 F    t                               | d|z  ||           dS )zCreate the exception
        
        :param name: the name of the directive
        :param filename: the filename of the template
        :param lineno: the number of line in the template at which the error
                       occurred
        zbad directive "%s"N)r   r   )r"   namer   r   s       r$   r   zBadDirectiveError.__init__N   s5     	$$T+?$+F%-v	7 	7 	7 	7 	7r%   )Nr   r'   r,   r%   r$   r   r   F   s2         	7 	7 	7 	7 	7 	7r%   r   c                       e Zd ZdZdS )r   zfException raised when an the evaluation of a Python expression in a
    template causes an error.
    N)r(   r)   r*   r+   r,   r%   r$   r   r   Z   s           r%   r   c                   t    e Zd ZdZd Zd Zd ZeZd Zd Z	d Z
d Zdd
ZddZd Zd Zd Zd Zd Zd Zd	S )r   ap  Container for template input data.
    
    A context provides a stack of scopes (represented by dictionaries).
    
    Template directives such as loops can push a new scope on the stack with
    data that should only be available inside the loop. When the loop
    terminates, that scope can get popped off the stack again.
    
    >>> ctxt = Context(one='foo', other=1)
    >>> ctxt.get('one')
    'foo'
    >>> ctxt.get('other')
    1
    >>> ctxt.push(dict(one='frost'))
    >>> ctxt.get('one')
    'frost'
    >>> ctxt.get('other')
    1
    >>> ctxt.pop()
    {'one': 'frost'}
    >>> ctxt.get('one')
    'foo'
    c                      t          |g           _         j        j         _         j        j         _        g  _        g  _         fd}d fd	}|                    d|           |                    d|           dS )zZInitialize the template context with the given keyword arguments as
        data.
        c                     | v S )z]Return whether a variable with the specified name exists in the
            expression scope.r,   )r6   r"   s    r$   definedz!Context.__init__.<locals>.defined   s     4<r%   Nc                 0                         | |          S )zIf a variable of the specified name is defined, return its value.
            Otherwise, return the provided default value, or ``None``.get)r6   defaultr"   s     r$   value_ofz"Context.__init__.<locals>.value_of   s     88D'***r%   r;   r@   N)	r   framespopleftpop
appendleftpush_match_templates_choice_stack
setdefault)r"   datar;   r@   s   `   r$   r   zContext.__init__y   s     TFmm;&K*	 "	  	  	  	  	 	+ 	+ 	+ 	+ 	+ 	+ 		7+++
H-----r%   c                 D    t          t          | j                            S rA   )reprlistrB   r"   s    r$   __repr__zContext.__repr__   s    D%%&&&r%   c                 <    |                      |          d         duS )zuReturn whether a variable exists in any of the scopes.
        
        :param key: the name of the variable
           N)_find)r"   keys     r$   __contains__zContext.__contains__   s    
 zz#q!--r%   c                 (    | j         D ]	}||v r||= 
dS )zaRemove a variable from all scopes.
        
        :param key: the name of the variable
        NrB   )r"   rS   frames      r$   __delitem__zContext.__delitem__   s0    
 [ 	 	Ee||#J	 	r%   c                 X    |                      |          \  }}|t          |          |S )a  Get a variables's value, starting at the current scope and going
        upward.
        
        :param key: the name of the variable
        :return: the variable value
        :raises KeyError: if the requested variable wasn't found in any scope
        )rR   KeyError)r"   rS   valuerW   s       r$   __getitem__zContext.__getitem__   s-     zz#u=3--r%   c                 D    t          |                                           S )zReturn the number of distinctly named variables in the context.
        
        :return: the number of variables in the context
        )lenitemsrN   s    r$   __len__zContext.__len__   s    
 4::<<   r%   c                 &    || j         d         |<   dS )zSet a variable in the current scope.
        
        :param key: the name of the variable
        :param value: the variable value
        r   NrV   )r"   rS   r[   s      r$   __setitem__zContext.__setitem__   s     $Asr%   Nc                 >    | j         D ]}||v r||         |fc S |dfS )a&  Retrieve a given variable's value and the frame it was found in.

        Intended primarily for internal use by directives.
        
        :param key: the name of the variable
        :param default: the default value to return when the variable is not
                        found
        NrV   r"   rS   r?   rW   s       r$   rR   zContext._find   sB     [ 	) 	)Ee||Sz5(((( }r%   c                 6    | j         D ]}||v r
||         c S |S )zGet a variable's value, starting at the current scope and going
        upward.
        
        :param key: the name of the variable
        :param default: the default value to return when the variable is not
                        found
        rV   rd   s       r$   r>   zContext.get   s8     [ 	" 	"Ee||Sz!!! r%   c                 B    g | j         D ]}fd|D             z  S )zlReturn the name of all variables in the context.
        
        :return: a list of variable names
        c                     g | ]}|v|	S r,   r,   ).0rS   keyss     r$   
<listcomp>z Context.keys.<locals>.<listcomp>   s    ===SS__S___r%   rV   )r"   rW   ri   s     @r$   ri   zContext.keys   sA    
 [ 	> 	>E====E====DDr%   c                 D      fd                                  D             S )zReturn a list of ``(name, value)`` tuples for all variables in the
        context.
        
        :return: a list of variables
        c                 >    g | ]}|                     |          fS r,   r=   )rh   rS   r"   s     r$   rj   z!Context.items.<locals>.<listcomp>   s(    <<<dhhsmm$<<<r%   )ri   rN   s   `r$   r_   zContext.items   s'     =<<<		<<<<r%   c                 F    | j         d                             |           dS )z-Update the context from the mapping provided.r   N)rB   update)r"   mappings     r$   rn   zContext.update   s"    Ag&&&&&r%   c                     dS )zwPush a new scope on the stack.
        
        :param data: the data dictionary to push on the context stack.
        Nr,   )r"   rJ   s     r$   rF   zContext.push         r%   c                     dS )z&Pop the top-most scope from the stack.Nr,   rN   s    r$   rD   zContext.pop   rq   r%   c                    t                      }|j                                         |j                            | j                   |j                            | j                   |j                            | j                   |S )z%Create a copy of this Context object.)r   rB   rD   extendrG   rH   )r"   ctxts     r$   copyzContext.copy   sp    
 yy4;'''$$T%:;;;!!$"4555r%   rA   )r(   r)   r*   r+   r   rO   rT   has_keyrX   r\   r`   rb   rR   r>   ri   r_   rn   rF   rD   rv   r,   r%   r$   r   r   `   s        0. . .,' ' '. . . G    ! ! !$ $ $        = = =' ' '  5 5 5
 
 
 
 
r%   r   c                 V    |r& |d         t          |           |dd         |fi |} | S )ar  Apply the given directives to the stream.
    
    :param stream: the stream the directives should be applied to
    :param directives: the list of directives to apply
    :param ctxt: the `Context`
    :param vars: additional variables that should be available when Python
                 code is executed
    :return: the stream with the given directives applied
    r   rQ   N)iter)stream
directivesru   varss       r$   _apply_directivesr}     s?      KAtF||Z^TJJTJJMr%   c                     |r|                     |           |                     |          }|r|                                 |S )a  Evaluate the given `Expression` object.
    
    :param expr: the expression to evaluate
    :param ctxt: the `Context`
    :param vars: additional variables that should be available to the
                 expression
    :return: the result of the evaluation
    )rF   evaluaterD   )exprru   r|   retvals       r$   
_eval_exprr     sF      		$]]4  F 


Mr%   c                     |r*|                     |           |                     i            |                     |           |rJ|                                }|                                 |j        d                             |           dS dS )zExecute the given `Suite` object.
    
    :param suite: the code suite to execute
    :param ctxt: the `Context`
    :param vars: additional variables that should be available to the
                 code
    r   N)rF   executerD   rB   rn   )suiteru   r|   tops       r$   _exec_suiter   &  s      		$		"	MM$ #hhjj


Ac"""""# #r%   c                       e Zd ZdZd ZdS )DirectiveFactoryMetaz#Meta class for directive factories.c                     d|v r-t          |d                   |d<   d |d         D             |d<   t                              | |||          S )Nr{   _dir_by_namec                     g | ]
}|d          S )rQ   r,   )rh   	directives     r$   rj   z0DirectiveFactoryMeta.__new__.<locals>.<listcomp>>  s    MMM	y|MMMr%   
_dir_order)dicttype__new__)clsr6   basesds       r$   r   zDirectiveFactoryMeta.__new__;  sV    1 $Q|_ 5 5AnMMQ|_MMMAlO||Cua000r%   N)r(   r)   r*   r+   r   r,   r%   r$   r   r   8  s)        --1 1 1 1 1r%   r   c                   $    e Zd ZdZg Z	 d Zd ZdS )r   z]Base for classes that provide a set of template directives.
    
    :since: version 0.6
    c                 6    | j                             |          S )zReturn the directive class for the given name.
        
        :param name: the directive name as used in the template
        :return: the directive class
        :see: `Directive`
        )r   r>   )r"   r6   s     r$   get_directivezDirectiveFactory.get_directiveO  s      $$T***r%   c                 p    || j         v r| j                             |          S t          | j                   S )aU  Return a key for the given directive class that should be used to
        sort it among other directives on the same `SUB` event.
        
        The default implementation simply returns the index of the directive in
        the `directives` list.
        
        :param dir_cls: the directive class
        :return: the sort key
        )r   indexr^   )r"   dir_clss     r$   get_directive_indexz$DirectiveFactory.get_directive_indexX  s7     do%%?((1114?###r%   N)r(   r)   r*   r+   r{   r   r   r,   r%   r$   r   r   C  sH         
 J+ + +$ $ $ $ $r%   r   c                       e Zd ZdZ ed          Z	  ed          Z	  ed          Z	  ed          Z	 dZ	e
Z	 	 dd	Zd
 Zd Zd Zd Zd Zed             Zd ZddZd Zd Zd Zd ZdS )r   zAbstract template base class.
    
    This class implements most of the template processing model, but does not
    specify the syntax of templates.
    EXECEXPRINCLUDESUBNstrictTc                    |p|| _         || _        || _        || _        || _        |                                  |                                  d| _        t          |t                    sDt          |d          s4t          |t                    rt          |          }nt          |          }	 |                     ||          | _        dS # t           $ r+}t#          |j        | j         |j        |j                  d}~ww xY w)a  Initialize a template from either a string, a file-like object, or
        an already parsed markup stream.
        
        :param source: a string, file-like object, or markup stream to read the
                       template from
        :param filepath: the absolute path to the template file
        :param filename: the path to the template file relative to the search
                         path
        :param loader: the `TemplateLoader` to use for loading included
                       templates
        :param encoding: the encoding of the `source`
        :param lookup: the variable lookup mechanism; either "strict" (the
                       default), "lenient", or a custom lookup class
        :param allow_exec: whether Python code blocks in templates should be
                           allowed
        
        :note: Changed in 0.5: Added the `allow_exec` argument
        FreadN)filepathr   loaderlookup
allow_exec_init_filters_init_loader	_preparedr0   r   hasattrr   r   r	   _parse_streamr   r   r   r   r    )	r"   sourcer   r   r   encodingr   r   es	            r$   r   zTemplate.__init__  s   ( !,H $&&)) 	)'&&2I2I 	)&),, )!&)) 	P;;vx88DLLL 	P 	P 	P%aeT]AHahOOO	Ps   /C 
D&C<<Dc                 B    | j                                         }g |d<   |S )Nfilters)__dict__rv   r"   states     r$   __getstate__zTemplate.__getstate__  s$    ""$$ir%   c                 <    || _         |                                  d S rA   )r   r   r   s     r$   __setstate__zTemplate.__setstate__  s!    r%   c                 B    dt          |           j        d| j        dS )N<z "z">)r   r(   r   rN   s    r$   rO   zTemplate.__repr__  s$     "4jj1114===AAr%   c                 ,    | j         | j        g| _        d S rA   )_flatten_includer   rN   s    r$   r   zTemplate._init_filters  s    t}5r%   c                    | j         ddlm} | j        r| j        | j        k    r]t
          j                            | j                  d t          t
          j                            | j                                      }n't
          j        	                    | j                  }nd} |t
          j        
                    |          g          | _         d S d S )Nr   )TemplateLoader.)r   genshi.template.loaderr   r   r   ospathnormpathr^   dirnameabspath)r"   r   basedirs      r$   r   zTemplate._init_loader  s    ;======} =DM11 g..t}== ?9((77A9 A9 @9 ?9 GG !goodm<<GG(."'//'*B*B)CDDDKKK r%   c                 F    | j         s|                                  | j        S rA   )r   _prepare_selfr   rN   s    r$   rz   zTemplate.stream  s&    ~ 	!   |r%   c                     t           )a  Parse the template.
        
        The parsing stage parses the template and constructs a list of
        directives that will be executed in the render stage. The input is
        split up into literal output (text that does not depend on the context
        data) and directives or expressions.
        
        :param source: a file-like object containing the XML source of the
                       template, or an XML event stream
        :param encoding: the encoding of the `source`
        )NotImplementedError)r"   r   r   s      r$   r   zTemplate._parse  s
     "!r%   c                     | j         s6t          |                     | j        |                    | _        d| _         d S d S )NT)r   rM   _preparer   )r"   inlineds     r$   r   zTemplate._prepare_self  s@    ~ 	"dlG D DEEDL!DNNN	" 	"r%   c           	   #     K   ddl m} |t          | j        f          }|D ]\  }}}|t          u rg }|d         }t          |d         d           D ];\  }	}
}}}|
                    | ||||          \  }}|r|                    |           <|                     ||          }|r||t          |          f|fV  |D ]}|V  |t          u r(|\  }}
}d}t          |t                    rt          | j        dd	          sd}	 | j                            ||d         |
p| j        
          }n# |$ r | Y nw xY w|c|j        |vrY|                    |j                   |                    |           |j        D ]}|V  |                    |j                   d	}n|                     ||          D ]}|V  d	}|r|r&||
t          |                     ||                    f}|||fV  |||fV  dS )zCall the `attach` method of every directive found in the template.
        
        :param stream: the event stream of the template
        r   TemplateNotFoundNrQ   c                     | d         S )Nr   r,   )xs    r$   <lambda>z#Template._prepare.<locals>.<lambda>  s
    qt r%   )rS   Fauto_reloadTrelative_tor   )r   r   setr   r   sortedattachappendr   rM   r   r0   r   getattrr   load	__class__addr   rz   discard)r"   rz   r   r   kindrJ   posr{   	substream_r   r[   
namespacesr   eventhreffallbacktmpl_inlinedtmpls                      r$   r   zTemplate._prepare  s     
 	<;;;;;?4=*++G% 6	* 6	*OD$s{{
 G	6<Q^^75 75 75 5 52AsE:s+.::dIu6@#,G ,G(Iy  5")))444 MM)W==	 $T)__ =sBBBBB!* $ $#$ 7??*.'D#x#(L"466 0 ']D I I0  $&#';#3#3Dc!f8;8Mt~ $4 $O $ODD/ & & &'/ %  0/&  +#}G;; 'DM : : : $ 2 27 ; ; ;-1[ !0 !0E*/KKKK ' > > >/3)-x)I)I , ,&++/L# !  >#S$ MM(G<<+> +>  >c/))))c/))))m6	* 6	*s   	*D44D?>D?c                 (   i }|rFt          |          dk    sJ |d         }|t          di |}n|}t          |t                    sJ nt          di |}| j        }| j        D ]} |t          |          |fi |}t          || j                  S )a  Apply the template to the given context data.
        
        Any keyword arguments are made available to the template as context
        data.
        
        Only one positional argument is accepted: if it is provided, it must be
        an instance of the `Context` class, and keyword arguments are ignored.
        This calling style is used for internal processing.
        
        :return: a markup event stream representing the result of applying
                 the template to the context data.
        rQ   r   Nr,   )r^   r   r0   rz   r   ry   r   
serializer)r"   argskwargsr|   ru   rz   filter_s          r$   generatezTemplate.generate  s      		%t99>>>>7D|((((dG,,,,,,$$V$$D| 	9 	9GWT&\\488488FFfdo...r%   c              +     K   | j         }g }|j        }|j        }t          |          }	 |D ]\  }}	}
|t          u r|	d         r|	\  }}g }|D ]c\  }}t          |          t          u r1d  | j        ||fi |D             }|s7d                    |          }|                    ||f           d||t          |          f|
fV  |t          u rt          |	||          }|t          |t                    rt          ||
fV  t          |t                    rt           ||          |
fV  t!          |d          r ||           t#          |          } nt          t%          |          |
fV  M|t&          u r+ ||           t)          |	d         |	d         ||          } n3|t*          u rt-          |	||           ||	|
fV  |sd S  |            })NrQ   c                 L    g | ]!}|d          t           u |d         |d         "S )r   rQ   )r   )rh   r   s     r$   rj   z%Template._flatten.<locals>.<listcomp>L  s>     & & &$)#(8t#3#3a8L ',Ah8L8L8Lr%   r/   __iter__r   )_number_convr   rD   ry   r   r   rM   r   joinr
   r   r   r0   r   r   r   r   r   r   r   r}   r   r   )r"   rz   ru   r|   number_convstackrF   rD   r   rJ   r   tagattrs	new_attrsr6   r[   valuesresults                     r$   r   zTemplate._flatten;  sm     '|if5	#) 4 4dC5==T!W= "&JC "I', 	8 	8e;;$..& &-:T]5$-O-O$-O-O& & &F $* ) ($&GGFOOE!(($7777eI&6&6 7<<<<<T\\'dD99F) &fl;; 	?"&"33333'>> ?"&F(;(;S"@@@@@$VZ88 ? DLLL%,V__F!E"&	&(9(93">>>>S[[ DLLL.tAwQtLLFET\\dD1111 c/))))  Ek5	r%   c              +   6  K   ddl m} |D ]}|d         t          u r|d         \  }}}t          |t                    sUg }	 | j        ||fi |D ]$\  }
}}|
t          u r|	                    |           %d                    d |	D                       }	 | j	        
                    ||d         d         |p| j                  } |j        |fi |D ]}|V  # |$ r1 | | j        D ]} |t          |          |fi |}|D ]}|V  Y w xY w|V  dS )	z[Internal stream filter that performs inclusion of external
        template files.
        r   r   rQ   r/   c                     g | ]}||S rA   r,   )rh   r   s     r$   rj   z%Template._include.<locals>.<listcomp>  s    #F#F#F!Ar%      r   N)r   r   r   r0   r   r   r   r   r   r   r   r   r   r   ry   )r"   rz   ru   r|   r   r   r   r   r   partssubkindsubdatasubposr   r   s                  r$   r   zTemplate._includey  s      	<;;;;; 	 	EQx7""&+Ah#c8!$55 HE4ADM$ 5J 5JDH5J 5J 2 20&"d??!LL11177#F#Fu#F#F#FGGD$;++DeAhqk030Et~ , G GD!.t!<!<t!<!< $ $#$' $ $ $'#'< I I#*74>>4#H#H4#H#H!) $ $#$ $$ /	 	s   AC2DD)NNNNr   TrA   )r(   r)   r*   r+   r   r   r   r   r   r   r   r   r   r   r   rO   r   r   propertyrz   r   r   r   r   r   r   r,   r%   r$   r   r   g  sb         ?6""DH?6""D=oi((GK
/%
 
 C JLDH<@%P %P %P %PN  
  B B B6 6 6E E E   X
" " "" " " "
?* ?* ?*B/ / /<< < <|    r%   r   rA   )(r+   collectionsr   r   genshi.compatr   r   r   r   r   r	   genshi.corer
   r   r   r   r   r   genshi.inputr   __all____docformat__r   r   r   r   r   objectr   r}   r   r   r   r   r   r   r   r   r   r   r,   r%   r$   <module>r     s   & %       				 b b b b b b b b b b b b b b b b L L L L L L L L L L L L L L L L # # # # # #O O O%    I   .@ @ @ @ @- @ @ @&7 7 7 7 7+ 7 7 7(    =   c c c c cf c c cL     "# # # #$1 1 1 1 14 1 1 1 #$$ $  $  $  $  $v  $  $ %$ $Fo o o o o o o od	 }}

lr%   