Skip to content

Commit 9bb796c

Browse files
committed
engine(refactor): Add ServerContext and Engine.bind()
Add ServerContext dataclass to capture server connection details (socket_name, socket_path, config_file) in an immutable container. Add Engine.bind() method called by Server.__init__ to provide connection details to engines. This enables engines to execute commands without requiring server_args on every hook call. Foundation for standardizing hook signatures in next step.
1 parent 2638148 commit 9bb796c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/libtmux/_internal/engines/base.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@
1313
from libtmux.session import Session
1414

1515

16+
@dataclasses.dataclass(frozen=True)
17+
class ServerContext:
18+
"""Immutable server connection context.
19+
20+
Passed to :meth:`Engine.bind` so engines can execute commands
21+
without requiring `server_args` on every hook call.
22+
"""
23+
24+
socket_name: str | None = None
25+
socket_path: str | None = None
26+
config_file: str | None = None
27+
28+
def to_args(self) -> tuple[str, ...]:
29+
"""Convert context to tmux server argument tuple."""
30+
args: list[str] = []
31+
if self.socket_name:
32+
args.extend(["-L", self.socket_name])
33+
if self.socket_path:
34+
args.extend(["-S", str(self.socket_path)])
35+
if self.config_file:
36+
args.extend(["-f", str(self.config_file)])
37+
return tuple(args)
38+
39+
1640
class ExitStatus(enum.Enum):
1741
"""Exit status returned by tmux control mode commands."""
1842

@@ -123,6 +147,22 @@ class Engine(ABC):
123147
a strong reason to override both.
124148
"""
125149

150+
_server_context: ServerContext | None = None
151+
152+
def bind(self, context: ServerContext) -> None:
153+
"""Bind engine to server context.
154+
155+
Called by :class:`Server.__init__` to provide connection details.
156+
Engines can use this to run commands without requiring ``server_args``
157+
on every hook call.
158+
159+
Parameters
160+
----------
161+
context : ServerContext
162+
Immutable server connection context.
163+
"""
164+
self._server_context = context
165+
126166
def run(
127167
self,
128168
cmd: str,

src/libtmux/server.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import warnings
1717

1818
from libtmux import exc, formats
19+
from libtmux._internal.engines.base import ServerContext
1920
from libtmux._internal.engines.subprocess_engine import SubprocessEngine
2021
from libtmux._internal.query_list import QueryList
2122
from libtmux.common import tmux_cmd
@@ -173,6 +174,15 @@ def __init__(
173174
if colors:
174175
self.colors = colors
175176

177+
# Bind engine to server context for hook calls
178+
self.engine.bind(
179+
ServerContext(
180+
socket_name=self.socket_name,
181+
socket_path=str(self.socket_path) if self.socket_path else None,
182+
config_file=self.config_file,
183+
),
184+
)
185+
176186
if on_init is not None:
177187
on_init(self)
178188

0 commit comments

Comments
 (0)