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"    