o
    �°-^–  ã                   @   s†   d Z ddlZddlZzddlmZ W n   Y ddlmZ G dd„ deƒZG dd„ deƒZ	d	Z
G d
d„ deƒZdadai add„ ZdS )a¹  WSGI Paste wrapper for mod_python. Requires Python 2.2 or greater.


Example httpd.conf section for a Paste app with an ini file::

    <Location />
        SetHandler python-program
        PythonHandler paste.modpython
        PythonOption paste.ini /some/location/your/pasteconfig.ini
    </Location>

Or if you want to load a WSGI application under /your/homedir in the module
``startup`` and the WSGI app is ``app``::

    <Location />
        SetHandler python-program
        PythonHandler paste.modpython
        PythonPath "['/virtual/project/directory'] + sys.path"
        PythonOption wsgi.application startup::app
    </Location>


If you'd like to use a virtual installation, make sure to add it in the path
like so::

    <Location />
        SetHandler python-program
        PythonHandler paste.modpython
        PythonPath "['/virtual/project/directory', '/virtual/lib/python2.4/'] + sys.path"
        PythonOption paste.ini /virtual/project/directory/pasteconfig.ini
    </Location>

Some WSGI implementations assume that the SCRIPT_NAME environ variable will
always be equal to "the root URL of the app"; Apache probably won't act as
you expect in that case. You can add another PythonOption directive to tell
modpython_gateway to force that behavior:

    PythonOption SCRIPT_NAME /mcontrol

Some WSGI applications need to be cleaned up when Apache exits. You can
register a cleanup handler with yet another PythonOption directive:

    PythonOption wsgi.cleanup module::function

The module.function will be called with no arguments on server shutdown,
once for each child process or thread.

This module highly based on Robert Brewer's, here:
http://projects.amor.org/misc/svn/modpython_gateway.py
é    N)Úapache)Úloadappc                   @   sB   e Zd Zdd„ Zdd„ Zddd„Zddd	„Zdd
d„Zdd„ ZdS )ÚInputWrapperc                 C   ó
   || _ d S ©N©Úreq©Úselfr   © r   ú1/usr/lib/python3/dist-packages/paste/modpython.pyÚ__init__?   ó   
zInputWrapper.__init__c                 C   ó   d S r   r   ©r
   r   r   r   ÚcloseB   ó   zInputWrapper.closeéÿÿÿÿc                 C   ó   | j  |¡S r   )r   Úread©r
   Úsizer   r   r   r   E   ó   zInputWrapper.readc                 C   r   r   )r   Úreadliner   r   r   r   r   H   r   zInputWrapper.readlinec                 C   r   r   )r   Ú	readlines)r
   Úhintr   r   r   r   K   r   zInputWrapper.readlinesc                 c   s(   � |   ¡ }|r|V  |   ¡ }|sd S d S r   )r   )r
   Úliner   r   r   Ú__iter__N   s   €üzInputWrapper.__iter__N)r   )	Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r   r   r   =   s    


r   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚErrorWrapperc                 C   r   r   r   r	   r   r   r   r   Y   r   zErrorWrapper.__init__c                 C   r   r   r   r   r   r   r   Úflush\   r   zErrorWrapper.flushc                 C   s   | j  |¡ d S r   )r   Ú	log_error)r
   Úmsgr   r   r   Úwrite_   s   zErrorWrapper.writec                 C   s   |   d |¡¡ d S )NÚ )r%   Újoin)r
   Úseqr   r   r   Ú
writelinesb   s   zErrorWrapper.writelinesN)r   r   r    r   r"   r%   r)   r   r   r   r   r!   W   s
    r!   zfYou must provide a PythonOption '%s', either 'on' or 'off', when running a version of mod_python < 3.1c                   @   s.   e Zd Zdd„ Zdd„ Zd
dd„Zdd	„ ZdS )ÚHandlerc                 C   sh  d| _ | ¡ }ztj}|tjƒ}|tjƒ}W nA tyW   | dd¡ ¡ }|dkr,d}n|dkr3d}nt	t
d ƒ‚| dd¡ ¡ }|dkrHd}n|dkrOd}nt	t
d ƒ‚Y nw tt |¡ƒ }| _d|v rz|d |d< |jt|d ƒd … |d	< n	d|d< |j|d	< t|ƒ|d
< t|ƒ|d< d|d< d|d< | d¡dv r£d|d< nd|d< ||d< ||d< || _d S )NFÚmultithreadr&   ÚonTÚoffÚmultiprocessÚSCRIPT_NAMEÚ	PATH_INFOz
wsgi.inputúwsgi.errors)é   r   zwsgi.versionzwsgi.run_onceÚHTTPS)Úyesr,   Ú1Úhttpszwsgi.url_schemeÚhttpzwsgi.multithreadzwsgi.multiprocess)ÚstartedÚget_optionsr   Ú	mpm_queryÚAP_MPMQ_IS_THREADEDÚAP_MPMQ_IS_FORKEDÚAttributeErrorÚgetÚlowerÚ
ValueErrorÚ	bad_valueÚdictÚbuild_cgi_envÚenvironÚuriÚlenr   r!   Úrequest)r
   r   ÚoptionsÚqÚthreadedÚforkedÚenvr   r   r   r   l   sJ   
€ñ


zHandler.__init__c                 C   s®   z)|| j | jƒ}|D ]}|  |¡ q
| js| j d¡ t|dƒr'| ¡  W d S W d S    t 	d | j d ¡ | jsTd| j_
d| j_d}| j t|ƒ¡ | j |¡ Y d S Y d S )Nr   r   r1   iô  z
text/plainz:A server error occurred. Please contact the administrator.)rD   Ústart_responser%   r8   rG   Úset_content_lengthÚhasattrr   Ú	tracebackÚ	print_excÚstatusÚcontent_typerF   )r
   ÚapplicationÚresultÚdatar   r   r   Úrunž   s$   
ÿûzHandler.runNc                 C   s    |rz| j rt |d |d |d ¡ W d }nd }w t|d d… ƒ| j_|D ]&\}}| ¡ dkr9| j t|ƒ¡ q&| ¡ dkrD|| j_q&| jj	 
||¡ q&| jS )Nr   r2   é   é   zcontent-lengthzcontent-type)r8   ÚsixÚreraiseÚintrG   rR   r?   rN   rS   Úheaders_outÚaddr%   )r
   rR   ÚheadersÚexc_infoÚkeyÚvalr   r   r   rM   °   s   €
zHandler.start_responsec                 C   s   | j sd| _ | j |¡ d S )NT)r8   rG   r%   )r
   rV   r   r   r   r%   Ä   s   zHandler.writer   )r   r   r    r   rW   rM   r%   r   r   r   r   r*   j   s
    2
r*   c                 C   s^  |   ¡ }d|v r,ts,|d }|r,| dd¡\}}t|tƒ tƒ dgƒ}t ||¡at| ƒ d|v rltsl|d }|rl| dd¡\}}t|tƒ tƒ dgƒ}t ||¡add„ }zt 	|¡ W n t
yk   | j 	| |¡ Y nw | d¡}d }|r…|tvr�td	| ƒt|< t| }| d
¡}	|	r¥|s¥|	 dd¡\}
}t|
tƒ tƒ dgƒ}t||ƒ}t| ƒ |¡ tjS )Nzwsgi.startupz::r2   r&   zwsgi.cleanupc                 S   s
   t ƒ  d S r   )Úcleanup)rV   r   r   r   Úcleanerâ   r   zhandler.<locals>.cleanerz	paste.iniz	config:%szwsgi.application)r9   ÚstartupÚsplitÚ
__import__ÚglobalsÚlocalsr   Úresolve_objectrc   Úregister_cleanupr=   Úserverr>   Úwsgiappsr   Úgetattrr*   rW   ÚOK)r   rH   ÚfuncÚmodule_nameÚ
object_strÚmodulerd   ÚappiniÚappÚappwsgiÚmodnameÚobjnamer   r   r   ÚhandlerÎ   sB   ÿ


ry   )Ú__doc__rZ   rP   Ú
mod_pythonr   Úpaste.deployr   Úobjectr   r!   rA   r*   re   rc   rm   ry   r   r   r   r   Ú<module>   s     3`