
    hF                    .   S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJrJrJrJrJrJrJrJrJrJrJrJr  SS	KJrJr  SS
KJr  SSKJ r   SSK!J"r"J#r#J$r$J%r%J&r&J'r'  SSK(J)r)J*r*J+r+  SSK,J-r-J.r.  \" S5      r/\r0 \\1\2\&\"\$\%\\0S4   \\0   \\0SS4   \#\\0   \\\0   \3\04   \-4   r4 Sr5SS jr6SS jr7    SS jr8SS jr9SS jr: S        S!S jjr;S"S jr<              S#S jr=        S$S jr>        S%S jr?S&S jr@g)'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                    [        U [        5      (       a  U R                  S5      n U R                  SS5      R                  SS5      R                  SS5      n U $ )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    [/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/web/_flatten.pyescapeForContentr2   G   sN     ${{7#<<h'//g>FFtWUDK    c                R    [        U [        5      (       a  U R                  S5      $ U $ )a  
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                   ^  SU 4S jjnU$ )a  
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" [        U 5      R                  SS5      5        g )N   "s   &quot;)r2   r.   )r0   writes    r1   _write*writeWithAttributeEscaping.<locals>._write   s    t$,,T9=>r3   )r0   bytesreturnNone )r9   r:   s   ` r1   writeWithAttributeEscapingr@   m   s    R? Mr3   c                r    [        U [        5      (       a  U R                  S5      n U R                  SS5      $ )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   escapedCDATArC      s1     ${{7#<< 233r3   c                    [        U [        5      (       a  U R                  S5      n U R                  SS5      n U (       a  U SS S:X  a  U S-  n U $ )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   escapedCommentrI      sM     ${{7#<<	*DRS	T!Kr3   c                f    [        U5       H  nUc  M  X;   d  M  X0   s  $    Ub  U$ [        U 5      e)zC
Find the value of the named slot in the given stack of slot data.
)reversedr    )nameslotDatadefault	slotFrames       r1   _getSlotValuerP      s>     h'	 T%6?" ( N4  r3   c                j   ^ ^ [        U 4S j5      mSU4S jjnSU4S jjnT R                  X5        T$ )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                $   > TR                  5       $ N)cancel)_ds    r1   <lambda>_fork.<locals>.<lambda>   s    r3   c                *   > TR                  U 5        U $ rS   )callback)resultd2s    r1   rZ   _fork.<locals>.callback   s    
Fr3   c                *   > TR                  U 5        U $ rS   )errback)failurer\   s    r1   r_   _fork.<locals>.errback   s    


7r3   )r[   r$   r=   r$   )r`   r   r=   r   )r   addCallbacks)rV   rZ   r_   r\   s   `  @r1   _forkrc      s-    
 34B NN8%Ir3   c           
   #  f  ^ ^^#    UUU4         SU U4S jjjmSU4S jjn[        U[        [        45      (       a  U" U" U5      5        g[        U[        5      (       a,  [	        UR
                  TUR                  5      nT" U5      v   g[        U[        5      (       a,  U" S5        U" [        UR                  5      5        U" S5        g[        U[        5      (       a,  U" S5        U" [        UR                  5      5        U" S5        g[        U[        5      (       Ga  TR                  UR                  5        UR                  nUb_  Uc  [!        SU S	35      eUR#                  S
5      n	SU	l        UR%                  U5      n
U
" T U	5      nT" U5      v   TR'                  5         gUR(                  (       d  T" UR*                  5      v   gU" S5        [        UR(                  [        5      (       a  UR(                  R-                  S5      nOUR(                  nU" U5        UR.                  R1                  5        HX  u  p[        U[        5      (       a  UR-                  S5      nU" SU-   S-   5        T" U[2        [5        U5      S9v   U" S5        MZ     UR*                  (       d  [7        U5      [8        ;  a0  U" S5        T" UR*                  [:        5      v   U" SU-   S-   5        gU" S5        g[        U[<        [>        [@        45      (       a  U H  nT" U5      v   M     g[        U[B        5      (       a(  SURD                  4-  nU" UR-                  S5      5        g[        U[F        5      (       a  U" [I        U5      5      v   g[K        U5      (       aF  U" [F        RL                  " [O        [P        [F        [R           [T        [R        4   U5      5      5      v   g[V        RX                  " U5      (       a  UR                  T 5      nT" XS9v   g[[        U5      e7f)a  
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XTX!5      $ rS   )_flattenElement)newRootdataEscaperrenderFactoryr9   requestrM   s       r1   	keepGoing"_flattenElement.<locals>.keepGoing  s     WX}
 	
r3   c                &   > U R                  T5      $ rS   )addCallback)r[   rk   s    r1   keepGoingAsync'_flattenElement.<locals>.keepGoingAsync  s    !!),,r3   s	   <![CDATA[rB   s   <!--rE   Nz$Tag wants to be rendered by method "z)" but is not contained in any IRenderableFr(   asciirH   s   =")r9   r8   r)   s   </s    />z&#%d;)ri   )
rg   Flattenablerh   $Callable[[Union[bytes, str]], bytes]ri   Optional[IRenderable]r9   Callable[[bytes], object]r=   z@Generator[Union[Flattenable, Deferred[Flattenable]], None, None])r[   Deferred[Flattenable]r=   rv   ).r+   r<   r,   r   rP   rL   rN   r   rC   r0   r   rI   r   appendrM   render
ValueErrorclonelookupRenderMethodpoptagNamechildrenr-   
attributesitemsr5   r@   r   r   r2   tuplelistr   r   ordinalr   rc   r   fromCoroutiner   r   rr   objectr"   
providedByr!   )rj   rootr9   rM   ri   rh   ro   	slotValuerendererName	rootClonerenderMethodr[   r}   kvelementescapedrk   s   `  `             @r1   rf   rf      s    b =H/<+0	

9
 -
 )	

 
J
 
- $%%k$ 	D$		!$))Xt||D		""	D%	 	 ll499%&f	D'	"	"gnTYY'(f	D#		&{{#$ :<. I> ?  

5)I#I(;;LIL!'95FF##LLN||DMM**ddllC((ll))'2GllGgOO))+DA!S!!HHW%$(U"# /7QRW7X  $K , ==L1E$K DMM+;<<%'/D()&M	D5$6	7	7GG$$ 	D'	"	"T\\O+gnnW%&	D(	#	#U4[))	T		""Yx4fkIJDQ
 	

 
			%	%W%33d##s   P,P1c           	     h  ^^	^
^#    / m	Sm
SU	U
U4S jjnS	U	U
U4S jjm[        XU/ S[        5      /nU(       aO   [        US   5      n[        U[        5      (       a  T" 5         UI Sh  vN nUR                  U5        U(       a  MO  T" 5         g N&! [         a    UR                  5          N/[         aw  n/ nU H:  nUR                  c  M  UR                  UR                  R                  S   5        M<     UR                  5         [        Xg[        [        5       S   5      5      eSnAff = f7f)
aY  
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                h   > TR                  U 5        T[        U 5      -  mT[        :  a  T" 5         g g rS   )rw   lenBUFFER_SIZE)bsbufbufSizeflushBuffers    r1   bufferedWrite#_flattenTree.<locals>.bufferedWrite  s.    

23r7k!M "r3   c                 P   > TS:  a  T" SR                  T 5      5        T S S 2	 Smg g )Nr   r3   )join)r   r   r9   s   r1   r   !_flattenTree.<locals>.flushBuffer  s+    Q;#((3- AG r3   NrF   r      )r   r<   r=   r>   )r=   r>   )rf   r2   nextr+   r   rw   StopIterationr|   	Exceptiongi_framef_localsr   r   r   )rj   r   r9   r   stackr   eroots	generatorr   r   r   s     `      @@@r1   _flattenTreer   s  s    ( CG   	}b$@PQ-E 	"59oG'8,,  '- LL!% %* M ( 	IIK 	FE"	%%1LL!3!3!<!<V!DE # IIK :hjm+DEE	FsS   4D2/B *B+B /D2	D2B D/-D2/	D/8D*AD**D//D2c                ,    [        [        XU5      5      $ )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   )rj   r   r9   s      r1   flattenr     s    2 ,we<==r3   c                   ^ [        5       m[        XTR                  5      nUR                  U4S j5        [	        [
        [           U5      $ )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                $   > TR                  5       $ rS   )getvalue)rU   ios    r1   rW   flattenString.<locals>.<lambda>  s    BKKMr3   )r   r   r9   rn   r   r   r<   )rj   r   rV   r   s      @r1   flattenStringr     s:     
Brxx(AMM)*##r3   )r0   zUnion[bytes, str]r=   r<   )r9   ru   r=   zCallable[[bytes], None]rS   )rL   r,   rM   z-Sequence[Optional[Mapping[str, Flattenable]]]rN   zOptional[Flattenable]r=   rr   )rV   Deferred[T]r=   r   )rj   Optional[IRequest]r   rr   r9   ru   rM   z)List[Optional[Mapping[str, Flattenable]]]ri   rt   rh   rs   r=   zMGenerator[Union[Generator[Any, Any, Any], Deferred[Flattenable]], None, None])rj   r   r   rr   r9   ru   r=   r>   )rj   r   r   rr   r9   ru   r=   zDeferred[None])rj   r   r   rr   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   rr   r   r2   r5   r@   rC   rI   rP   rc   rf   r   r   r   r?   r3   r1   <module>r      s  
 #          < . * N N K K 2CL  			

#$	"D$./!"h+,f6JJK &&,$,,^4. &*!
!;! #! 	!"&N$N$
N$ %N$ 8	N$
 )N$ 6N$ SN$bEE'2E;TE	EP>>'2>;T>>8$r3   