1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/context.py
Matt Griswold 5147028bee clean up / format / poetry (#1000)
* stub in poetry for pipenv

* re-add tester image

* add pre-commit / formatting

* fix ghactions

* revert test data whitespace, exclude tests/data

* revert ws

* decruft, rm tox/pipenv

* install dev packages for base image

* add lgtm config to force to py3
2021-07-10 10:12:35 -05:00

30 lines
649 B
Python

import contextvars
from contextlib import contextmanager
# stores current request in a thread safe context aware
# manner.
_current_request = contextvars.ContextVar("current_request")
@contextmanager
def current_request(request=None):
"""
Will yield the current request, if there is one.
To se the current request for the context pass it to
the request parameter.
"""
if request:
token = _current_request.set(request)
else:
token = None
try:
yield _current_request.get()
except LookupError:
yield None
finally:
if token:
_current_request.reset(token)