-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathterminal.py
More file actions
69 lines (55 loc) · 2.28 KB
/
terminal.py
File metadata and controls
69 lines (55 loc) · 2.28 KB
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
from __future__ import annotations
from contextlib import suppress
from .connection import Connection
from .meta import CLIENT_METHODS
from .schema import (
KillTerminalCommandResponse,
ReleaseTerminalResponse,
TerminalOutputResponse,
WaitForTerminalExitResponse,
)
__all__ = ["TerminalHandle"]
class TerminalHandle:
def __init__(self, terminal_id: str, session_id: str, conn: Connection) -> None:
self.id = terminal_id
self._session_id = session_id
self._conn = conn
@property
def terminal_id(self) -> str:
return self.id
async def current_output(self) -> TerminalOutputResponse:
response = await self._conn.send_request(
CLIENT_METHODS["terminal_output"],
{"sessionId": self._session_id, "terminalId": self.id},
)
return TerminalOutputResponse.model_validate(response)
async def wait_for_exit(self) -> WaitForTerminalExitResponse:
response = await self._conn.send_request(
CLIENT_METHODS["terminal_wait_for_exit"],
{"sessionId": self._session_id, "terminalId": self.id},
)
return WaitForTerminalExitResponse.model_validate(response)
async def kill(self) -> KillTerminalCommandResponse:
response = await self._conn.send_request(
CLIENT_METHODS["terminal_kill"],
{"sessionId": self._session_id, "terminalId": self.id},
)
payload = response if isinstance(response, dict) else {}
return KillTerminalCommandResponse.model_validate(payload)
async def release(self) -> ReleaseTerminalResponse:
response = await self._conn.send_request(
CLIENT_METHODS["terminal_release"],
{"sessionId": self._session_id, "terminalId": self.id},
)
payload = response if isinstance(response, dict) else {}
return ReleaseTerminalResponse.model_validate(payload)
async def aclose(self) -> None:
"""Release the terminal, ignoring errors that occur during shutdown."""
with suppress(Exception):
await self.release()
async def close(self) -> None:
await self.aclose()
async def __aenter__(self) -> TerminalHandle:
return self
async def __aexit__(self, exc_type, exc, tb) -> None:
await self.aclose()