https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /lib/python3/dist-packages/genshi/template/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/genshi/template/__pycache__/directives.cpython-310.pyc
o

!�b�k�@sZdZddlZddlmZmZddlmZddlmZm	Z	m
Z
mZmZddl
mZmZmZgd�ZdZGd	d
�d
e�Ze�e�Gdd�de��Zd
d�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Z Gdd �d e�Z!Gd!d"�d"e�Z"Gd#d$�d$e�Z#Gd%d&�d&e�Z$dS)'z2Implementation of the various template directives.�N)�QName�Stream)�Path)�TemplateRuntimeError�TemplateSyntaxError�EXPR�_apply_directives�
_eval_expr)�
Expression�_ast�_parse)�AttrsDirective�ChooseDirective�ContentDirective�DefDirective�ForDirective�IfDirective�MatchDirective�OtherwiseDirective�ReplaceDirective�StripDirective�
WhenDirective�
WithDirectivezrestructuredtext enc@seZdZdZdd�ZdS)�
DirectiveMetaz#Meta class for template directives.cCs$|���dd�|d<t�||||�S)N�	directive��tagname)�lower�replace�type�__new__)�cls�name�bases�d�r%�</usr/lib/python3/dist-packages/genshi/template/directives.pyr "szDirectiveMeta.__new__N)�__name__�
__module__�__qualname__�__doc__r r%r%r%r&rsrc@sNeZdZdZdgZ		ddd�Zedd��Zd	d
�Zdd�Z	edd
d��Z
dS)�	DirectiveafAbstract base class for template directives.
    
    A directive is basically a callable that takes three positional arguments:
    ``ctxt`` is the template data context, ``stream`` is an iterable over the
    events that the directive applies to, and ``directives`` is is a list of
    other directives on the same stream that need to be applied.
    
    Directives can be "anonymous" or "registered". Registered directives can be
    applied by the template author using an XML attribute with the
    corresponding name in the template. Such directives should be subclasses of
    this base class that can  be instantiated with the value of the directive
    attribute as parameter.
    
    Anonymous directives are simply functions conforming to the protocol
    described above, and can only be applied programmatically (for example by
    template filters).
    �exprN���cCs|�||||�|_dS�N)�_parse_exprr,��self�value�template�
namespaces�lineno�offsetr%r%r&�__init__<szDirective.__init__cCs ||||g|dd��R�|fS)a=Called after the template stream has been completely parsed.
        
        :param template: the `Template` object
        :param stream: the event stream associated with the directive
        :param value: the argument value for the directive; if the directive was
                      specified as an element, this will be an `Attrs` instance
                      with all specified attributes, otherwise it will be a
                      `unicode` object with just the attribute value
        :param namespaces: a mapping of namespace URIs to prefixes
        :param pos: a ``(filename, lineno, offset)`` tuple describing the
                    location where the directive was found in the source
        
        This class method should return a ``(directive, stream)`` tuple. If
        ``directive`` is not ``None``, it should be an instance of the `Directive`
        class, and gets added to the list of directives applied to the substream
        at runtime. `stream` is an event stream that replaces the original
        stream associated with the directive.
        �Nr%�r!r3�streamr2r4�posr%r%r&�attach@s zDirective.attachcKst�)azApply the directive to the given stream.
        
        :param stream: the event stream
        :param directives: a list of the remaining directives that should
                           process the stream
        :param ctxt: the context data
        :param vars: additional variables that should be made available when
                     Python code is executed
        )�NotImplementedError�r1r:�
directives�ctxt�varsr%r%r&�__call__Vs
zDirective.__call__cCs2d}t|dd�durd|jj}dt|�j|fS)Nrr,z "%s"z<%s%s>)�getattrr,�sourcerr')r1r,r%r%r&�__repr__bszDirective.__repr__c
Csjz|r
t||j||jd�pdWSty4}z|jd||jf7_t||j|||jp-d��d}~ww)zpParses the given expression, raising a useful error message when a
        syntax error is encountered.
        ��lookupN�% in expression "%s" of "%s" directiver)r
�filepathrG�SyntaxError�msgrrr6)r!r,r3r5r6�errr%r%r&r/hs��

�
���zDirective._parse_expr�NNr-r-)r-r-)r'r(r)r*�	__slots__r7�classmethodr<rBrEr/r%r%r%r&r+'s
�
r+cs$�fdd���|�f�fdd�	��S)z�Takes the AST representation of an assignment, and returns a
    function that applies the assignment of a given value to a dictionary.
    cs:t|tj�rt�fdd�|jD��St|tj�r|jSdS)Ncsg|]}�|��qSr%r%)�.0�child��_namesr%r&�
<listcomp>}�z/_assignment.<locals>._names.<locals>.<listcomp>)�
isinstancer�Tuple�tuple�elts�Name�id)�noderRr%r&rS{s
�z_assignment.<locals>._namescsBt|�turtt|��D]}�|||||�qdS|||<dSr.)rrX�range�len)�datar2�names�idx)�_assignr%r&rb�s
�z_assignment.<locals>._assignr%)�astr%)rbrSr&�_assignmentwsrdc@�eZdZdZgZdd�ZdS)r
a_Implementation of the ``py:attrs`` template directive.
    
    The value of the ``py:attrs`` attribute should be a dictionary or a sequence
    of ``(name, value)`` tuples. The items in that dictionary or sequence are
    added as attributes to the element:
    
    >>> from genshi.template import MarkupTemplate
    >>> tmpl = MarkupTemplate('''<ul xmlns:py="http://genshi.edgewall.org/">
    ...   <li py:attrs="foo">Bar</li>
    ... </ul>''')
    >>> print(tmpl.generate(foo={'class': 'collapse'}))
    <ul>
      <li class="collapse">Bar</li>
    </ul>
    >>> print(tmpl.generate(foo=[('class', 'collapse')]))
    <ul>
      <li class="collapse">Bar</li>
    </ul>
    
    If the value evaluates to ``None`` (or any other non-truth value), no
    attributes are added:
    
    >>> print(tmpl.generate(foo=None))
    <ul>
      <li>Bar</li>
    </ul>
    c�"����fdd�}t|�|���S)Nc3s��t��\}\}}}t�j���}|r>t|t�r,ztt|��}Wnty+g}Yn
wt|t�s5|��}|dd�|D�O}|||f|fV�D]}|VqHdS)NcSs2g|]\}}t|�|durt�|���pdf�qSr.)r�six�	text_type�strip)rP�n�vr%r%r&rT�s ��z>AttrsDirective.__call__.<locals>._generate.<locals>.<listcomp>)	�nextr	r,rVr�iter�
StopIteration�list�items)�kind�tag�attribr;�attrs�event�r@r1r:rAr%r&�	_generate�s&�
�
��z*AttrsDirective.__call__.<locals>._generate�r�r1r:r?r@rArwr%rvr&rB�szAttrsDirective.__call__N�r'r(r)r*rNrBr%r%r%r&r
�sr
c@� eZdZdZgZedd��ZdS)ra�Implementation of the ``py:content`` template directive.
    
    This directive replaces the content of the element with the result of
    evaluating the value of the ``py:content`` attribute:
    
    >>> from genshi.template import MarkupTemplate
    >>> tmpl = MarkupTemplate('''<ul xmlns:py="http://genshi.edgewall.org/">
    ...   <li py:content="bar">Hello</li>
    ... </ul>''')
    >>> print(tmpl.generate(bar='Bye'))
    <ul>
      <li>Bye</li>
    </ul>
    cCs`t|�turtd|jg|dd��R��|j||g|dd��R�}d|dt||f|dgfS)Nz3The content directive can not be used as an elementr8rr-)r�dictrrIr/r�r!r3r:r2r4r;r,r%r%r&r<�s�
�zContentDirective.attachN�r'r(r)r*rNrOr<r%r%r%r&r�s
rcsFeZdZdZgd�Zd
dd�Ze�fdd��Zd	d
�Zdd�Z	�Z
S)raImplementation of the ``py:def`` template directive.
    
    This directive can be used to create "Named Template Functions", which
    are template snippets that are not actually output during normal
    processing, but rather can be expanded from expressions in other places
    in the template.
    
    A named template function can be used just like a normal Python function
    from template expressions:
    
    >>> from genshi.template import MarkupTemplate
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
    ...   <p py:def="echo(greeting, name='world')" class="message">
    ...     ${greeting}, ${name}!
    ...   </p>
    ...   ${echo('Hi', name='you')}
    ... </div>''')
    >>> print(tmpl.generate(bar='Bye'))
    <div>
      <p class="message">
        Hi, you!
      </p>
    </div>
    
    If a function does not require parameters, the parenthesis can be omitted
    in the definition:
    
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
    ...   <p py:def="helloworld" class="message">
    ...     Hello, world!
    ...   </p>
    ...   ${helloworld()}
    ... </div>''')
    >>> print(tmpl.generate(bar='Bye'))
    <div>
      <p class="message">
        Hello, world!
      </p>
    </div>
    )r"�args�	star_args�
dstar_args�defaultsNr-c
Cst�|d||||�t|�j}g|_d|_d|_i|_t|t	j
�r�|jj|_
|jD]}tt	d�r<t|t	j�r<|jj|_q)|j�|j�q)|jD]%}|jdurT|jj|_qG|j�|j�t|j|j||jd�}	|	|j|j<qGt|dd�rx|jj|_t|dd�r�|jj|_dSdS|j|_
dS)N�StarredrF�starargs�kwargs)r+r7r�bodyrr�r�r�rVr�Call�funcr[r"�hasattrr�r2�append�keywords�argr
rIrGrCr�r�)
r1rr3r4r5r6rcr��kwd�expr%r%r&r7s4





�
�zDefDirective.__init__c�.t|�tur|�d�}tt|��|||||�S)N�function)rr|�get�superrr<r9��	__class__r%r&r<$�

�zDefDirective.attachcs8t��������fdd�}�j|_|�jd�j<gS)Nc?s��i}t|�}�jD]$}|r|�d�||<q
||vr |�|�}n
t�j�|����}|||<q
�jdur9||�j<�jdurC||�j<��|�t	�����D]}|VqO���dS)Nr)
ror�popr	r�r�r�r��pushr)rr��scoper"�valru�r@r?r1r:rAr%r&r�.s$�






z'DefDirective.__call__.<locals>.functionr-)ror"r'�frames)r1r:r?r@rAr�r%r�r&rB+s
zDefDirective.__call__cCsdt|�j|jfS�Nz	<%s "%s">)rr'r"�r1r%r%r&rEKszDefDirective.__repr__�Nr-r-�r'r(r)r*rNr7rOr<rBrE�
__classcell__r%r%r�r&r�s(
  rcsFeZdZdZddgZddd�Ze�fdd	��Zd
d�Zdd
�Z	�Z
S)ra�Implementation of the ``py:for`` template directive for repeating an
    element based on an iterable in the context data.
    
    >>> from genshi.template import MarkupTemplate
    >>> tmpl = MarkupTemplate('''<ul xmlns:py="http://genshi.edgewall.org/">
    ...   <li py:for="item in items">${item}</li>
    ... </ul>''')
    >>> print(tmpl.generate(items=[1, 2, 3]))
    <ul>
      <li>1</li><li>2</li><li>3</li>
    </ul>
    �assign�filenameNr-cCspd|vrtd|j||��|�dd�\}}t|d�}d|��}t|jdj�|_|j|_	t
�||||||�dS)Nz in z'"in" keyword missing in "for" directiver8�execziter(%s)r)rrI�splitrrirdr�r2r�r�r+r7)r1r2r3r4r5r6r�rcr%r%r&r7^s�
zForDirective.__init__cr�)N�each)rr|r�r�rr<r9r�r%r&r<ir�zForDirective.attachc
ksr�t|j||�}|durdS|j}i}t|�}|D]}|||�|�|�t||||�D]}	|	Vq,|��qdSr.)r	r,r�ror�rr�)
r1r:r?r@rA�iterabler�r��itemrur%r%r&rBps�


�zForDirective.__call__cC�dt|�jS�Nz<%s>�rr'r�r%r%r&rE�zForDirective.__repr__r�r�r%r%r�r&rOs
rcs0eZdZdZgZe�fdd��Zdd�Z�ZS)ra�Implementation of the ``py:if`` template directive for conditionally
    excluding elements from being output.
    
    >>> from genshi.template import MarkupTemplate
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
    ...   <b py:if="foo">${bar}</b>
    ... </div>''')
    >>> print(tmpl.generate(foo=True, bar='Hello'))
    <div>
      <b>Hello</b>
    </div>
    cr��N�test)rr|r�r�rr<r9r�r%r&r<�r�zIfDirective.attachcKs$t|j||�}|rt||||�SgSr.)r	r,r)r1r:r?r@rAr2r%r%r&rB�szIfDirective.__call__�	r'r(r)r*rNrOr<rBr�r%r%r�r&r�src@sBeZdZdZgd�Z		d
dd�Zedd��Zd	d
�Zdd�Z	dS)ra�Implementation of the ``py:match`` template directive.

    >>> from genshi.template import MarkupTemplate
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
    ...   <span py:match="greeting">
    ...     Hello ${select('@name')}
    ...   </span>
    ...   <greeting name="Dude" />
    ... </div>''')
    >>> print(tmpl.generate())
    <div>
      <span>
        Hello Dude
      </span>
    </div>
    )�pathr4�hintsNr-cCs<t�|d||||�t||j|�|_|pi|_|pd|_dS)Nr%)r+r7rrIr�r4r�)r1r2r3r�r4r5r6r%r%r&r7�s
zMatchDirective.__init__cCs�g}t|�tur:|�dd���dkr|�d�|�dd���dkr&|�d�|�dd���dkr5|�d	�|�d
�}|||t|�|g|dd��R�|fS)N�bufferr�false�not_buffered�once�true�
match_once�	recursive�
not_recursiver�r8)rr|r�rr��	frozenset)r!r3r:r2r4r;r�r%r%r&r<�s



 �zMatchDirective.attachcKs0|j�|jjdd�|jt|�|j|j|f�gS)NT)�ignore_context)�_match_templatesr�r�r�ror�r4r>r%r%r&rB�s
�zMatchDirective.__call__cCsdt|�j|jjfSr�)rr'r�rDr�r%r%r&rE�szMatchDirective.__repr__rM)
r'r(r)r*rNr7rOr<rBrEr%r%r%r&r�s
�

rc@r{)ra=Implementation of the ``py:replace`` template directive.
    
    This directive replaces the element with the result of evaluating the
    value of the ``py:replace`` attribute:
    
    >>> from genshi.template import MarkupTemplate
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
    ...   <span py:replace="bar">Hello</span>
    ... </div>''')
    >>> print(tmpl.generate(bar='Bye'))
    <div>
      Bye
    </div>
    
    This directive is equivalent to ``py:content`` combined with ``py:strip``,
    providing a less verbose way to achieve the same effect:
    
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
    ...   <span py:content="bar" py:strip="">Hello</span>
    ... </div>''')
    >>> print(tmpl.generate(bar='Bye'))
    <div>
      Bye
    </div>
    cCsbt|�tur|�d�}|std|jg|dd��R��|j||g|dd��R�}dt||fgfS)Nr2z%missing value for "replace" directiver8)rr|r�rrIr/rr}r%r%r&r<�s
�
�zReplaceDirective.attachNr~r%r%r%r&r�s
rc@re)ra�Implementation of the ``py:strip`` template directive.
    
    When the value of the ``py:strip`` attribute evaluates to ``True``, the
    element is stripped from the output
    
    >>> from genshi.template import MarkupTemplate
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
    ...   <div py:strip="True"><b>foo</b></div>
    ... </div>''')
    >>> print(tmpl.generate())
    <div>
      <b>foo</b>
    </div>
    
    Leaving the attribute value empty is equivalent to a truth value.
    
    This directive is particulary interesting for named template functions or
    match templates that do not generate a top-level element:
    
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
    ...   <div py:def="echo(what)" py:strip="">
    ...     <b>${what}</b>
    ...   </div>
    ...   ${echo('foo')}
    ... </div>''')
    >>> print(tmpl.generate())
    <div>
        <b>foo</b>
    </div>
    crf)Nc3sR��jrt�j���rt��t��}�D]}|V|}qdS�D]}|Vq!dSr.)r,r	rl)�previousrurvr%r&rws���z*StripDirective.__call__.<locals>._generaterxryr%rvr&rBs
zStripDirective.__call__Nrzr%r%r%r&r�srcs4eZdZdZddgZe�fdd��Zdd�Z�ZS)ra�Implementation of the ``py:choose`` directive for conditionally selecting
    one of several body elements to display.
    
    If the ``py:choose`` expression is empty the expressions of nested
    ``py:when`` directives are tested for truth.  The first true ``py:when``
    body is output. If no ``py:when`` directive is matched then the fallback
    directive ``py:otherwise`` will be used.
    
    >>> from genshi.template import MarkupTemplate
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/"
    ...   py:choose="">
    ...   <span py:when="0 == 1">0</span>
    ...   <span py:when="1 == 1">1</span>
    ...   <span py:otherwise="">2</span>
    ... </div>''')
    >>> print(tmpl.generate())
    <div>
      <span>1</span>
    </div>
    
    If the ``py:choose`` directive contains an expression, the nested
    ``py:when`` directives are tested for equality to the ``py:choose``
    expression:
    
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/"
    ...   py:choose="2">
    ...   <span py:when="1">1</span>
    ...   <span py:when="2">2</span>
    ... </div>''')
    >>> print(tmpl.generate())
    <div>
      <span>2</span>
    </div>
    
    Behavior is undefined if a ``py:choose`` block contains content outside a
    ``py:when`` or ``py:otherwise`` block.  Behavior is also undefined if a
    ``py:otherwise`` occurs before ``py:when`` blocks.
    �matchedr2cr�r�)rr|r�r�rr<r9r�r%r&r<Qr�zChooseDirective.attachcks^�dt|j�dg}|jrt|j||�|d<|j�|�t||||�D]}|Vq"|j��dS)NF�)�boolr,r	�
_choice_stackr�rr�)r1r:r?r@rA�inforur%r%r&rBXs�zChooseDirective.__call__r�r%r%r�r&r(s&rcs<eZdZdZdgZddd�Ze�fdd��Zd	d
�Z�Z	S)rz�Implementation of the ``py:when`` directive for nesting in a parent with
    the ``py:choose`` directive.
    
    See the documentation of the `ChooseDirective` for usage.
    r�Nr-cCs t�||||||�|j|_dSr.�r+r7rIr�r0r%r%r&r7j�zWhenDirective.__init__cr�r�)rr|r�r�rr<r9r�r%r&r<nr�zWhenDirective.attachcKs�|jo|jd}|std|jgt|�ddd��R��|dr"gS|js;|ds;td|jgt|�ddd��R��|drU|d}|jrP|t|j||�k}nt|�}n	tt|j||��}||d<|sfgSt||||�S)Nr-z>"when" directives can only be used inside a "choose" directiver�r8rz?either "choose" or "when" directive must have a test expression)r�rr�rlr,r	r�r)r1r:r?r@rAr�r2r�r%r%r&rBus2����
zWhenDirective.__call__r�)
r'r(r)r*rNr7rOr<rBr�r%r%r�r&rbs
rc@s(eZdZdZdgZd	dd�Zdd�ZdS)
rz�Implementation of the ``py:otherwise`` directive for nesting in a parent
    with the ``py:choose`` directive.
    
    See the documentation of `ChooseDirective` for usage.
    r�Nr-cCs t�|d||||�|j|_dSr.r�r0r%r%r&r7�r�zOtherwiseDirective.__init__cKsZ|jo|jd}|std|jgt|�ddd��R��|dr"gSd|d<t||||�S)Nr-zEan "otherwise" directive can only be used inside a "choose" directiver�r8rT)r�rr�rlr)r1r:r?r@rAr�r%r%r&rB�s��zOtherwiseDirective.__call__r�)r'r(r)r*rNr7rBr%r%r%r&r�s

rcsDeZdZdZdgZd
dd�Ze�fdd��Zd	d
�Zdd�Z	�Z
S)ra�Implementation of the ``py:with`` template directive, which allows
    shorthand access to variables and expressions.
    
    >>> from genshi.template import MarkupTemplate
    >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
    ...   <span py:with="y=7; z=x+10">$x $y $z</span>
    ... </div>''')
    >>> print(tmpl.generate(x=42))
    <div>
      <span>42 7 52</span>
    </div>
    rANr-c	
Cs�t�|d||||�g|_|��}z3t|d�}|jD]'}t|tj�s*t	d|j
||��|j�dd�|jD�t
|j|j
||jd�f�qWdStyi}z|jd||jf7_t	||j
|||jpbd��d}~ww)Nr�z8only assignment allowed in value of the "with" directivecSsg|]}t|��qSr%)rd)rPrjr%r%r&rT�rUz*WithDirective.__init__.<locals>.<listcomp>rFrHr)r+r7rArirr�rVr�AssignrrIr��targetsr
r2rGrJrKrr6)	r1r2r3r4r5r6rcr\rLr%r%r&r7�s2

�
���

�
���zWithDirective.__init__cr�)NrA)rr|r�r�rr<r9r�r%r&r<�r�zWithDirective.attachcksf�i}|�|�|jD]\}}t|||�}|D]}	|	||�qqt||||�D]}
|
Vq'|��dSr.)r�rAr	rr�)r1r:r?r@rA�framer�r,r2r�rur%r%r&rB�s�
�zWithDirective.__call__cCr�r�r�r�r%r%r&rE�r�zWithDirective.__repr__r�r�r%r%r�r&r�s
r)%r*rg�genshi.corerr�genshi.pathr�genshi.template.baserrrrr	�genshi.template.evalr
rr�__all__�
__docformat__rr�
add_metaclass�objectr+rdr
rrrrrrrrrrrr%r%r%r&�<module>s0
O5v42'/:.

https://t.me/RX1948 - 2025