Format: application/x-python-code
⬇ Download File (14.5 KB)
   1  o
   2  
   3  D'h;�@s0ddlmZddlZddlZddlZddlmZddlm	Z	ddl
   4  mZddlmZ
   5  ddlmZdd	lmZdd
   6  lmZddlmZejr]ddlmZdd
   7  lmZddlmZddlmZe�ZGdd�d�Zd%dd�Z ej!dej"dej#fd�Z$d&dd�Z%d'dd�Z&d'dd �Z'Gd!d"�d"�Z(Gd#d$�d$�Z)dS)(�)�annotationsN)�update_wrapper)�
   8  TracebackType)�
   9  HTTPException�)�typing)�_cv_app)�_cv_request)�appcontext_popped)�appcontext_pushed)�WSGIEnvironment)�Flask)�SessionMixin)�Requestc@sreZdZdZd dd�Zd!d
  10  d�Zd"dd
  11  �Zd#d$dd�Zefd%dd�Z	d#d%dd�Z
  12  d&dd�Zd'dd�Zd(dd�Z
  13  dS))�_AppCtxGlobalsa�A plain object. Used as a namespace for storing data during an
  14      application context.
  15  
  16      Creating an app context automatically creates this object, which is
  17      made available as the :data:`g` proxy.
  18  
  19      .. describe:: 'key' in g
  20  
  21          Check whether an attribute is present.
  22  
  23          .. versionadded:: 0.10
  24  
  25      .. describe:: iter(g)
  26  
  27          Return an iterator over the attribute names.
  28  
  29          .. versionadded:: 0.10
  30      �name�str�return�t.AnycCs&z|j|WStyt|�d�w�N��__dict__�KeyError�AttributeError��selfr�r�?C:\Users\shubh\Videos\agent\nova\Lib\site-packages\flask/ctx.py�__getattr__4s
  31  
  32  �z_AppCtxGlobals.__getattr__�value�NonecCs||j|<dSr�r)rrrrrr�__setattr__:sz_AppCtxGlobals.__setattr__cCs(z|j|=WdStyt|�d�wrrrrrr�__delattr__=s
  33  
  34  �z_AppCtxGlobals.__delattr__N�default�t.Any | NonecC�|j�||�S)z�Get an attribute by name, or a default value. Like
  35          :meth:`dict.get`.
  36  
  37          :param name: Name of attribute to get.
  38          :param default: Value to return if the attribute is not present.
  39  
  40          .. versionadded:: 0.10
  41          )r�get�rrr$rrrr'Cs	z_AppCtxGlobals.getcCs"|tur
  42  |j�|�S|j�||�S)aGet and remove an attribute by name. Like :meth:`dict.pop`.
  43  
  44          :param name: Name of attribute to pop.
  45          :param default: Value to return if the attribute is not present,
  46              instead of raising a ``KeyError``.
  47  
  48          .. versionadded:: 0.11
  49          )�	_sentinelr�popr(rrrr*Ns	z_AppCtxGlobals.popcCr&)a5Get the value of an attribute if it is present, otherwise
  50          set and return a default value. Like :meth:`dict.setdefault`.
  51  
  52          :param name: Name of attribute to get.
  53          :param default: Value to set and return if the attribute is not
  54              present.
  55  
  56          .. versionadded:: 0.11
  57          )r�
  58  setdefaultr(rrrr+\s
  59  z_AppCtxGlobals.setdefault�item�boolcCs
  60  ||jvSrr!)rr,rrr�__contains__h�
  61  z_AppCtxGlobals.__contains__�t.Iterator[str]cCs
  62  t|j�Sr)�iterr�rrrr�__iter__kr/z_AppCtxGlobals.__iter__cCs,t�d�}|durd|jj�d�St�|�S)Nz
  63  <flask.g of 'z'>)rr'�appr�object�__repr__)r�ctxrrrr6ns
  64  
  65  z_AppCtxGlobals.__repr__)rrrr)rrrrrr )rrrr r)rrr$r%rr)rrr$rrr)r,rrr-)rr0�rr)�__name__�
  66  __module__�__qualname__�__doc__rr"r#r'r)r*r+r.r3r6rrrrrs
  67  
  68  
  69  
  70  
  71  r�f�ft.AfterRequestCallable[t.Any]rcCs*t�d�}|dur
  72  td��|j�|�|S)a�Executes a function after this request.  This is useful to modify
  73      response objects.  The function is passed the response object and has
  74      to return the same or a new one.
  75  
  76      Example::
  77  
  78          @app.route('/')
  79          def index():
  80              @after_this_request
  81              def add_header(response):
  82                  response.headers['X-Foo'] = 'Parachute'
  83                  return response
  84              return 'Hello World!'
  85  
  86      This is more useful if a function other than the view function wants to
  87      modify a response.  For instance think of a decorator that wants to add
  88      some headers without converting the return value into a response object.
  89  
  90      .. versionadded:: 0.9
  91      Nzc'after_this_request' can only be used when a request context is active, such as in a view function.)r	r'�RuntimeError�_after_request_functions�append)r=r7rrr�after_this_requestus
  92  �rB�F.)�boundcs<t�d���dur
  93  td������d	��fdd�}t|��S)
  94  a:A helper function that decorates a function to retain the current
  95      request context.  This is useful when working with greenlets.  The moment
  96      the function is decorated a copy of the request context is created and
  97      then pushed when the function is called.  The current session is also
  98      included in the copied request context.
  99  
 100      Example::
 101  
 102          import gevent
 103          from flask import copy_current_request_context
 104  
 105          @app.route('/')
 106          def index():
 107              @copy_current_request_context
 108              def do_some_work():
 109                  # do some work here, it can access flask.request or
 110                  # flask.session like you would otherwise in the view function.
 111                  ...
 112              gevent.spawn(do_some_work)
 113              return 'Regular response'
 114  
 115      .. versionadded:: 0.10
 116      Nzm'copy_current_request_context' can only be used when a request context is active, such as in a view function.�argsr�kwargsrcs>���j���|i|��Wd�S1swYdSr)r4�ensure_sync)rErF�r7r=rr�wrapper�s$�z-copy_current_request_context.<locals>.wrapper)rErrFrrr)r	r'r?�copyr)r=rIrrHr�copy_current_request_context�s
 117  �
 118  rKr-cC�t�d�duS)a�If you have code that wants to test if a request context is there or
 119      not this function can be used.  For instance, you may want to take advantage
 120      of request information if the request object is available, but fail
 121      silently if it is unavailable.
 122  
 123      ::
 124  
 125          class User(db.Model):
 126  
 127              def __init__(self, username, remote_addr=None):
 128                  self.username = username
 129                  if remote_addr is None and has_request_context():
 130                      remote_addr = request.remote_addr
 131                  self.remote_addr = remote_addr
 132  
 133      Alternatively you can also just test any of the context bound objects
 134      (such as :class:`request` or :class:`g`) for truthness::
 135  
 136          class User(db.Model):
 137  
 138              def __init__(self, username, remote_addr=None):
 139                  self.username = username
 140                  if remote_addr is None and request:
 141                      remote_addr = request.remote_addr
 142                  self.remote_addr = remote_addr
 143  
 144      .. versionadded:: 0.7
 145      N)r	r'rrrr�has_request_context�srMcCrL)z�Works like :func:`has_request_context` but for the application
 146      context.  You can also just do a boolean check on the
 147      :data:`current_app` object instead.
 148  
 149      .. versionadded:: 0.9
 150      N)rr'rrrr�has_app_context�srNc@sFeZdZdZddd�Zddd	�Zefddd
 151  �Zddd�Zddd�Z	dS)�
 152  AppContextz�The app context contains application-specific information. An app
 153      context is created and pushed at the beginning of each request if
 154      one is not already active. An app context is also pushed when
 155      running CLI commands.
 156      r4r
 157  rr cCs&||_|�d�|_|��|_g|_dSr)r4�create_url_adapter�url_adapterZapp_ctx_globals_class�g�
 158  _cv_tokens)rr4rrr�__init__�s
 159  
 160  zAppContext.__init__cCs*|j�t�|��tj|j|jjd�dS)z-Binds the app context to the current context.�Z_async_wrapperN)rSrAr�setr�sendr4rGr2rrr�push�szAppContext.push�exc�BaseException | Nonec
 161  Cs�z%t|j�dkr|turt��d}|j�|�Wt��}t�	|j�
 162  ��n
 163  t��}t�	|j�
 164  ��w||urBtd|�d|�d���tj
 165  |j|jjd�dS)zPops the app context.rzPopped wrong app context. (� instead of �)rUN)�lenrSr)�sys�exc_infor4Zdo_teardown_appcontextrr'�resetr*�AssertionErrorr
 166  rWrG)rrYr7rrrr*s���zAppContext.popcC�|��|Sr�rXr2rrr�	__enter__�zAppContext.__enter__�exc_type�type | None�	exc_value�tb�TracebackType | NonecC�|�|�dSr�r*�rrfrhrirrr�__exit__�zAppContext.__exit__N)r4r
 167  rr �rr �rYrZrr )rrO�rfrgrhrZrirjrr )
 168  r9r:r;r<rTrXr)r*rdrnrrrrrO�s
 169  
 170  
 171  rOc@sjeZdZdZ		d%d&d
 172  d�Zd'dd�Zd(dd�Zd(dd�Zefd)dd�Z	d'dd�Z
 173  d*d d!�Zd+d#d$�ZdS),�RequestContexta�The request context contains per-request information. The Flask
 174      app creates and pushes it at the beginning of the request, then pops
 175      it at the end of the request. It will create the URL adapter and
 176      request object for the WSGI environment provided.
 177  
 178      Do not attempt to use this class directly, instead use
 179      :meth:`~flask.Flask.test_request_context` and
 180      :meth:`~flask.Flask.request_context` to create this object.
 181  
 182      When the request context is popped, it will evaluate all the
 183      functions registered on the application for teardown execution
 184      (:meth:`~flask.Flask.teardown_request`).
 185  
 186      The request context is automatically popped at the end of the
 187      request. When using the interactive debugger, the context will be
 188      restored so ``request`` is still accessible. Similarly, the test
 189      client can preserve the context after the request ends. However,
 190      teardown functions may already have closed some resources such as
 191      database connections.
 192      Nr4r
 193  �environr�request�Request | None�session�SessionMixin | Nonerr c
 194  Cs�||_|dur|�|�}|j|_||_d|_z	|�|j�|_Wnty5}z
 195  ||j_WYd}~nd}~wwd|_	||_
 196  g|_g|_dSr)
 197  r4Z
 198  request_class�jsonZjson_modulerurQrPr�routing_exceptionZflashesrwr@rS)rr4rtrurw�errrrT5s"
 199  ���zRequestContext.__init__cCs|j|j|jj|j|jd�S)a5Creates a copy of this request context with the same request object.
 200          This can be used to move a request context to a different greenlet.
 201          Because the actual request object is the same this cannot be used to
 202          move a request context to a different thread unless access to the
 203          request object is locked.
 204  
 205          .. versionadded:: 0.10
 206  
 207          .. versionchanged:: 1.1
 208             The current session object is used instead of reloading the original
 209             data. This prevents `flask.session` pointing to an out-of-date object.
 210          )rtrurw)�	__class__r4rurtrwr2rrrrJQs
 211  �zRequestContext.copyc
 212  CsTz|jjdd�}|\|j_|j_WdSty)}z||j_WYd}~dSd}~ww)zZCan be overridden by a subclass to hook into the matching
 213          of the request.
 214          T)Zreturn_ruleN)rQ�matchruZurl_ruleZ	view_argsrrz)r�resultr{rrr�
 215  match_requestes��zRequestContext.match_requestcCs�t�d�}|dus|j|jur|j��}|��nd}|j�t�|�|f�|j	durD|jj
 216  }|�|j|j�|_	|j	durD|�
 217  |j�|_	|jdurO|��dSdSr)rr'r4Zapp_contextrXrSrAr	rVrw�session_interfaceZopen_sessionruZmake_null_sessionrQr)r�app_ctxr�rrrrXos
 218  
 219  
 220  
 221  
 222  
 223  �zRequestContext.pushrYrZcCst|j�dk}zS|r(|turt��d}|j�|�t|jdd�}|dur(|�Wt	�
 224  �}|j��\}}t	�|�|rAd|jj
 225  d<|durJ|�|�||urYtd|�d|�d���dSt	�
 226  �}|j��\}}t	�|�|rsd|jj
 227  d<|dur||�|�||ur�td|�d|�d���w)aPops the request context and unbinds it by doing that.  This will
 228          also trigger the execution of functions registered by the
 229          :meth:`~flask.Flask.teardown_request` decorator.
 230  
 231          .. versionchanged:: 0.9
 232             Added the `exc` argument.
 233          r�closeNzwerkzeug.requestzPopped wrong request context. (r[r\)r]rSr)r^r_r4Zdo_teardown_request�getattrrur	r'r*r`rtra)rrYZ
 234  clear_requestZ
 235  request_closer7�tokenr�rrrr*�sB�
 236  
 237  ���
 238  
 239  �zRequestContext.popcCrbrrcr2rrrrd�rezRequestContext.__enter__rfrgrhrirjcCrkrrlrmrrrrn�rozRequestContext.__exit__rc	Cs0dt|�j�d|jj�d|jj�d|jj�d�	S)N�<� z [z] of �>)�typer9ru�url�methodr4rr2rrrr6�s���zRequestContext.__repr__)NN)
 240  r4r
 241  rtrrurvrwrxrr )rrsrprqrrr8)
 242  r9r:r;r<rTrJrrXr)r*rdrnr6rrrrrss�
 243  
 244  
 245  
 246  
 247  %
 248  rs)r=r>rr>)r=rCrrC)rr-)*�
 249  __future__r�contextvarsr^r�t�	functoolsr�typesrZwerkzeug.exceptionsr��ft�globalsrr	Zsignalsr
 250  r�
 251  TYPE_CHECKINGZ_typeshed.wsgirr4r
 252  �sessionsrZwrappersrr5r)rrB�TypeVar�Callable�AnyrCrKrMrNrOrsrrrr�<module>s4
 253  X#
 254  
 255  )
 256   
 257  1