15
15
import typing as t
16
16
from dataclasses import field
17
17
from datetime import datetime
18
- from types import TracebackType
18
+
19
+ from typing_extensions import Self
19
20
20
21
from libtmux ._internal .frozen_dataclass_sealable import frozen_dataclass_sealable
21
22
from libtmux ._internal .query_list import QueryList
25
26
from libtmux .window import Window
26
27
27
28
if t .TYPE_CHECKING :
28
- pass
29
+ from types import TracebackType
29
30
30
31
31
32
@frozen_dataclass_sealable
@@ -39,10 +40,11 @@ class PaneSnapshot(Pane):
39
40
pane_content : list [str ] | None = None
40
41
created_at : datetime = field (default_factory = datetime .now )
41
42
window_snapshot : WindowSnapshot | None = field (
42
- default = None , metadata = {"mutable_during_init" : True }
43
+ default = None ,
44
+ metadata = {"mutable_during_init" : True },
43
45
)
44
46
45
- def __enter__ (self ) -> PaneSnapshot :
47
+ def __enter__ (self ) -> Self :
46
48
"""Context manager entry point."""
47
49
return self
48
50
@@ -53,7 +55,6 @@ def __exit__(
53
55
exc_tb : TracebackType | None ,
54
56
) -> None :
55
57
"""Context manager exit point."""
56
- pass
57
58
58
59
def cmd (self , * args : t .Any , ** kwargs : t .Any ) -> t .NoReturn :
59
60
"""Prevent executing tmux commands on a snapshot."""
@@ -136,13 +137,15 @@ class WindowSnapshot(Window):
136
137
# Fields only present in snapshot
137
138
created_at : datetime = field (default_factory = datetime .now )
138
139
session_snapshot : SessionSnapshot | None = field (
139
- default = None , metadata = {"mutable_during_init" : True }
140
+ default = None ,
141
+ metadata = {"mutable_during_init" : True },
140
142
)
141
143
panes_snapshot : list [PaneSnapshot ] = field (
142
- default_factory = list , metadata = {"mutable_during_init" : True }
144
+ default_factory = list ,
145
+ metadata = {"mutable_during_init" : True },
143
146
)
144
147
145
- def __enter__ (self ) -> WindowSnapshot :
148
+ def __enter__ (self ) -> Self :
146
149
"""Context manager entry point."""
147
150
return self
148
151
@@ -153,7 +156,6 @@ def __exit__(
153
156
exc_tb : TracebackType | None ,
154
157
) -> None :
155
158
"""Context manager exit point."""
156
- pass
157
159
158
160
def cmd (self , * args : t .Any , ** kwargs : t .Any ) -> t .NoReturn :
159
161
"""Prevent executing tmux commands on a snapshot."""
@@ -217,7 +219,9 @@ def from_window(
217
219
panes_snapshot = []
218
220
for pane in window .panes :
219
221
pane_snapshot = PaneSnapshot .from_pane (
220
- pane , capture_content = capture_content , window_snapshot = snapshot
222
+ pane ,
223
+ capture_content = capture_content ,
224
+ window_snapshot = snapshot ,
221
225
)
222
226
panes_snapshot .append (pane_snapshot )
223
227
object .__setattr__ (snapshot , "panes_snapshot" , panes_snapshot )
@@ -238,13 +242,15 @@ class SessionSnapshot(Session):
238
242
# Fields only present in snapshot
239
243
created_at : datetime = field (default_factory = datetime .now )
240
244
server_snapshot : ServerSnapshot | None = field (
241
- default = None , metadata = {"mutable_during_init" : True }
245
+ default = None ,
246
+ metadata = {"mutable_during_init" : True },
242
247
)
243
248
windows_snapshot : list [WindowSnapshot ] = field (
244
- default_factory = list , metadata = {"mutable_during_init" : True }
249
+ default_factory = list ,
250
+ metadata = {"mutable_during_init" : True },
245
251
)
246
252
247
- def __enter__ (self ) -> SessionSnapshot :
253
+ def __enter__ (self ) -> Self :
248
254
"""Context manager entry point."""
249
255
return self
250
256
@@ -255,7 +261,6 @@ def __exit__(
255
261
exc_tb : TracebackType | None ,
256
262
) -> None :
257
263
"""Context manager exit point."""
258
- pass
259
264
260
265
def cmd (self , * args : t .Any , ** kwargs : t .Any ) -> t .NoReturn :
261
266
"""Prevent executing tmux commands on a snapshot."""
@@ -326,7 +331,9 @@ def from_session(
326
331
windows_snapshot = []
327
332
for window in session .windows :
328
333
window_snapshot = WindowSnapshot .from_window (
329
- window , capture_content = capture_content , session_snapshot = snapshot
334
+ window ,
335
+ capture_content = capture_content ,
336
+ session_snapshot = snapshot ,
330
337
)
331
338
windows_snapshot .append (window_snapshot )
332
339
object .__setattr__ (snapshot , "windows_snapshot" , windows_snapshot )
@@ -347,16 +354,19 @@ class ServerSnapshot(Server):
347
354
# Fields only present in snapshot
348
355
created_at : datetime = field (default_factory = datetime .now )
349
356
sessions_snapshot : list [SessionSnapshot ] = field (
350
- default_factory = list , metadata = {"mutable_during_init" : True }
357
+ default_factory = list ,
358
+ metadata = {"mutable_during_init" : True },
351
359
)
352
360
windows_snapshot : list [WindowSnapshot ] = field (
353
- default_factory = list , metadata = {"mutable_during_init" : True }
361
+ default_factory = list ,
362
+ metadata = {"mutable_during_init" : True },
354
363
)
355
364
panes_snapshot : list [PaneSnapshot ] = field (
356
- default_factory = list , metadata = {"mutable_during_init" : True }
365
+ default_factory = list ,
366
+ metadata = {"mutable_during_init" : True },
357
367
)
358
368
359
- def __enter__ (self ) -> ServerSnapshot :
369
+ def __enter__ (self ) -> Self :
360
370
"""Context manager entry point."""
361
371
return self
362
372
@@ -367,7 +377,6 @@ def __exit__(
367
377
exc_tb : TracebackType | None ,
368
378
) -> None :
369
379
"""Context manager exit point."""
370
- pass
371
380
372
381
def cmd (self , * args : t .Any , ** kwargs : t .Any ) -> t .NoReturn :
373
382
"""Prevent executing tmux commands on a snapshot."""
@@ -400,7 +409,9 @@ def panes(self) -> QueryList[PaneSnapshot]:
400
409
401
410
@classmethod
402
411
def from_server (
403
- cls , server : Server , include_content : bool = True
412
+ cls ,
413
+ server : Server ,
414
+ include_content : bool = True ,
404
415
) -> ServerSnapshot :
405
416
"""Create a ServerSnapshot from a live Server.
406
417
@@ -430,11 +441,11 @@ def from_server(
430
441
431
442
# Copy server attributes
432
443
for name , value in vars (server ).items ():
433
- if not name .startswith ("_" ) and name not in [
444
+ if not name .startswith ("_" ) and name not in {
434
445
"sessions" ,
435
446
"windows" ,
436
447
"panes" ,
437
- ] :
448
+ } :
438
449
object .__setattr__ (snapshot , name , copy .deepcopy (value ))
439
450
440
451
# Set snapshot-specific fields
@@ -474,7 +485,8 @@ def from_server(
474
485
def filter_snapshot (
475
486
snapshot : ServerSnapshot | SessionSnapshot | WindowSnapshot | PaneSnapshot ,
476
487
filter_func : t .Callable [
477
- [ServerSnapshot | SessionSnapshot | WindowSnapshot | PaneSnapshot ], bool
488
+ [ServerSnapshot | SessionSnapshot | WindowSnapshot | PaneSnapshot ],
489
+ bool ,
478
490
],
479
491
) -> ServerSnapshot | SessionSnapshot | WindowSnapshot | PaneSnapshot | None :
480
492
"""Filter a snapshot hierarchy based on a filter function.
@@ -525,7 +537,7 @@ def filter_snapshot(
525
537
return server_copy
526
538
527
539
# Handle filtering SessionSnapshot
528
- elif isinstance (snapshot , SessionSnapshot ):
540
+ if isinstance (snapshot , SessionSnapshot ):
529
541
filtered_windows = []
530
542
531
543
# Filter each window
@@ -544,7 +556,7 @@ def filter_snapshot(
544
556
return session_copy
545
557
546
558
# Handle filtering WindowSnapshot
547
- elif isinstance (snapshot , WindowSnapshot ):
559
+ if isinstance (snapshot , WindowSnapshot ):
548
560
filtered_panes = []
549
561
550
562
# Filter each pane - panes are leaf nodes
@@ -560,7 +572,7 @@ def filter_snapshot(
560
572
return window_copy
561
573
562
574
# Handle filtering PaneSnapshot (leaf node)
563
- elif isinstance (snapshot , PaneSnapshot ):
575
+ if isinstance (snapshot , PaneSnapshot ):
564
576
if filter_func (snapshot ):
565
577
return snapshot
566
578
return None
@@ -588,22 +600,23 @@ def snapshot_to_dict(
588
600
"""
589
601
# Base case: For non-snapshot objects, just return them directly
590
602
if not isinstance (
591
- snapshot , (ServerSnapshot , SessionSnapshot , WindowSnapshot , PaneSnapshot )
603
+ snapshot ,
604
+ (ServerSnapshot , SessionSnapshot , WindowSnapshot , PaneSnapshot ),
592
605
):
593
- return t .cast (dict [str , t .Any ], snapshot )
606
+ return t .cast (" dict[str, t.Any]" , snapshot )
594
607
595
608
# Convert dataclass to dict
596
609
result : dict [str , t .Any ] = {}
597
610
598
611
# Get all fields from the instance
599
612
for name , value in vars (snapshot ).items ():
600
613
# Skip internal and parent reference fields - we want a tree, not a graph with cycles
601
- if name .startswith ("_" ) or name in [
614
+ if name .startswith ("_" ) or name in {
602
615
"server" ,
603
616
"server_snapshot" ,
604
617
"session_snapshot" ,
605
618
"window_snapshot" ,
606
- ] :
619
+ } :
607
620
continue
608
621
609
622
# Handle lists of snapshots
@@ -618,7 +631,8 @@ def snapshot_to_dict(
618
631
result [name ] = [snapshot_to_dict (item ) for item in value ]
619
632
# Handle nested snapshots
620
633
elif isinstance (
621
- value , (ServerSnapshot , SessionSnapshot , WindowSnapshot , PaneSnapshot )
634
+ value ,
635
+ (ServerSnapshot , SessionSnapshot , WindowSnapshot , PaneSnapshot ),
622
636
):
623
637
result [name ] = snapshot_to_dict (value )
624
638
# Handle QueryList (convert to regular list first)
@@ -670,7 +684,7 @@ def is_active(
670
684
"""Return True if the object is active."""
671
685
if isinstance (obj , PaneSnapshot ):
672
686
return getattr (obj , "pane_active" , "0" ) == "1"
673
- elif isinstance (obj , WindowSnapshot ):
687
+ if isinstance (obj , WindowSnapshot ):
674
688
return getattr (obj , "window_active" , "0" ) == "1"
675
689
# Servers and sessions are always considered active
676
690
return isinstance (obj , (ServerSnapshot , SessionSnapshot ))
@@ -679,4 +693,4 @@ def is_active(
679
693
if filtered is None :
680
694
error_msg = "No active objects found!"
681
695
raise ValueError (error_msg )
682
- return t .cast (ServerSnapshot , filtered )
696
+ return t .cast (" ServerSnapshot" , filtered )
0 commit comments