
    /h                        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
 	 ddlmZ n# e$ r	 ddlmZ Y nw xY wej        rddlmZ ddlmZ ddlmZ  G 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                  8    e Zd ZdZej        ddddf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 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                Z    || _         || _        || _        || _        || _        || _        d S N)_app_stream_sort_by_restrictions_profile_dir_filename_format)selfr   r   r   r   r   r   s          O/var/www/html/venv/lib/python3.11/site-packages/werkzeug/middleware/profiler.py__init__zProfilerMiddleware.__init__Y   s7     	)' /    environr   start_responser   t.Iterable[bytes]c                    g dfd	d fd}t                      }t          j                    }|                    |           d                              }t          j                    |z
  } j        t           j                  r2|dz  t          j                    dd<                                  }ng j                            d	         d
                             d          	                    dd          pd|dz  t          j                              }t          j                             j        |          }|                    |            j        t          | j                  }	 |	j         j          t#          d j                                       d
d          }
t#          d|
 j                    |	j         j          t#          d d j                   |gS )Nc                ,     | ||           j         S r   )append)statusheadersexc_inforesponse_bodyr(   s      r$   catching_start_responsez<ProfilerMiddleware.__call__.<locals>.catching_start_responsen   s     N67H555 ''r&   r   r   c                                          t          j        d                    }                     |            t	          | d          r|                                  d S d S )Nr   close)r   tcastextendhasattrr3   )app_iterr1   r'   r0   r#   s    r$   runappz+ProfilerMiddleware.__call__.<locals>.runappr   sp    yy1HII H   ***x)) !     ! !r&   r&   g     @@)elapsedr   zwerkzeug.profilerREQUEST_METHOD	PATH_INFO/.root)methodpathr:   r   )r   zP--------------------------------------------------------------------------------)file zPATH: 
r   )r   r   )r   r   runcalljoinr!   callabler"   formatstripreplaceosrA   
dump_statsr   r   
sort_statsr   printgetprint_statsr    )r#   r'   r(   r9   profilestartbodyr:   filenamestats	path_infor1   r0   s   ```        @@r$   __call__zProfilerMiddleware.__call__i   s6    &(	( 	( 	( 	( 	( 	( 	(	! 	! 	! 	! 	! 	! 	! 	! 	! ))	xx&&)++%(-.. &/ IKK0 0+,  0099077"#34 -33C88@@cJJTf#f,	 8   w||D$5x@@Hx(((<#'$,777EEdm,,(....K44I(9((t|<<<<Et122X///5555vr&   )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%   rW   r   r&   r$   r
   r
   "   s_        4 4r $':#468"&P0 0 0 0 0 2 2 2 2 2 2r&   r
   )r[   
__future__r   os.pathrK   r\   r   typingr4   pstatsr   cProfiler   ImportErrorrQ   TYPE_CHECKING_typeshed.wsgir   r   r   r
   r   r&   r$   <module>rf      s'    # " " " " "  



                           ? /,,,,,,............y y y y y y y y y ys   ' 55