-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathtemporary.py
More file actions
252 lines (197 loc) · 6.14 KB
/
temporary.py
File metadata and controls
252 lines (197 loc) · 6.14 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""Temporary object helpers for libtmux and downstream libtmux libraries."""
from __future__ import annotations
import contextlib
import logging
import typing as t
from libtmux.test.random import get_test_session_name, get_test_window_name
logger = logging.getLogger(__name__)
if t.TYPE_CHECKING:
import sys
from typing_extensions import Unpack
from libtmux._internal.types import StrPath
from libtmux.constants import WindowDirection
from libtmux.server import Server
from libtmux.session import Session
from libtmux.window import Window
if sys.version_info >= (3, 11):
pass
class TempSessionParams(t.TypedDict, total=False):
"""Keyword arguments for :func:`temp_session`."""
session_name: str | None
kill_session: bool
attach: bool
start_directory: StrPath | None
window_name: str | None
window_command: str | None
x: int | t.Literal["-"] | None
y: int | t.Literal["-"] | None
environment: dict[str, str] | None
class TempSessionKwargs(t.TypedDict, total=False):
"""Keyword arguments forwarded to :meth:`Server.new_session`."""
kill_session: bool
attach: bool
start_directory: StrPath | None
window_name: str | None
window_command: str | None
x: int | t.Literal["-"] | None
y: int | t.Literal["-"] | None
environment: dict[str, str] | None
class TempWindowParams(t.TypedDict, total=False):
"""Keyword arguments for :func:`temp_window`."""
window_name: str | None
start_directory: StrPath | None
attach: bool
window_index: str
window_shell: str | None
environment: dict[str, str] | None
direction: WindowDirection | None
target_window: str | None
class TempWindowKwargs(t.TypedDict, total=False):
"""Keyword arguments forwarded to :meth:`Session.new_window`."""
start_directory: StrPath | None
attach: bool
window_index: str
window_shell: str | None
environment: dict[str, str] | None
direction: WindowDirection | None
target_window: str | None
@contextlib.contextmanager
def _temp_session(
server: Server,
*args: t.Any,
**kwargs: object,
) -> t.Iterator[Session]:
kwargs_typed = t.cast("TempSessionParams", dict(kwargs))
if "session_name" in kwargs_typed:
session_name = kwargs_typed["session_name"]
else:
session_name = get_test_session_name(server)
kwargs_no_name = t.cast(
"TempSessionKwargs",
{k: v for k, v in kwargs_typed.items() if k != "session_name"},
)
session = server.new_session(
session_name,
*args,
**kwargs_no_name,
)
try:
yield session
finally:
if isinstance(session_name, str) and server.has_session(session_name):
session.kill()
@t.overload
def temp_session(
server: Server,
**kwargs: Unpack[TempSessionParams],
) -> contextlib.AbstractContextManager[Session]: ...
@t.overload
def temp_session(
server: Server,
*args: t.Any,
**kwargs: object,
) -> contextlib.AbstractContextManager[Session]: ...
def temp_session(
server: Server,
*args: t.Any,
**kwargs: object,
) -> contextlib.AbstractContextManager[Session]:
"""
Return a context manager with a temporary session.
If no ``session_name`` is entered, :func:`get_test_session_name` will make
an unused session name.
The session will destroy itself upon closing with :meth:`Session.session()`.
Parameters
----------
server : :class:`libtmux.Server`
Other Parameters
----------------
args : list
Arguments passed into :meth:`Server.new_session`
kwargs : dict
Keyword arguments passed into :meth:`Server.new_session`
Yields
------
:class:`libtmux.Session`
Temporary session
Examples
--------
>>> with temp_session(server) as session:
... session.new_window(window_name='my window')
Window(@3 2:my window, Session($... ...))
"""
return _temp_session(server, *args, **kwargs)
@contextlib.contextmanager
def _temp_window(
session: Session,
*args: t.Any,
**kwargs: object,
) -> t.Iterator[Window]:
kwargs_typed = t.cast("TempWindowParams", dict(kwargs))
if "window_name" in kwargs_typed:
window_name = kwargs_typed["window_name"]
else:
window_name = get_test_window_name(session)
kwargs_no_name = t.cast(
"TempWindowKwargs",
{k: v for k, v in kwargs_typed.items() if k != "window_name"},
)
window = session.new_window(
window_name,
*args,
**kwargs_no_name,
)
# Get ``window_id`` before returning it, it may be killed within context.
window_id = window.window_id
assert window_id is not None
assert isinstance(window_id, str)
try:
yield window
finally:
if len(session.windows.filter(window_id=window_id)) > 0:
window.kill()
@t.overload
def temp_window(
session: Session,
**kwargs: Unpack[TempWindowParams],
) -> contextlib.AbstractContextManager[Window]: ...
@t.overload
def temp_window(
session: Session,
*args: t.Any,
**kwargs: object,
) -> contextlib.AbstractContextManager[Window]: ...
def temp_window(
session: Session,
*args: t.Any,
**kwargs: object,
) -> contextlib.AbstractContextManager[Window]:
"""
Return a context manager with a temporary window.
The window will destroy itself upon closing with :meth:`window.
kill()`.
If no ``window_name`` is entered, :func:`get_test_window_name` will make
an unused window name.
Parameters
----------
session : :class:`libtmux.Session`
Other Parameters
----------------
args : list
Arguments passed into :meth:`Session.new_window`
kwargs : dict
Keyword arguments passed into :meth:`Session.new_window`
Yields
------
:class:`libtmux.Window`
temporary window
Examples
--------
>>> with temp_window(session) as window:
... window
Window(@2 2:... Session($1 libtmux_...))
>>> with temp_window(session) as window:
... window.split()
Pane(%4 Window(@3 2:libtmux_..., Session($1 libtmux_...)))
"""
return _temp_window(session, *args, **kwargs)