Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/web/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/web/__pycache__/_flatten.cpython-37.pyc

B

;��](>�@s4dZddlmZmZddlmZddlmZddlm	Z	ddl
mZyddlm
Z
Wnek
rpdd	�Z
YnXdd
lmZmZmZddlmZmZddlmZmZmZmZmZmZdd
lmZmZm Z ddl!m"Z"dd�Z#dd�Z$dd�Z%dd�Z&dd�Z'd&dd�Z(dd�Z)dd�Z*d d!�Z+d"d#�Z,d$d%�Z-dS)'zu
Context-free flattener/serializer for rendering Python objects, possibly
complex or arbitrarily nested, as strings.
�)�division�absolute_import)�BytesIO)�exc_info)�
GeneratorType)�
extract_tb)�iscoroutinecOsdS)NF�)�args�kwargsr	r	�G/opt/alt/python37/lib64/python3.7/site-packages/twisted/web/_flatten.pyrsr)�unicode�nativeString�	iteritems)�Deferred�ensureDeferred)�Tag�slot�voidElements�Comment�CDATA�CharRef)�UnfilledSlot�UnsupportedType�FlattenerError)�IRenderablecCs4t|t�r|�d�}|�dd��dd��dd�}|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.

    @type data: C{bytes} or C{unicode}
    @param data: The string to escape.

    @rtype: C{bytes}
    @return: The quoted form of C{data}.  If C{data} is unicode, return a utf-8
        encoded string.
    zutf-8�&s&amp;�<s&lt;�>s&gt;)�
isinstancer
�encode�replace)�datar	r	r�escapeForContent s

r#cCst|t�r|�d�S|S)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.

    @type data: C{bytes} or C{unicode}
    @param data: The string to escape.

    @return: The string, unchanged, except for encoding.
    @rtype: C{bytes}
    zutf-8)rr
r )r"r	r	r�attributeEscapingDoneOutside8s

r$cs�fdd�}|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.
    cs�t|��dd��dS)N�"s&quot;)r#r!)r")�writer	r�_writetsz*writeWithAttributeEscaping.<locals>._writer	)r&r'r	)r&r�writeWithAttributeEscapingNs&r(cCs t|t�r|�d�}|�dd�S)z�
    Escape CDATA for inclusion in a document.

    @type data: L{str} or L{unicode}
    @param data: The string to escape.

    @rtype: L{str}
    @return: The quoted form of C{data}. If C{data} is unicode, return a utf-8
        encoded string.
    zutf-8s]]>s]]]]><![CDATA[>)rr
r r!)r"r	r	r�escapedCDATAzs

r)cCsHt|t�r|�d�}|�dd��dd�}|rD|dd�dkrD|d	7}|S)
z�
    Escape a comment for inclusion in a document.

    @type data: L{str} or L{unicode}
    @param data: The string to escape.

    @rtype: C{str}
    @return: The quoted form of C{data}. If C{data} is unicode, return a utf-8
        encoded string.
    zutf-8s--s- - rs&gt;���N�-� )rr
r r!)r"r	r	r�escapedComment�s

r-NcCsHxB|ddd�D]}|dk	r||kr||SqW|dk	r<|St|��dS)zK
    Find the value of the named slot in the given stack of slot data.
    Nr*)r)�name�slotData�defaultZ	slotFramer	r	r�
_getSlotValue�sr1c#s�|||f��fdd�	�t|ttf�r4|||���n�t|t�r\t|j�|j�}�|�V�nxt|t�r�|d�|t|j	��|d��nLt|t
�r�|d�|t|j	��|d��n t|t��r��
|j�|jdk	�r|j}|�d�}d|_|�|�}	|	�|�}
�|
�V���dS|j�s0�|j�VdS|d	�t|jt��rT|j�d
�}n|j}||�xZt|j�D]L\}}
t|t��r�|�d
�}|d|d��|
tt|�d
�V|d��qnW|j�s�t|�tk�r�|d��|jt�V|d|d�n|d�n�t|tttf��r4x�|D]}�|�V�qWn�t|t ��r\d|j!f}||�d
��nxt|t"��r~|�#�fdd��VnVt$|��r�t%|�}|�#�fdd��Vn.t&�'|��r�|���}
�|
|d�Vnt(|��dS)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 yields L{bytes} that should be written
        to the output.  However it may also 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
        either be L{bytes}, in which case it's written, or another generator,
        in which case it is iterated.  See L{_flattenTree} for the trampoline
        that consumes said values.
    @rtype: An iterator which yields L{bytes}, L{Deferred}, and more iterators
        of the same type.
    cst�||�||�S)N)�_flattenElement)ZnewRoot�dataEscaper�
renderFactoryr&)�requestr/r	r�	keepGoing�s
z"_flattenElement.<locals>.keepGoings	<![CDATA[s]]>s<!--s-->NFr�asciir,s=")r&r%rs</s />z&#%d;cs|�|�fS)Nr	)�result)r6r	r�<lambda>�z!_flattenElement.<locals>.<lambda>cs|�|�fS)Nr	)r8)r6r	rr9r:)r4))r�bytesr
rr1r.r0rr)r"rr-r�appendr/Zrender�cloneZlookupRenderMethod�pop�tagName�childrenr rZ
attributesr$r(rrr#�tuple�listrrZordinalr�addCallbackrrrZ
providedByr)r5�rootr&r/r4r3Z	slotValueZrendererNameZ	rootCloneZrenderMethodr8r?�k�v�element�escaped�dr	)r6r5r/rr2�s~(











r2c	
#s�t|||gdt�g�x�r�y�dj}t�d�}Wn�tk
rP���Yqtk
r�}zV���g}x�D]}|�|jjd�qrW|�|jd�t	||t
t�d���Wdd}~XYqXt|t
�r�fdd�}|�|�Vq��|�qWdS)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: An iterator which yields objects of type L{bytes} and L{Deferred}.
        A L{Deferred} is only yielded when one is encountered in the process of
        flattening C{root}.  The returned iterator must not be iterated again
        until the L{Deferred} is called back.
    Nr*rD�cs|\}}��|�|S)N)r<)ZoriginalAndToFlatten�originalZ	toFlatten)�stackr	r�cbxMs
z_flattenTree.<locals>.cbx)r2r#�gi_frame�next�
StopIterationr>�	Exceptionr<�f_localsrrrrrrC)	r5rDr&�framerG�e�roots�	generatorrMr	)rLr�_flattenTree(s$

(
rWcshxbyt��}Wn0tk
r,��d�Yn2���Yn X���fdd�}|�|�j�PqWdS)a�
    Take strings from an iterator and pass them to a writer function.

    @param state: An iterator of L{str} and L{Deferred}.  L{str} instances will
        be passed to C{write}.  L{Deferred} instances will be waited on before
        resuming iteration of C{state}.

    @param write: A callable which will be invoked with each L{str}
        produced by iterating C{state}.

    @param result: A L{Deferred} which will be called back when C{state} has
        been completely flattened into C{write} or which will be errbacked if
        an exception in a generator passed to C{state} or an errback from a
        L{Deferred} from state occurs.

    @return: L{None}
    Ncst����|S)N)�_writeFlattenedData)rK)r8�stater&r	r�cbypsz _writeFlattenedData.<locals>.cby)rOrP�callbackZerrbackZaddCallbacks)rYr&r8rGrZr	)r8rYr&rrXVsrXcCs"t�}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{unicode},
        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 when C{root} has been
        completely flattened into C{write} or which will be errbacked if an
        unexpected exception occurs.
    )rrWrX)r5rDr&r8rYr	r	r�flattenxsr\cs*t��t||�j�}|��fdd��|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 string as
        its result when C{root} has been completely flattened into C{write} or
        which will be errbacked if an unexpected exception occurs.
    cs���S)N)�getvalue)�_)�ior	rr9�r:zflattenString.<locals>.<lambda>)rr\r&rC)r5rDrIr	)r_r�
flattenString�sr`)N).�__doc__�
__future__rrr_r�sysr�typesr�	tracebackr�inspectr�ImportErrorZtwisted.python.compatr
rrZtwisted.internet.deferrrZtwisted.web._stanrrrrrrZtwisted.web.errorrrrZtwisted.web.iwebrr#r$r(r)r-r1r2rWrXr\r`r	r	r	r�<module>s2 ,
{."