-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathglobals.py
46 lines (36 loc) · 1.08 KB
/
globals.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
# !/usr/local/python/bin/python
# -*- coding: utf-8 -*-
# (C) Wu Dong, 2021
# All rights reserved
# @Author: 'Wu Dong <[email protected]>'
# @Time: '5/26/21 2:14 PM'
# sys
import typing as t
from functools import partial
# project
from .local import (
LocalProxy,
LocalStack
)
if t.TYPE_CHECKING:
from .app import Mask # pylint: disable=unused-import
def _lookup_req_object(name):
top = _request_ctx_stack.top
if top is None:
raise RuntimeError("Working outside of request context")
return getattr(top, name)
def _lookup_app_object(name):
top = _app_ctx_stack.top
if top is None:
raise RuntimeError("Working outside of application context")
return getattr(top, name)
def _find_app():
top = _app_ctx_stack.top
if top is None:
raise RuntimeError("Working outside of application context")
return top.app
_request_ctx_stack = LocalStack()
_app_ctx_stack = LocalStack()
current_app: "Mask" = LocalProxy(_find_app)
g: dict = LocalProxy(partial(_lookup_app_object, "g"))
request = LocalProxy(partial(_lookup_req_object, "request"))