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