
    YfF                       d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZ dd	lmZmZ dd
lmZ ddlm 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+ ddl,m-Z-m.Z.  ed          Z/eZ0	 ee1e2e&e"e$e%ee0df         ee0         ee0ddf         e#ee0         eee0         e3e0f         e-f         Z4	 dZ5d:dZ6d:dZ7d;dZ8d:dZ9d:dZ:	 d<d=d&Z;d>d)Z<d?d3Z=d@d5Z>dAd7Z?dBd9Z@dS )Czu
Context-free flattener/serializer for rendering Python objects, possibly
complex or arbitrarily nested, as strings.
    )annotations)iscoroutine)BytesIO)exc_info)
extract_tb)GeneratorType)AnyCallable	Coroutine	GeneratorListMappingOptionalSequenceTupleTypeVarUnioncast)DeferredensureDeferred)nativeString)Failure)CDATACharRefCommentTagslotvoidElements)FlattenerErrorUnfilledSlotUnsupportedType)IRenderableIRequestT.Ni   dataUnion[bytes, str]returnbytesc                    t          | t                    r|                     d          } |                     dd                              dd                              dd          } | S )a  
    Escape some character or UTF-8 byte data for inclusion in an HTML or XML
    document, by replacing metacharacters (C{&<>}) with their entity
    equivalents (C{&amp;&lt;&gt;}).

    This is used as an input to L{_flattenElement}'s C{dataEscaper} parameter.

    @param data: The string to escape.

    @return: The quoted form of C{data}.  If C{data} is L{str}, return a utf-8
        encoded string.
    utf-8   &s   &amp;   <s   &lt;   >s   &gt;
isinstancestrencodereplacer%   s    F/var/www/html/env/lib/python3.11/site-packages/twisted/web/_flatten.pyescapeForContentr5   G   s_     $ ${{7##<<h''//g>>FFtWUUDK    c                Z    t          | t                    r|                     d          S | S )aG  
    Escape some character or UTF-8 byte data for inclusion in the top level of
    an attribute.  L{attributeEscapingDoneOutside} actually passes the data
    through unchanged, because L{writeWithAttributeEscaping} handles the
    quoting of the text within attributes outside the generator returned by
    L{_flattenElement}; this is used as the C{dataEscaper} argument to that
    L{_flattenElement} call so that that generator does not redundantly escape
    its text output.

    @param data: The string to escape.

    @return: The string, unchanged, except for encoding.
    r*   )r/   r0   r1   r3   s    r4   attributeEscapingDoneOutsider8   Z   s-     $ ${{7###Kr6   writeCallable[[bytes], object]Callable[[bytes], None]c                     d fd}|S )aU  
    Decorate a C{write} callable so that all output written is properly quoted
    for inclusion within an XML attribute value.

    If a L{Tag <twisted.web.template.Tag>} C{x} is flattened within the context
    of the contents of another L{Tag <twisted.web.template.Tag>} C{y}, the
    metacharacters (C{<>&"}) delimiting C{x} should be passed through
    unchanged, but the textual content of C{x} should still be quoted, as
    usual.  For example: C{<y><x>&amp;</x></y>}.  That is the default behavior
    of L{_flattenElement} when L{escapeForContent} is passed as the
    C{dataEscaper}.

    However, when a L{Tag <twisted.web.template.Tag>} C{x} is flattened within
    the context of an I{attribute} of another L{Tag <twisted.web.template.Tag>}
    C{y}, then the metacharacters delimiting C{x} should be quoted so that it
    can be parsed from the attribute's value.  In the DOM itself, this is not a
    valid thing to do, but given that renderers and slots may be freely moved
    around in a L{twisted.web.template} template, it is a condition which may
    arise in a document and must be handled in a way which produces valid
    output.  So, for example, you should be able to get C{<y attr="&lt;x /&gt;"
    />}.  This should also be true for other XML/HTML meta-constructs such as
    comments and CDATA, so if you were to serialize a L{comment
    <twisted.web.template.Comment>} in an attribute you should get C{<y
    attr="&lt;-- comment --&gt;" />}.  Therefore in order to capture these
    meta-characters, flattening is done with C{write} callable that is wrapped
    with L{writeWithAttributeEscaping}.

    The final case, and hopefully the much more common one as compared to
    serializing L{Tag <twisted.web.template.Tag>} and arbitrary L{IRenderable}
    objects within an attribute, is to serialize a simple string, and those
    should be passed through for L{writeWithAttributeEscaping} to quote
    without applying a second, redundant level of quoting.

    @param write: A callable which will be invoked with the escaped L{bytes}.

    @return: A callable that writes data with escaping.
    r%   r(   r'   Nonec                `     t          |                               dd                     d S )N   "s   &quot;)r5   r2   )r%   r9   s    r4   _writez*writeWithAttributeEscaping.<locals>._write   s2    t$$,,T9==>>>>>r6   )r%   r(   r'   r=    )r9   r@   s   ` r4   writeWithAttributeEscapingrB   m   s)    R? ? ? ? ? ? Mr6   c                    t          | t                    r|                     d          } |                     dd          S )z
    Escape CDATA for inclusion in a document.

    @param data: The string to escape.

    @return: The quoted form of C{data}. If C{data} is unicode, return a utf-8
        encoded string.
    r*      ]]>s   ]]]]><![CDATA[>r.   r3   s    r4   escapedCDATArE      s<     $ ${{7##<< 2333r6   c                    t          | t                    r|                     d          } |                     dd          } | r| dd         dk    r| dz  } | S )a  
    Within comments the sequence C{-->} can be mistaken as the end of the comment.
    To ensure consistent parsing and valid output the sequence is replaced with C{--&gt;}.
    Furthermore, whitespace is added when a comment ends in a dash. This is done to break
    the connection of the ending C{-} with the closing C{-->}.

    @param data: The string to escape.

    @return: The quoted form of C{data}. If C{data} is unicode, return a utf-8
        encoded string.
    r*      -->s   --&gt;N   -    r.   r3   s    r4   escapedCommentrK      sb     $ ${{7##<<	**D RSS	T!!Kr6   namer0   slotData-Sequence[Optional[Mapping[str, Flattenable]]]defaultOptional[Flattenable]Flattenablec                l    t          |          D ]}|| |v r
||          c S ||S t          |           )zK
    Find the value of the named slot in the given stack of slot data.
    )reversedr    )rL   rM   rO   	slotFrames       r4   _getSlotValuerU      sU     h'' ! !	 TY%6%6T?"""N4   r6   dDeferred[T]c                r     t           fd          d	fd}d
fd}                     ||           S )z
    Create a new L{Deferred} based on C{d} that will fire and fail with C{d}'s
    result or error, but will not modify C{d}'s callback type.
    c                ,                                     S N)cancel)_rV   s    r4   <lambda>z_fork.<locals>.<lambda>   s     r6   resultr$   r'   c                2                         |            | S rZ   )callback)r^   d2s    r4   r`   z_fork.<locals>.callback   s    
Fr6   failurer   c                2                         |            | S rZ   )errback)rb   ra   s    r4   rd   z_fork.<locals>.errback   s    


7r6   )r^   r$   r'   r$   )rb   r   r'   r   )r   addCallbacks)rV   r`   rd   ra   s   `  @r4   _forkrf      st    
 333344B           NN8W%%%Ir6   requestOptional[IRequest]root)List[Optional[Mapping[str, Flattenable]]]renderFactoryOptional[IRenderable]dataEscaper$Callable[[Union[bytes, str]], bytes]MGenerator[Union[Generator[Any, Any, Any], Deferred[Flattenable]], None, None]c           
   #     K   |||fd" fdd#fd}t          |t          t          f          r | ||                     dS t          |t                    r*t	          |j        |j                  } |          V  dS t          |t                    r5 |d            |t          |j	                              |d           dS t          |t                    r5 |d            |t          |j	                              |d           dS t          |t                    r                    |j                   |j        }|u|t!          d| d          |                    d          }	d|	_        |                    |          }
 |
 |	          } |          V                                   dS |j        s |j                  V  dS  |d           t          |j        t                    r|j                            d          }n|j        } ||           |j                                        D ]m\  }}t          |t                    r|                    d          } |d|z   dz               |t2          t5          |                    V   |d           n|j        st7          |          t8          vr6 |d            |j        t:                    V   |d|z   dz              dS  |d           dS t          |t<          t>          t@          f          r|D ]} |          V  dS t          |tB                    r+d |j"        fz  } ||                    d                     dS t          |tF                    r |tI          |                    V  dS tK          |          rW |tG          j&        tO          tP          tF          tR                   tT          tR          f         |                              V  dS tW          j,        |          r&|                               } ||!          V  dS t[          |          )$at  
    Make C{root} slightly more flat by yielding all its immediate contents as
    strings, deferreds or generators that are recursive calls to itself.

    @param request: A request object which will be passed to
        L{IRenderable.render}.

    @param root: An object to be made flatter.  This may be of type C{unicode},
        L{str}, L{slot}, L{Tag <twisted.web.template.Tag>}, L{tuple}, L{list},
        L{types.GeneratorType}, L{Deferred}, or an object that implements
        L{IRenderable}.

    @param write: A callable which will be invoked with each L{bytes} produced
        by flattening C{root}.

    @param slotData: A L{list} of L{dict} mapping L{str} slot names to data
        with which those slots will be replaced.

    @param renderFactory: If not L{None}, an object that provides
        L{IRenderable}.

    @param dataEscaper: A 1-argument callable which takes L{bytes} or
        L{unicode} and returns L{bytes}, quoted as appropriate for the
        rendering context.  This is really only one of two values:
        L{attributeEscapingDoneOutside} or L{escapeForContent}, depending on
        whether the rendering context is within an attribute or not.  See the
        explanation in L{writeWithAttributeEscaping}.

    @return: An iterator that eventually writes L{bytes} to C{write}.
        It can yield other iterators or L{Deferred}s; if it yields another
        iterator, the caller will iterate it; if it yields a L{Deferred},
        the result of that L{Deferred} will be another generator, in which
        case it is iterated.  See L{_flattenTree} for the trampoline that
        consumes said values.
    newRootrQ   rm   rn   rk   rl   r9   r:   r'   @Generator[Union[Flattenable, Deferred[Flattenable]], None, None]c                ,    t          | |||          S rZ   )_flattenElement)rq   rm   rk   r9   rg   rM   s       r4   	keepGoingz"_flattenElement.<locals>.keepGoing  s$     WeX}k
 
 	
r6   r^   Deferred[Flattenable]c                .    |                                S rZ   )addCallback)r^   ru   s    r4   keepGoingAsyncz'_flattenElement.<locals>.keepGoingAsync  s    !!),,,r6   s	   <![CDATA[rD   s   <!--rG   Nz$Tag wants to be rendered by method "z)" but is not contained in any IRenderableFr,   asciirJ   s   =")r9   r?   r-   s   </s    />z&#%d;)rk   )
rq   rQ   rm   rn   rk   rl   r9   r:   r'   rr   )r^   rv   r'   rv   ).r/   r(   r0   r   rU   rL   rO   r   rE   r%   r   rK   r   appendrM   render
ValueErrorclonelookupRenderMethodpoptagNamechildrenr1   
attributesitemsr8   rB   r   r   r5   tuplelistr   r   ordinalr   rf   r   fromCoroutiner   r   rQ   objectr"   
providedByr!   )rg   ri   r9   rM   rk   rm   ry   	slotValuerendererName	rootClonerenderMethodr^   r   kvelementescapedru   s   `  `             @r4   rt   rt      sE     b =H/<+0	
 
 
 
 
 
 
 
- - - - - - $%% R$kk$     	D$		 P$!$)Xt|DD	i	"""""""	D%	 	  M$ll49%%&&&f	D'	"	" I$gnTY''(((f	D#		 E$&&&{#$ ?< ? ? ?   

5))I#I(;;LIIL!\'955F)F#####LLNNNF| 	)DM*****FddlC(( 	#l))'22GGlGgO))++ 		 		DAq!S!! &HHW%%E$(U"### )/7QRW7X7X      E$KKKK= 	L11EEE$KKK )DM+;<<<<<E%'/D()))))E&MMMMM	D5$6	7	7 $ 	% 	%G)G$$$$$$	% 	%	D'	"	" $T\O+gnnW%%&&&&&	D(	#	# $nU4[[)))))))	T		 
$n"Yx4fkIJDQQ 
 
 	
 	
 	
 	
 	

 
		%	% $W%%id33333333d###r6   r=   c           	       	
K   g 	d
d	
fd}d	
fdt          | ||g dt                    g}|r	 t          |d	                   }t          |t                    r              | d{V }|                    |           n# t          $ r |                                 Y nt          $ r|}g }|D ].}|j	        %|                    |j	        j
        d
                    /|                                 t          ||t          t                      d                             d}~ww xY w|              dS )a  
    Make C{root} into an iterable of L{bytes} and L{Deferred} by doing a depth
    first traversal of the tree.

    @param request: A request object which will be passed to
        L{IRenderable.render}.

    @param root: An object to be made flatter.  This may be of type C{unicode},
        L{bytes}, L{slot}, L{Tag <twisted.web.template.Tag>}, L{tuple},
        L{list}, L{types.GeneratorType}, L{Deferred}, or something providing
        L{IRenderable}.

    @param write: A callable which will be invoked with each L{bytes} produced
        by flattening C{root}.

    @return: A C{Deferred}-returning coroutine that resolves to C{None}.
    r   bsr(   r'   r=   c                                         |            t          |           z  t          k    r              d S d S rZ   )r{   lenBUFFER_SIZE)r   bufbufSizeflushBuffers    r4   bufferedWritez#_flattenTree.<locals>.bufferedWrite  sD    

23r77k!!KMMMMM "!r6   c                 b    dk    r' d                                            d d = dd S d S )Nr   r6   )join)r   r   r9   s   r4   r   z!_flattenTree.<locals>.flushBuffer  sA    Q;;E#((3--   AAAGGG ;r6   NrH   ri      )r   r(   r'   r=   )r'   r=   )rt   r5   nextr/   r   r{   StopIterationr   	Exceptiongi_framef_localsr   r   r   )rg   ri   r9   r   stackr   eroots	generatorr   r   r   s     `      @@@r4   _flattenTreer   s  s     ( CG               	}b$@PQQ-E  "	"59ooG'8,, (  '------ LL!!!!  	 	 	IIKKKKK 	F 	F 	FE" F F	%1LL!3!<V!DEEEIIKKK E:hjjm+D+DEEE	F  "* KMMMMMs   <B
 
D/*	D/3A7D**D/Deferred[None]c                >    t          t          | ||                    S )a  
    Incrementally write out a string representation of C{root} using C{write}.

    In order to create a string representation, C{root} will be decomposed into
    simpler objects which will themselves be decomposed and so on until strings
    or objects which can easily be converted to strings are encountered.

    @param request: A request object which will be passed to the C{render}
        method of any L{IRenderable} provider which is encountered.

    @param root: An object to be made flatter.  This may be of type L{str},
        L{bytes}, L{slot}, L{Tag <twisted.web.template.Tag>}, L{tuple},
        L{list}, L{types.GeneratorType}, L{Deferred}, or something that
        provides L{IRenderable}.

    @param write: A callable which will be invoked with each L{bytes} produced
        by flattening C{root}.

    @return: A L{Deferred} which will be called back with C{None} when C{root}
        has been completely flattened into C{write} or which will be errbacked
        if an unexpected exception occurs.
    )r   r   )rg   ri   r9   s      r4   flattenr     s    2 ,we<<===r6   Deferred[bytes]c                    t                      t          | |j                  }|                    fd           t	          t
          t                   |          S )a  
    Collate a string representation of C{root} into a single string.

    This is basically gluing L{flatten} to an L{io.BytesIO} and returning
    the results. See L{flatten} for the exact meanings of C{request} and
    C{root}.

    @return: A L{Deferred} which will be called back with a single UTF-8 encoded
        string as its result when C{root} has been completely flattened or which
        will be errbacked if an unexpected exception occurs.
    c                ,                                     S rZ   )getvalue)r\   ios    r4   r]   zflattenString.<locals>.<lambda>  s    BKKMM r6   )r   r   r9   rx   r   r   r(   )rg   ri   rV   r   s      @r4   flattenStringr     sQ     
Brx((AMM))))***###r6   )r%   r&   r'   r(   )r9   r:   r'   r;   rZ   )rL   r0   rM   rN   rO   rP   r'   rQ   )rV   rW   r'   rW   )rg   rh   ri   rQ   r9   r:   rM   rj   rk   rl   rm   rn   r'   ro   )rg   rh   ri   rQ   r9   r:   r'   r=   )rg   rh   ri   rQ   r9   r:   r'   r   )rg   rh   ri   rQ   r'   r   )A__doc__
__future__r   inspectr   r   r   sysr   	tracebackr   typesr   typingr	   r
   r   r   r   r   r   r   r   r   r   r   twisted.internet.deferr   r   twisted.python.compatr   twisted.python.failurer   twisted.web._stanr   r   r   r   r   r   twisted.web.errorr   r    r!   twisted.web.iwebr"   r#   r$   FlattenableRecursiver(   r0   r   rQ   r   r5   r8   rB   rE   rK   rU   rf   rt   r   r   r   rA   r6   r4   <module>r      s;  
  # " " " " "                                                                < ; ; ; ; ; ; ; . . . . . . * * * * * * N N N N N N N N N N N N N N N N K K K K K K K K K K 2 2 2 2 2 2 2 2GCLL  			

#$	"D$./!"h+,f6JJK    &   &, , , ,^4 4 4 4   . &*! ! ! ! !"   &N$ N$ N$ N$bE E E EP> > > >8$ $ $ $ $ $r6   