ó
ú2ec           @   sz   d  Z  d d l m Z d d l m Z d d l Z d d l m Z d g Z d d „ Z	 d e
 f d	 „  ƒ  YZ d
 „  Z d S(   sZ   
Cascades through several applications, so long as applications
return ``404 Not Found``.
iÿÿÿÿ(   t   httpexceptions(   t
   convertersN(   t   BytesIOt   Cascadet   404c         K   sÄ   t  t t j | ƒ ƒ } g  } xj | j ƒ  D]\ \ } } | j d ƒ s_ t d | | f ƒ ‚ n  |  j | d | ƒ} | j | | f ƒ q+ W| j	 ƒ  g  | D] \ } } | ^ qœ } t
 | d | ƒS(   s  
    Entry point for Paste Deploy configuration

    Expects configuration like::

        [composit:cascade]
        use = egg:Paste#cascade
        # all start with 'app' and are sorted alphabetically
        app1 = foo
        app2 = bar
        ...
        catch = 404 500 ...
    t   appsL   Bad configuration key %r (=%r); all configuration keys must start with 'app't   global_conft   catch(   t   mapt   intR   t   aslistt   itemst
   startswitht
   ValueErrort   get_appt   appendt   sortR   (   t   loaderR   R   t
   local_conft   appst   namet   valueR   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/cascade.pyt   make_cascade   s    
c           B   s#   e  Z d  Z d d „ Z d „  Z RS(   sf  
    Passed a list of applications, ``Cascade`` will try each of them
    in turn.  If one returns a status code listed in ``catch`` (by
    default just ``404 Not Found``) then the next application is
    tried.

    If all applications fail, then the last application's failure
    response is used.

    Instances of this class are WSGI applications.
    i”  c         C   sÆ   | |  _  i  |  _ g  |  _ x’ | D]Š } t | t ƒ rV t | j d  d ƒ d ƒ } n  t | t j	 ƒ rz | } | j
 } n t j | ƒ } | } | |  j | <|  j j | ƒ q" Wt |  j ƒ |  _ d  S(   Ni   i    (   R   t   catch_codest   catch_exceptionst
   isinstancet   strR	   t   splitt   NoneR    t   HTTPExceptiont   codet   get_exceptionR   t   tuple(   t   selft   applicationsR   t   errort   excR   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/cascade.pyt   __init__9   s    			c            s%  g  ‰  d	 ‡  ‡ ‡ f d † } y" t | j d d ƒ p9 d ƒ } Wn t k
 rY d } n X| d k rSt } | d k s„ | d k  r-t j ƒ  } | d k  r¶ | j | d j ƒ  ƒ ng | } x^ | d k r| d j t	 | d ƒ ƒ } | sü t
 d ƒ ‚ n  | j | ƒ | t | ƒ 8} q¿ W| j d ƒ n t | d j | ƒ ƒ } | | d <n t } x› ˆ j d  D]Œ }	 | j ƒ  }
 | r“|
 d j d ƒ n  g  ‰  yC |	 |
 | ƒ } ˆ  sµ| St | d ƒ rÛt | ƒ | j ƒ  n  Wqgˆ j k
 ròqgXqgW| r| d j d ƒ n  ˆ j d | ˆ ƒ S(
   s,   
        WSGI application interface
        c            sL   t  |  j d  d ƒ d ƒ } | ˆ j k r< ˆ  j d  ƒ t Sˆ |  | | ƒ S(   Ni   i    (   R	   R   R   R   R   t   _consuming_writer(   t   statust   headerst   exc_infoR   (   t   failedR!   t   start_response(    s7   /usr/local/lib/python2.7/dist-packages/paste/cascade.pyt   repl_start_responseO   s
    t   CONTENT_LENGTHi    i   s
   wsgi.inputs   Request body truncatediÿÿÿÿt   closeN(   R   R	   t   getR   t   Truet   tempfilet   TemporaryFilet   writet   readt   mint   IOErrort   lent   seekR   t   FalseR   t   copyt   hasattrt   listR.   R   (   R!   t   environR+   R,   t   lengtht   copy_wsgi_inputt   ft   copy_lent   chunkR   t   environ_copyt   v(    (   R*   R!   R+   s7   /usr/local/lib/python2.7/dist-packages/paste/cascade.pyt   __call__J   sP    "

(   i”  (   t   __name__t
   __module__t   __doc__R%   RE   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/cascade.pyR   +   s   c         C   s   d  S(   N(    (   t   s(    (    s7   /usr/local/lib/python2.7/dist-packages/paste/cascade.pyR&   †   s    (   RH   t   pasteR    t
   paste.utilR   R1   t   sixR   t   __all__R   t   objectR   R&   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/cascade.pyt   <module>   s   	[