ó
`¾Tc           @   sä   d  Z  d d l Z d d l Z d d l Z yR y d d l m Z Wn! e k
 rd d d l m Z n Xd d l	 m
 Z
 e Z Wn e k
 r• e Z n Xd e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 d
 e d d d d d „ Z d S(   s9  
    werkzeug.contrib.profiler
    ~~~~~~~~~~~~~~~~~~~~~~~~~

    This module provides a simple WSGI profiler middleware for finding
    bottlenecks in web application.  It uses the :mod:`profile` or
    :mod:`cProfile` module to do the profiling and writes the stats to the
    stream provided (defaults to stderr).

    Example usage::

        from werkzeug.contrib.profiler import ProfilerMiddleware
        app = ProfilerMiddleware(app)

    :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿN(   t   Profile(   t   Statst   MergeStreamc           B   s    e  Z d  Z d „  Z d „  Z RS(   sü   An object that redirects `write` calls to multiple streams.
    Use this to log to both `sys.stdout` and a file::

        f = open('profiler.log', 'w')
        stream = MergeStream(sys.stdout, f)
        profiler = ProfilerMiddleware(app, stream)
    c         G   s"   | s t  d ƒ ‚ n  | |  _ d  S(   Ns!   at least one stream must be given(   t	   TypeErrort   streams(   t   selfR   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyt   __init__(   s    c         C   s%   x |  j  D] } | j | ƒ q
 Wd  S(   N(   R   t   write(   R   t   datat   stream(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyR   -   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyR      s   	t   ProfilerMiddlewarec           B   s,   e  Z d  Z d d d d d „ Z d „  Z RS(   s  Simple profiler middleware.  Wraps a WSGI application and profiles
    a request.  This intentionally buffers the response so that timings are
    more exact.

    By giving the `profile_dir` argument, pstat.Stats files are saved to that
    directory, one file per request. Without it, a summary is printed to
    `stream` instead.

    For the exact meaning of `sort_by` and `restrictions` consult the
    :mod:`profile` documentation.

    .. versionadded:: 0.9
       Added support for `restrictions` and `profile_dir`.

    :param app: the WSGI application to profile.
    :param stream: the stream for the profiled stats.  defaults to stderr.
    :param sort_by: a tuple of columns to sort the result by.
    :param restrictions: a tuple of profiling strictions, not used if dumping
                         to `profile_dir`.
    :param profile_dir: directory name to save pstat files
    t   timet   callsc         C   sO   t  s t d ƒ ‚ n  | |  _ | p* t j |  _ | |  _ | |  _ | |  _ d  S(   NsH   the profiler is not available because profile or pstat is not installed.(	   t	   availablet   RuntimeErrort   _appt   syst   stdoutt   _streamt   _sort_byt   _restrictionst   _profile_dir(   R   t   appR	   t   sort_byt   restrictionst   profile_dir(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyR   I   s    			c   
         sr  g  ‰ d  ‡ ‡  f d † ‰ ‡ ‡ ‡ ‡ f d †  } t ƒ  } t j ƒ  } | j | ƒ d j ˆ ƒ } t j ƒ  | } ˆ j d  k	 rí t j j ˆ j d ˆ d ˆ j d ƒ j	 d ƒ j
 d d ƒ pÃ d	 | d
 t j ƒ  f ƒ } | j | ƒ n~ t | d ˆ j ƒ}	 |	 j ˆ j Œ  ˆ j j d d ƒ ˆ j j d ˆ j d ƒ ƒ |	 j ˆ j Œ  ˆ j j d d d ƒ | g S(   Nc            s   ˆ |  | | ƒ ˆ  j  S(   N(   t   append(   t   statust   headerst   exc_info(   t   response_bodyt   start_response(    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyt   catching_start_responseW   s    c             s?   ˆ j  ˆ ˆ ƒ }  ˆ  j |  ƒ t |  d ƒ r; |  j ƒ  n  d  S(   Nt   close(   R   t   extendt   hasattrR$   (   t   appiter(   R!   R   R#   t   environ(    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyt   runapp[   s    t    s   %s.%s.%06dms.%d.proft   REQUEST_METHODt	   PATH_INFOt   /t   .t   rootg     @@R	   t   -iP   s
   
PATH: %r
s   

(   t   NoneR    R   t   runcallt   joinR   t   ost   patht   gett   stript   replacet
   dump_statsR   R   t
   sort_statsR   R   t   print_statsR   (
   R   R(   R"   R)   t   pt   startt   bodyt   elapsedt   prof_filenamet   stats(    (   R"   R!   R   R#   R(   s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyt   __call__T   s.    	'N(   s   timeR   (    (   R
   R   R   R1   R   RB   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyR   2   s   
t	   localhostiˆ  i   R   R   c   	         s4   d | f d | f | | ‡  ‡ ‡ ‡ f d † } | S(   sÞ   Return a new callback for :mod:`werkzeug.script` that starts a local
    server with the profiler enabled.

    ::

        from werkzeug.contrib import profiler
        action_profile = profiler.make_action(make_app)
    t   hR<   c            sH   d d l  m } t ˆ ƒ  ˆ ˆ ˆ  ƒ } | |  | | t d | | ƒ d S(   s   Start a new development server.iÿÿÿÿ(   t
   run_simpleN(   t   werkzeug.servingRE   R   t   FalseR1   (   t   hostnamet   portt   threadedt	   processesRE   R   (   R   R   R	   t   app_factory(    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyt   actionˆ   s    (    (	   RL   RH   RI   RJ   RK   R	   R   R   RM   (    (   R   RL   R	   R   s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyt   make_action}   s    (   s   times   calls(    (   R   R   R   t   os.pathR4   t   cProfileR    t   ImportErrort   profilet   pstatsR   t   TrueR   RG   t   objectR   R   R1   RN   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/profiler.pyt   <module>   s   $

K	