
_w                 @   s   d  Z  d d l m Z d d l Z d d l Z d d l Z d d l m Z y d d l	 m
 Z
 Wn" e k
 r d d l m
 Z
 Yn XGd d   d e  Z d S)a9  
Application Profiler
====================

This module provides a middleware that profiles each request with the
:mod:`cProfile` module. This can help identify bottlenecks in your code
that may be slowing down your application.

.. autoclass:: ProfilerMiddleware

:copyright: 2007 Pallets
:license: BSD-3-Clause
    )print_functionN)Stats)Profilec               @   s@   e  Z d  Z d Z e j d
 f  d d d d  Z d d	   Z d S)ProfilerMiddlewarea  Wrap a WSGI application and profile the execution of each
    request. Responses are buffered so that timings are more exact.

    If ``stream`` is given, :class:`pstats.Stats` are written to it
    after each request. If ``profile_dir`` is given, :mod:`cProfile`
    data files are saved to that directory, one file per request.

    The filename can be customized by passing ``filename_format``. If
    it is a string, it will be formatted using :meth:`str.format` with
    the following fields available:

    -   ``{method}`` - The request method; GET, POST, etc.
    -   ``{path}`` - The request path or 'root' should one not exist.
    -   ``{elapsed}`` - The elapsed time of the request.
    -   ``{time}`` - The time of the request.

    If it is a callable, it will be called with the WSGI ``environ``
    dict and should return a filename.

    :param app: The WSGI application to wrap.
    :param stream: Write stats to this stream. Disable with ``None``.
    :param sort_by: A tuple of columns to sort stats by. See
        :meth:`pstats.Stats.sort_stats`.
    :param restrictions: A tuple of restrictions to filter stats by. See
        :meth:`pstats.Stats.print_stats`.
    :param profile_dir: Save profile data files to this directory.
    :param filename_format: Format string for profile data file names,
        or a callable returning a name. See explanation above.

    .. code-block:: python

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

    .. versionchanged:: 0.15
        Stats are written even if ``profile_dir`` is given, and can be
        disable by passing ``stream=None``.

    .. versionadded:: 0.15
        Added ``filename_format``.

    .. versionadded:: 0.9
        Added ``restrictions`` and ``profile_dir``.
    timecallsNz/{method}.{path}.{elapsed:.0f}ms.{time:.0f}.profc             C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  S)N)_app_stream_sort_by_restrictions_profile_dir_filename_format)selfZappstreamZsort_byZrestrictionsZprofile_dirZfilename_format r   @/tmp/pip-build-5gj8f0j9/Werkzeug/werkzeug/middleware/profiler.py__init__I   s    						zProfilerMiddleware.__init__c       
   	      s  g   d    f d d         f d d   } t    } t j   } | j |  d j   } t j   | }  j d  k	 r(t  j  r  j   } nY  j j d  d d  j d	  j	 d
  j
 d
 d  p d d | d d t j    } t j j  j |  } | j |   j d  k	 rt | d  j }	 |	 j  j   t d d d  j t d j  j d	 d   d  j |	 j  j   t d d d d  j | g S)Nc                s    |  | |    j  S)N)append)statusheadersexc_info)response_bodystart_responser   r   catching_start_response\   s    z<ProfilerMiddleware.__call__.<locals>.catching_start_responsec                 s<    j      }   j |   t |  d  r8 |  j   d  S)Nclose)r   extendhasattrr   )Zapp_iter)r   environr   r   r   r   runapp`   s    z+ProfilerMiddleware.__call__.<locals>.runapp    methodREQUEST_METHODpathZ	PATH_INFO/.rootelapsedg     @@r   r   -P   filez
PATH: {!r} 
)r   r   Zruncalljoinr   callabler   formatgetstripreplaceosr"   Z
dump_statsr	   r   Z
sort_statsr
   printZprint_statsr   )
r   r   r   r   profilestartbodyr&   filenamestatsr   )r   r   r   r   r   r   __call__Y   s4    	
*
(zProfilerMiddleware.__call__)r   r   )__name__
__module____qualname____doc__sysstdoutr   r9   r   r   r   r   r      s   ,	r   )r=   
__future__r   os.pathr2   r>   r   Zpstatsr   ZcProfiler   ImportErrorr4   objectr   r   r   r   r   <module>   s   