Skip to content

Commit c3eefd5

Browse files
authored
refactor!(EnvironmentMixin): Simplify for typings (#390)
In preparation for #383
2 parents f606a42 + 2a3bea2 commit c3eefd5

File tree

7 files changed

+113
-48
lines changed

7 files changed

+113
-48
lines changed

CHANGES

+49
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,55 @@ $ pip install --user --upgrade --pre libtmux
1212

1313
- _Insert changes/features/fixes for next release here_
1414

15+
# Breaking changes
16+
17+
- Deprecated individual item lookups ({issue}`390`)
18+
19+
- Removed key lookups from {meth}`libtmux.common.EnvironmentMixin.show_environment`
20+
21+
Only `EnvironmentMixin.show_environment()` (without an argument) exists, and
22+
it still returns a `dict`.
23+
24+
- Add key lookups via {meth}`libtmux.common.EnvironmentMixin.getenv`
25+
26+
```python
27+
# Before
28+
server.show_environment('DISPLAY')
29+
30+
# After
31+
server.getenv('DISPLAY')
32+
33+
# Before
34+
session.show_environment('DISPLAY')
35+
36+
# After
37+
session.getenv('DISPLAY')
38+
```
39+
40+
- Removed key lookups from {meth}`Session.show_options`
41+
42+
```python
43+
session.show_options() # still returns dict, without an argument
44+
45+
# Old
46+
session.show_options('DISPLAY')
47+
48+
# Now
49+
session.show_option('DISPLAY')
50+
```
51+
52+
- Removed key lookups from {meth}`Window.show_window_options`
53+
54+
```python
55+
window.show_window_options() # still returns dict, without an argument
56+
57+
# Old
58+
window.show_window_options('DISPLAY')
59+
60+
# Now
61+
window.show_window_option('DISPLAY')
62+
```
63+
1564
### Development
1665

1766
- Fix incorrect function name `findWhere()` ({issue}`391`)

libtmux/common.py

+40-13
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,43 @@ def remove_environment(self, name):
108108
proc.stderr = proc.stderr[0]
109109
raise ValueError("tmux set-environment stderr: %s" % proc.stderr)
110110

111-
def show_environment(self, name=None):
112-
"""Show environment ``$ tmux show-environment -t [session] <name>``.
111+
def show_environment(self):
112+
"""Show environment ``$ tmux show-environment -t [session]``.
113113
114-
Return dict of environment variables for the session or the value of a
115-
specific variable if the name is specified.
114+
Return dict of environment variables for the session.
115+
116+
.. versionchanged:: 0.13
117+
118+
Removed per-item lookups. Use :meth:`libtmux.common.EnvironmentMixin.getenv`.
119+
120+
Returns
121+
-------
122+
dict
123+
environmental variables in dict, if no name, or str if name
124+
entered.
125+
"""
126+
tmux_args = ["show-environment"]
127+
if self._add_option:
128+
tmux_args += [self._add_option]
129+
vars = self.cmd(*tmux_args).stdout
130+
vars = [tuple(item.split("=", 1)) for item in vars]
131+
vars_dict = {}
132+
for t in vars:
133+
if len(t) == 2:
134+
vars_dict[t[0]] = t[1]
135+
elif len(t) == 1:
136+
vars_dict[t[0]] = True
137+
else:
138+
raise ValueError("unexpected variable %s", t)
139+
140+
return vars_dict
141+
142+
def getenv(self, name):
143+
"""Show environment variable ``$ tmux show-environment -t [session] <name>``.
144+
145+
Return the value of a specific variable if the name is specified.
146+
147+
.. versionadded:: 0.13
116148
117149
Parameters
118150
----------
@@ -121,15 +153,13 @@ def show_environment(self, name=None):
121153
122154
Returns
123155
-------
124-
str or dict
125-
environmental variables in dict, if no name, or str if name
126-
entered.
156+
str
157+
Value of environment variable
127158
"""
128159
tmux_args = ["show-environment"]
129160
if self._add_option:
130161
tmux_args += [self._add_option]
131-
if name:
132-
tmux_args += [name]
162+
tmux_args += [name]
133163
vars = self.cmd(*tmux_args).stdout
134164
vars = [tuple(item.split("=", 1)) for item in vars]
135165
vars_dict = {}
@@ -141,10 +171,7 @@ def show_environment(self, name=None):
141171
else:
142172
raise ValueError("unexpected variable %s", t)
143173

144-
if name:
145-
return vars_dict.get(name)
146-
147-
return vars_dict
174+
return vars_dict.get(name)
148175

149176

150177
class tmux_cmd:

libtmux/session.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def set_option(self, option, value, _global=False):
421421
if isinstance(proc.stderr, list) and len(proc.stderr):
422422
handle_option_error(proc.stderr[0])
423423

424-
def show_options(self, option=None, _global=False):
424+
def show_options(self, _global=False):
425425
"""
426426
Return a dict of options for the window.
427427
@@ -430,9 +430,6 @@ def show_options(self, option=None, _global=False):
430430
431431
Parameters
432432
----------
433-
option : str, optional
434-
name of option, e.g. 'visual-silence'. Defaults to None, which
435-
returns all options.
436433
_global : bool, optional
437434
Pass ``-g`` flag for global variable (server-wide)
438435
@@ -450,11 +447,8 @@ def show_options(self, option=None, _global=False):
450447
if _global:
451448
tmux_args += ("-g",)
452449

453-
if option:
454-
return self.show_option(option, _global=_global)
455-
else:
456-
tmux_args += ("show-options",)
457-
session_options = self.cmd(*tmux_args).stdout
450+
tmux_args += ("show-options",)
451+
session_options = self.cmd(*tmux_args).stdout
458452

459453
session_options = [tuple(item.split(" ")) for item in session_options]
460454

libtmux/window.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def set_window_option(self, option, value):
193193
if isinstance(cmd.stderr, list) and len(cmd.stderr):
194194
handle_option_error(cmd.stderr[0])
195195

196-
def show_window_options(self, option=None, g=False):
196+
def show_window_options(self, g=False):
197197
"""
198198
Return a dict of options for the window.
199199
@@ -202,8 +202,6 @@ def show_window_options(self, option=None, g=False):
202202
203203
Parameters
204204
----------
205-
option : str, optional
206-
show a single option.
207205
g : str, optional
208206
Pass ``-g`` flag for global variable, default False.
209207
@@ -217,11 +215,8 @@ def show_window_options(self, option=None, g=False):
217215
if g:
218216
tmux_args += ("-g",)
219217

220-
if option:
221-
return self.show_window_option(option, g=g)
222-
else:
223-
tmux_args += ("show-window-options",)
224-
cmd = self.cmd(*tmux_args).stdout
218+
tmux_args += ("show-window-options",)
219+
cmd = self.cmd(*tmux_args).stdout
225220

226221
# The shlex.split function splits the args at spaces, while also
227222
# retaining quoted sub-strings.

tests/test_server.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ def test_show_environment(server):
6363
assert isinstance(_vars, dict)
6464

6565

66-
def test_set_show_environment_single(server, session):
66+
def test_getenv(server, session):
6767
"""Set environment then Server.show_environment(key)."""
6868
server.set_environment("FOO", "BAR")
69-
assert "BAR" == server.show_environment("FOO")
69+
assert "BAR" == server.getenv("FOO")
7070

7171
server.set_environment("FOO", "DAR")
72-
assert "DAR" == server.show_environment("FOO")
72+
assert "DAR" == server.getenv("FOO")
7373

7474
assert "DAR" == server.show_environment()["FOO"]
7575

7676

7777
def test_show_environment_not_set(server):
7878
"""Unset environment variable returns None."""
79-
assert server.show_environment("BAR") is None
79+
assert server.getenv("BAR") is None
8080

8181

8282
def test_new_session(server):

tests/test_session.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ def test_set_show_options_single(session):
108108
"""Set option then Session.show_options(key)."""
109109

110110
session.set_option("history-limit", 20)
111-
assert session.show_options("history-limit") == 20
111+
assert session.show_option("history-limit") == 20
112112

113113
session.set_option("history-limit", 40)
114-
assert session.show_options("history-limit") == 40
114+
assert session.show_option("history-limit") == 40
115115

116116
assert session.show_options()["history-limit"] == 40
117117

@@ -172,35 +172,35 @@ def test_set_show_environment_single(session):
172172
"""Set environment then Session.show_environment(key)."""
173173

174174
session.set_environment("FOO", "BAR")
175-
assert session.show_environment("FOO") == "BAR"
175+
assert session.getenv("FOO") == "BAR"
176176

177177
session.set_environment("FOO", "DAR")
178-
assert session.show_environment("FOO") == "DAR"
178+
assert session.getenv("FOO") == "DAR"
179179

180180
assert session.show_environment()["FOO"] == "DAR"
181181

182182

183183
def test_show_environment_not_set(session):
184184
"""Not set environment variable returns None."""
185-
assert session.show_environment("BAR") is None
185+
assert session.getenv("BAR") is None
186186

187187

188188
def test_remove_environment(session):
189189
"""Remove environment variable."""
190-
assert session.show_environment("BAM") is None
190+
assert session.getenv("BAM") is None
191191
session.set_environment("BAM", "OK")
192-
assert session.show_environment("BAM") == "OK"
192+
assert session.getenv("BAM") == "OK"
193193
session.remove_environment("BAM")
194-
assert session.show_environment("BAM") is None
194+
assert session.getenv("BAM") is None
195195

196196

197197
def test_unset_environment(session):
198198
"""Unset environment variable."""
199-
assert session.show_environment("BAM") is None
199+
assert session.getenv("BAM") is None
200200
session.set_environment("BAM", "OK")
201-
assert session.show_environment("BAM") == "OK"
201+
assert session.getenv("BAM") == "OK"
202202
session.unset_environment("BAM")
203-
assert session.show_environment("BAM") is None
203+
assert session.getenv("BAM") is None
204204

205205

206206
@pytest.mark.parametrize(

tests/test_window.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ def test_set_show_window_options(session):
184184
window = session.new_window(window_name="test_window")
185185

186186
window.set_window_option("main-pane-height", 20)
187-
assert window.show_window_options("main-pane-height") == 20
187+
assert window.show_window_option("main-pane-height") == 20
188188

189189
window.set_window_option("main-pane-height", 40)
190-
assert window.show_window_options("main-pane-height") == 40
190+
assert window.show_window_option("main-pane-height") == 40
191191
assert window.show_window_options()["main-pane-height"] == 40
192192

193193
if has_gte_version("2.3"):
194194
window.set_window_option("pane-border-format", " #P ")
195-
assert window.show_window_options("pane-border-format") == " #P "
195+
assert window.show_window_option("pane-border-format") == " #P "
196196

197197

198198
def test_empty_window_option_returns_None(session):

0 commit comments

Comments
 (0)