
    Ϫf                     H   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
e      Z G d d      Z ej,                  dde      Zd Zd Z G d dej4                        Z G d dej4                        Z G d dej4                        Zy)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                       e Zd ZdZy)AlreadyCachedzG
    This exception is raised when a path has already been cached.
    N)__name__
__module____qualname____doc__     4/usr/lib/python3/dist-packages/twisted/web/script.pyr   r      s    r   r   c                       e Zd Zd Zd Zd Zy)CacheScannerc                 .    || _         || _        d| _        y )Nr   )pathregistrydoCacheselfr   r   s      r   __init__zCacheScanner.__init__%   s    	 r   c                     | j                   j                  | j                        }|t        |      | j	                          y N)r   getCachedPathr   r   recache)r   cs     r   cachezCacheScanner.cache*   s3    MM''		2=""r   c                     d| _         y )N   )r   )r   s    r   r"   zCacheScanner.recache0   s	    r   N)r   r   r   r   r$   r"   r   r   r   r   r   $   s    
r   r     Whoops! Internal Errorc                 .   t        | |      }t        d|       t        ||j                  |j                  d}	 t        | ||       |d   }|j                  r|t        ur|j                  | |       |S # t        $ r}|j                  d   cY d}~S d}~ww xY w)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rsrcs         r   ResourceScriptr3   7   s     
dH	%B/D9::DtT" 
D	zzd&(4&K  wwqzs   A2 2	B;B	BBc                    ddl m} t        d|       t        j                  ddt
              |d}t        |       5 }|j                  ||       }d d d        t        dd      }t        |||       |d	   S # 1 sw Y   (xY w)
Nr   )ptl_compiler*   r'   r(   )r+   r	   r   z<source>execr	   )
quixoter5   r   r	   _UnsafeErrorPagerpyNoResourceopencompile_templatecompileeval)r   r   r5   r0   fecodes          r   ResourceTemplaterA   O   s    # 0D9--)=
 D 
d 2q((D121j&)DtT
	2 2s   A55A>c                        e Zd ZddZd Zd Zy)ResourceScriptWrapperNc                     t         j                  j                  |        || _        |xs t	        j
                         | _        y r    r	   Resourcer   r   r   Registryr   r   s      r   r   zResourceScriptWrapper.__init__b   s0    ""4(	 5FOO$5r   c                 d    t        | j                  | j                        }|j                  |      S r    )r3   r   r   render)r   requestress      r   rI   zResourceScriptWrapper.renderg   s%    TYY6zz'""r   c                 f    t        | j                  | j                        }|j                  ||      S r    )r3   r   r   getChildWithDefault)r   r   rJ   rK   s       r   rM   z)ResourceScriptWrapper.getChildWithDefaultk   s)    TYY6&&tW55r   r    )r   r   r   r   rI   rM   r   r   r   rC   rC   a   s    6
#6r   rC   c                   $    e Zd ZdZddZd Zd Zy)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                     t         j                  j                  |        || _        |xs t	        j
                         | _        y r    rE   )r   pathnamer   s      r   r   z ResourceScriptDirectory.__init__~   s0    ""4(	 5FOO$5r   c                 R   t         j                  j                  | j                  |      }t         j                  j                  |      rt	        || j
                        S t         j                  j                  |      rt        || j
                        S t        j                         S r    )
osr   joinisdirrO   r   existsr3   r	   _UnsafeNoResource)r   r   rJ   fns       r   getChildz ResourceScriptDirectory.getChild   sk    WW\\$))T*77==*2t}}==77>>"!"dmm44))++r   c                 H    t        j                         j                  |      S r    )r	   rW   rI   )r   rJ   s     r   rI   zResourceScriptDirectory.render   s    ))+227;;r   r    )r   r   r   r   r   rY   rI   r   r   r   rO   rO   p   s    6
,<r   rO   c                        e Zd ZdZdZd Zd Zy)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                      || _         || _        y)z3
        Initialize me with a script name.
        N)filenamer   )r   r^   r   s      r   r   zPythonScript.__init__   s     ! r   c                    |j                  dt        dt        j                  z               |t	        d| j
                        | j                  d}	 t        | j
                  ||       |j3                          t4        j6                  S # t        $ rk}|j                  dk(  rR|j                  t        j                         |j                  t        j                  d      j!                  |             Y d}~d}~wt"        $ ri t%               }t'        j(                  |       t+        j,                  |j/                               }|j1                  d	      }|j                  |       Y w xY w)
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*   )rJ   r+   r      zFile not found.N)fileutf8)	setHeaderr   r   versionr   r^   r   r   OSErrorerrnosetResponseCoder   	NOT_FOUNDwriter	   rW   rI   BaseExceptionr   	traceback	print_excr   _PREgetvalueencodefinishr
   NOT_DONE_YET)r   rJ   	namespacer?   iooutputs         r   rI   zPythonScript.render   s    	]<):K:K+KL	
 3BF
	
	"T]]Iy9 	"""  	ww!|''7../@AHHQ  	"BR(YYr{{}-F]]6*FMM&!	"s    B	 		E-A!C88A1E-,E-N)r   r   r   r   isLeafr   rI   r   r   r   r\   r\      s     F! #r   r\   )r   rS   rk   rs   r   twistedr   twisted.python.compatr   r   twisted.python.filepathr   twisted.webr   r	   r
   r   r   r9   	Exceptionr   r   r8   r,   r3   rA   rF   rC   rO   r\   r   r   r   <module>r{      s   

 
    9 ? < <I    
#	"	"3(@-	P0$6H-- 6<h// <@1#8$$ 1#r   