ó
ú2ec           @   sµ   d  Z  d d l m Z d d l Z d d l Z d d l m Z d d l m Z d d l m	 Z	 d d l
 Z
 d d l Z e a d g Z d e f d	 „  ƒ  YZ d e f d
 „  ƒ  YZ d S(   s  
Middleware that displays everything that is printed inline in
application pages.

Anything printed during the request will get captured and included on
the page.  It will usually be included as a floating element in the
top right hand corner of the page.  If you want to override this
you can include a tag in your template where it will be placed::

  <pre id="paste-debug-prints"></pre>

You might want to include ``style="white-space: normal"``, as all the
whitespace will be quoted, and this allows the text to wrap if
necessary.

iÿÿÿÿ(   t   StringION(   t   threadedprint(   t   wsgilib(   t   responset   PrintDebugMiddlewaret   TeeFilec           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   files(   t   selfR   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/prints.pyt   __init__#   s    c         C   sC   t  | t ƒ r t | ƒ } n  x |  j D] } | j | ƒ q( Wd  S(   N(   t
   isinstancet   unicodet   strR   t   write(   R   t   vt   file(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/prints.pyR   &   s    (   t   __name__t
   __module__R   R   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/prints.pyR   !   s   	c           B   sl   e  Z d  Z d Z d e e e d „ Z d „  Z e	 j
 d e	 j ƒ Z e	 j
 d e	 j e	 j ƒ Z d „  Z RS(   sL  
    This middleware captures all the printed statements, and inlines
    them in HTML pages, so that you can see all the (debug-intended)
    print statements in the page itself.

    There are two keys added to the environment to control this:
    ``environ['paste.printdebug_listeners']`` is a list of functions
    that will be called everytime something is printed.

    ``environ['paste.remove_printdebug']`` is a function that, if
    called, will disable printing of output for that request.

    If you have ``replace_stdout=True`` then stdout is replaced, not
    captured.
    s¿   <pre style="width: 40%%; border: 2px solid #000; white-space: normal; background-color: #ffd; color: #000; float: right;"><b style="border-bottom: 1px solid #000">Log messages</b><br>%s</pre>c         C   sb   | |  _  | |  _ t | t j ƒ rC d d l m } | | ƒ } n  | |  _ | |  _ d  |  _
 d  S(   Niÿÿÿÿ(   t   asbool(   t   appt   force_content_typeR	   t   sixt   string_typest   paste.deploy.convertersR   t   print_wsgi_errorst   replace_stdoutt   Nonet   _threaded_print_stdout(   R   R   t   global_confR   R   R   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/prints.pyR   E   s    				c            sï  | j  d ƒ r |  j | | ƒ St s8 |  j t j k	 ra t a t j d |  j	 ƒ t j |  _ n  g  ‰  ‡  f d †  } | | d <t
 ƒ  } | g } | | d <|  j r¹ | j | d ƒ n  t | ƒ } t j | ƒ zt j | |  j ƒ \ } } }	 | d  k r-d } d g } | | | ƒ |	 s-d
 }	 q-n  t j | d ƒ }
 ˆ  sf|  j r |
 sf|
 j d	 ƒ r | | k rŒ| d j | j ƒ  ƒ n  | | | ƒ |	 g St j | d ƒ |  j |	 | j ƒ  ƒ }	 | | | ƒ |	 g SWd  t j ƒ  Xd  S(   Ns   paste.testingt   leave_stdoutc              s   ˆ  j  d  ƒ d  S(   N(   t   appendR   (    (   t   removed(    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/prints.pyt   remove_printdebug_   s    s   paste.remove_printdebugs   paste.printdebug_listenerss   wsgi.errorss   500 Server Errors   Content-types	   text/htmls   An error occurreds   content-types   content-length(   s   Content-types	   text/html(   t   getR   t   _threadedprint_installedR   t   syst   stdoutt   TrueR   t   installR   R    R   R   R   t   registerR   t   intercept_outputR   R   t   header_valueR   t
   startswithR   t   getvaluet   remove_headert   add_logt
   deregister(   R   t   environt   start_responseR   t   loggedt	   listenerst   replacement_stdoutt   statust   headerst   bodyt   content_type(    (   R   s<   /usr/local/lib/python2.7/dist-packages/paste/debug/prints.pyt   __call__R   sP    
		
		
s   <body[^>]*>s'   <pre\s*[^>]*id="paste-debug-prints".*?>c         C   s©   | s
 | St  j | ƒ } | j d d ƒ } | j d d ƒ } |  j j | ƒ } | sw |  j | } |  j j | ƒ } n  | s… | | S| | j ƒ   | | | j ƒ  Sd  S(   Ns   
s   <br>s     s   &nbsp; (   t   cgit   escapet   replacet   _explicit_ret   searcht   log_templatet   _body_ret   end(   R   t   htmlt   logt   textt   match(    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/prints.pyR,   ˆ   s    N(   R   R   t   __doc__R=   R   t   FalseR$   R   R7   t   ret   compilet   IR>   t   SR;   R,   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/prints.pyR   -   s   	2	(   RD   t	   cStringIOR    RF   R8   t
   paste.utilR   t   pasteR   R   R   R"   RE   R!   t   __all__t   objectR   R   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/paste/debug/prints.pyt   <module>   s   	