U
    P|jÖw  ã                x   @   sª  d Z ddlZddlZddl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 ddl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 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, ddl-m.Z.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8m9Z9 dd„ Z:e;e8e;dœdd„Z<G dd„ de)j=ƒZ>G dd„ de)j=ƒZ?G dd„ de)j=ƒZ@G dd „ d e+ƒZAG d!d"„ d"e+ƒZBG d#d$„ d$e+ƒZCG d%d&„ d&e+ƒZDG d'd(„ d(ƒZEd)ZFG d*d+„ d+ejGejHƒZIee	e eJf ed, d-œd.d/„ZKee9ƒG d0d1„ d1ƒƒZLG d2d3„ d3e+ƒZMd4d5„ ZNd6ZOeƒ ZPee9ƒG d7d8„ d8ƒƒZQee9ƒG d9d:„ d:ƒƒZRd;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|d}d~dd€d�d‚dƒd„d…d†d‡dˆd‰dŠd‹dŒd�dŽd�d�d‘d’d“d”d•d–d—d˜d™dšd›dœd�dždŸd d¡d¢d£d¤d¥d¦d§d¨d©dªd«d¬d­d®d¯d°d±d²hxZSG d³d´„ d´ƒZTeTƒ ZUdºe8e7ee; eee#gd¶f  eVd·œd¸d¹„ZWdS )»zG
twisted.web.util and twisted.web.template merged to avoid cyclic deps
é    N)ÚOrderedDict©Úescape)ÚIOÚAnyÚAnyStrÚCallableÚDictÚListÚMappingÚOptionalÚTupleÚUnionÚcast)ÚhandlerÚmake_parser)ÚAttributesNSImplÚLocator)Úimplementer)ÚDeferred)ÚLogger)Úurlpath)ÚFailure)ÚFilePath)ÚfullyQualifiedName)Úresource)ÚElementÚrenderer)ÚFlattenableÚflattenÚflattenString)ÚCDATAÚCommentÚTagÚslot)ÚIRenderableÚIRequestÚITemplateLoaderc                 C   s   dt | ƒ› d�S )aK  
    Wraps <pre> tags around some text and HTML-escape it.

    This is here since once twisted.web.html was deprecated it was hard to
    migrate the html.PRE from current code to twisted.web.template.

    For new code consider using twisted.web.template.

    @return: Escaped text wrapped in <pre> tags.
    @rtype: C{str}
    z<pre>z</pre>r   )Útext© r)   ú>/tmp/pip-unpacked-wheel-audpp6lv/twisted/web/_template_util.pyÚ_PRE,   s    r+   )ÚURLÚrequestÚreturnc                 C   sH   t | tƒstdƒ‚| dd¡ | | ¡ ddt|  d¡ƒ d¡i }|S )a¿  
    Generate a redirect to the given location.

    @param URL: A L{bytes} giving the location to which to redirect.

    @param request: The request object to use to generate the redirect.
    @type request: L{IRequest<twisted.web.iweb.IRequest>} provider

    @raise TypeError: If the type of C{URL} a L{str} instead of L{bytes}.

    @return: A L{bytes} containing HTML which tries to convince the client
        agent
        to visit the new location even if it doesn't respect the I{FOUND}
        response code.  This is intended to be returned from a render method,
        eg::

            def render_GET(self, request):
                return redirectTo(b"http://example.com/", request)
    zURL must be bytess   Content-Types   text/html; charset=utf-8sÀ   
<html>
    <head>
        <meta http-equiv="refresh" content="0;URL=%(url)s">
    </head>
    <body bgcolor="#FFFFFF" text="#000000">
    <a href="%(url)s">click here</a>
    </body>
</html>
s   urlúutf-8)Ú
isinstanceÚbytesÚ	TypeErrorZ	setHeaderÚredirectr   ÚdecodeÚencode)r,   r-   Úcontentr)   r)   r*   Ú
redirectTo;   s    


 ÿ÷r7   c                       s:   e Zd ZdZdZedœ‡ fdd„Zdd„ Zdd	„ Z‡  Z	S )
ÚRedirectz›
    Resource that redirects to a specific URL.

    @ivar url: Redirect target URL to put in the I{Location} response header.
    @type url: L{bytes}
    T)Úurlc                    s   t ƒ  ¡  || _d S ©N)ÚsuperÚ__init__r9   )Úselfr9   ©Ú	__class__r)   r*   r<   n   s    
zRedirect.__init__c                 C   s   t | j|ƒS r:   )r7   r9   )r=   r-   r)   r)   r*   Úrenderr   s    zRedirect.renderc                 C   s   | S r:   r)   ©r=   Únamer-   r)   r)   r*   ÚgetChildu   s    zRedirect.getChild)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚisLeafr1   r<   r@   rC   Ú__classcell__r)   r)   r>   r*   r8   d   s
   r8   c                   @   s$   e Zd ZdZdZeedœdd„ZdS )ÚParentRedirectuÆ  
    Redirect to the nearest directory and strip any query string.

    This generates redirects like::

        /              â†’  /
        /foo           â†’  /
        /foo?bar       â†’  /
        /foo/          â†’  /foo/
        /foo/bar       â†’  /foo/
        /foo/bar?baz   â†’  /foo/

    However, the generated I{Location} header contains an absolute URL rather
    than a path.

    The response is the same regardless of HTTP method.
    é   )r-   r.   c                 C   s$   t tj |¡ ¡ ƒ d¡}t||ƒS )zN
        Respond to all requests by redirecting to nearest directory.
        Úascii)Ústrr   ZURLPathZfromRequestÚherer5   r7   )r=   r-   rN   r)   r)   r*   r@   Ž   s    zParentRedirect.renderN)rD   rE   rF   rG   rH   r&   r1   r@   r)   r)   r)   r*   rJ   y   s   rJ   c                   @   s<   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )ÚDeferredResourcezT
    I wrap up a Deferred that will eventually result in a Resource
    object.
    rK   c                 C   s   t j | ¡ || _d S r:   )r   ÚResourcer<   Úd)r=   rQ   r)   r)   r*   r<   ž   s    zDeferredResource.__init__c                 C   s   | S r:   r)   rA   r)   r)   r*   rC   ¢   s    zDeferredResource.getChildc                 C   s*   | j  | j|¡ | j|¡ ddlm} |S )Nr   )ÚNOT_DONE_YET)rQ   ZaddCallbackÚ_cbChildÚ
addErrbackÚ_ebChildZtwisted.web.serverrR   )r=   r-   rR   r)   r)   r*   r@   ¥   s    zDeferredResource.renderc                 C   s   |  t ||¡¡ d S r:   )r@   r   ZgetChildForRequest)r=   Úchildr-   r)   r)   r*   rS   «   s    zDeferredResource._cbChildc                 C   s   |  |¡ d S r:   )ZprocessingFailed)r=   Úreasonr-   r)   r)   r*   rU   ®   s    zDeferredResource._ebChildN)
rD   rE   rF   rG   rH   r<   rC   r@   rS   rU   r)   r)   r)   r*   rO   –   s   rO   c                   @   s0   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZdS )	Ú_SourceLineElementa  
    L{_SourceLineElement} is an L{IRenderable} which can render a single line of
    source code.

    @ivar number: A C{int} giving the line number of the source code to be
        rendered.
    @ivar source: A C{str} giving the source code to be rendered.
    c                 C   s   t  | |¡ || _|| _d S r:   )r   r<   ÚnumberÚsource)r=   ÚloaderrY   rZ   r)   r)   r*   r<   ¼   s    z_SourceLineElement.__init__c                 C   s   || j  dd¡ƒS )zA
        Render the line of source as a child of C{tag}.
        z  u    Â )rZ   Úreplace©r=   r-   Útagr)   r)   r*   Ú
sourceLineÁ   s    z_SourceLineElement.sourceLinec                 C   s   |t | jƒƒS )z>
        Render the line number as a child of C{tag}.
        )rM   rY   r]   r)   r)   r*   Ú
lineNumberÈ   s    z_SourceLineElement.lineNumberN)rD   rE   rF   rG   r<   r   r_   r`   r)   r)   r)   r*   rX   ²   s   	
rX   c                   @   s,   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdS )	Ú_SourceFragmentElementa¾  
    L{_SourceFragmentElement} is an L{IRenderable} which can render several lines
    of source code near the line number of a particular frame object.

    @ivar frame: A L{Failure<twisted.python.failure.Failure>}-style frame object
        for which to load a source line to render.  This is really a tuple
        holding some information from a frame object.  See
        L{Failure.frames<twisted.python.failure.Failure>} for specifics.
    c                 C   s   t  | |¡ || _d S r:   ©r   r<   Úframe©r=   r[   rc   r)   r)   r*   r<   Û   s    z_SourceFragmentElement.__init__c                 c   sF   | j d }| j d }t|d |d ƒD ]}|t ||¡ ¡ fV  q&dS )a$  
        Find the source line references by C{self.frame} and yield, in source
        line order, it and the previous and following lines.

        @return: A generator which yields two-tuples.  Each tuple gives a source
            line number and the contents of that source line.
        rK   é   N)rc   ÚrangeÚ	linecacheÚgetlineÚrstrip)r=   Úfilenamer`   ZsnipLineNumberr)   r)   r*   Ú_getSourceLinesß   s    

z&_SourceFragmentElement._getSourceLinesc                 c   sV   |   ¡ D ]H\}}| ¡ }|| jd kr,d}nd}t|f d|iŽƒ}t|||ƒV  qdS )zý
        Render the source line indicated by C{self.frame} and several
        surrounding lines.  The active line will be given a I{class} of
        C{"snippetHighlightLine"}.  Other lines will be given a I{class} of
        C{"snippetLine"}.
        re   ZsnippetHighlightLineZsnippetLineÚclassN)rk   Úclonerc   Ú	TagLoaderrX   )r=   r-   r^   r`   r_   ZnewTagZcssClassr[   r)   r)   r*   ÚsourceLinesì   s    z"_SourceFragmentElement.sourceLinesN)rD   rE   rF   rG   r<   rk   r   ro   r)   r)   r)   r*   ra   Ð   s
   
ra   c                   @   sH   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZed
d„ ƒZ	dS )Ú_FrameElementa°  
    L{_FrameElement} is an L{IRenderable} which can render details about one
    frame from a L{Failure<twisted.python.failure.Failure>}.

    @ivar frame: A L{Failure<twisted.python.failure.Failure>}-style frame object
        for which to load a source line to render.  This is really a tuple
        holding some information from a frame object.  See
        L{Failure.frames<twisted.python.failure.Failure>} for specifics.
    c                 C   s   t  | |¡ || _d S r:   rb   rd   r)   r)   r*   r<   	  s    z_FrameElement.__init__c                 C   s   || j d ƒS )zY
        Render the name of the file this frame references as a child of C{tag}.
        rK   ©rc   r]   r)   r)   r*   rj     s    z_FrameElement.filenamec                 C   s   |t | jd ƒƒS )zc
        Render the source line number this frame references as a child of
        C{tag}.
        re   )rM   rc   r]   r)   r)   r*   r`     s    z_FrameElement.lineNumberc                 C   s   || j d ƒS )zV
        Render the function name this frame references as a child of C{tag}.
        r   rq   r]   r)   r)   r*   Úfunction  s    z_FrameElement.functionc                 C   s   t t|ƒ| jƒS )zn
        Render the source code surrounding the line this frame references,
        replacing C{tag}.
        )ra   rn   rc   r]   r)   r)   r*   rZ   #  s    z_FrameElement.sourceN)
rD   rE   rF   rG   r<   r   rj   r`   rr   rZ   r)   r)   r)   r*   rp   þ   s   



rp   c                   @   s$   e Zd ZdZdd„ Zedd„ ƒZdS )Ú_StackElementzW
    L{_StackElement} renders an L{IRenderable} which can render a list of frames.
    c                 C   s   t  | |¡ || _d S r:   )r   r<   ÚstackFrames)r=   r[   rt   r)   r)   r*   r<   1  s    z_StackElement.__init__c                    s   ‡ fdd„| j D ƒS )zW
        Render the list of frames in this L{_StackElement}, replacing C{tag}.
        c                    s   g | ]}t tˆ  ¡ ƒ|ƒ‘qS r)   )rp   rn   rm   )Ú.0rc   ©r^   r)   r*   Ú
<listcomp>:  s    z(_StackElement.frames.<locals>.<listcomp>)rt   r]   r)   rv   r*   Úframes5  s    
ÿz_StackElement.framesN)rD   rE   rF   rG   r<   r   rx   r)   r)   r)   r*   rs   ,  s   rs   c                   @   sv   e Zd ZdZded  dœdd„Zdee ee ee dœdd„Zee ee dd	œd
d„Zee ee dœdd„Z	dS )Ú
_NSContextzL
    A mapping from XML namespaces onto their prefixes in the document.
    N)Úparentc                 C   s*   || _ |dk	rt|jƒ| _n
ddi| _dS )ze
        Pull out the parent's namespaces, if there's no parent then default to
        XML.
        Nz$http://www.w3.org/XML/1998/namespaceÚxml)rz   r   Únss)r=   rz   r)   r)   r*   r<   D  s    z_NSContext.__init__)ÚkrQ   r.   c                 C   s   | j  ||¡S )z\
        Get a prefix for a namespace.

        @param d: The default prefix value.
        )r|   Úget)r=   r}   rQ   r)   r)   r*   r~   O  s    z_NSContext.get)r}   Úvr.   c                 C   s   | j  ||¡ dS )zH
        Proxy through to setting the prefix for the namespace.
        N)r|   Ú__setitem__)r=   r}   r   r)   r)   r*   r€   W  s    z_NSContext.__setitem__)r}   r.   c                 C   s   | j  |¡S )zH
        Proxy through to getting the prefix for the namespace.
        )r|   Ú__getitem__)r=   r}   r)   r)   r*   r�   ]  s    z_NSContext.__getitem__)N)N)
rD   rE   rF   rG   r   r<   rM   r~   r€   r�   r)   r)   r)   r*   ry   ?  s
    ry   z4http://twistedmatrix.com/ns/twisted.web.template/0.1c                   @   s(  e Zd ZdZee dœdd„Zeddœdd„Zdd	œd
d„Z	dd	œdd„Z
eeddœdd„Zee eddœdd„Zee ddœdd„Zeeef ee eddœdd„Zeddœdd„Zeeef ee ddœdd„Zeeedd œd!d"„Zedd#œd$d%„Zdd	œd&d'„Zdd	œd(d)„Zedd*œd+d,„ZdS )-Ú_ToStanzd
    A SAX parser which converts an XML document to the Twisted STAN
    Document Object Model.
    )ÚsourceFilenamec                 C   s   || _ tƒ | _d| _dS )zP
        @param sourceFilename: the filename the XML was loaded out of.
        FN)rƒ   ry   Ú	prefixMapÚinCDATA)r=   rƒ   r)   r)   r*   r<   m  s    z_ToStan.__init__N)Úlocatorr.   c                 C   s
   || _ dS )zY
        Set the document locator, which knows about line and character numbers.
        N)r†   )r=   r†   r)   r)   r*   ÚsetDocumentLocatoru  s    z_ToStan.setDocumentLocator©r.   c                 C   s   g | _ | j | _g | _g | _dS )z*
        Initialise the document.
        N)ÚdocumentÚcurrentÚstackÚ
xmlnsAttrs©r=   r)   r)   r*   ÚstartDocument{  s    z_ToStan.startDocumentc                 C   s   dS )z!
        Document ended.
        Nr)   r�   r)   r)   r*   ÚendDocument‡  s    z_ToStan.endDocument)ÚtargetÚdatar.   c                 C   s   dS )z6
        Processing instructions are ignored.
        Nr)   )r=   r�   r‘   r)   r)   r*   ÚprocessingInstructionŒ  s    z_ToStan.processingInstruction)ÚprefixÚurir.   c                 C   sT   t | jƒ| _|| j|< |tkr"dS |dkr<| j d|f¡ n| j d| |f¡ dS )z×
        Set up the prefix mapping, which maps fully qualified namespace URIs
        onto namespace prefixes.

        This gets called before startElementNS whenever an C{xmlns} attribute
        is seen.
        NÚxmlnszxmlns:%s)ry   r„   ÚTEMPLATE_NAMESPACErŒ   Úappend)r=   r“   r”   r)   r)   r*   ÚstartPrefixMapping‘  s    	
z_ToStan.startPrefixMapping)r“   r.   c                 C   s"   | j j}|dk	stdƒ‚|| _ dS )zb
        "Pops the stack" on the prefix mapping.

        Gets called after endElementNS.
        Nz$More prefix mapping ends than starts)r„   rz   ÚAssertionError)r=   r“   rz   r)   r)   r*   ÚendPrefixMapping§  s    z_ToStan.endPrefixMapping)ÚnamespaceAndNameÚqnameÚattrsr.   c                 C   sd  | j }| j ¡ }| j ¡ }|\}}|tkr |dkr8d}nh|dkr z|d }	W n tk
rd   d}	Y nX t|d |	|||d�}
| j |
¡ | j	 |
¡ |
j
| _	dS d}t|ƒ}t| ¡ ƒD ],\}}|\}}|tkrÒq¸|dkr¸|}||= q¸tƒ }| ¡ D ]>\\}}}| j |¡}|dk�r|}n|› d	|› �}|||< qô|tk�r¸|d
k�r¸| j�s`tdt› d�ƒ‚d|k�rztdt› d�ƒ‚td||||d�}|| jd j|d < | j |¡ |j
| _	dS | j�rÖ| t| jƒ¡ g | _|tk�r|dk	�r| j| }|dk	�r| j| › d	|› �}t|ttttttf tf |ƒƒ||||d�}| j |¡ | j	 |¡ |j
| _	dS )a]  
        Gets called when we encounter a new xmlns attribute.

        @param namespaceAndName: a (namespace, name) tuple, where name
            determines which type of action to take, if the namespace matches
            L{TEMPLATE_NAMESPACE}.
        @param qname: ignored.
        @param attrs: attributes on the element being started.
        ÚtransparentÚ r$   )NÚdefaultN)NrB   )r    rj   r`   ÚcolumnNumberr@   ú:Úattrz<{z}attr> as top-level elementrB   z }attr> requires a name attribute)r@   rj   r`   r¡   éÿÿÿÿ)Ú
attributesr@   rj   r`   r¡   )rƒ   r†   ÚgetLineNumberÚgetColumnNumberr–   ÚKeyErrorr$   r‹   r—   rŠ   Úchildrenr   ÚlistÚitemsr„   r~   r™   r#   r¥   rŒ   Úupdater   r   r   r1   rM   )r=   r›   rœ   r�   rj   r`   r¡   ÚnsrB   r    Úslr@   Zorderedr}   r   ZattrNSZjustTheNameZnonTemplateAttrsZattrNsÚattrNameZnsPrefixZattrKeyÚelr“   r)   r)   r*   ÚstartElementNS±  sž    


û	


ÿ

ÿû

ÿø
z_ToStan.startElementNS)Úchr.   c                 C   s*   | j r| jd  |¡ dS | j |¡ dS )zm
        Called when we receive some characters.  CDATA characters get passed
        through as is.
        r¤   N)r…   r‹   r—   rŠ   )r=   r²   r)   r)   r*   Ú
characters)  s    z_ToStan.characters)rB   rœ   r.   c                 C   s,   | j  ¡  | j r | j d j| _n| j| _dS )z’
        A namespace tag is closed.  Pop the stack, if there's anything left in
        it, otherwise return to the document's namespace.
        r¤   N)r‹   Úpopr©   rŠ   r‰   )r=   rB   rœ   r)   r)   r*   ÚendElementNS3  s    
z_ToStan.endElementNS)rB   ÚpublicIdÚsystemIdr.   c                 C   s   dS ©z#
        DTDs are ignored.
        Nr)   )r=   rB   r¶   r·   r)   r)   r*   ÚstartDTD>  s    z_ToStan.startDTD)Úargsr.   c                 G   s   dS r¸   r)   )r=   rº   r)   r)   r*   ÚendDTDC  s    z_ToStan.endDTDc                 C   s   d| _ | j g ¡ dS )zO
        We're starting to be in a CDATA element, make a note of this.
        TN)r…   r‹   r—   r�   r)   r)   r*   Ú
startCDATAH  s    z_ToStan.startCDATAc                 C   s*   d| _ d | j ¡ ¡}| j t|ƒ¡ dS )z‰
        We're no longer in a CDATA element.  Collect up the characters we've
        parsed and put them in a new CDATA object.
        FrŸ   N)r…   Újoinr‹   r´   rŠ   r—   r!   )r=   Úcommentr)   r)   r*   ÚendCDATAO  s    z_ToStan.endCDATA)r6   r.   c                 C   s   | j  t|ƒ¡ dS )z=
        Add an XML comment which we've encountered.
        N)rŠ   r—   r"   )r=   r6   r)   r)   r*   r¾   X  s    z_ToStan.comment)rD   rE   rF   rG   r   rM   r<   r   r‡   rŽ   r�   r’   r˜   rš   r   r   r±   r³   rµ   r¹   Úobjectr»   r¼   r¿   r¾   r)   r)   r)   r*   r‚   g  s(   
ûx
	r‚   r   )Úflr.   c                 C   s€   t ƒ }| tjd¡ | tjd¡ | tjd¡ | tjd¡ tt| ddƒƒ}| 	|¡ | 
|¡ | tj|¡ | | ¡ |jS )z 
    Perform a SAX parse of an XML document with the _ToStan class.

    @param fl: The XML document to be parsed.

    @return: a C{list} of Stan objects.
    r   rK   rB   N)r   Ú
setFeaturer   Úfeature_validationÚfeature_namespacesÚfeature_external_gesÚfeature_external_pesr‚   ÚgetattrÚsetContentHandlerÚsetEntityResolverÚsetPropertyÚproperty_lexical_handlerÚparser‰   )rÁ   ÚparserÚsr)   r)   r*   Ú_flatsaxParse_  s    


rÏ   c                   @   s8   e Zd ZdZeeef dœdd„Zed dœdd„Z	d	S )
Ú	XMLStringzH
    An L{ITemplateLoader} that loads and parses XML from a string.
    )rÎ   c                 C   s(   t |tƒs| d¡}tt |¡ƒ| _dS )z¸
        Run the parser on a L{io.StringIO} copy of the string.

        @param s: The string from which to load the XML.
        @type s: L{str}, or a UTF-8 encoded L{bytes}.
        Úutf8N)r0   rM   r4   rÏ   ÚioÚStringIOÚ_loadedTemplate)r=   rÎ   r)   r)   r*   r<   }  s    

zXMLString.__init__r   rˆ   c                 C   s   | j S )zM
        Return the document.

        @return: the loaded document.
        )rÔ   r�   r)   r)   r*   ÚloadŠ  s    zXMLString.loadN)
rD   rE   rF   rG   r   rM   r1   r<   r
   rÕ   r)   r)   r)   r*   rÐ   w  s   rÐ   c                   @   sF   e Zd ZdZedƒZddd„Zedd„ ƒZedd	„ ƒZ	ed
d„ ƒZ
dS )ÚFailureElementa  
    L{FailureElement} is an L{IRenderable} which can render detailed information
    about a L{Failure<twisted.python.failure.Failure>}.

    @ivar failure: The L{Failure<twisted.python.failure.Failure>} instance which
        will be rendered.

    @since: 12.1
    ao  
<div xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">
  <style type="text/css">
    div.error {
      color: red;
      font-family: Verdana, Arial, helvetica, sans-serif;
      font-weight: bold;
    }

    div {
      font-family: Verdana, Arial, helvetica, sans-serif;
    }

    div.stackTrace {
    }

    div.frame {
      padding: 1em;
      background: white;
      border-bottom: thin black dashed;
    }

    div.frame:first-child {
      padding: 1em;
      background: white;
      border-top: thin black dashed;
      border-bottom: thin black dashed;
    }

    div.location {
    }

    span.function {
      font-weight: bold;
      font-family: "Courier New", courier, monospace;
    }

    div.snippet {
      margin-bottom: 0.5em;
      margin-left: 1em;
      background: #FFFFDD;
    }

    div.snippetHighlightLine {
      color: red;
    }

    span.code {
      font-family: "Courier New", courier, monospace;
    }
  </style>

  <div class="error">
    <span t:render="type" />: <span t:render="value" />
  </div>
  <div class="stackTrace" t:render="traceback">
    <div class="frame" t:render="frames">
      <div class="location">
        <span t:render="filename" />:<span t:render="lineNumber" /> in
        <span class="function" t:render="function" />
      </div>
      <div class="snippet" t:render="source">
        <div t:render="sourceLines">
          <span class="lineno" t:render="lineNumber" />
          <code class="code" t:render="sourceLine" />
        </div>
      </div>
    </div>
  </div>
  <div class="error">
    <span t:render="type" />: <span t:render="value" />
  </div>
</div>
Nc                 C   s   t  | |¡ || _d S r:   )r   r<   Úfailure)r=   r×   r[   r)   r)   r*   r<   ë  s    zFailureElement.__init__c                 C   s   |t | jjƒƒS )zA
        Render the exception type as a child of C{tag}.
        )r   r×   Útyper]   r)   r)   r*   rØ   ï  s    zFailureElement.typec                 C   s   |t | jjƒ d¡ƒS )zB
        Render the exception value as a child of C{tag}.
        rÑ   )rM   r×   Úvaluer5   r]   r)   r)   r*   rÙ   ö  s    zFailureElement.valuec                 C   s   t t|ƒ| jjƒS )z–
        Render all the frames in the wrapped
        L{Failure<twisted.python.failure.Failure>}'s traceback stack, replacing
        C{tag}.
        )rs   rn   r×   rx   r]   r)   r)   r*   Ú	tracebacký  s    zFailureElement.traceback)N)rD   rE   rF   rG   rÐ   r[   r<   r   rØ   rÙ   rÚ   r)   r)   r)   r*   rÖ   “  s   
ÿM


rÖ   c                 C   sN   g }t dt| ƒƒ |j¡ t|d tƒr>|d  d¡ dd¡S |d  ¡  dS )a  
    Construct an HTML representation of the given failure.

    Consider using L{FailureElement} instead.

    @type myFailure: L{Failure<twisted.python.failure.Failure>}

    @rtype: L{bytes}
    @return: A string containing the HTML representation of the given failure.
    Nr   r/   rL   Úxmlcharrefreplace)	r    rÖ   ÚaddBothr—   r0   r1   r4   r5   ZraiseException)Z	myFailureÚresultr)   r)   r*   ÚformatFailure  s
    rÞ   rK   c                   @   s0   e Zd ZdZddœdd„Zed dœdd„Zd	S )
rn   zJ
    An L{ITemplateLoader} that loads an existing flattenable object.
    r   rv   c                 C   s
   || _ dS )z>
        @param tag: The object which will be loaded.
        Nrv   )r=   r^   r)   r)   r*   r<   -  s    zTagLoader.__init__rˆ   c                 C   s   | j gS r:   rv   r�   r)   r)   r*   rÕ   5  s    zTagLoader.loadN)rD   rE   rF   rG   r<   r
   rÕ   r)   r)   r)   r*   rn   '  s   rn   c                   @   sT   e Zd ZdZee dœdd„Zed dœdd„Ze	dœd	d
„Z
ed dœdd„ZdS )ÚXMLFilezF
    An L{ITemplateLoader} that loads and parses XML from a file.
    )Úpathc                 C   s*   t |tƒstjdtdd� d| _|| _dS )zf
        Run the parser on a file.

        @param path: The file from which to load the XML.
        zhPassing filenames or file objects to XMLFile is deprecated since Twisted 12.1.  Pass a FilePath instead.re   )ÚcategoryÚ
stacklevelN)r0   r   ÚwarningsÚwarnÚDeprecationWarningrÔ   Ú_path)r=   rà   r)   r)   r*   r<   ?  s    
üzXMLFile.__init__r   rˆ   c              
   C   sB   t | jtƒst| jƒS | j d¡�}t|ƒW  5 Q R £ S Q R X dS )zP
        Read and parse the XML.

        @return: the loaded document.
        ÚrN)r0   ræ   r   rÏ   Úopen)r=   Úfr)   r)   r*   Ú_loadDocS  s    
zXMLFile._loadDocc                 C   s   d| j ›d�S )Nz<XMLFile of ú>)ræ   r�   r)   r)   r*   Ú__repr___  s    zXMLFile.__repr__c                 C   s   | j dkr|  ¡ | _ | j S )zl
        Return the document, first loading it if necessary.

        @return: the loaded document.
        N)rÔ   rê   r�   r)   r)   r*   rÕ   b  s    

zXMLFile.loadN)rD   rE   rF   rG   r   r   r<   r
   rê   rM   rì   rÕ   r)   r)   r)   r*   rß   9  s
   rß   ÚaZabbrZacronymÚaddressÚappletÚareaÚarticleÚasideÚaudioÚbÚbaseÚbasefontZbdiZbdoÚbigÚ
blockquoteÚbodyÚbrÚbuttonZcanvasÚcaptionÚcenterZciteÚcodeÚcolÚcolgroupÚcommandZdatalistÚddÚdelÚdetailsÚdfnÚdirÚdivÚdlÚdtÚemÚembedÚfieldsetÚ
figcaptionÚfigureÚfontÚfooterÚformrc   ÚframesetÚh1Úh2Úh3Úh4Úh5Úh6ÚheadÚheaderÚhgroupÚhrÚhtmlÚiÚiframeÚimgÚinputZinsÚisindexÚkeygenZkbdÚlabelZlegendÚliÚlinkÚmapÚmarkÚmenuÚmetaZmeterÚnavÚnoframesÚnoscriptrÀ   ÚolÚoptgroupÚoptionÚoutputÚpÚparamÚpreÚprogressÚqÚrpÚrtÚrubyrÎ   ZsampÚscriptÚsectionÚselectÚsmallrZ   ÚspanÚstrikeÚstrongÚstyleÚsubÚsummaryÚsupÚtableÚtbodyÚtdÚtextareaÚtfootÚthÚtheadÚtimeÚtitleÚtrÚttÚuÚulÚvarÚvideoÚwbrc                   @   s    e Zd ZdZeedœdd„ZdS )Ú_TagFactorya†  
    A factory for L{Tag} objects; the implementation of the L{tags} object.

    This allows for the syntactic convenience of C{from twisted.web.template
    import tags; tags.a(href="linked-page.html")}, where 'a' can be basically
    any HTML tag.

    The class is not exposed publicly because you only ever need one of these,
    and we already made it for you.

    @see: L{tags}
    )ÚtagNamer.   c                 C   s8   |dkrt dƒS | d¡}|tkr0td|›�ƒ‚t |ƒS )Nrž   rŸ   Ú_zunknown tag )r#   ri   ÚVALID_HTML_TAG_NAMESÚAttributeError)r=   rV  r)   r)   r*   Ú__getattr__û  s    
z_TagFactory.__getattr__N)rD   rE   rF   rG   rM   r#   rZ  r)   r)   r)   r*   rU  í  s   rU  ó   <!DOCTYPE html>r   )r-   ÚelementÚdoctypeÚ_failElementr.   c                    s„   |dk	rˆ  |¡ ˆ  d¡ ˆ dkr(t‰ tˆ|ˆj ƒ}tttd  dœ‡ ‡fdd„}ˆdœtttdœdd	„}| |¡ | 	|¡ t
S )
aã  
    Render an element or other L{IRenderable}.

    @param request: The L{IRequest} being rendered to.
    @param element: An L{IRenderable} which will be rendered.
    @param doctype: A L{bytes} which will be written as the first line of
        the request, or L{None} to disable writing of a doctype.  The argument
        should not include a trailing newline and will default to the HTML5
        doctype C{'<!DOCTYPE html>'}.

    @returns: NOT_DONE_YET

    @since: 12.1
    Nó   
)r×   r.   c                    sX   t jd| d� tˆdd ƒ}|d k	rF|jrFˆ d k	s4t‚tˆˆ | ƒˆjƒS ˆ d¡ d S d S )Nz/An error occurred while rendering the response.)r×   Úsitesr   <div style="font-size:800%;background-color:#FFF;color:#F00">An error occurred while rendering the response.</div>)Ú
_moduleLogr×   rÇ   ZdisplayTracebacksr™   r   Úwrite)r×   r`  ©r^  r-   r)   r*   Úeb%  s     ÿÿzrenderElement.<locals>.eb)r-   )rÝ   r-   r.   c                S   s   |  ¡  | S r:   )Úfinish)rÝ   r-   r)   r)   r*   re  6  s    zrenderElement.<locals>.finish)rb  rÖ   r   r   r   r   rÀ   r&   rT   rÜ   rR   )r-   r\  r]  r^  rQ   rd  re  r)   rc  r*   ÚrenderElement  s    



rf  )r[  N)XrG   rÒ   rg   rã   Úcollectionsr   r  r   Útypingr   r   r   r   r	   r
   r   r   r   r   r   Zxml.saxr   r   Zxml.sax.xmlreaderr   r   Zzope.interfacer   Ztwisted.internet.deferr   Ztwisted.loggerr   Ztwisted.pythonr   Ztwisted.python.failurer   Ztwisted.python.filepathr   Ztwisted.python.reflectr   Ztwisted.webr   Ztwisted.web._elementr   r   Ztwisted.web._flattenr   r   r    Ztwisted.web._stanr!   r"   r#   r$   Ztwisted.web.iwebr%   r&   r'   r+   r1   r7   rP   r8   rJ   rO   rX   ra   rp   rs   ry   r–   ÚContentHandlerÚEntityResolverr‚   rM   rÏ   rÐ   rÖ   rÞ   rR   ra  rn   rß   rX  rU  ÚtagsrÀ   rf  r)   r)   r)   r*   Ú<module>   s`  4)..% y t8ˆ|  üû