ó
ú2ec           @   s   d  Z  d d l Z d d l Z d d l m Z d d l Z d d l m Z d e	 f d „  ƒ  YZ
 d e	 f d „  ƒ  YZ e
 d d	 d
 d „ Z d S(   s¦   
WSGI middleware

Captures any exceptions and prints a pretty report.  See the `cgitb
documentation <http://python.org/doc/current/lib/module-cgitb.html>`_
for more.
iÿÿÿÿN(   t	   cStringIO(   t
   converterst	   NoDefaultc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/cgitb_catcher.pyR      s   t   CgitbMiddlewarec           B   s;   e  Z d e d d  d d „ Z d „  Z d „  Z d „  Z RS(   i   t   htmlc         C   sŽ   | |  _  | d  k r i  } n  | t k r< | j d ƒ } n  t | t j ƒ r` t j | ƒ } n  | |  _	 | |  _
 t | ƒ |  _ | |  _ d  S(   Nt   debug(   t   appt   NoneR   t   gett
   isinstancet   sixt   string_typesR   t   asboolt   displayt   logdirt   intt   contextt   format(   t   selfR   t   global_confR   R   R   R   (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/cgitb_catcher.pyt   __init__   s    				c         C   s„   y& |  j  | | ƒ } |  j | | ƒ SWnW t j ƒ  } | d d g | ƒ |  j | | ƒ } t j rx | j d ƒ } n  | g SXd  S(   Ns   500 Internal Server Errors   content-types	   text/htmlt   utf8(   s   content-types	   text/html(   R   t   catching_itert   syst   exc_infot   exception_handlerR   t   PY3t   encode(   R   t   environt   start_responset   app_iterR   t   response(    (    s=   /usr/local/lib/python2.7/dist-packages/paste/cgitb_catcher.pyt   __call__*   s    	c         c   sé   | s
 d  St  } y< x | D] } | Vq Wt | d ƒ rK t } | j ƒ  n  Wn– |  j t j ƒ  | ƒ } | rÁ t | d ƒ rÁ y | j ƒ  WqÁ |  j t j ƒ  | ƒ } | d | 7} qÁ Xn  t j rÜ | j	 d ƒ } n  | Vn Xd  S(   Nt   closes$   <hr noshade>Error in .close():<br>%sR   (
   t   Falset   hasattrt   TrueR#   R   R   R   R   R   R   (   R   R    R   t   error_on_closet   vR!   t   close_response(    (    s=   /usr/local/lib/python2.7/dist-packages/paste/cgitb_catcher.pyR   8   s.    		c         C   sS   t  ƒ  } t j d | d |  j d |  j d |  j d |  j ƒ } | | Œ  | j ƒ  S(   Nt   fileR   R   R   R   (   t   StringIOt   cgitbt   HookR   R   R   R   t   getvalue(   R   R   R   t
   dummy_filet   hook(    (    s=   /usr/local/lib/python2.7/dist-packages/paste/cgitb_catcher.pyR   Q   s    				
N(   R   R   R	   R   R   R"   R   R   (    (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/cgitb_catcher.pyR      s   		i   R   c         C   sv   d d l  m } | t k	 r+ | | ƒ } n  d | k rN | | d ƒ | d <n  t |  d | d | d | d | d | ƒS(	   s†  
    Wraps the application in the ``cgitb`` (standard library)
    error catcher.

      display:
        If true (or debug is set in the global configuration)
        then the traceback will be displayed in the browser

      logdir:
        Writes logs of all errors in that directory

      context:
        Number of lines of context to show around each line of
        source code
    iÿÿÿÿ(   R   R   R   R   R   R   R   (   t   paste.deploy.convertersR   R   R   (   R   R   R   R   R   R   R   (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/cgitb_catcher.pyt   make_cgitb_middleware[   s    (   t   __doc__R,   R   t	   six.movesR    R+   R   t
   paste.utilR   t   objectR   R   R	   R2   (    (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/cgitb_catcher.pyt   <module>
   s   F