-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.py
89 lines (66 loc) · 2.3 KB
/
server.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
from __future__ import annotations
import asyncio
from aiohttp.web import run_app
from aiohttp.web_app import Application
from aiohttp.web_request import Request
from aiohttp.web_routedef import get
from aiohttp.web_ws import WebSocketResponse
from textual.constants import DEVTOOLS_PORT
from .service import DevtoolsService
DEFAULT_SIZE_CHANGE_POLL_DELAY_SECONDS = 2
async def websocket_handler(request: Request) -> WebSocketResponse:
"""aiohttp websocket handler for sending data between devtools client and server
Args:
request: The request to the websocket endpoint
Returns:
The websocket response
"""
service: DevtoolsService = request.app["service"]
return await service.handle(request)
async def _on_shutdown(app: Application) -> None:
"""aiohttp shutdown handler, called when the aiohttp server is stopped"""
service: DevtoolsService = app["service"]
await service.shutdown()
async def _on_startup(app: Application) -> None:
service: DevtoolsService = app["service"]
await service.start()
def _run_devtools(
verbose: bool, exclude: list[str] | None = None, port: int | None = None
) -> None:
app = _make_devtools_aiohttp_app(port=port, verbose=verbose, exclude=exclude)
def noop_print(_: str) -> None:
pass
try:
run_app(
app,
port=DEVTOOLS_PORT if port is None else port,
print=noop_print,
loop=asyncio.get_event_loop(),
)
except OSError:
from rich import print
print()
print("[bold red]Couldn't start server")
print("Is there another instance of [reverse]textual console[/] running?")
def _make_devtools_aiohttp_app(
size_change_poll_delay_secs: float = DEFAULT_SIZE_CHANGE_POLL_DELAY_SECONDS,
port: int | None = None,
verbose: bool = False,
exclude: list[str] | None = None,
) -> Application:
app = Application()
app.on_shutdown.append(_on_shutdown)
app.on_startup.append(_on_startup)
app["verbose"] = verbose
app["service"] = DevtoolsService(
update_frequency=size_change_poll_delay_secs,
port=port,
verbose=verbose,
exclude=exclude,
)
app.add_routes(
[
get("/textual-devtools-websocket", websocket_handler),
]
)
return app