ó
ú2ec           @   sã   d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 m
 Z
 d d l m Z d d g Z d e f d „  ƒ  YZ d „  Z d	 „  Z d
 e f d „  ƒ  YZ d d d „ Z d S(   se   
Middleware that profiles the request and displays profiling
information at the bottom of each page.
iÿÿÿÿN(   t   StringIO(   t   responset   ProfileMiddlewaret   profile_decoratorc           B   s/   e  Z d  Z d Z d d d d „ Z d „  Z RS(   s•  
    Middleware that profiles all requests.

    All HTML pages will have profiling information appended to them.
    The data is isolated to that single request, and does not include
    data from previous requests.

    This uses the ``hotshot`` module, which affects performance of the
    application.  It also runs in a single-threaded mode, so it is
    only usable in development environments.
    sW   clear: both; background-color: #ff9; color: #000; border: 2px solid #000; padding: 5px;s   profile.log.tmpi(   c         C   s.   | |  _  t j ƒ  |  _ | |  _ | |  _ d  S(   N(   t   appt	   threadingt   Lockt   lockt   log_filenamet   limit(   t   selfR   t   global_confR   R	   (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyt   __init__'   s    		c   
         s‰  g  ‰ g  ‰  d  ‡  ‡ ‡ f d † ‰ ‡  ‡ ‡ ‡ f d †  } ˆ j j ƒ  z+t j ˆ j ƒ } | j d ˆ j d d ƒ ƒ z | j | ƒ Wd  | j	 ƒ  Xd j
 ˆ  ƒ ‰  ˆ d } t j | d ƒ } | d  k sà | j d ƒ rç ˆ  g St j j ˆ j ƒ } | j ƒ  | j d	 d
 ƒ t | j ˆ j ƒ } t | j ˆ j ƒ }	 ˆ  d ˆ j t j | ƒ t j |	 ƒ f 7‰  ˆ  g SWd  ˆ j j ƒ  Xd  S(   Nc            s*   ˆ j  |  | g ƒ ˆ |  | | ƒ ˆ  j S(   N(   t   extendt   append(   t   statust   headerst   exc_info(   t   bodyt   catch_responset   start_response(    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyt   replace_start_response2   s    c             sG   ˆ j  ˆ ˆ ƒ }  z ˆ  j |  ƒ Wd  t |  d ƒ rB |  j ƒ  n  Xd  S(   Nt   close(   R   R   t   hasattrR   (   t   app_iter(   R   t   environR   R
   (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyt   run_app6   s
    t   URLt	   PATH_INFOt    i   s   content-types	   text/htmlt   timet   callss   <pre style="%s">%s
%s</pre>(   t   NoneR   t   acquiret   hotshott   ProfileR   t   addinfot   gett   runcallR   t   joinR   t   header_valuet
   startswitht   statst   loadt
   strip_dirst
   sort_statst   capture_outputt   print_statsR	   t   print_callerst   stylet   cgit   escapet   release(
   R
   R   R   R   t   profR   t   content_typeR*   t   outputt   output_callers(    (   R   R   R   R   R
   R   s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyt   __call__/   s4    

&N(   t   __name__t
   __module__t   __doc__R1   R    R   R9   (    (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyR      s   c         O   sC   t  ƒ  } t j } | t _ z |  | | Ž  Wd  | t _ X| j ƒ  S(   N(   R    t   syst   stdoutt   getvalue(   t   funct   argst   kwt   outt
   old_stdout(    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyR.   W   s    			
c             s/   ˆ  j  d ƒ r d „  } | S‡  f d †  } | S(   sö  
    Profile a single function call.

    Used around a function, like::

        @profile_decorator(options...)
        def ...

    All calls to the function will be profiled.  The options are
    all keywords, and are:

        log_file:
            The filename to log to (or ``'stdout'`` or ``'stderr'``).
            Default: stderr.
        display_limit:
            Only show the top N items, default: 20.
        sort_stats:
            A list of string-attributes to sort on.  Default
            ``('time', 'calls')``.
        strip_dirs:
            Strip directories/module names from files?  Default True.
        add_info:
            If given, this info will be added to the report (for your
            own tracking).  Default: none.
        log_filename:
            The temporary filename to log profiling data to.  Default;
            ``./profile_data.log.tmp``
        no_profile:
            If true, then don't actually profile anything.  Useful for
            conditional profiling.
    t
   no_profilec         S   s   |  S(   N(    (   R@   (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyt	   decorator†   s    c            s   ‡  ‡ f d †  } | S(   Nc             s   t  ˆ  ˆ  |  | Ž  S(   N(   t   DecoratedProfile(   RA   RB   (   R@   t   options(    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyt   replacementŠ   s    (    (   R@   RI   (   RH   (   R@   s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyRF   ‰   s    (   R%   (   RH   RF   (    (   RH   s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyR   c   s
    "	RG   c           B   s8   e  Z e j ƒ  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         K   s   | |  _  | |  _ d  S(   N(   R@   RH   (   R
   R@   RH   (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyR   “   s    	c         O   s<   |  j  j ƒ  z |  j |  j | | Ž SWd  |  j  j ƒ  Xd  S(   N(   R   R!   t   profileR@   R4   (   R
   RA   RB   (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyR9   —   s    c         O   sl  |  j  } | j d d ƒ } t j | ƒ } | j d |  j | | | Ž ƒ | j d ƒ ro | j d | d ƒ n  d  } zK t j ƒ  } y | j | | | Ž }	 Wn t	 j
 ƒ  } n Xt j ƒ  }
 Wd  | j ƒ  Xt j j | ƒ } t j | ƒ | j d t ƒ r| j ƒ  n  | j | j d d ƒ Œ  | j d
 d ƒ } t | j | ƒ } t | j | ƒ } | j d ƒ } | d k r‚t	 j } nV | d k ršt	 j } n> t | d ƒ } | j d d d ƒ | j d t j d ƒ ƒ | j d |  j | | | Ž ƒ | j d |
 | ƒ | j | ƒ | j | ƒ | d k r@| j ƒ  n  | rht j | d | d | d ƒ n  |	 S(   NR   s   profile_data.log.tmps   Function Callt   add_infos
   Extra infoR,   R-   R   R   t   display_limiti   t   log_filet   stderrt   -R>   t   as   
%s
i<   s	   Date: %s
s   %cs   Function call: %s
s   Wall time: %0.2f seconds
i    i   i   (   R   R   (   NRN   (   RO   R>   (   NRO   R>   RN   (   RH   R%   R"   R#   R$   t   format_functionR    R   R&   R=   R   R   R*   R+   t   ost   unlinkt   TrueR,   R-   R.   R/   R0   RN   R>   t   opent   writet   strftimet   sixt   reraise(   R
   R@   RA   RB   t   opst   prof_filenameR5   R   t
   start_timet   resultt   end_timeR*   RL   R7   R8   t   output_filet   f(    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyRJ   ž   sX    				"c         O   s_   t  t | ƒ } | j g  | j ƒ  D] \ } } d | | f ^ q" ƒ d | j d j | ƒ f S(   Ns   %s=%rs   %s(%s)s   , (   t   mapt   reprR   t   itemsR:   R'   (   R
   R@   RA   RB   t   kt   v(    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyRQ   Ï   s    0(	   R:   R;   R   R   R   R   R9   RJ   RQ   (    (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyRG      s
   			1s   profile.log.tmpi(   c         C   s"   t  | ƒ } t |  d | d | ƒS(   s  
    Wrap the application in a component that will profile each
    request.  The profiling data is then appended to the output
    of each page.

    Note that this serializes all requests (i.e., removing
    concurrency).  Therefore never use this in production.
    R   R	   (   t   intR   (   R   R   R   R	   (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyt   make_profile_middlewareÖ   s    (   R<   R=   RR   R"   t   hotshot.statsR   R2   RX   R   t	   cStringIOR    t   pasteR   t   __all__t   objectR   R.   R   RG   Rg   (    (    (    s=   /usr/local/lib/python2.7/dist-packages/paste/debug/profile.pyt   <module>   s"   A		,I