o
    Óó·iÁF  ã                   @  sÄ  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Z5dEdd„Z6dEdd„Z7dFdd„Z8dEd d!„Z9dEd"d#„Z:	dGdHd+d,„Z;dId/d0„Z<dJd:d;„Z=dKd=d>„Z>dLd@dA„Z?dMdCdD„Z@dS )Nzu
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                 C  s4   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;N©Ú
isinstanceÚstrÚencodeÚreplace©r$   © r2   úZC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/web/_flatten.pyÚescapeForContentG   s   

r4   c                 C  s   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(   N)r-   r.   r/   r1   r2   r2   r3   ÚattributeEscapingDoneOutsideZ   s   

r5   ÚwriteúCallable[[bytes], object]úCallable[[bytes], None]c                   s   d‡ fd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                   s   ˆ t | ƒ dd¡ƒ d S )Nó   "s   &quot;)r4   r0   r1   ©r6   r2   r3   Ú_write–   s   z*writeWithAttributeEscaping.<locals>._writeN)r$   r'   r&   r9   r2   )r6   r<   r2   r;   r3   ÚwriteWithAttributeEscapingm   s   )r=   c                 C  s    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[>Nr,   r1   r2   r2   r3   ÚescapedCDATAœ   s   
	
r?   c                 C  s@   t | tƒr
|  d¡} |  dd¡} | r| dd… dkr| d7 } | 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,   r1   r2   r2   r3   ÚescapedCommentª   s   

rD   Únamer.   ÚslotDataú-Sequence[Optional[Mapping[str, Flattenable]]]ÚdefaultúOptional[Flattenable]ÚFlattenablec                 C  s>   t |ƒD ]}|dur| |v r||    S q|dur|S t| ƒ‚)zK
    Find the value of the named slot in the given stack of slot data.
    N)Úreversedr   )rE   rF   rH   Z	slotFramer2   r2   r3   Ú_getSlotValue¾   s   €rL   ÚdúDeferred[T]c                   s<   t ‡ fdd„ƒ‰d‡fdd„}d‡fd
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©Ú_)rM   r2   r3   Ú<lambda>Ô   ó    z_fork.<locals>.<lambda>Úresultr#   r&   c                   ó   ˆ   | ¡ | S rP   )Úcallback©rV   ©Úd2r2   r3   rX   Ö   ó   
z_fork.<locals>.callbackÚfailurer   c                   rW   rP   )Úerrback)r]   rZ   r2   r3   r^   Ú   r\   z_fork.<locals>.errbackN)rV   r#   r&   r#   )r]   r   r&   r   )r   ZaddCallbacks)rM   rX   r^   r2   )rM   r[   r3   Ú_forkÏ   s
   r_   Ú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                 #  sø  � |||fd$‡‡fdd„‰ d%‡ fdd„}t |ttfƒr#|||ƒƒ dS t |tƒr7t|jˆ|jƒ}ˆ |ƒV  dS t |tƒrM|dƒ |t|j	ƒƒ |dƒ dS t |t
ƒrc|dƒ |t|j	ƒƒ |dƒ dS t |tƒ�rˆ |j¡ |j}|durŸ|du r‚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 ]$\}}t |tƒrÕ| d¡}|d| d ƒ ˆ |tt|ƒd�V  |dƒ qÇ|jsöt|ƒtv�r|dƒ ˆ |jtƒV  |d | d ƒ dS |d!ƒ dS t |ttt fƒ�r'|D ]}ˆ |ƒV  �qdS t |t!ƒ�r<d"|j"f }|| d¡ƒ dS t |t#ƒ�rK|t$|ƒƒV  dS t%|ƒ�rd|t# &t't(t#t) t*t)f |ƒ¡ƒV  dS t+ ,|¡�rx| ˆ¡}ˆ ||d#�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.
    ÚnewRootrJ   rf   rg   rd   re   r6   r7   r&   ú@Generator[Union[Flattenable, Deferred[Flattenable]], None, None]c                   s   t ˆ | |ˆ||ƒS rP   )Ú_flattenElement)ri   rf   rd   r6   )r`   rF   r2   r3   Ú	keepGoing  s   ÿz"_flattenElement.<locals>.keepGoingrV   úDeferred[Flattenable]c                   s
   |   ˆ ¡S rP   )ÚaddCallbackrY   )rl   r2   r3   ÚkeepGoingAsync  s   
z'_flattenElement.<locals>.keepGoingAsyncs	   <![CDATA[r>   s   <!--r@   Nz$Tag wants to be rendered by method "z)" but is not contained in any IRenderableFr*   ÚasciirC   s   ="r;   r:   r+   s   </s    />z&#%d;)rd   )
ri   rJ   rf   rg   rd   re   r6   r7   r&   rj   )rV   rm   r&   rm   ).r-   r'   r.   r   rL   rE   rH   r   r?   r$   r   rD   r   ÚappendrF   ÚrenderÚ
ValueErrorÚcloneZlookupRenderMethodÚpopÚtagNameÚchildrenr/   Ú
attributesÚitemsr5   r=   r   r   r4   ÚtupleÚlistr   r   Zordinalr   r_   r   ZfromCoroutiner   r
   rJ   Úobjectr!   Z
providedByr    )r`   rb   r6   rF   rd   rf   ro   Z	slotValueZrendererNameZ	rootCloneZrenderMethodrV   rv   ÚkÚvÚelementÚescapedr2   )rl   r`   rF   r3   rk   â   s”   €1ü




ÿ






ÿ
ÿ
ÿ
ÿ
rk   r9   c           	   
   ƒ  s  �g ‰ d‰d‡ ‡‡fdd„}d‡ ‡‡fdd	„‰t | ||g d
tƒg}|r|zt|d ƒ}t|tƒr7ˆƒ  |I d
H }W n< tyE   | ¡  Y n5 tyt } z$g }|D ]}|jd
ur`| 	|jj
d ¡ qP| ¡  t||ttƒ d ƒƒ‚d
}~ww | 	|¡ |s#ˆƒ  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&   r9   c                   s,   ˆ   | ¡ ˆt| ƒ7 ‰ˆtkrˆƒ  d S d S rP   )rq   ÚlenÚBUFFER_SIZE)r�   )ÚbufÚbufSizeÚflushBufferr2   r3   ÚbufferedWriteŒ  s
   

ÿz#_flattenTree.<locals>.bufferedWritec                     s,   ˆdkrˆd  ˆ ¡ƒ ˆ d d …= d‰d S d S )Nr   ó    )Újoinr2   )r„   r…   r6   r2   r3   r†   ˜  s
   
ýz!_flattenTree.<locals>.flushBufferNrA   rb   é   )r�   r'   r&   r9   )r&   r9   )rk   r4   Únextr-   r   ÚStopIterationru   Ú	ExceptionÚgi_framerq   Úf_localsr   r   r   )	r`   rb   r6   r‡   Ústackr   ÚeÚrootsÚ	generatorr2   )r„   r…   r†   r6   r3   Ú_flattenTrees  s:   €ÿ

€
€€ú
î
r”   úDeferred[None]c                 C  s   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.
    N)r   r”   )r`   rb   r6   r2   r2   r3   Úflatten»  s   r–   úDeferred[bytes]c                   s4   t ƒ ‰ t| |ˆ jƒ}| ‡ fd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                   rO   rP   )ÚgetvaluerR   ©Úior2   r3   rT   å  rU   zflattenString.<locals>.<lambda>N)r   r–   r6   rn   r   r   r'   )r`   rb   rM   r2   r™   r3   ÚflattenString×  s   r›   )r$   r%   r&   r'   )r6   r7   r&   r8   rP   )rE   r.   rF   rG   rH   rI   r&   rJ   )rM   rN   r&   rN   )r`   ra   rb   rJ   r6   r7   rF   rc   rd   re   rf   rg   r&   rh   )r`   ra   rb   rJ   r6   r7   r&   r9   )r`   ra   rb   rJ   r6   r7   r&   r•   )r`   ra   rb   rJ   r&   r—   )AÚ__doc__Ú
__future__r   Úinspectr   rš   r   Úsysr   Ú	tracebackr   Útypesr   Útypingr   r	   r
   r   r   r   r   r   r   r   r   r   Ztwisted.internet.deferr   r   Ztwisted.python.compatr   Ztwisted.python.failurer   Ztwisted.web._stanr   r   r   r   r   r   Ztwisted.web.errorr   r   r    Ztwisted.web.iwebr!   r"   r#   ZFlattenableRecursiver'   r.   r|   rJ   rƒ   r4   r5   r=   r?   rD   rL   r_   rk   r”   r–   r›   r2   r2   r2   r3   Ú<module>   s`   8 
ôÿ



/
ý

 

H