o
    Óó·iŽ  ã                   @   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   úXC:\Users\psant\Downloads\PAF backend\school\venv\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 u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 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   Z	cachePath)r   r   ÚcsÚglobÚacZ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   )
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   ó$   t j | ¡ || _|pt ¡ | _d S r   ©r   ÚResourcer   r   r
   ZRegistryr   r   r   r   r   r   b   ó   zResourceScriptWrapper.__init__c                 C   s   t | j| jƒ}| |¡S r   )r(   r   r   Úrender)r   ÚrequestÚresr   r   r   r9   g   s   
zResourceScriptWrapper.renderc                 C   s   t | j| jƒ}| ||¡S r   )r(   r   r   ÚgetChildWithDefault)r   r   r:   r;   r   r   r   r<   k   s   z)ResourceScriptWrapper.getChildWithDefaultr   )r   r   r   r   r9   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   r5   r   r6   )r   Úpathnamer   r   r   r   r   ~   r8   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   r:   Úfnr   r   r   ÚgetChildƒ   s   z ResourceScriptDirectory.getChildc                 C   s   t  ¡  |¡S r   )r   rC   r9   )r   r:   r   r   r   r9   Œ   s   zResourceScriptDirectory.renderr   )r   r   r   r   r   rE   r9   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   rG   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)Z	setHeaderr   r   Úversionr   rG   r   r   ÚOSErrorÚerrnoZsetResponseCoder   Ú	NOT_FOUNDÚwriter   rC   r9   ÚBaseExceptionr   Ú	tracebackÚ	print_excr   Z_PREÚgetvalueÚencodeÚfinishr	   ZNOT_DONE_YET)r   r:   Ú	namespacer1   ÚioÚoutputr   r   r   r9   ¡   s2   	ÿ
ý
ÿ€
ûzPythonScript.renderN)r   r   r   r   ZisLeafr   r9   r   r   r   r   rF   �   s
    rF   )r   r?   rQ   rW   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   r7   r4   r=   rF   r   r   r   r   Ú<module>   s"    