U
    /WjŽ  ã                   @   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   ú]c:\users\psant\downloads\hotstarbackend\school\venv38\Lib\site-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   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 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   ÚcsÚglobÚacZrsrcr   r   r   ÚResourceScript7   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   Ú_UnsafeErrorPageÚrpyNoResourceÚopenZcompile_templateÚcompileÚeval)r   r   r)   r&   ÚfÚeÚcoder   r   r   ÚResourceTemplateO   s      ÿû
r3   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   b   s    zResourceScriptWrapper.__init__c                 C   s   t | j| jƒ}| |¡S r   )r(   r   r   Úrender)r   ÚrequestÚresr   r   r   r7   g   s    zResourceScriptWrapper.renderc                 C   s   t | j| jƒ}| ||¡S r   )r(   r   r   ÚgetChildWithDefault)r   r   r8   r9   r   r   r   r:   k   s    z)ResourceScriptWrapper.getChildWithDefault)N)r   r   r   r   r7   r:   r   r   r   r   r4   a   s   
r4   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   r5   )r   Úpathnamer   r   r   r   r   ~   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   Ú_UnsafeNoResource)r   r   r8   Úfnr   r   r   ÚgetChildƒ   s    z ResourceScriptDirectory.getChildc                 C   s   t  ¡  |¡S r   )r   rA   r7   )r   r8   r   r   r   r7   Œ   s    zResourceScriptDirectory.render)N)r   r   r   r   r   rC   r7   r   r   r   r   r;   p   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   rE   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 nH tk
rÒ   tƒ }tj|d� t | ¡ ¡}| 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!   )r8   r"   r   é   zFile not found.N)ÚfileÚutf8)Z	setHeaderr   r   Úversionr   rE   r   r   ÚOSErrorÚerrnoZsetResponseCoder   Ú	NOT_FOUNDÚwriter   rA   r7   ÚBaseExceptionr   Ú	tracebackÚ	print_excr   Z_PREÚgetvalueÚencodeÚfinishr	   ZNOT_DONE_YET)r   r8   Ú	namespacer1   ÚioÚoutputr   r   r   r7   ¡   s0    	 ÿ
ý
ÿ
zPythonScript.renderN)r   r   r   r   ZisLeafr   r7   r   r   r   r   rD   �   s   rD   )r   r=   rO   rU   r   Ztwistedr   Ztwisted.python.compatr   r   Ztwisted.python.filepathr   Ztwisted.webr   r   r	   r
   r   r,   Ú	Exceptionr   r   r+   r#   r(   r3   r6   r4   r;   rD   r   r   r   r   Ú<module>   s     