o
    ßý°jº  ã                   @  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y5   ddlmZ Y nw ejrKd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                   @  s2   e Zd ZdZejddddfddd„Z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``.
    )ÚtimeÚcalls© Nz/{method}.{path}.{elapsed:.0f}ms.{time:.0f}.profÚappr   Ústreamút.IO[str] | NoneÚsort_byút.Iterable[str]Úrestrictionsút.Iterable[str | int | float]Úprofile_dirú
str | NoneÚfilename_formatÚstrÚreturnÚNonec                 C  s(   || _ || _|| _|| _|| _|| _d S ©N)Ú_appÚ_streamÚ_sort_byÚ_restrictionsÚ_profile_dirÚ_filename_format)Úselfr   r   r   r   r   r   r   r   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/werkzeug/middleware/profiler.pyÚ__init__Y   s   	
zProfilerMiddleware.__init__Úenvironr   Ústart_responser   út.Iterable[bytes]c                   sT  g ‰d‡‡fdd„	‰ d‡ ‡‡‡fdd„}t ƒ }t ¡ }| |¡ d ˆ¡}t ¡ | }ˆjd urntˆjƒrF|d t ¡ d	œˆd
< ˆ ˆ¡}nˆjjˆd ˆd  d¡ 	dd¡pXd|d t ¡ d�}t
j ˆj|¡}| |¡ ˆjd ur§t|ˆ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                    s8   ˆ  ˆt dˆ ¡¡} ˆ | ¡ t| dƒr|  ¡  d S d S )Nr   Úclose)r   ÚtÚcastÚextendÚhasattrr,   )Úapp_iter)r+   r#   r*   r    r   r!   Úrunappr   s   ÿ

ÿz+ProfilerMiddleware.__call__.<locals>.runappó    g     @�@)Úelapsedr	   zwerkzeug.profilerÚREQUEST_METHODÚ	PATH_INFOú/Ú.Úroot)ÚmethodÚpathr4   r	   )r   zP--------------------------------------------------------------------------------)ÚfileÚ zPATH: Ú
r   )r   r   )r   r	   ÚruncallÚjoinr   Úcallabler   ÚformatÚstripÚreplaceÚosr;   Ú
dump_statsr   r   Ú
sort_statsr   ÚprintÚgetÚprint_statsr   )r    r#   r$   r2   ÚprofileÚstartÚbodyr4   ÚfilenameÚstatsÚ	path_infor   )r+   r#   r*   r    r$   r!   Ú__call__i   s>   	




þü

zProfilerMiddleware.__call__)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r#   r   r$   r   r   r%   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚsysÚstdoutr"   rQ   r   r   r   r!   r   "   s    9ùr   )rU   Ú
__future__r   Úos.pathrE   rV   r	   Útypingr-   Úpstatsr   ÚcProfiler   ÚImportErrorrK   ÚTYPE_CHECKINGÚ_typeshed.wsgir   r   r   r   r   r   r   r!   Ú<module>   s"    ÿ