1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2024-12-27 01:57:33 +00:00
bazarr/libs/flask/ctx.py

439 lines
14 KiB
Python
Raw Normal View History

2022-11-07 18:06:49 +00:00
import contextvars
2019-11-28 11:40:45 +00:00
import sys
import typing as t
2019-11-28 11:40:45 +00:00
from functools import update_wrapper
from types import TracebackType
2019-11-28 11:40:45 +00:00
from werkzeug.exceptions import HTTPException
2022-11-07 18:06:49 +00:00
from . import typing as ft
from .globals import _cv_app
from .globals import _cv_request
2019-11-28 11:40:45 +00:00
from .signals import appcontext_popped
from .signals import appcontext_pushed
2022-11-07 18:06:49 +00:00
if t.TYPE_CHECKING: # pragma: no cover
from .app import Flask
from .sessions import SessionMixin
from .wrappers import Request
2019-11-28 11:40:45 +00:00
# a singleton sentinel value for parameter defaults
_sentinel = object()
class _AppCtxGlobals:
2019-11-28 11:40:45 +00:00
"""A 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
"""
# Define attr methods to let mypy know this is a namespace object
# that has arbitrary attributes.
def __getattr__(self, name: str) -> t.Any:
try:
return self.__dict__[name]
except KeyError:
raise AttributeError(name) from None
def __setattr__(self, name: str, value: t.Any) -> None:
self.__dict__[name] = value
def __delattr__(self, name: str) -> None:
try:
del self.__dict__[name]
except KeyError:
raise AttributeError(name) from None
def get(self, name: str, default: t.Optional[t.Any] = None) -> t.Any:
2019-11-28 11:40:45 +00:00
"""Get 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
"""
return self.__dict__.get(name, default)
def pop(self, name: str, default: t.Any = _sentinel) -> t.Any:
2019-11-28 11:40:45 +00:00
"""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``.
2019-11-28 11:40:45 +00:00
.. versionadded:: 0.11
"""
if default is _sentinel:
return self.__dict__.pop(name)
else:
return self.__dict__.pop(name, default)
def setdefault(self, name: str, default: t.Any = None) -> t.Any:
2019-11-28 11:40:45 +00:00
"""Get 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
2019-11-28 11:40:45 +00:00
present.
.. versionadded:: 0.11
"""
return self.__dict__.setdefault(name, default)
def __contains__(self, item: str) -> bool:
2019-11-28 11:40:45 +00:00
return item in self.__dict__
def __iter__(self) -> t.Iterator[str]:
2019-11-28 11:40:45 +00:00
return iter(self.__dict__)
def __repr__(self) -> str:
2022-11-07 18:06:49 +00:00
ctx = _cv_app.get(None)
if ctx is not None:
return f"<flask.g of '{ctx.app.name}'>"
2019-11-28 11:40:45 +00:00
return object.__repr__(self)
2022-11-07 18:06:49 +00:00
def after_this_request(f: ft.AfterRequestCallable) -> ft.AfterRequestCallable:
2019-11-28 11:40:45 +00:00
"""Executes 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
"""
2022-11-07 18:06:49 +00:00
ctx = _cv_request.get(None)
if ctx is None:
raise RuntimeError(
"'after_this_request' can only be used when a request"
" context is active, such as in a view function."
)
ctx._after_request_functions.append(f)
2019-11-28 11:40:45 +00:00
return f
def copy_current_request_context(f: t.Callable) -> t.Callable:
2019-11-28 11:40:45 +00:00
"""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
"""
2022-11-07 18:06:49 +00:00
ctx = _cv_request.get(None)
if ctx is None:
2019-11-28 11:40:45 +00:00
raise RuntimeError(
2022-11-07 18:06:49 +00:00
"'copy_current_request_context' can only be used when a"
" request context is active, such as in a view function."
2019-11-28 11:40:45 +00:00
)
2022-11-07 18:06:49 +00:00
ctx = ctx.copy()
2019-11-28 11:40:45 +00:00
def wrapper(*args, **kwargs):
2022-11-07 18:06:49 +00:00
with ctx:
return ctx.app.ensure_sync(f)(*args, **kwargs)
2019-11-28 11:40:45 +00:00
return update_wrapper(wrapper, f)
def has_request_context() -> bool:
2019-11-28 11:40:45 +00:00
"""If 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
"""
2022-11-07 18:06:49 +00:00
return _cv_request.get(None) is not None
2019-11-28 11:40:45 +00:00
def has_app_context() -> bool:
2019-11-28 11:40:45 +00:00
"""Works 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
"""
2022-11-07 18:06:49 +00:00
return _cv_app.get(None) is not None
2019-11-28 11:40:45 +00:00
class AppContext:
2022-11-07 18:06:49 +00:00
"""The app context contains application-specific information. An app
context is created and pushed at the beginning of each request if
one is not already active. An app context is also pushed when
running CLI commands.
2019-11-28 11:40:45 +00:00
"""
def __init__(self, app: "Flask") -> None:
2019-11-28 11:40:45 +00:00
self.app = app
self.url_adapter = app.create_url_adapter(None)
2022-11-07 18:06:49 +00:00
self.g: _AppCtxGlobals = app.app_ctx_globals_class()
self._cv_tokens: t.List[contextvars.Token] = []
2019-11-28 11:40:45 +00:00
def push(self) -> None:
2019-11-28 11:40:45 +00:00
"""Binds the app context to the current context."""
2022-11-07 18:06:49 +00:00
self._cv_tokens.append(_cv_app.set(self))
2019-11-28 11:40:45 +00:00
appcontext_pushed.send(self.app)
def pop(self, exc: t.Optional[BaseException] = _sentinel) -> None: # type: ignore
2019-11-28 11:40:45 +00:00
"""Pops the app context."""
try:
2022-11-07 18:06:49 +00:00
if len(self._cv_tokens) == 1:
2019-11-28 11:40:45 +00:00
if exc is _sentinel:
exc = sys.exc_info()[1]
self.app.do_teardown_appcontext(exc)
finally:
2022-11-07 18:06:49 +00:00
ctx = _cv_app.get()
_cv_app.reset(self._cv_tokens.pop())
if ctx is not self:
raise AssertionError(
f"Popped wrong app context. ({ctx!r} instead of {self!r})"
)
2019-11-28 11:40:45 +00:00
appcontext_popped.send(self.app)
def __enter__(self) -> "AppContext":
2019-11-28 11:40:45 +00:00
self.push()
return self
def __exit__(
2022-11-07 18:06:49 +00:00
self,
exc_type: t.Optional[type],
exc_value: t.Optional[BaseException],
tb: t.Optional[TracebackType],
) -> None:
2019-11-28 11:40:45 +00:00
self.pop(exc_value)
class RequestContext:
2022-11-07 18:06:49 +00:00
"""The request context contains per-request information. The Flask
app creates and pushes it at the beginning of the request, then pops
it at the end of the request. It will create the URL adapter and
request object for the WSGI environment provided.
2019-11-28 11:40:45 +00:00
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`).
2022-11-07 18:06:49 +00:00
The request context is automatically popped at the end of the
request. When using the interactive debugger, the context will be
restored so ``request`` is still accessible. Similarly, the test
client can preserve the context after the request ends. However,
teardown functions may already have closed some resources such as
database connections.
2019-11-28 11:40:45 +00:00
"""
def __init__(
self,
app: "Flask",
environ: dict,
request: t.Optional["Request"] = None,
session: t.Optional["SessionMixin"] = None,
) -> None:
2019-11-28 11:40:45 +00:00
self.app = app
if request is None:
request = app.request_class(environ)
2022-11-07 18:06:49 +00:00
request.json_module = app.json # type: ignore[misc]
self.request: Request = request
2019-11-28 11:40:45 +00:00
self.url_adapter = None
try:
self.url_adapter = app.create_url_adapter(self.request)
except HTTPException as e:
self.request.routing_exception = e
2022-11-07 18:06:49 +00:00
self.flashes: t.Optional[t.List[t.Tuple[str, str]]] = None
self.session: t.Optional["SessionMixin"] = session
2019-11-28 11:40:45 +00:00
# Functions that should be executed after the request on the response
# object. These will be called before the regular "after_request"
# functions.
2022-11-07 18:06:49 +00:00
self._after_request_functions: t.List[ft.AfterRequestCallable] = []
2019-11-28 11:40:45 +00:00
2022-11-07 18:06:49 +00:00
self._cv_tokens: t.List[t.Tuple[contextvars.Token, t.Optional[AppContext]]] = []
2019-11-28 11:40:45 +00:00
def copy(self) -> "RequestContext":
2019-11-28 11:40:45 +00:00
"""Creates 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.
"""
return self.__class__(
self.app,
environ=self.request.environ,
request=self.request,
session=self.session,
)
def match_request(self) -> None:
2019-11-28 11:40:45 +00:00
"""Can be overridden by a subclass to hook into the matching
of the request.
"""
try:
result = self.url_adapter.match(return_rule=True) # type: ignore
self.request.url_rule, self.request.view_args = result # type: ignore
2019-11-28 11:40:45 +00:00
except HTTPException as e:
self.request.routing_exception = e
def push(self) -> None:
2019-11-28 11:40:45 +00:00
# Before we push the request context we have to ensure that there
# is an application context.
2022-11-07 18:06:49 +00:00
app_ctx = _cv_app.get(None)
if app_ctx is None or app_ctx.app is not self.app:
2019-11-28 11:40:45 +00:00
app_ctx = self.app.app_context()
app_ctx.push()
else:
2022-11-07 18:06:49 +00:00
app_ctx = None
2019-11-28 11:40:45 +00:00
2022-11-07 18:06:49 +00:00
self._cv_tokens.append((_cv_request.set(self), app_ctx))
2019-11-28 11:40:45 +00:00
# Open the session at the moment that the request context is available.
# This allows a custom open_session method to use the request context.
# Only open a new session if this is the first time the request was
# pushed, otherwise stream_with_context loses the session.
if self.session is None:
session_interface = self.app.session_interface
self.session = session_interface.open_session(self.app, self.request)
if self.session is None:
self.session = session_interface.make_null_session(self.app)
# Match the request URL after loading the session, so that the
# session is available in custom URL converters.
2019-11-28 11:40:45 +00:00
if self.url_adapter is not None:
self.match_request()
def pop(self, exc: t.Optional[BaseException] = _sentinel) -> None: # type: ignore
2019-11-28 11:40:45 +00:00
"""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.
"""
2022-11-07 18:06:49 +00:00
clear_request = len(self._cv_tokens) == 1
2019-11-28 11:40:45 +00:00
try:
2022-11-07 18:06:49 +00:00
if clear_request:
2019-11-28 11:40:45 +00:00
if exc is _sentinel:
exc = sys.exc_info()[1]
self.app.do_teardown_request(exc)
request_close = getattr(self.request, "close", None)
if request_close is not None:
request_close()
finally:
2022-11-07 18:06:49 +00:00
ctx = _cv_request.get()
token, app_ctx = self._cv_tokens.pop()
_cv_request.reset(token)
2019-11-28 11:40:45 +00:00
# get rid of circular dependencies at the end of the request
# so that we don't require the GC to be active.
if clear_request:
2022-11-07 18:06:49 +00:00
ctx.request.environ["werkzeug.request"] = None
2019-11-28 11:40:45 +00:00
if app_ctx is not None:
app_ctx.pop(exc)
2022-11-07 18:06:49 +00:00
if ctx is not self:
raise AssertionError(
f"Popped wrong request context. ({ctx!r} instead of {self!r})"
)
2019-11-28 11:40:45 +00:00
def __enter__(self) -> "RequestContext":
2019-11-28 11:40:45 +00:00
self.push()
return self
def __exit__(
2022-11-07 18:06:49 +00:00
self,
exc_type: t.Optional[type],
exc_value: t.Optional[BaseException],
tb: t.Optional[TracebackType],
) -> None:
2022-11-07 18:06:49 +00:00
self.pop(exc_value)
2019-11-28 11:40:45 +00:00
def __repr__(self) -> str:
return (
f"<{type(self).__name__} {self.request.url!r}"
f" [{self.request.method}] of {self.app.name}>"
2019-11-28 11:40:45 +00:00
)