ó
ú2ec           @   s³   d  Z  d d l Z d d l Z y d d l m Z Wn n Xd d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d	 Z
 d
 e f d „  ƒ  YZ d a d a i  a d „  Z d S(   s¹  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
iÿÿÿÿN(   t   apache(   t   loadappt   InputWrapperc           B   sG   e  Z d  „  Z d „  Z d d „ Z d d „ Z d d „ Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   req(   t   selfR   (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyt   __init__?   s    c         C   s   d  S(   N(    (   R   (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyt   closeB   s    iÿÿÿÿc         C   s   |  j  j | ƒ S(   N(   R   t   read(   R   t   size(    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyR   E   s    c         C   s   |  j  j | ƒ S(   N(   R   t   readline(   R   R   (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyR	   H   s    c         C   s   |  j  j | ƒ S(   N(   R   t	   readlines(   R   t   hint(    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyR
   K   s    c         c   s.   |  j  ƒ  } x | r) | V|  j  ƒ  } q Wd  S(   N(   R	   (   R   t   line(    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyt   __iter__N   s    	(   t   __name__t
   __module__R   R   R   R	   R
   R   (    (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyR   =   s   		t   ErrorWrapperc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyR   Y   s    c         C   s   d  S(   N(    (   R   (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyt   flush\   s    c         C   s   |  j  j | ƒ d  S(   N(   R   t	   log_error(   R   t   msg(    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyt   write_   s    c         C   s   |  j  d j | ƒ ƒ d  S(   Nt    (   R   t   join(   R   t   seq(    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyt
   writelinesb   s    (   R   R   R   R   R   R   (    (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyR   W   s   			sf   You must provide a PythonOption '%s', either 'on' or 'off', when running a version of mod_python < 3.1t   Handlerc           B   s/   e  Z d  „  Z d „  Z d d „ Z d „  Z RS(   c         C   sä  t  |  _ | j ƒ  } y+ t j } | t j ƒ } | t j ƒ } Wnµ t k
 r÷ | j d d ƒ j	 ƒ  } | d k r} t
 } n% | d k r’ t  } n t t d ƒ ‚ | j d d ƒ j	 ƒ  } | d k rÏ t
 } qø | d k rä t  } qø t t d ƒ ‚ n Xt t j | ƒ ƒ } |  _ d | k rL| d | d <| j t | d ƒ | d <n d | d <| j | d <t | ƒ | d <t | ƒ | d	 <d | d <t  | d <| j d ƒ d k r¹d | d <n
 d | d <| | d <| | d <| |  _ d  S(   Nt   multithreadR   t   ont   offt   multiprocesst   SCRIPT_NAMEt	   PATH_INFOs
   wsgi.inputs   wsgi.errorsi   i    s   wsgi.versions   wsgi.run_oncet   HTTPSt   yest   1t   httpss   wsgi.url_schemet   https   wsgi.multithreads   wsgi.multiprocess(   i   i    (   R!   R   R"   (   t   Falset   startedt   get_optionsR    t	   mpm_queryt   AP_MPMQ_IS_THREADEDt   AP_MPMQ_IS_FORKEDt   AttributeErrort   gett   lowert   Truet
   ValueErrort	   bad_valuet   dictt   build_cgi_envt   environt   urit   lenR   R   t   request(   R   R   t   optionst   qt   threadedt   forkedt   env(    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyR   l   sF    						





c         C   sä   yo | |  j  |  j ƒ } x | D] } |  j | ƒ q W|  j sR |  j j d ƒ n  t | d ƒ rn | j ƒ  n  Wnn t j	 d  |  j  d ƒ |  j sà d |  j _ d |  j _ d } |  j j t | ƒ ƒ |  j j | ƒ qà n Xd  S(   Ni    R   s   wsgi.errorsiô  s
   text/plains:   A server error occurred. Please contact the administrator.(   R3   t   start_responseR   R&   R6   t   set_content_lengtht   hasattrR   t	   tracebackt	   print_exct   Nonet   statust   content_typeR5   (   R   t   applicationt   resultt   data(    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyt   runž   s     		c         C   sØ   | rB z/ |  j  r4 t j | d | d | d ƒ n  Wd  d  } Xn  t | d  ƒ |  j _ xv | D]n \ } } | j ƒ  d k r– |  j j t | ƒ ƒ q_ | j ƒ  d k r· | |  j _	 q_ |  j j
 j | | ƒ q_ W|  j S(   Ni    i   i   i   s   content-lengths   content-type(   R&   t   sixt   reraiseRA   t   intR6   RB   R-   R=   RC   t   headers_outt   addR   (   R   RB   t   headerst   exc_infot   keyt   val(    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyR<   °   s    	&
c         C   s)   |  j  s t |  _  n  |  j j | ƒ d  S(   N(   R&   R.   R6   R   (   R   RF   (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyR   Ä   s    	N(   R   R   R   RG   RA   R<   R   (    (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyR   j   s   	2	c         C   s  |  j  ƒ  } d | k r‡ t r‡ | d } | r‡ | j d d ƒ \ } } t | t ƒ  t ƒ  d g ƒ } t j | | ƒ a t |  ƒ q‡ n  d | k r9t r9| d } | r9| j d d ƒ \ } } t | t ƒ  t ƒ  d g ƒ } t j | | ƒ a d „  } y t j	 | ƒ Wq6t
 k
 r2|  j j	 |  | ƒ q6Xq9n  | j d ƒ } d  } | r„| t k rwt d | ƒ t | <n  t | } n  | j d	 ƒ }	 |	 rè| rè|	 j d d ƒ \ }
 } t |
 t ƒ  t ƒ  d g ƒ } t | | ƒ } n  t |  ƒ j | ƒ t j S(
   Ns   wsgi.startups   ::i   R   s   wsgi.cleanupc         S   s   t  ƒ  d  S(   N(   t   cleanup(   RF   (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyt   cleanerâ   s    s	   paste.inis	   config:%ss   wsgi.application(   R'   t   startupt   splitt
   __import__t   globalst   localsR    t   resolve_objectRQ   t   register_cleanupR+   t   serverR,   RA   t   wsgiappsR   t   getattrR   RG   t   OK(   R   R7   t   funct   module_namet
   object_strt   moduleRR   t   appinit   appt   appwsgit   modnamet   objname(    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyt   handlerÎ   s@    

	(   t   __doc__RH   R?   t
   mod_pythonR    t   paste.deployR   t   objectR   R   R0   R   RA   RS   RQ   R[   Rg   (    (    (    s9   /usr/local/lib/python2.7/dist-packages/paste/modpython.pyt   <module>2   s   `