Skip to content

Commit bd89e82

Browse files
authored
Server: Add __repr__, __eq__, socket_path (#463)
2 parents 1dd6c2e + 2609f43 commit bd89e82

File tree

7 files changed

+59
-12
lines changed

7 files changed

+59
-12
lines changed

CHANGES

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ $ pip install --user --upgrade --pre libtmux
1414

1515
<!-- Maintainers and contributors: Insert change notes for the next release above -->
1616

17+
### Breaking
18+
- Server: Add `__repr__` and set `socket_path` if none set.
19+
20+
Before (0.17 and below):
21+
22+
```python
23+
libtmux.server.Server object at ...>
24+
```
25+
26+
New `__repr__` (0.18+):
27+
28+
```python
29+
Server(socket_name=test)
30+
```
31+
32+
```python
33+
Server(socket_path=/tmp/tmux-1000/default)
34+
```
35+
1736
## libtmux 0.17.2 (2022-12-27)
1837

1938
- Server: Move `_list_panes` and `_update_panes` to deprecated

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Connect to a live tmux session:
5555
>>> import libtmux
5656
>>> s = libtmux.Server()
5757
>>> s
58-
<libtmux.server.Server object at ...>
58+
Server(socket_path=/tmp/tmux-.../default)
5959
```
6060

6161
Tip: You can also use [tmuxp]'s [`tmuxp shell`] to drop straight into your

docs/quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ First, we can grab a {class}`Server`.
9898
>>> import libtmux
9999
>>> server = libtmux.Server()
100100
>>> server
101-
<libtmux.server.Server object at ...>
101+
Server(socket_path=/tmp/tmux-.../default)
102102
```
103103

104104
:::{tip}

docs/reference/properties.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Attach default tmux {class}`~libtmux.Server` to `t`:
3333
>>> import libtmux
3434
>>> t = libtmux.Server()
3535
>>> t
36-
<libtmux.server.Server object at ...>
36+
Server(socket_path=/tmp/tmux-.../default)
3737
```
3838

3939
## Session

docs/topics/traversal.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Attach default tmux {class}`~libtmux.Server` to `t`:
3434
>>> import libtmux
3535
>>> t = libtmux.Server();
3636
>>> t
37-
<libtmux.server.Server object at ...>
37+
Server(socket_path=/tmp/tmux-.../default)
3838
```
3939

4040
Get first session {class}`~libtmux.Session` to `session`:
@@ -96,7 +96,7 @@ Access the window/server of a pane:
9696
Window(@1 ...:..., Session($1 ...))
9797

9898
>>> p.server
99-
<libtmux.server.Server object at ...>
99+
Server(socket_name=libtmux_test...)
100100
```
101101

102102
[target]: http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#COMMANDS

src/libtmux/pytest_plugin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ def server(
133133
134134
>>> result.assert_outcomes(passed=1)
135135
"""
136-
t = Server()
137-
t.socket_name = "libtmux_test%s" % next(namer)
136+
t = Server(socket_name="libtmux_test%s" % next(namer))
138137

139138
def fin() -> None:
140139
t.kill_server()

src/libtmux/server.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
import logging
88
import os
9+
import pathlib
910
import shutil
1011
import subprocess
1112
import typing as t
@@ -55,7 +56,7 @@ class Server(EnvironmentMixin):
5556
Examples
5657
--------
5758
>>> server
58-
<libtmux.server.Server object at ...>
59+
Server(socket_name=libtmux_test...)
5960
6061
>>> server.sessions
6162
[Session($1 ...)]
@@ -99,7 +100,7 @@ class Server(EnvironmentMixin):
99100
def __init__(
100101
self,
101102
socket_name: t.Optional[str] = None,
102-
socket_path: t.Optional[str] = None,
103+
socket_path: t.Optional[t.Union[str, pathlib.Path]] = None,
103104
config_file: t.Optional[str] = None,
104105
colors: t.Optional[int] = None,
105106
**kwargs: t.Any,
@@ -108,11 +109,19 @@ def __init__(
108109
self._windows: t.List[WindowDict] = []
109110
self._panes: t.List[PaneDict] = []
110111

111-
if socket_name:
112+
if socket_path is not None:
113+
self.socket_path = socket_path
114+
elif socket_name is not None:
112115
self.socket_name = socket_name
113116

114-
if socket_path:
115-
self.socket_path = socket_path
117+
tmux_tmpdir = pathlib.Path(os.getenv("TMUX_TMPDIR", "/tmp"))
118+
socket_name = self.socket_name or "default"
119+
if (
120+
tmux_tmpdir is not None
121+
and self.socket_path is None
122+
and self.socket_name is None
123+
):
124+
self.socket_path = str(tmux_tmpdir / f"tmux-{os.geteuid()}" / socket_name)
116125

117126
if config_file:
118127
self.config_file = config_file
@@ -531,6 +540,26 @@ def panes(self) -> QueryList[Pane]: # type:ignore
531540

532541
return QueryList(panes)
533542

543+
#
544+
# Dunder
545+
#
546+
def __eq__(self, other: object) -> bool:
547+
assert isinstance(other, Server)
548+
return (
549+
self.socket_name == other.socket_name
550+
and self.socket_path == other.socket_path
551+
)
552+
553+
def __repr__(self) -> str:
554+
if self.socket_name is not None:
555+
return (
556+
f"{self.__class__.__name__}"
557+
f"(socket_name={getattr(self, 'socket_name')})"
558+
)
559+
return (
560+
f"{self.__class__.__name__}" f"(socket_path={getattr(self, 'socket_path')})"
561+
)
562+
534563
#
535564
# Legacy: Redundant stuff we want to remove
536565
#

0 commit comments

Comments
 (0)