o
    ¯bG  ã                   @   sÞ   d 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	 ddl
mZ ddlmZmZmZmZmZ dZG d	d
„ d
eƒZG dd„ dƒZe dde¡Zdd„ Zdd„ ZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdS )zH
I contain PythonScript, which is a very simple python script resource.
é    N)ÚStringIO)Ú	copyright)ÚexecfileÚnetworkString)Ú_coerceToFilesystemEncoding)ÚhttpÚresourceÚserverÚstaticÚutilzº<p>You forgot to assign to the variable "resource" in your script. For example:</p>
<pre>
# MyCoolWebApp.rpy

import mygreatresource

resource = mygreatresource.MyGreatResource()
</pre>
c                   @   s   e Zd ZdZdS )ÚAlreadyCachedzG
    This exception is raised when a path has already been cached.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú4/usr/lib/python3/dist-packages/twisted/web/script.pyr      s    r   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚCacheScannerc                 C   s   || _ || _d| _d S )Nr   )ÚpathÚregistryÚdoCache©Úselfr   r   r   r   r   Ú__init__%   s   
zCacheScanner.__init__c                 C   s*   | j  | j¡}|d urt|ƒ‚|  ¡  d S ©N)r   ÚgetCachedPathr   r   Úrecache)r   Úcr   r   r   Úcache*   s   zCacheScanner.cachec                 C   s
   d| _ d S )Né   )r   )r   r   r   r   r   0   s   
zCacheScanner.recacheN)r   r   r   r   r   r   r   r   r   r   r   $   s    r   éô  úWhoops! Internal Errorc              
   C   sŠ   t | |ƒ}td| ƒt||j|jdœ}zt| ||ƒ W n ty1 } z|jd W  Y d}~S d}~ww |d }|jrC|turC| 	| |¡ |S )zª
    I am a normal py file which must define a 'resource' global, which should
    be an instance of (a subclass of) web.resource.Resource; it will be
    renderred.
    Ú )Ú__file__r   r   r   r   r   Nr   )
r   r   ÚnoRsrcr   r   r   r   Úargsr   Ú	cachePath)r   r   ÚcsÚglobÚacÚrsrcr   r   r   ÚResourceScript7   s"   
û€ÿr+   c                 C   s|   ddl m} td| ƒt ddt¡|dœ}t| ƒ�}| || ¡}W d   ƒ n1 s)w   Y  t|ddƒ}t	|||ƒ |d	 S )
Nr   )Úptl_compiler"   r    r!   )r#   r   r   z<source>Úexecr   )
Úquixoter,   r   r   Ú	ErrorPageÚrpyNoResourceÚopenÚcompile_templateÚcompileÚeval)r   r   r,   r(   ÚfÚeÚcoder   r   r   ÚResourceTemplateO   s   ý
ÿr8   c                   @   s&   e Zd Zddd„Zdd„ Zdd„ ZdS )	ÚResourceScriptWrapperNc                 C   ó$   t j | ¡ || _|pt ¡ | _d S r   ©r   ÚResourcer   r   r
   ÚRegistryr   r   r   r   r   r   `   ó   zResourceScriptWrapper.__init__c                 C   s   t | j| jƒ}| |¡S r   )r+   r   r   Úrender)r   ÚrequestÚresr   r   r   r?   e   s   
zResourceScriptWrapper.renderc                 C   s   t | j| jƒ}| ||¡S r   )r+   r   r   ÚgetChildWithDefault)r   r   r@   rA   r   r   r   rB   i   s   z)ResourceScriptWrapper.getChildWithDefaultr   )r   r   r   r   r?   rB   r   r   r   r   r9   _   s    
r9   c                   @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚResourceScriptDirectorya  
    L{ResourceScriptDirectory} is a resource which serves scripts from a
    filesystem directory.  File children of a L{ResourceScriptDirectory} will
    be served using L{ResourceScript}.  Directory children will be served using
    another L{ResourceScriptDirectory}.

    @ivar path: A C{str} giving the filesystem path in which children will be
        looked up.

    @ivar registry: A L{static.Registry} instance which will be used to decide
        how to interpret scripts found as children of this resource.
    Nc                 C   r:   r   r;   )r   Úpathnamer   r   r   r   r   |   r>   z ResourceScriptDirectory.__init__c                 C   sH   t j | j|¡}t j |¡rt|| jƒS t j |¡r t|| jƒS t 	¡ S r   )
Úosr   ÚjoinÚisdirrC   r   Úexistsr+   r   Ú
NoResource)r   r   r@   Úfnr   r   r   ÚgetChild�   s   z ResourceScriptDirectory.getChildc                 C   s   t  ¡  |¡S r   )r   rI   r?   )r   r@   r   r   r   r?   Š   s   zResourceScriptDirectory.renderr   )r   r   r   r   r   rK   r?   r   r   r   r   rC   n   s
    
	rC   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚPythonScriptz¾
    I am an extremely simple dynamic resource; an embedded python script.

    This will execute a file (usually of the extension '.epy') as Python code,
    internal to the webserver.
    Tc                 C   s   || _ || _dS )z3
        Initialize me with a script name.
        N)Úfilenamer   )r   rM   r   r   r   r   r   ˜   s   
zPythonScript.__init__c              
   C   sâ   |  dtdtj ƒ¡ |td| jƒ| jdœ}z	t| j||ƒ W nJ tyG } z|j	dkr=| 
tj¡ | t d¡ |¡¡ W Y d}~n'd}~w tyi   tƒ }tj|d� t | ¡ ¡}| d	¡}| |¡ Y nw | ¡  tjS )
a4  
        Render me to a web client.

        Load my file, execute it in a special namespace (with 'request' and
        '__file__' global vars) and finish the request.  Output to the web-page
        will NOT be handled with print - standard output goes to the log - but
        with request.write.
        s   x-powered-byz
Twisted/%sr"   )r@   r#   r   é   zFile not found.N)ÚfileÚutf8)Ú	setHeaderr   r   Úversionr   rM   r   r   ÚOSErrorÚerrnoÚsetResponseCoder   Ú	NOT_FOUNDÚwriter   rI   r?   ÚBaseExceptionr   Ú	tracebackÚ	print_excr   Ú_PREÚgetvalueÚencodeÚfinishr	   ÚNOT_DONE_YET)r   r@   Ú	namespacer6   ÚioÚoutputr   r   r   r?   Ÿ   s.   	ÿ
ý
€
ûzPythonScript.renderN)r   r   r   r   ÚisLeafr   r?   r   r   r   r   rL   Ž   s
    rL   )r   rE   rY   ra   r   Útwistedr   Útwisted.python.compatr   r   Útwisted.python.filepathr   Útwisted.webr   r   r	   r
   r   r0   Ú	Exceptionr   r   r/   r$   r+   r8   r<   r9   rC   rL   r   r   r   r   Ú<module>   s"    