Skip to content

Commit e668e6f

Browse files
Use AppKey (#505)
1 parent 45e0dca commit e668e6f

File tree

7 files changed

+63
-36
lines changed

7 files changed

+63
-36
lines changed

aiohttp_debugtoolbar/main.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import secrets
22
from pathlib import Path
3-
from typing import Iterable, Literal, Sequence, Tuple, Type, TypedDict, Union
3+
from typing import Iterable, Literal, Sequence, Type, TypedDict, Union
44

55
import aiohttp_jinja2
66
import jinja2
@@ -11,10 +11,12 @@
1111
from .panels.base import DebugPanel
1212
from .utils import (
1313
APP_KEY,
14+
AppState,
1415
ExceptionHistory,
1516
STATIC_ROUTE_NAME,
1617
TEMPLATE_KEY,
1718
ToolbarStorage,
19+
_Config,
1820
)
1921
from .views import ExceptionDebugView
2022

@@ -35,21 +37,6 @@
3537
)
3638

3739

38-
class _Config(TypedDict):
39-
enabled: bool
40-
intercept_exc: Literal["debug", "display", False]
41-
intercept_redirects: bool
42-
panels: Tuple[Type[DebugPanel], ...]
43-
extra_panels: Tuple[Type[DebugPanel], ...]
44-
global_panels: Tuple[Type[DebugPanel], ...]
45-
hosts: Sequence[str]
46-
exclude_prefixes: Tuple[str, ...]
47-
check_host: bool
48-
button_style: str
49-
max_visible_requests: int
50-
path_prefix: str
51-
52-
5340
class _AppDetails(TypedDict):
5441
exc_history: ExceptionHistory
5542
pdtb_token: str
@@ -90,7 +77,6 @@ def setup(
9077
path_prefix=path_prefix,
9178
)
9279

93-
app[APP_KEY] = {"settings": config}
9480
if middleware not in app.middlewares:
9581
app.middlewares.append(middleware)
9682

@@ -151,8 +137,13 @@ def setup(
151137
"GET", path_prefix, views.request_view, name="debugtoolbar.main"
152138
)
153139

154-
app[APP_KEY]["request_history"] = ToolbarStorage(max_request_history)
155-
app[APP_KEY]["exc_history"] = ExceptionHistory()
156-
app[APP_KEY]["pdtb_token"] = secrets.token_hex(10)
140+
app[APP_KEY] = AppState(
141+
{
142+
"exc_history": ExceptionHistory(),
143+
"pdtb_token": secrets.token_hex(10),
144+
"request_history": ToolbarStorage(max_request_history),
145+
"settings": config,
146+
}
147+
)
157148
if intercept_exc:
158149
app[APP_KEY]["exc_history"].eval_exc = intercept_exc == "debug"

aiohttp_debugtoolbar/middlewares.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import aiohttp_jinja2
44
from aiohttp import web
55
from aiohttp.typedefs import Handler
6-
from aiohttp.web_exceptions import _HTTPMove as HTTPMove
6+
from aiohttp.web_exceptions import HTTPMove
77

88
from .tbtools.tbtools import get_traceback
99
from .toolbar import DebugToolbar

aiohttp_debugtoolbar/utils.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
import sys
55
from collections import deque
66
from itertools import islice
7+
from typing import Literal, Sequence, TYPE_CHECKING, Tuple, Type, TypedDict
78

8-
APP_KEY = "aiohttp_debugtoolbar"
9-
TEMPLATE_KEY = "aiohttp_debugtoolbar_jinja2"
9+
import jinja2
10+
from aiohttp.web import AppKey
11+
12+
if TYPE_CHECKING: # pragma: no cover
13+
from .panels.base import DebugPanel
14+
else:
15+
DebugPanel = None
1016

1117
REDIRECT_CODES = (300, 301, 302, 303, 305, 307, 308)
1218
STATIC_PATH = "static/"
@@ -48,6 +54,32 @@ def __init__(self):
4854
self.eval_exc = "show"
4955

5056

57+
class _Config(TypedDict):
58+
enabled: bool
59+
intercept_exc: Literal["debug", "display", False]
60+
intercept_redirects: bool
61+
panels: Tuple[Type[DebugPanel], ...]
62+
extra_panels: Tuple[Type[DebugPanel], ...]
63+
global_panels: Tuple[Type[DebugPanel], ...]
64+
hosts: Sequence[str]
65+
exclude_prefixes: Tuple[str, ...]
66+
check_host: bool
67+
button_style: str
68+
max_visible_requests: int
69+
path_prefix: str
70+
71+
72+
class AppState(TypedDict):
73+
exc_history: ExceptionHistory
74+
pdtb_token: str
75+
request_history: ToolbarStorage
76+
settings: _Config
77+
78+
79+
APP_KEY = AppKey("APP_KEY", AppState)
80+
TEMPLATE_KEY = AppKey("TEMPLATE_KEY", jinja2.Environment)
81+
82+
5183
def addr_in(addr, hosts):
5284
for host in hosts:
5385
if ipaddress.ip_address(addr) in ipaddress.ip_network(host):

examples/extra_panels/server.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121

2222
PATH_PARENT = pathlib.Path(__file__).parent
2323

24+
db_key = web.AppKey["aiopg.Pool"]("db_key")
25+
redis_key = web.AppKey["aioredis.Redis"]("redis_key")
26+
2427

2528
@aiohttp_jinja2.template("index.html")
2629
async def basic_handler(request):
2730
# testing for PgSQL
2831
if "db" in request.app:
29-
conn = await request.app["db"].acquire()
32+
conn = await request.app[db_key].acquire()
3033
cur = await conn.cursor()
3134

3235
await cur.execute("SELECT 1")
@@ -35,11 +38,11 @@ async def basic_handler(request):
3538
ret.append(row)
3639
assert ret == [(1,)] # noqa: S101
3740

38-
await request.app["db"].release(conn)
41+
await request.app[db_key].release(conn)
3942

4043
# testing for Redis
4144
if "redis" in request.app:
42-
with await request.app["redis"] as redis:
45+
with await request.app[redis_key] as redis:
4346
await redis.set("TEST", "VAR", expire=5)
4447
assert b"VAR" == (await redis.get("TEST")) # noqa: S101
4548

@@ -55,13 +58,13 @@ async def exception_handler(request):
5558

5659

5760
async def close_pg(app):
58-
app["db"].close()
59-
await app["db"].wait_closed()
61+
app[db_key].close()
62+
await app[db_key].wait_closed()
6063

6164

6265
async def close_redis(app):
63-
app["redis"].close()
64-
await app["redis"].wait_closed()
66+
app[redis_key].close()
67+
await app[redis_key].wait_closed()
6568

6669

6770
async def init():
@@ -106,13 +109,13 @@ async def init():
106109
dsn = "host={host} dbname={db} user={user} password={passw} ".format(
107110
db="postgres", user="developer", passw="1", host="localhost"
108111
)
109-
app["db"] = await aiopg.create_pool(dsn, minsize=1, maxsize=2)
112+
app[db_key] = await aiopg.create_pool(dsn, minsize=1, maxsize=2)
110113
# Correct PostgreSQL shutdown
111114
app.on_cleanup.append(close_pg)
112115

113116
if "aioredis" in sys.modules:
114117
# create redis pool
115-
app["redis"] = await aioredis.Redis("127.0.0.1", 6379)
118+
app[redis_key] = await aioredis.Redis()
116119
# Correct Redis shutdown
117120
app.on_cleanup.append(close_redis)
118121

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-e .
22

3-
aiohttp==3.8.5
4-
aiohttp-jinja2==1.5.1
3+
aiohttp==3.9.0
4+
aiohttp-jinja2==1.6
55
aiohttp-mako==0.4.0
66
aioredis==2.0.1
77
coverage==7.2.7

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def read(fname):
3838
license="Apache 2",
3939
packages=find_packages(),
4040
install_requires=(
41-
"aiohttp>=3.8",
41+
"aiohttp>=3.9",
4242
"aiohttp_jinja2",
4343
),
4444
include_package_data=True,

tests/test_panel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import aiohttp_jinja2
44

55
from aiohttp_debugtoolbar.panels.base import DebugPanel
6+
from aiohttp_debugtoolbar.utils import TEMPLATE_KEY
67

78

89
async def test_request_vars_panel(create_server, aiohttp_client):
@@ -63,7 +64,7 @@ async def process_response(self, response):
6364
assert "pDebugToolbarHandle" in txt
6465

6566
# check template from extra_templates
66-
assert "test.jinja2" in app["aiohttp_debugtoolbar_jinja2"].list_templates()
67+
assert "test.jinja2" in app[TEMPLATE_KEY].list_templates()
6768

6869
# make sure that debug toolbar page working and extra panel exists
6970
resp = await client.get("/_debugtoolbar")

0 commit comments

Comments
 (0)