Skip to content

Commit 58a189e

Browse files
committed
Use __getattr__ to access objects easier
See also: tmux-python/tmuxp#118
1 parent b3214ee commit 58a189e

File tree

5 files changed

+11
-6
lines changed

5 files changed

+11
-6
lines changed

libtmux/common.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,12 @@ def get_by_id(self, id):
317317

318318
return None
319319

320+
def __getattr__(self, key):
321+
try:
322+
return self[self.formatter_prefix + key]
323+
except:
324+
raise AttributeError
325+
320326

321327
def which(exe=None):
322328
"""Return path of bin. Python clone of /usr/bin/which.

libtmux/pane.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Pane(TmuxMappingObject, TmuxRelationalObject):
2626
Renamed from ``.tmux`` to ``.cmd``.
2727
2828
"""
29+
formatter_prefix = 'pane'
2930

3031
def __init__(self, window=None, **kwargs):
3132
if not window:

libtmux/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class Server(TmuxRelationalObject, EnvironmentMixin):
4545
colors = None
4646
#: unique child ID key
4747
childIdAttribute = 'session_id'
48+
#: namespace used by tmux formatter variables
49+
formatter_prefix = 'server_'
4850

4951
def __init__(
5052
self,

libtmux/session.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Session(
3030
"""
3131

3232
childIdAttribute = 'window_id'
33+
formatter_prefix = 'session'
3334

3435
def __init__(self, server=None, **kwargs):
3536
EnvironmentMixin.__init__(self)
@@ -40,12 +41,6 @@ def __init__(self, server=None, **kwargs):
4041
self._session_id = kwargs['session_id']
4142
self.server._update_windows()
4243

43-
for f in formats.SESSION_FORMATS:
44-
try:
45-
setattr(self, f.replace('session_', ''), self.get(f, None))
46-
except:
47-
pass
48-
4944
@property
5045
def _info(self, *args):
5146

libtmux/window.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Window(TmuxMappingObject, TmuxRelationalObject):
2222
""":term:`tmux(1)` window."""
2323

2424
childIdAttribute = 'pane_id'
25+
formatter_prefix = 'window'
2526

2627
def __init__(self, session=None, **kwargs):
2728

0 commit comments

Comments
 (0)