| 1 |
"""Temporary object helpers for libtmux and downstream libtmux libraries.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import contextlib |
| 6 |
import typing as t |
| 7 |
|
| 8 |
from libtmux.test.random import get_test_session_name, get_test_window_name |
| 9 |
|
| 10 |
if t.TYPE_CHECKING: |
| 11 |
import sys |
| 12 |
from collections.abc import Generator |
| 13 |
|
| 14 |
from libtmux.server import Server |
| 15 |
from libtmux.session import Session |
| 16 |
from libtmux.window import Window |
| 17 |
|
| 18 |
if sys.version_info >= (3, 11): |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
@contextlib.contextmanager |
| 23 |
def temp_session( |
| 24 |
server: Server, |
| 25 |
*args: t.Any, |
| 26 |
**kwargs: t.Any, |
| 27 |
) -> Generator[Session, t.Any, t.Any]: |
| 28 |
""" |
| 29 |
Return a context manager with a temporary session. |
| 30 |
|
| 31 |
If no ``session_name`` is entered, :func:`get_test_session_name` will make |
| 32 |
an unused session name. |
| 33 |
|
| 34 |
The session will destroy itself upon closing with :meth:`Session.session()`. |
| 35 |
|
| 36 |
Parameters |
| 37 |
---------- |
| 38 |
server : :class:`libtmux.Server` |
| 39 |
|
| 40 |
Other Parameters |
| 41 |
---------------- |
| 42 |
args : list |
| 43 |
Arguments passed into :meth:`Server.new_session` |
| 44 |
kwargs : dict |
| 45 |
Keyword arguments passed into :meth:`Server.new_session` |
| 46 |
|
| 47 |
Yields |
| 48 |
------ |
| 49 |
:class:`libtmux.Session` |
| 50 |
Temporary session |
| 51 |
|
| 52 |
Examples |
| 53 |
-------- |
| 54 |
>>> with temp_session(server) as session: |
| 55 |
... session.new_window(window_name='my window') |
| 56 |
Window(@3 2:my window, Session($... ...)) |
| 57 |
""" |
| 58 |
if "session_name" in kwargs: |
| 59 |
session_name = kwargs.pop("session_name") |
| 60 |
else: |
| 61 |
session_name = get_test_session_name(server) |
| 62 |
|
| 63 |
session = server.new_session(session_name, *args, **kwargs) |
| 64 |
|
| 65 |
try: |
| 66 |
yield session |
| 67 |
finally: |
| 68 |
if server.has_session(session_name): |
| 69 |
session.kill() |
| 70 |
return |
| 71 |
|
| 72 |
|
| 73 |
@contextlib.contextmanager |
| 74 |
def temp_window( |
| 75 |
session: Session, |
| 76 |
*args: t.Any, |
| 77 |
**kwargs: t.Any, |
| 78 |
) -> Generator[Window, t.Any, t.Any]: |
| 79 |
""" |
| 80 |
Return a context manager with a temporary window. |
| 81 |
|
| 82 |
The window will destroy itself upon closing with :meth:`window. |
| 83 |
kill()`. |
| 84 |
|
| 85 |
If no ``window_name`` is entered, :func:`get_test_window_name` will make |
| 86 |
an unused window name. |
| 87 |
|
| 88 |
Parameters |
| 89 |
---------- |
| 90 |
session : :class:`libtmux.Session` |
| 91 |
|
| 92 |
Other Parameters |
| 93 |
---------------- |
| 94 |
args : list |
| 95 |
Arguments passed into :meth:`Session.new_window` |
| 96 |
kwargs : dict |
| 97 |
Keyword arguments passed into :meth:`Session.new_window` |
| 98 |
|
| 99 |
Yields |
| 100 |
------ |
| 101 |
:class:`libtmux.Window` |
| 102 |
temporary window |
| 103 |
|
| 104 |
Examples |
| 105 |
-------- |
| 106 |
>>> with temp_window(session) as window: |
| 107 |
... window |
| 108 |
Window(@2 2:... Session($1 libtmux_...)) |
| 109 |
|
| 110 |
|
| 111 |
>>> with temp_window(session) as window: |
| 112 |
... window.split() |
| 113 |
Pane(%4 Window(@3 2:libtmux_..., Session($1 libtmux_...))) |
| 114 |
""" |
| 115 |
if "window_name" not in kwargs: |
| 116 |
window_name = get_test_window_name(session) |
| 117 |
else: |
| 118 |
window_name = kwargs.pop("window_name") |
| 119 |
|
| 120 |
window = session.new_window(window_name, *args, **kwargs) |
| 121 |
|
| 122 |
|
| 123 |
window_id = window.window_id |
| 124 |
assert window_id is not None |
| 125 |
assert isinstance(window_id, str) |
| 126 |
|
| 127 |
try: |
| 128 |
yield window |
| 129 |
finally: |
| 130 |
if len(session.windows.filter(window_id=window_id)) > 0: |
| 131 |
window.kill() |
| 132 |
return |