6
6
"""
7
7
import logging
8
8
import os
9
+ import shutil
10
+ import subprocess
9
11
import typing as t
10
12
11
13
from libtmux .common import tmux_cmd
@@ -115,6 +117,42 @@ def __init__(
115
117
if colors :
116
118
self .colors = colors
117
119
120
+ def is_alive (self ) -> bool :
121
+ """If server alive or not.
122
+
123
+ >>> tmux = Server(socket_name="no_exist")
124
+ >>> assert not tmux.is_alive()
125
+ """
126
+ try :
127
+ res = self .cmd ("list-sessions" )
128
+ return res .returncode == 0
129
+ except Exception :
130
+ return False
131
+
132
+ def raise_if_dead (self ) -> None :
133
+ """Raise if server not connected.
134
+
135
+ >>> tmux = Server(socket_name="no_exist")
136
+ >>> try:
137
+ ... tmux.raise_if_dead()
138
+ ... except Exception as e:
139
+ ... print(type(e))
140
+ <class 'subprocess.CalledProcessError'>
141
+ """
142
+ tmux_bin = shutil .which ("tmux" )
143
+ if tmux_bin is None :
144
+ raise exc .TmuxCommandNotFound ()
145
+
146
+ cmd_args : t .List [str ] = ["list-sessions" ]
147
+ if self .socket_name :
148
+ cmd_args .insert (0 , f"-L{ self .socket_name } " )
149
+ if self .socket_path :
150
+ cmd_args .insert (0 , f"-S{ self .socket_path } " )
151
+ if self .config_file :
152
+ cmd_args .insert (0 , f"-f{ self .config_file } " )
153
+
154
+ subprocess .check_call ([tmux_bin ] + cmd_args )
155
+
118
156
def cmd (self , * args : t .Any , ** kwargs : t .Any ) -> tmux_cmd :
119
157
"""
120
158
Execute tmux command and return output.
@@ -207,7 +245,10 @@ def list_sessions(self) -> t.List[Session]:
207
245
@property
208
246
def sessions (self ) -> t .List [Session ]:
209
247
"""Property / alias to return :meth:`~.list_sessions`."""
210
- return self .list_sessions ()
248
+ try :
249
+ return self .list_sessions ()
250
+ except Exception :
251
+ return []
211
252
212
253
#: Alias :attr:`sessions` for :class:`~libtmux.common.TmuxRelationalObject`
213
254
children = sessions # type: ignore
@@ -348,7 +389,7 @@ def _update_panes(self) -> "Server":
348
389
return self
349
390
350
391
@property
351
- def attached_sessions (self ) -> t .Optional [ t . List [Session ] ]:
392
+ def attached_sessions (self ) -> t .List [Session ]:
352
393
"""
353
394
Return active :class:`Session` objects.
354
395
@@ -357,19 +398,22 @@ def attached_sessions(self) -> t.Optional[t.List[Session]]:
357
398
list of :class:`Session`
358
399
"""
359
400
360
- sessions = self ._sessions
361
- attached_sessions = list ()
401
+ try :
402
+ sessions = self ._sessions
403
+ attached_sessions = list ()
362
404
363
- for session in sessions :
364
- attached = session .get ("session_attached" )
365
- # for now session_active is a unicode
366
- if attached != "0" :
367
- logger .debug (f"session { session .get ('name' )} attached" )
368
- attached_sessions .append (session )
369
- else :
370
- continue
405
+ for session in sessions :
406
+ attached = session .get ("session_attached" )
407
+ # for now session_active is a unicode
408
+ if attached != "0" :
409
+ logger .debug (f"session { session .get ('name' )} attached" )
410
+ attached_sessions .append (session )
411
+ else :
412
+ continue
371
413
372
- return [Session (server = self , ** s ) for s in attached_sessions ] or None
414
+ return [Session (server = self , ** s ) for s in attached_sessions ] or []
415
+ except Exception :
416
+ return []
373
417
374
418
def has_session (self , target_session : str , exact : bool = True ) -> bool :
375
419
"""
0 commit comments