
    2Bf"                         d 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
 ej                  rddlmZ ddlmZ ddlmZ  G d d	      Zy# e$ r	 ddlm
Z
 Y 6w xY w)
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
    N)Stats)Profile)StartResponse)WSGIApplication)WSGIEnvironmentc                      e Zd ZdZej
                  ddddfdddej                  d	ej                  e	   d
ej                  ej                  e	eef      dej                  e	   de	ddfdZdddddej                  e   fdZy)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calls Nz/{method}.{path}.{elapsed:.0f}ms.{time:.0f}.profappr   streamsort_byrestrictionsprofile_dirfilename_formatreturnc                 X    || _         || _        || _        || _        || _        || _        y N)_app_stream_sort_by_restrictions_profile_dir_filename_format)selfr   r   r   r   r   r   s          \/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/werkzeug/middleware/profiler.py__init__zProfilerMiddleware.__init__M   s1     	)' /    environr   start_responser   c                     g dfd	d fd}t               }t        j                         }|j                  |       dj                        }t        j                         |z
  } j                  t         j                        r j                        }n] j                  j                  d   d   j                  d      j                  dd      xs d|d	z  t        j                         
      }t        j                  j                   j                  |      }|j                  |        j                  t        | j                        }	 |	j                   j                     t#        d j                         j%                  dd      }
t#        d|
 j                          |	j&                   j(                    t#        d d j                         |gS )Nc                 0     | ||       j                   S r   )append)statusheadersexc_inforesponse_bodyr!   s      r   catching_start_responsez<ProfilerMiddleware.__call__.<locals>.catching_start_responseb   s    67H5 '''r   c                      j                  t        j                  d            } j                  |        t	        | d      r| j                          y y )Nr   close)r   tcastextendhasattrr+   )app_iterr)   r    r(   r   s    r   runappz+ProfilerMiddleware.__call__.<locals>.runappf   sK    yy1HIH   *x)  *r   r   REQUEST_METHOD	PATH_INFO/.rootg     @@)methodpathelapsedr
   )r   zP--------------------------------------------------------------------------------)file zPATH: 
r   )r   N)r   r
   runcalljoinr   callabler   formatstripreplaceosr8   
dump_statsr   r   
sort_statsr   printgetprint_statsr   )r   r    r!   r1   profilestartbodyr9   filenamestats	path_infor)   r(   s   ```        @@r   __call__zProfilerMiddleware.__call__]   s    (*	(	! 	! )		xx&))+%(--.0090077"#34 -33C8@@cJTf#f,	 8  ww||D$5$5x@Hx(<<#'$,,7EEdmm,(.K4IF9-(t||<Et112XJb/5vr   )__name__
__module____qualname____doc__sysstdoutr,   TextIOIterablestrUnionintfloatOptionalr   bytesrO   r   r   r   r	   r	      s    +` ::#4=?'+P00 0 C	0
 jjc5!9:0 ZZ_0 0 
0 .(.:I.	
E	.r   r	   )rS   os.pathrC   rT   r
   typingr,   pstatsr   cProfiler   ImportErrorrI   TYPE_CHECKING_typeshed.wsgir   r   r   r	   r   r   r   <module>re      sU     
      ??,..l l    s   A	 	AA