Skip to content

Commit c9598fb

Browse files
committed
Split init / exec of tmux_cmd for debuggability
tmux_cmd initialization composes the command the command will be available in the instance attribute .cmd .execute() instance method will run tmux_cmd see Server.cmd for example of new usage Related #77
1 parent 84fd521 commit c9598fb

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

libtmux/common.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ class tmux_cmd(object):
141141
142142
Usage::
143143
144-
proc = tmux_cmd('new-session', '-s%' % 'my session')
144+
c = tmux_cmd('new-session', '-s%' % 'my session')
145+
146+
# You can actually see the command in the .cmd attribute
147+
print(c.cmd)
148+
149+
proc = c.execute()
145150
146151
if proc.stderr:
147152
raise exc.LibTmuxException(
@@ -178,6 +183,8 @@ def __init__(self, *args, **kwargs):
178183

179184
self.cmd = cmd
180185

186+
def execute(self):
187+
cmd = self.cmd
181188
try:
182189
self.process = subprocess.Popen(
183190
cmd,
@@ -213,6 +220,7 @@ def __init__(self, *args, **kwargs):
213220
'self.stdout for %s: \n%s' %
214221
(' '.join(cmd), self.stdout)
215222
)
223+
return self
216224

217225

218226
class TmuxMappingObject(collections.MutableMapping):
@@ -260,7 +268,7 @@ def __len__(self):
260268
def __getattr__(self, key):
261269
try:
262270
return self._info[self.formatter_prefix + key]
263-
except:
271+
except KeyError:
264272
raise AttributeError('%s has no property %s' %
265273
(self.__class__, key))
266274

@@ -354,8 +362,8 @@ def get_by_id(self, id):
354362

355363

356364
def which(exe=None, default_paths=[
357-
'/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin'
358-
], append_env_path=True):
365+
'/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin'
366+
], append_env_path=True):
359367
"""Return path of bin. Python clone of /usr/bin/which.
360368
361369
from salt.util - https://www.github.com/saltstack/salt - license apache
@@ -413,7 +421,7 @@ def get_version():
413421
:returns: tmux version
414422
:rtype: :class:`distutils.version.LooseVersion`
415423
"""
416-
proc = tmux_cmd('-V')
424+
proc = tmux_cmd('-V').execute()
417425
if proc.stderr:
418426
if proc.stderr[0] == 'tmux: unknown option -- V':
419427
if sys.platform.startswith("openbsd"): # openbsd has no tmux -V

libtmux/server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def cmd(self, *args, **kwargs):
9696
else:
9797
raise ValueError('Server.colors must equal 88 or 256')
9898

99-
return tmux_cmd(*args, **kwargs)
99+
return tmux_cmd(*args, **kwargs).execute()
100100

101101
def _list_sessions(self):
102102
"""Return list of sessions in :py:obj:`dict` form.

tests/test_common.py

+15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def mock_tmux_cmd(param):
2525
class Hi(object):
2626
stdout = ['tmux master']
2727
stderr = None
28+
29+
def execute(self):
30+
return self
2831
return Hi()
2932
monkeypatch.setattr(libtmux.common, 'tmux_cmd', mock_tmux_cmd)
3033

@@ -42,6 +45,9 @@ def test_get_version_openbsd(monkeypatch):
4245
def mock_tmux_cmd(param):
4346
class Hi(object):
4447
stderr = ['tmux: unknown option -- V']
48+
49+
def execute(self):
50+
return self
4551
return Hi()
4652
monkeypatch.setattr(libtmux.common, 'tmux_cmd', mock_tmux_cmd)
4753
monkeypatch.setattr(sys, 'platform', 'openbsd 5.2')
@@ -59,6 +65,9 @@ def test_get_version_too_low(monkeypatch):
5965
def mock_tmux_cmd(param):
6066
class Hi(object):
6167
stderr = ['tmux: unknown option -- V']
68+
69+
def execute(self):
70+
return self
6271
return Hi()
6372
monkeypatch.setattr(libtmux.common, 'tmux_cmd', mock_tmux_cmd)
6473
with pytest.raises(LibTmuxException) as exc_info:
@@ -153,6 +162,12 @@ def test_tmux_cmd_raises_on_not_found():
153162
tmux_cmd('-V')
154163

155164

165+
def test_tmux_cmd_makes_cmd_available():
166+
"""tmux_cmd objects should make .cmd attribute available."""
167+
command = tmux_cmd('-V')
168+
assert hasattr(command, 'cmd')
169+
170+
156171
@pytest.mark.parametrize("session_name,raises,exc_msg_regex", [
157172
('', True, 'may not be empty'),
158173
(None, True, 'may not be empty'),

0 commit comments

Comments
 (0)