replacer/utils.py

34 lines
1.1 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: AGPL-3.0-only
import random
import asyncio
import contextvars
from aiohttp import web
class ContextVar:
"""
contextvars.ContextVar, but you can call the var to get its value, rather than calling var.get()
"""
__slots__ = frozenset({'_var'})
def __init__(self, *a, **kw): self._var = contextvars.ContextVar(*a, **kw)
def __call__(self, *args): return self._var.get(*args) # convenience
def get(self, *args): return self._var.get(*args)
def set(self, *args): return self._var.set(*args)
def reset(self, *args): return self._var.reset(*args)
2021-09-08 02:51:40 -07:00
def build_dprint_factory(debug: bool):
if debug:
2021-09-08 02:51:40 -07:00
def dprint_factory():
# 8 byte pseudo-random request tag
2021-09-08 02:51:40 -07:00
req_id = ''.join(random.choices('0123456789abcdef', k=8 * 2))
def dprint(*args, **kwargs): print(f'[{req_id}]', *args, **kwargs)
return dprint
else:
2021-09-08 02:51:40 -07:00
def dprint(*args, **kwargs): return None
def dprint_factory(): return dprint
2021-09-08 02:51:40 -07:00
return dprint_factory
def make_unlimited_request(*args):
return web.BaseRequest(*args, loop=asyncio.get_running_loop(), client_max_size=None)