U
    W[q                     @   s   d Z ddlmZm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 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.
    )divisionabsolute_importN)	copyright)_coerceToFilesystemEncoding)execfilenetworkStringNativeStringIO_PY3)httpserverstaticresource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 k	rt||   d S N)r   ZgetCachedPathr   r   recache)r   cr   r   r   cache(   s    zCacheScanner.cachec                 C   s
   d| _ d S )N   )r   )r   r   r   r   r   .   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 n0 tk
rd } z|jd  W Y S d}~X Y nX |d }|jr|tk	r|	| | |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   Z	cachePath)r   r   ZcsglobZacZrsrcr   r   r   ResourceScript3   s    
 r)   c              	   C   sh   ddl m} td| tddt|d}t| }||| }W 5 Q R X t|dd}t	||| |d	 S )
Nr   )ptl_compiler$   r"   r#   )r%   r   r   z<source>execr   )
Zquixoter*   r   r   	ErrorPagerpyNoResourceopenZcompile_templatecompileeval)r   r   r*   r(   fecoder   r   r   ResourceTemplateJ   s    
r4   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	ResourceScriptWrapperNc                 C   s$   t j|  || _|pt | _d S r   r   Resourcer   r   r   ZRegistryr   r   r   r   r   r   \   s    zResourceScriptWrapper.__init__c                 C   s   t | j| j}||S r   )r)   r   r   render)r   requestresr   r   r   r8   a   s    zResourceScriptWrapper.renderc                 C   s   t | j| j}|||S r   )r)   r   r   getChildWithDefault)r   r   r9   r:   r   r   r   r;   e   s    z)ResourceScriptWrapper.getChildWithDefault)N)r   r   r   r   r8   r;   r   r   r   r   r5   Z   s   
r5   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   s$   t j|  || _|pt | _d S r   r6   )r   pathnamer   r   r   r   r   x   s    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isdirr<   r   existsr)   r   
NoResource)r   r   r9   fnr   r   r   getChild}   s    z ResourceScriptDirectory.getChildc                 C   s   t  |S r   )r   rB   r8   )r   r9   r   r   r   r8      s    zResourceScriptDirectory.render)N)r   r   r   r   r   rD   r8   r   r   r   r   r<   k   s   
	r<   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   rF   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 n tk
r } z0|j	dkr||
tj |td| W 5 d}~X Y nD   t }tj|d t| }tr|d	}|| Y nX |  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$   )r9   r%   r      zFile not found.N)fileutf8)Z	setHeaderr   r   versionr   rF   r   r   IOErrorerrnoZsetResponseCoder
   Z	NOT_FOUNDwriter   rB   r8   r   	traceback	print_excr   Z_PREgetvaluer	   encodeZfinishr   ZNOT_DONE_YET)r   r9   	namespacer2   iooutputr   r   r   r8      s(    	

&
zPythonScript.renderN)r   r   r   r   ZisLeafr   r8   r   r   r   r   rE      s   rE   )!r   Z
__future__r   r   r>   rN   Ztwistedr   Ztwisted.python.filepathr   Ztwisted.python.compatr   r   r   r	   Ztwisted.webr
   r   r   r   r   r-   	Exceptionr   r   r,   r&   r)   r4   r7   r5   r<   rE   r   r   r   r   <module>   s   
 