U aD@sddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl m Z dd l mZdd lmZejrdd lmZdd lmZdd lmZeZGdddZeedddZejejdddZedddZedddZGdddZ GdddZ!dS)N)update_wrapper) TracebackType) HTTPException)_app_ctx_stack)_request_ctx_stack)appcontext_popped)appcontext_pushed)AfterRequestCallable)Flask) SessionMixin)Requestc@seZdZdZeejdddZeejddddZeddd d Z deej ejejd d d Z e feejejd ddZ deejejd ddZeedddZejedddZedddZdS)_AppCtxGlobalsaA plain object. Used as a namespace for storing data during an application context. Creating an app context automatically creates this object, which is made available as the :data:`g` proxy. .. describe:: 'key' in g Check whether an attribute is present. .. versionadded:: 0.10 .. describe:: iter(g) Return an iterator over the attribute names. .. versionadded:: 0.10 )namereturncCs0z |j|WStk r*t|dYnXdSN__dict__KeyErrorAttributeErrorselfrrJC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-e702oxwa\flask\ctx.py __getattr__/s z_AppCtxGlobals.__getattr__N)rvaluercCs||j|<dSrr)rrrrrr __setattr__5sz_AppCtxGlobals.__setattr__cCs0z |j|=Wntk r*t|dYnXdSrrrrrr __delattr__8s z_AppCtxGlobals.__delattr__)rdefaultrcCs|j||S)zGet an attribute by name, or a default value. Like :meth:`dict.get`. :param name: Name of attribute to get. :param default: Value to return if the attribute is not present. .. versionadded:: 0.10 )rgetrrrrrrr >s z_AppCtxGlobals.getcCs&|tkr|j|S|j||SdS)a Get and remove an attribute by name. Like :meth:`dict.pop`. :param name: Name of attribute to pop. :param default: Value to return if the attribute is not present, instead of raising a ``KeyError``. .. versionadded:: 0.11 N) _sentinelrpopr!rrrr#Is  z_AppCtxGlobals.popcCs|j||S)a5Get the value of an attribute if it is present, otherwise set and return a default value. Like :meth:`dict.setdefault`. :param name: Name of attribute to get. :param default: Value to set and return if the attribute is not present. .. versionadded:: 0.11 )r setdefaultr!rrrr$Ws z_AppCtxGlobals.setdefault)itemrcCs ||jkSrr)rr%rrr __contains__csz_AppCtxGlobals.__contains__rcCs t|jSr)iterrrrrr__iter__fsz_AppCtxGlobals.__iter__cCs(tj}|dk rd|jjdSt|S)Nz )rtopapprobject__repr__)rr,rrrr/isz_AppCtxGlobals.__repr__)N)N)__name__ __module__ __qualname____doc__strtAnyrrrOptionalr r"r#r$boolr&Iteratorr*r/rrrrrs  r)frcCstjj||S)aExecutes a function after this request. This is useful to modify response objects. The function is passed the response object and has to return the same or a new one. Example:: @app.route('/') def index(): @after_this_request def add_header(response): response.headers['X-Foo'] = 'Parachute' return response return 'Hello World!' This is more useful if a function other than the view function wants to modify a response. For instance think of a decorator that wants to add some headers without converting the return value into a response object. .. versionadded:: 0.9 )rr,_after_request_functionsappend)r:rrrafter_this_requestpsr=cs6tj}|dkrtd|fdd}t|S)a:A helper function that decorates a function to retain the current request context. This is useful when working with greenlets. The moment the function is decorated a copy of the request context is created and then pushed when the function is called. The current session is also included in the copied request context. Example:: import gevent from flask import copy_current_request_context @app.route('/') def index(): @copy_current_request_context def do_some_work(): # do some work here, it can access flask.request or # flask.session like you would otherwise in the view function. ... gevent.spawn(do_some_work) return 'Regular response' .. versionadded:: 0.10 Nz|This decorator can only be used at local scopes when a request context is on the stack. For instance within view functions.c s&||W5QRSQRXdSrr)argskwargsr:Zreqctxrrwrappersz-copy_current_request_context..wrapper)rr, RuntimeErrorcopyr)r:r,rArr@rcopy_current_request_contextsrDr'cCs tjdk S)aIf you have code that wants to test if a request context is there or not this function can be used. For instance, you may want to take advantage of request information if the request object is available, but fail silently if it is unavailable. :: class User(db.Model): def __init__(self, username, remote_addr=None): self.username = username if remote_addr is None and has_request_context(): remote_addr = request.remote_addr self.remote_addr = remote_addr Alternatively you can also just test any of the context bound objects (such as :class:`request` or :class:`g`) for truthness:: class User(db.Model): def __init__(self, username, remote_addr=None): self.username = username if remote_addr is None and request: remote_addr = request.remote_addr self.remote_addr = remote_addr .. versionadded:: 0.7 N)rr,rrrrhas_request_contextsrEcCs tjdk S)zWorks like :func:`has_request_context` but for the application context. You can also just do a boolean check on the :data:`current_app` object instead. .. versionadded:: 0.9 N)rr,rrrrhas_app_contextsrFc@sjeZdZdZdddddZdddd Zefeje dd d d Z ddd dZ e e e ddddZdS) AppContexta]The application context binds an application object implicitly to the current thread or greenlet, similar to how the :class:`RequestContext` binds request information. The application context is also implicitly created if a request context is created but the application is not on top of the individual application context. r N)r-rcCs&||_|d|_||_d|_dS)Nr)r-create_url_adapter url_adapterZapp_ctx_globals_classg_refcnt)rr-rrr__init__s  zAppContext.__init__r'cCs(|jd7_t|t|jdS)z-Binds the app context to the current context.rN)rKrpushr sendr-r)rrrrMs zAppContext.pushexcrcCsvz<|jd8_|jdkr:|tkr.td}|j|W5t}X||ksftd|d|dt |jdS)zPops the app context.rrzPopped wrong app context. ( instead of )N) rr#rKr"sysexc_infor-Zdo_teardown_appcontextAssertionErrorrrN)rrPrvrrrr#s   zAppContext.popcCs ||SrrMr)rrr __enter__szAppContext.__enter__exc_type exc_valuetbrcCs||dSr)r#rrZr[r\rrr__exit__szAppContext.__exit__)r0r1r2r3rLrMr"r5r7 BaseExceptionr#rXtyperr^rrrrrGs  rGc@seZdZdZd deejdejdddddZee d d d Z e j e dd d d Z dd ddZ dd ddZ dd ddZefejeddddZejeddddZdd ddZeeeddddZed ddZdS)!RequestContextaThe request context contains all request relevant information. It is created at the beginning of the request and pushed to the `_request_ctx_stack` and removed at the end of it. It will create the URL adapter and request object for the WSGI environment provided. Do not attempt to use this class directly, instead use :meth:`~flask.Flask.test_request_context` and :meth:`~flask.Flask.request_context` to create this object. When the request context is popped, it will evaluate all the functions registered on the application for teardown execution (:meth:`~flask.Flask.teardown_request`). The request context is automatically popped at the end of the request for you. In debug mode the request context is kept around if exceptions happen so that interactive debuggers have a chance to introspect the data. With 0.4 this can also be forced for requests that did not fail and outside of ``DEBUG`` mode. By setting ``'flask._preserve_context'`` to ``True`` on the WSGI environment the context will not pop itself at the end of the request. This is used by the :meth:`~flask.Flask.test_client` for example to implement the deferred cleanup functionality. You might find this helpful for unittests where you need the information from the context local around for a little longer. Make sure to properly :meth:`~werkzeug.LocalStack.pop` the stack yourself in that situation, otherwise your unittests will leak memory. Nr r r )r-environrequestsessionrc Cs||_|dkr||}||_d|_z||j|_Wn*tk r`}z ||j_W5d}~XYnXd|_||_g|_ d|_ d|_ g|_ dS)NF) r-Z request_classrcrIrHrrouting_exceptionZflashesrd_implicit_app_ctx_stack preserved_preserved_excr;)rr-rbrcrderrrrL(s zRequestContext.__init__r'cCstjjSrrr,rJr)rrrrJNszRequestContext.g)rrcCs |tj_dSrrj)rrrrrrJRscCs|j|j|jj|j|jdS)a5Creates a copy of this request context with the same request object. This can be used to move a request context to a different greenlet. Because the actual request object is the same this cannot be used to move a request context to a different thread unless access to the request object is locked. .. versionadded:: 0.10 .. versionchanged:: 1.1 The current session object is used instead of reloading the original data. This prevents `flask.session` pointing to an out-of-date object. )rbrcrd) __class__r-rcrbrdr)rrrrCVs zRequestContext.copyc CsRz"|jjdd}|\|j_|j_Wn*tk rL}z ||j_W5d}~XYnXdS)zZCan be overridden by a subclass to hook into the matching of the request. T)Z return_ruleN)rImatchrcZurl_ruleZ view_argsrre)rresultrirrr match_requestjs zRequestContext.match_requestcCstj}|dk r |jr ||jtj}|dks:|j|jkrZ|j}||j |n |j dt||j dkr|jj }| |j|j|_ |j dkr||j|_ |jdk r|dS)z1Binds the request context to the current context.N)rr,rgr#rhrr-Z app_contextrMrfr<rdsession_interfaceZ open_sessionrcZmake_null_sessionrIrn)rr,app_ctxrorrrrMts"        zRequestContext.pushrOc Cs|j}d}zV|jsbd|_d|_|tkr6t d}|j |t |jdd}|dk r^|d }W5t}|r~d|jjd<|dk r||||kstd|d|dXdS) a Pops the request context and unbinds it by doing that. This will also trigger the execution of functions registered by the :meth:`~flask.Flask.teardown_request` decorator. .. versionchanged:: 0.9 Added the `exc` argument. FNzwerkzeug.requestzPopped wrong request context. (rQrRrcloseT)rfr#rrcrbrUrgrhr"rSrTr-Zdo_teardown_requestgetattr)rrPrpZ clear_requestrVZ request_closerrrr#s,     zRequestContext.popcCs:|jjds|dk r,|jjr,d|_||_n ||dS)Nzflask._preserve_contextT)rcrbr r-Zpreserve_context_on_exceptionrgrhr#)rrPrrrauto_popszRequestContext.auto_popcCs ||SrrWr)rrrrXszRequestContext.__enter__rYcCs||dSr)rsr]rrrr^szRequestContext.__exit__c Cs0dt|jd|jjd|jjd|jjd S)N< z [z] of r+)r`r0rcurlmethodr-rr)rrrr/s.zRequestContext.__repr__)NN)r0r1r2r3dictr5r7rLpropertyrGrJsetterrCrnrMr"r_r#rsrXr`rr^r4r/rrrrra s4! & *'  ra)"rStypingr5 functoolsrtypesrZwerkzeug.exceptionsrglobalsrrZsignalsrr r TYPE_CHECKINGr-r sessionsr wrappersr r.r"rr=CallablerDr8rErFrGrarrrrs*           X(  /