Ë
    ojâF  ã                  ó  — 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„Z7	 	 	 	 dd„Z8dd„Z9dd„Z:	 d 	 	 	 	 	 	 	 d!d„Z;d"d„Z<	 	 	 	 	 	 	 	 	 	 	 	 	 	 d#d„Z=	 	 	 	 	 	 	 	 d$d„Z>	 	 	 	 	 	 	 	 d%d„Z?d&d„Z@y)'zu
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   c                ó¬   — t        | t        «      r| j                  d«      } | j                  dd«      j                  dd«      j                  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©Údatas    ú6/usr/lib/python3/dist-packages/twisted/web/_flatten.pyÚescapeForContentr2   G   sK   € ô �$œÔØ�{‰{˜7Ó#ˆØ�<‰<˜˜hÓ'×/Ñ/°°gÓ>×FÑFÀtÈWÓU€DØ€Kó    c                óH   — t        | t        «      r| j                  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+   r,   r-   r/   s    r1   ÚattributeEscapingDoneOutsider5   Z   s"   € ô �$œÔØ�{‰{˜7Ó#Ð#Ø€Kr3   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.
    c                óH   •—  ‰t        | «      j                  dd«      «       y )Nó   "s   &quot;)r2   r.   )r0   Úwrites    €r1   Ú_writez*writeWithAttributeEscaping.<locals>._write–   s   ø€ ÙÔ˜tÓ$×,Ñ,¨T°9Ó=Õ>r3   )r0   ÚbytesÚreturnÚNone© )r9   r:   s   ` r1   ÚwriteWithAttributeEscapingr?   m   s   ø€ õR?ð €Mr3   c                óh   — t        | t        «      r| j                  d«      } | j                  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*   r/   s    r1   ÚescapedCDATArB   œ   s.   € ô �$œÔØ�{‰{˜7Ó#ˆØ�<‰<˜Ð 2Ó3Ð3r3   c                óŠ   — t        | t        «      r| j                  d«      } | j                  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*   r/   s    r1   ÚescapedCommentrH   ª   sJ   € ô �$œÔØ�{‰{˜7Ó#ˆØ�<‰<˜ 	Ó*€DÙ��R�S�	˜TÒ!Ø�‰ˆØ€Kr3   c                ó\   — t        |«      D ]  }|€Œ| |v sŒ||    c S  |�|S t        | «      ‚)zK
    Find the value of the named slot in the given stack of slot data.
    )Úreversedr    )ÚnameÚslotDataÚdefaultÚ	slotFrames       r1   Ú_getSlotValuerO   ¾   sG   € ô ˜hÓ'ò !ˆ	ØÑ  T¨YÒ%6Ø˜T‘?Ò"ð!ð ÐØˆNÜ˜4Ó Ð r3   c                ób   ‡ ‡— t        ˆ fd„«      Šdˆfd„}dˆfd„}‰ j                  ||«       ‰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                ó$   •— ‰j                  «       S ©N)Úcancel)Ú_Úds    €r1   ú<lambda>z_fork.<locals>.<lambda>Ô   s   ø€ ¨¯©«€ r3   c                ó*   •— ‰j                  | «       | S rR   )Úcallback)ÚresultÚd2s    €r1   rX   z_fork.<locals>.callbackÖ   s   ø€ Ø
�‰�FÔØˆr3   c                ó*   •— ‰j                  | «       | S rR   )Úerrback)ÚfailurerZ   s    €r1   r\   z_fork.<locals>.errbackÚ   s   ø€ Ø
�
‰
�7ÔØˆr3   )rY   r$   r<   r$   )r]   r   r<   r   )r   ÚaddCallbacks)rU   rX   r\   rZ   s   `  @r1   Ú_forkr_   Ï   s/   ù€ ô
 Ó3Ó4€Bõõð ‡N�N�8˜WÔ%Ø€Ir3   c           
   #  óÌ  ‡ ‡‡K  — |||f	 	 	 	 	 	 	 	 	 dˆ ˆfd„Šdˆfd„}t        |t        t        f«      r | ||«      «       yt        |t        «      r,t	        |j
                  ‰|j                  «      } ‰|«      –— yt        |t        «      r, |d«        |t        |j                  «      «        |d«       yt        |t        «      r, |d«        |t        |j                  «      «        |d«       yt        |t        «      �rÃ‰j                  |j                  «       |j                  }|�^|€t!        d|› d	�«      ‚|j#                  d
«      }	d|	_        |j%                  |«      }
 |
‰ |	«      } ‰|«      –— ‰j'                  «        y|j(                  s ‰|j*                  «      –— y |d«       t        |j(                  t        «      r|j(                  j-                  d«      }n|j(                  } ||«       |j.                  j1                  «       D ]V  \  }}t        |t        «      r|j-                  d«      } |d|z   dz   «        ‰|t2        t5        |«      ¬«      –—  |d«       ŒX |j*                  st7        |«      t8        vr0 |d«        ‰|j*                  t:        «      –—  |d|z   dz   «       y |d«       yt        |t<        t>        t@        f«      r|D ]  } ‰|«      –— Œ yt        |tB        «      r(d|jD                  fz  } ||j-                  d«      «       yt        |tF        «      r |tI        |«      «      –— ytK        |«      rE |tG        jL                  tO        tP        tF        tR           tT        tR        f   |«      «      «      –— ytW        jX                  |«      r|j                  ‰ «      } ‰||¬«      –— yt[        |«      ‚­w)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.
    c                ó$   •— t        ‰| |‰||«      S rR   )Ú_flattenElement)ÚnewRootÚdataEscaperÚrenderFactoryr9   ÚrequestrL   s       €€r1   Ú	keepGoingz"_flattenElement.<locals>.keepGoing  s   ø€ ô Ø�W˜e X¨}¸kó
ð 	
r3   c                ó&   •— | j                  ‰«      S rR   )ÚaddCallback)rY   rg   s    €r1   ÚkeepGoingAsyncz'_flattenElement.<locals>.keepGoingAsync  s   ø€ Ø×!Ñ! )Ó,Ð,r3   s	   <![CDATA[rA   s   <!--rD   Nz$Tag wants to be rendered by method "z)" but is not contained in any IRenderableFr(   ÚasciirG   s   =")r9   r8   r)   s   </s    />z&#%d;)re   )
rc   ÚFlattenablerd   ú$Callable[[Union[bytes, str]], bytes]re   úOptional[IRenderable]r9   úCallable[[bytes], object]r<   z@Generator[Union[Flattenable, Deferred[Flattenable]], None, None])rY   úDeferred[Flattenable]r<   rp   ).r+   r;   r,   r   rO   rK   rM   r   rB   r0   r   rH   r   ÚappendrL   ÚrenderÚ
ValueErrorÚcloneÚlookupRenderMethodÚpopÚtagNameÚchildrenr-   Ú
attributesÚitemsr5   r?   r   r   r2   ÚtupleÚlistr   r   Úordinalr   r_   r   ÚfromCoroutiner   r   rl   Úobjectr"   Ú
providedByr!   )rf   Úrootr9   rL   re   rd   rj   Ú	slotValueÚrendererNameÚ	rootCloneÚrenderMethodrY   rw   ÚkÚvÚelementÚescapedrg   s   `  `             @r1   rb   rb   â   sn  úè ø€ ðb =HØ/<Ø+0ð	
Øð
à9ð
ð -ð
ð )ð	
ð
 
Jö
õ-ô �$œ¤˜Ô%Ù‰k˜$ÓÕ Ü	�Dœ$Ô	Ü! $§)¡)¨X°t·|±|ÓDˆ	Ù˜	Ó"Ó"Ü	�Dœ%Ô	 ÙˆlÔÙŒl˜4Ÿ9™9Ó%Ô&Ùˆf�Ü	�Dœ'Ô	"ÙˆgŒÙŒn˜TŸY™YÓ'Ô(Ùˆf�Ü	�Dœ#Õ	Ø�‰˜Ÿ™Ô&Ø—{‘{ˆØÐ#ØÐ$Ü Ø:¸<¸.ð I>ð ?óð ð Ÿ
™
 5Ó)ˆIØ#ˆIÔØ(×;Ñ;¸LÓIˆLÙ! '¨9Ó5ˆFÙ˜FÓ#Ò#Ø�L‰LŒNØà�|Š|Ù˜DŸM™MÓ*Ò*ØáˆdŒÜ�d—l‘l¤CÔ(Ø—l‘l×)Ñ)¨'Ó2‰Gà—l‘lˆGÙˆgŒØ—O‘O×)Ñ)Ó+ò 		‰DˆAˆqÜ˜!œSÔ!Ø—H‘H˜WÓ%�Ù�$˜‘(˜UÑ"Ô#ñ ØÔ/Ô7QÐRWÓ7Xôò ñ �$�Kð		ð �=Š=œL¨Ó1¼ÑEÙ�$ŒKñ ˜DŸM™MÔ+;Ó<Ò<Ù�%˜'‘/ DÑ(Õ)á�&�Mä	�Dœ5¤$¬Ð6Ô	7Øò 	%ˆGÙ˜GÓ$Ó$ñ	%ä	�Dœ'Ô	"Ø˜TŸ\™\˜OÑ+ˆÙˆg�n‰n˜WÓ%Õ&Ü	�Dœ(Ô	#ÙœU 4›[Ó)Ó)Ü	�TÔ	ÙÜ×"Ñ"Ü”Yœx¬Ñ4´f¼kÐIÑJÈDÓQóó
ó 	
ô
 
×	Ñ	 Ô	%Ø—‘˜WÓ%ˆÙ˜¨dÔ3Ó3ä˜dÓ#Ð#ùs   …OO$c           	   ƒ  óx  ‡‡
‡‡K  — g Š
dŠdˆ
ˆˆfd„}d	ˆ
ˆˆfd„Št        | ||g dt        «      g}|rS	 |d   j                  }t        |d   «      }t	        |t
        «      r ‰«        |ƒ d{  –—† }|j                  |«       |rŒS ‰«        y7 Œ # t        $ r |j                  «        Y Œ(t        $ r…}|j                  «        g }|D ]*  }	|j                  |	j                  j                  d   «       Œ, |j                  j                  d   «       t        ||t        t        «       d   «      «      ‚d}~ww xY w­w)
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   c                óf   •— ‰j                  | «       ‰t        | «      z  Š‰t        k\  r ‰«        y y rR   )rq   ÚlenÚBUFFER_SIZE)ÚbsÚbufÚbufSizeÚflushBuffers    €€€r1   ÚbufferedWritez#_flattenTree.<locals>.bufferedWriteŒ  s.   ø€ à�
‰
�2ŒØ”3�r“7ÑˆØ”kÒ!Ù�Mð "r3   c                 óN   •— ‰dkD  r ‰dj                  ‰ «      «       ‰ d d …= dŠy y )Nr   r3   )Újoin)r�   r�   r9   s   €€€r1   r‘   z!_flattenTree.<locals>.flushBuffer˜  s+   ø€ à�QŠ;Ù�#—(‘(˜3“-Ô Ø’A�Ø‰Gð r3   NrE   r�   é   )rŽ   r;   r<   r=   )r<   r=   )rb   r2   Úgi_frameÚnextr+   r   rq   ÚStopIterationrv   Ú	ExceptionÚf_localsr   r   r   )rf   r�   r9   r’   ÚstackÚframerˆ   ÚeÚrootsÚ	generatorr�   r�   r‘   s     `       @@@r1   Ú_flattenTreer    s  s#  ûè ø€ ð( €CØ€G÷÷ô 	˜  }°b¸$Ô@PÓQð-€Eñ ð	"Ø˜"‘I×&Ñ&ˆEÜ˜5 ™9“oˆGÜ˜'¤8Ô,ñ ”Ø 'Ÿ-�ð �L‰L˜Ô!ò' ñ, …Mð (ùÜò 	Ø�I‰IŽKÜò 	FØ�I‰IŒKØˆEØ"ò B�	Ø—‘˜Y×/Ñ/×8Ñ8¸Ñ@ÕAðBà�L‰L˜Ÿ™¨Ñ/Ô0Ü   E¬:´h³jÀ±mÓ+DÓEÐEûð	FüsM   †,D:³9B Á,BÁ-B Á1D:ÂD:ÂB ÂD7Â(D:Â*D7Â2B D2Ä2D7Ä7D: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    )rf   r�   r9   s      r1   Úflattenr¢   ¼  s   € ô2 œ, w°°eÓ<Ó=Ð=r3   c                óœ   ‡— t        «       Št        | |‰j                  «      }|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                ó$   •— ‰j                  «       S rR   )Úgetvalue)rT   Úios    €r1   rV   zflattenString.<locals>.<lambda>æ  s   ø€ ˜BŸK™K›M€ r3   )r   r¢   r9   ri   r   r   r;   )rf   r�   rU   r¦   s      @r1   ÚflattenStringr§   Ø  s<   ø€ ô 
‹€BÜ�˜˜rŸx™xÓ(€AØ‡M�MÓ)Ô*Ü”œ‘ Ó#Ð#r3   )r0   zUnion[bytes, str]r<   r;   )r9   ro   r<   zCallable[[bytes], None]rR   )rK   r,   rL   z-Sequence[Optional[Mapping[str, Flattenable]]]rM   zOptional[Flattenable]r<   rl   )rU   úDeferred[T]r<   r¨   )rf   úOptional[IRequest]r�   rl   r9   ro   rL   z)List[Optional[Mapping[str, Flattenable]]]re   rn   rd   rm   r<   zMGenerator[Union[Generator[Any, Any, Any], Deferred[Flattenable]], None, None])rf   r©   r�   rl   r9   ro   r<   r=   )rf   r©   r�   rl   r9   ro   r<   zDeferred[None])rf   r©   r�   rl   r<   zDeferred[bytes])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;   r,   r   rl   r�   r2   r5   r?   rB   rH   rO   r_   rb   r    r¢   r§   r>   r3   r1   ú<module>r¸      sÞ  ðñ
õ #å Ý Ý Ý  Ý ÷÷ ÷ ó ÷ <Ý .Ý *ß N× Nß KÑ Kß 2áˆCƒL€àÐ ðð Ø	ØØØ	ØØØ	Ð
 Ð
#Ñ$ØÐ	ÑØÐ" D¨$Ð.Ñ/ØØÐ!Ñ"ØˆhÐ+Ñ,¨fÐ6JÐJÑKØðñ€ðð €óó&ð&,Ø$ð,àó,ó^4óð. &*ð!Ø
ð!à;ð!ð #ð!ð ó	!ó"ð&N$ØðN$à
ðN$ð %ðN$ð 8ð	N$ð
 )ðN$ð 6ðN$ð SóN$ðbFØðFØ'2ðFØ;TðFà	óFðR>Øð>Ø'2ð>Ø;Tð>àó>ô8$r3   