|
| 1 | +(engines)= |
| 2 | + |
| 3 | +# Engines |
| 4 | + |
| 5 | +Every tmux command libtmux runs goes through an **engine**. An engine takes a |
| 6 | +rendered argv and returns a structured result — that is its whole job. |
| 7 | + |
| 8 | +By default that engine is |
| 9 | +{class}`~libtmux.engines.subprocess.SubprocessEngine`, which forks the tmux |
| 10 | +binary once per command. You never have to know it exists. But because it is a |
| 11 | +seam rather than hard-wired code, you can replace it — to test without tmux |
| 12 | +running, to record what libtmux would do, or to point one `Server` at a |
| 13 | +different tmux binary than another. |
| 14 | + |
| 15 | +## The default path |
| 16 | + |
| 17 | +Nothing changes if you ignore engines entirely: |
| 18 | + |
| 19 | +```python |
| 20 | +>>> server.cmd("display-message", "-p", "#{session_name}").stdout |
| 21 | +['libtmux_...'] |
| 22 | +``` |
| 23 | + |
| 24 | +Under that call, {class}`~libtmux.Server` built a |
| 25 | +{class}`~libtmux.engines.connection.ServerConnection` from its own |
| 26 | +`socket_name`, `socket_path`, `config_file`, and `colors`, handed it to a |
| 27 | +`SubprocessEngine`, and asked the engine to run the command: |
| 28 | + |
| 29 | +```python |
| 30 | +>>> from libtmux.engines import SubprocessEngine |
| 31 | +>>> server.connection.args |
| 32 | +('-L...',) |
| 33 | +>>> isinstance(server.engine, SubprocessEngine) |
| 34 | +True |
| 35 | +``` |
| 36 | + |
| 37 | +The connection is *derived*, not frozen at construction, so moving a server to a |
| 38 | +different socket is picked up on the next command: |
| 39 | + |
| 40 | +```python |
| 41 | +>>> from libtmux.server import Server |
| 42 | +>>> tmux = Server(socket_name="engines_doc_a") |
| 43 | +>>> tmux.connection.args |
| 44 | +('-Lengines_doc_a',) |
| 45 | +>>> tmux.socket_name = "engines_doc_b" |
| 46 | +>>> tmux.connection.args |
| 47 | +('-Lengines_doc_b',) |
| 48 | +``` |
| 49 | + |
| 50 | +## Requests and results |
| 51 | + |
| 52 | +An engine speaks two value types. |
| 53 | +{class}`~libtmux.engines.base.CommandRequest` is the argv *after* the binary and |
| 54 | +connection flags. {class}`~libtmux.engines.base.CommandResult` is what came |
| 55 | +back. |
| 56 | + |
| 57 | +```python |
| 58 | +>>> from libtmux.engines import CommandRequest |
| 59 | +>>> CommandRequest.from_args("kill-window", "-t", 2) |
| 60 | +CommandRequest(args=('kill-window', '-t', '2'), tmux_bin=None) |
| 61 | +``` |
| 62 | + |
| 63 | +A tmux-side failure is **data**, not an exception. An engine sets `returncode` |
| 64 | +and `stderr`; it does not raise. Only an engine-broken condition — a missing |
| 65 | +binary, a dropped connection — raises: |
| 66 | + |
| 67 | +```python |
| 68 | +>>> from libtmux.engines import CommandResult |
| 69 | +>>> result = CommandResult( |
| 70 | +... cmd=("tmux", "kill-window"), |
| 71 | +... stderr=("no such window",), |
| 72 | +... returncode=1, |
| 73 | +... ) |
| 74 | +>>> result.returncode, result.stderr |
| 75 | +(1, ('no such window',)) |
| 76 | +``` |
| 77 | + |
| 78 | +## Writing an engine |
| 79 | + |
| 80 | +{class}`~libtmux.engines.base.TmuxEngine` is a {class}`typing.Protocol`. There |
| 81 | +is no base class to inherit — any object with `run()` and `run_batch()` is an |
| 82 | +engine. |
| 83 | + |
| 84 | +Here is a complete one that runs nothing, records everything, and answers from a |
| 85 | +canned script. Hand it to a server and no tmux process is involved: |
| 86 | + |
| 87 | +```python |
| 88 | +>>> from libtmux.engines import CommandResult |
| 89 | +>>> from libtmux.server import Server |
| 90 | + |
| 91 | +>>> class RecordingEngine: |
| 92 | +... """Record every dispatch; answer from a canned script.""" |
| 93 | +... |
| 94 | +... def __init__(self, stdout=()): |
| 95 | +... self.requests = [] |
| 96 | +... self._stdout = tuple(stdout) |
| 97 | +... |
| 98 | +... def run(self, request): |
| 99 | +... self.requests.append(request.args) |
| 100 | +... return CommandResult(cmd=("tmux", *request.args), stdout=self._stdout) |
| 101 | +... |
| 102 | +... def run_batch(self, requests): |
| 103 | +... return [self.run(request) for request in requests] |
| 104 | + |
| 105 | +>>> recorder = RecordingEngine(stdout=("my_session",)) |
| 106 | +>>> offline = Server(engine=recorder) |
| 107 | +>>> offline.cmd("display-message", "-p", "#{session_name}").stdout |
| 108 | +['my_session'] |
| 109 | +>>> recorder.requests |
| 110 | +[('display-message', '-p', '#{session_name}')] |
| 111 | +``` |
| 112 | + |
| 113 | +This works because the socket flags live on the *engine*, not in the request, so |
| 114 | +your `run()` only ever sees the tmux subcommand — never a `-L` to parse back |
| 115 | +out: |
| 116 | + |
| 117 | +```python |
| 118 | +>>> from libtmux.engines import CommandResult |
| 119 | +>>> from libtmux.server import Server |
| 120 | + |
| 121 | +>>> class Recorder: |
| 122 | +... def __init__(self): |
| 123 | +... self.requests = [] |
| 124 | +... def run(self, request): |
| 125 | +... self.requests.append(request.args) |
| 126 | +... return CommandResult(cmd=("tmux", *request.args)) |
| 127 | +... def run_batch(self, requests): |
| 128 | +... return [self.run(request) for request in requests] |
| 129 | + |
| 130 | +>>> recorder = Recorder() |
| 131 | +>>> _ = Server(socket_name="engines_doc_scoped", engine=recorder).cmd("list-sessions") |
| 132 | +>>> recorder.requests |
| 133 | +[('list-sessions',)] |
| 134 | +``` |
| 135 | + |
| 136 | +## Injected engines and sockets |
| 137 | + |
| 138 | +An engine that names no tmux server of its own **adopts** the server's |
| 139 | +connection. Without that rule, injecting a bare engine into a socket-scoped |
| 140 | +server would silently dispatch to whichever server a flagless `tmux` reaches: |
| 141 | + |
| 142 | +```python |
| 143 | +>>> from libtmux.engines import SubprocessEngine |
| 144 | +>>> from libtmux.server import Server |
| 145 | +>>> scoped = Server(socket_name="engines_doc_c", engine=SubprocessEngine()) |
| 146 | +>>> scoped.engine.server_args |
| 147 | +('-Lengines_doc_c',) |
| 148 | +``` |
| 149 | + |
| 150 | +An engine that *does* name a server is left exactly as you built it: |
| 151 | + |
| 152 | +```python |
| 153 | +>>> from libtmux.engines import SubprocessEngine |
| 154 | +>>> from libtmux.server import Server |
| 155 | +>>> pinned = SubprocessEngine.of(server_args=("-Lengines_doc_pinned",)) |
| 156 | +>>> Server(socket_name="engines_doc_c", engine=pinned).engine.server_args |
| 157 | +('-Lengines_doc_pinned',) |
| 158 | +``` |
| 159 | + |
| 160 | +An in-memory engine has no connection at all, so neither rule applies and it is |
| 161 | +used untouched. |
| 162 | + |
| 163 | +## Optional capabilities |
| 164 | + |
| 165 | +An engine may implement extra protocols. Each is optional; libtmux checks with |
| 166 | +{func}`isinstance` and degrades gracefully when absent. |
| 167 | + |
| 168 | +{class}`~libtmux.engines.base.SupportsCommandLine` renders the argv an engine |
| 169 | +*would* run, which is how the full command line reaches the debug log before |
| 170 | +dispatch. {class}`~libtmux.engines.base.SupportsConnection` marks an engine that |
| 171 | +dispatches over a named server and can be rebound — the protocol behind the |
| 172 | +adoption rule above. |
| 173 | + |
| 174 | +```python |
| 175 | +>>> from libtmux.engines import ( |
| 176 | +... SubprocessEngine, |
| 177 | +... SupportsCommandLine, |
| 178 | +... SupportsConnection, |
| 179 | +... ) |
| 180 | +>>> engine = SubprocessEngine() |
| 181 | +>>> isinstance(engine, SupportsCommandLine), isinstance(engine, SupportsConnection) |
| 182 | +(True, True) |
| 183 | +``` |
| 184 | + |
| 185 | +An engine that implements neither simply is not matched: |
| 186 | + |
| 187 | +```python |
| 188 | +>>> from libtmux.engines import CommandResult, SupportsCommandLine |
| 189 | +>>> class Bare: |
| 190 | +... def run(self, request): |
| 191 | +... return CommandResult(cmd=("tmux", *request.args)) |
| 192 | +... def run_batch(self, requests): |
| 193 | +... return [self.run(request) for request in requests] |
| 194 | +>>> isinstance(Bare(), SupportsCommandLine) |
| 195 | +False |
| 196 | +``` |
| 197 | + |
| 198 | +## What an engine does not change |
| 199 | + |
| 200 | +An engine chooses *how* a command runs, not what libtmux does with the answer. |
| 201 | +Arguments reach tmux exactly as they always have, results read exactly as they |
| 202 | +always have, and {meth}`Server.cmd() <libtmux.Server.cmd>` still returns a |
| 203 | +{class}`~libtmux.common.tmux_cmd`. Under the default engine there is nothing new |
| 204 | +to learn and nothing to migrate. |
0 commit comments