
    \d                     F   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j                  Z G d dej                  Z G 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                       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__     2lib/python3.11/site-packages/twisted/web/script.pyr   r      s           r   r   c                        e Zd Zd Zd Zd ZdS )CacheScannerc                 0    || _         || _        d| _        d S )Nr   )pathregistrydoCacheselfr   r   s      r   __init__zCacheScanner.__init__%   s    	 r   c                     | j                             | j                  }|t          |          |                                  d S N)r   getCachedPathr   r   recache)r   cs     r   cachezCacheScanner.cache*   s=    M''	22="""r   c                     d| _         d S )N   )r   )r   s    r   r"   zCacheScanner.recache0   s    r   N)r   r   r   r   r$   r"   r   r   r   r   r   $   sA          
      r   r     Whoops! Internal Errorc                 :   t          | |          }t          d|           t          ||j        |j        d}	 t          | ||           n$# t          $ r}|j        d         cY d}~S d}~ww xY w|d         }|j        r|t          u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   	cachePath)r   r   csglobacrsrcs         r   ResourceScriptr3   7   s     
dH	%	%B/D99: DtT""""   wqz
D	z 'd&((4&&&Ks   A	 	
A*A%A*%A*c                 6   ddl m} t          d|           t          j        ddt
                    |d}t          |           5 }|                    ||           }d d d            n# 1 swxY w Y   t          |dd          }t          |||           |d	         S )
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9-)=
 
  D 
d 2q((D112 2 2 2 2 2 2 2 2 2 2 2 2 2 21j&))DtT
s   A&&A*-A*c                   "    e Zd ZddZd Zd ZdS )ResourceScriptWrapperNc                     t           j                            |            || _        |pt	          j                    | _        d S r    r	   Resourcer   r   r   Registryr   r   s      r   r   zResourceScriptWrapper.__init__b   s8    ""4(((	 5FO$5$5r   c                 `    t          | j        | j                  }|                    |          S r    )r3   r   r   render)r   requestress      r   rI   zResourceScriptWrapper.renderg   s'    TY66zz'"""r   c                 b    t          | j        | j                  }|                    ||          S r    )r3   r   r   getChildWithDefault)r   r   rJ   rK   s       r   rM   z)ResourceScriptWrapper.getChildWithDefaultk   s+    TY66&&tW555r   r    )r   r   r   r   rI   rM   r   r   r   rC   rC   a   sF        6 6 6 6
# # #6 6 6 6 6r   rC   c                   &    e Zd ZdZddZd Z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                     t           j                            |            || _        |pt	          j                    | _        d S r    rE   )r   pathnamer   s      r   r   z ResourceScriptDirectory.__init__~   s8    ""4(((	 5FO$5$5r   c                 B   t           j                            | j        |          }t           j                            |          rt	          || j                  S t           j                            |          rt          || j                  S t          j	                    S r    )
osr   joinisdirrO   r   existsr3   r	   _UnsafeNoResource)r   r   rJ   fns       r   getChildz ResourceScriptDirectory.getChild   sx    W\\$)T**7== 	>*2t}===7>>" 	5!"dm444)+++r   c                 N    t          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   sP         6 6 6 6
, , ,< < < < <r   rO   c                   "    e Zd ZdZdZd Z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                 "    || _         || _        dS )z3
        Initialize me with a script name.
        N)filenamer   )r   r^   r   s      r   r   zPythonScript.__init__   s     ! r   c                     |                     dt          dt          j        z                       |t	          d| j                  | j        d}	 t          | j        ||           n# t          $ rn}|j	        dk    rY|
                    t          j                   |                    t          j        d                              |                     Y d}~nd}~wt"          $ rv t%                      }t'          j        |           t+          j        |                                          }|                    d	          }|                    |           Y nw xY w|                                 t4          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*   )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   sv    	]<):K+KLL	
 	
 	
 3BFF
 
	
	"T]Iy9999 	 	 	w!||''777./@AAHHQQ    	" 	" 	"BR((((Yr{{}}--F]]6**FMM&!!!!!	" 	""s    A' '
E1A$CB EEN)r   r   r   r   isLeafr   rI   r   r   r   r\   r\      sC          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 9 9 9 9 9 9 9 ? ? ? ? ? ? < < < < < < < < < < < < < <    I            
#	"3(@-	P	P  0  $6 6 6 6 6H- 6 6 6< < < < <h/ < < <@1# 1# 1# 1# 1#8$ 1# 1# 1# 1# 1#r   