forked from Checkmk/checkmk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctx_stack.py
92 lines (63 loc) · 2.76 KB
/
ctx_stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.
from __future__ import annotations
from functools import partial
from typing import Any, Literal, TypeVar
from flask import g, session # pylint: disable=unused-import
from typing_extensions import assert_never
from werkzeug.local import LocalProxy
T = TypeVar("T")
def set_global_var(name: str, obj: Any) -> None:
setattr(g, name, obj)
def session_attr(
name: str | tuple[Literal["user"], Literal["transactions"]], type_class: type[T]
) -> T:
def get_attr_or_item(obj, key):
if hasattr(obj, key):
return getattr(obj, key)
try:
return obj[key]
except TypeError:
return None
def maybe_tuple_lookup(attr_names: tuple[str, ...]) -> T | None:
rv = session
for attr in attr_names:
rv = get_attr_or_item(rv, attr)
if rv is None:
return None
if not isinstance(rv, type_class):
raise ValueError(f"Object g.{'.'.join(attr_names)} is not of type {type_class}")
return rv
def maybe_str_lookup(_name: str) -> T | None:
return getattr(session, _name)
if isinstance(name, tuple): # pylint: disable=no-else-return
return LocalProxy(partial(maybe_tuple_lookup, name)) # type: ignore
if isinstance(name, str):
return LocalProxy(partial(maybe_str_lookup, name)) # type: ignore
assert_never(name)
# NOTE: Flask offers the proxies below, and we should go into that direction,
# too. But currently our html class is a swiss army knife with tons of
# responsibilities which we should really, really split up...
def request_local_attr(name: str, type_class: type[T]) -> T:
"""Delegate access to the corresponding attribute on RequestContext
When the returned object is accessed, the Proxy will fetch the current
RequestContext from the LocalStack and return the attribute given by `name`.
Args:
name (str): The name of the attribute on RequestContext
type_class (type): The type of the return value. No checking is done.
Returns:
A proxy which wraps the value stored on RequestContext.
"""
def maybe_str_lookup(_name: str) -> T | None:
try:
rv = getattr(g, _name)
except AttributeError:
return None
if rv is None:
return rv
if not isinstance(rv, type_class):
raise ValueError(f"Object g.{_name} ({rv!r}) is not of type {type_class}")
return rv
return LocalProxy(partial(maybe_str_lookup, name)) # type: ignore