a
    Nf                     @  s   d Z ddlmZ ddlZddlZddlZddlZddl	m
Z
 zddlmZ W n eyj   ddlmZ Y n0 ejrddlmZ ddlmZ ddlmZ G d	d
 d
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
    )annotationsN)Stats)Profile)StartResponse)WSGIApplication)WSGIEnvironmentc                	   @  sJ   e Zd ZdZejddddfdddd	d
dddddZdddd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 in milliseconds.
    -   ``{time}`` - The time of the request.

    If it is a callable, it will be called with the WSGI ``environ`` and
    be expected to return a filename string. The ``environ`` dictionary
    will also have the ``"werkzeug.profiler"`` key populated with a
    dictionary containing the following fields (more may be added in the
    future):
    -   ``{elapsed}`` - The elapsed time of the request in milliseconds.
    -   ``{time}`` - The time of the request.

    :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:: 3.0
        Added the ``"werkzeug.profiler"`` key to the ``filename_format(environ)``
        parameter with the  ``elapsed`` and ``time`` fields.

    .. 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``.
    )timeZcalls Nz/{method}.{path}.{elapsed:.0f}ms.{time:.0f}.profr   zt.IO[str] | Nonezt.Iterable[str]zt.Iterable[str | int | float]z
str | NonestrNone)appstreamsort_byrestrictionsprofile_dirfilename_formatreturnc                 C  s(   || _ || _|| _|| _|| _|| _d S N)_app_stream_sort_by_restrictions_profile_dir_filename_format)selfr   r   r   r   r   r   r
   r
   U/var/www/ai-form-bot/venv/lib/python3.9/site-packages/werkzeug/middleware/profiler.py__init__Y   s    	zProfilerMiddleware.__init__r   r   zt.Iterable[bytes])environstart_responser   c                   sZ  g dfdd	 dd fdd}t  }t }|| d}t | }jd urtjr|d t d	d
< }n6jjd d d	ddpd|d t d}t
jj|}|| jd urTt|jd}	|	jj  tdjd dd}
td|
jd |	jj  td djd |gS )Nc                   s   | ||  j S r   )append)statusheadersexc_info)response_bodyr   r
   r   catching_start_responsen   s    z<ProfilerMiddleware.__call__.<locals>.catching_start_responser   )r   c                    s4    td } |  t| dr0|   d S )Nr   close)r   tcastextendhasattrr&   )Zapp_iter)r%   r   r$   r   r
   r   runappr   s    

z+ProfilerMiddleware.__call__.<locals>.runapp    g     @@)elapsedr	   zwerkzeug.profilerREQUEST_METHODZ	PATH_INFO/.root)methodpathr-   r	   )r   zP--------------------------------------------------------------------------------)file zPATH: 
)N)r   r	   Zruncalljoinr   callabler   formatstripreplaceosr3   Z
dump_statsr   r   Z
sort_statsr   printgetZprint_statsr   )r   r   r   r+   profilestartbodyr-   filenamestatsZ	path_infor
   )r%   r   r$   r   r   r   __call__i   s>    	





zProfilerMiddleware.__call__)__name__
__module____qualname____doc__sysstdoutr   rD   r
   r
   r
   r   r   "   s   9r   )rH   
__future__r   os.pathr<   rI   r	   typingr'   Zpstatsr   ZcProfiler   ImportErrorr?   TYPE_CHECKINGZ_typeshed.wsgir   r   r   r   r
   r
   r
   r   <module>   s   