Skip to content

Commit eef13f8

Browse files
authored
Refactor: Cleaning up (#362)
* Fixed incorrectly indented `return True` line in `by` filter method in `_info` class methods * Created intermediate filtered list variables for `_info` class methods to more explicitly define the return type * Reduced nesting of `if` statements within class `attached` methods * Removed redundant `int` casts * Moved `MutableMapping` import statement from `_compat` to `common`
2 parents 2db8bba + f937e68 commit eef13f8

File tree

5 files changed

+42
-40
lines changed

5 files changed

+42
-40
lines changed

libtmux/_compat.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# flake8: NOQA
22
import sys
33
import typing as t
4-
from collections.abc import MutableMapping
54

65
console_encoding = sys.__stdout__.encoding
76

libtmux/common.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
import subprocess
1212
import sys
1313
import typing as t
14+
from collections.abc import MutableMapping
1415
from distutils.version import LooseVersion
1516

1617
from . import exc
17-
from ._compat import MutableMapping, console_to_str, str_from_console
18+
from ._compat import console_to_str, str_from_console
1819

1920
logger = logging.getLogger(__name__)
2021

@@ -62,7 +63,7 @@ def set_environment(self, name, value):
6263
proc = self.cmd(*args)
6364

6465
if proc.stderr:
65-
if isinstance(proc.stderr, list) and len(proc.stderr) == int(1):
66+
if isinstance(proc.stderr, list) and len(proc.stderr) == 1:
6667
proc.stderr = proc.stderr[0]
6768
raise ValueError("tmux set-environment stderr: %s" % proc.stderr)
6869

@@ -83,7 +84,7 @@ def unset_environment(self, name):
8384
proc = self.cmd(*args)
8485

8586
if proc.stderr:
86-
if isinstance(proc.stderr, list) and len(proc.stderr) == int(1):
87+
if isinstance(proc.stderr, list) and len(proc.stderr) == 1:
8788
proc.stderr = proc.stderr[0]
8889
raise ValueError("tmux set-environment stderr: %s" % proc.stderr)
8990

@@ -103,7 +104,7 @@ def remove_environment(self, name):
103104
proc = self.cmd(*args)
104105

105106
if proc.stderr:
106-
if isinstance(proc.stderr, list) and len(proc.stderr) == int(1):
107+
if isinstance(proc.stderr, list) and len(proc.stderr) == 1:
107108
proc.stderr = proc.stderr[0]
108109
raise ValueError("tmux set-environment stderr: %s" % proc.stderr)
109110

@@ -336,19 +337,20 @@ def where(self, attrs, first=False):
336337
"""
337338

338339
# from https://github.com/serkanyersen/underscore.py
339-
def by(val, *args):
340-
for key, value in attrs.items():
340+
def by(val) -> bool:
341+
for key in attrs.keys():
341342
try:
342343
if attrs[key] != val[key]:
343344
return False
344345
except KeyError:
345346
return False
346-
return True
347+
return True
347348

349+
# TODO add type hint
350+
target_children = list(filter(by, self.children))
348351
if first:
349-
return list(filter(by, self.children))[0]
350-
else:
351-
return list(filter(by, self.children))
352+
return target_children[0]
353+
return target_children
352354

353355
def get_by_id(self, id):
354356
"""

libtmux/pane.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,23 @@ def __init__(self, window=None, **kwargs):
5858
self.server._update_panes()
5959

6060
@property
61-
def _info(self, *args):
61+
def _info(self):
6262

6363
attrs = {"pane_id": self._pane_id}
6464

6565
# from https://github.com/serkanyersen/underscore.py
66-
def by(val, *args):
67-
for key, value in attrs.items():
66+
def by(val) -> bool:
67+
for key in attrs.keys():
6868
try:
6969
if attrs[key] != val[key]:
7070
return False
7171
except KeyError:
7272
return False
73-
return True
73+
return True
7474

75-
return list(filter(by, self.server._panes))[0]
75+
# TODO add type hint
76+
target_panes = list(filter(by, self.server._panes))
77+
return target_panes[0]
7678

7779
def cmd(self, cmd, *args, **kwargs):
7880
"""Return :meth:`Server.cmd` defaulting to ``target_pane`` as target.

libtmux/session.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,19 @@ def _info(self):
6565

6666
attrs = {"session_id": str(self._session_id)}
6767

68-
def by(val):
69-
for key, value in attrs.items():
68+
def by(val) -> bool:
69+
for key in attrs.keys():
7070
try:
7171
if attrs[key] != val[key]:
7272
return False
7373
except KeyError:
7474
return False
75-
return True
75+
return True
7676

77+
# TODO add type hint
78+
target_sessions = list(filter(by, self.server._sessions))
7779
try:
78-
return list(filter(by, self.server._sessions))[0]
80+
return target_sessions[0]
7981
except IndexError as e:
8082
logger.error(e)
8183

@@ -316,21 +318,18 @@ def attached_window(self) -> Window:
316318
"""
317319
active_windows = []
318320
for window in self._windows:
319-
if "window_active" in window:
320-
# for now window_active is a unicode
321-
if window.get("window_active") == "1":
322-
active_windows.append(Window(session=self, **window))
323-
else:
324-
continue
325-
326-
if len(active_windows) == int(1):
321+
# for now window_active is a unicode
322+
if "window_active" in window and window.get("window_active") == "1":
323+
active_windows.append(Window(session=self, **window))
324+
325+
if len(active_windows) == 1:
327326
return active_windows[0]
328327
else:
329328
raise exc.LibTmuxException(
330329
"multiple active windows found. %s" % active_windows
331330
)
332331

333-
if len(self._windows) == int(0):
332+
if len(self._windows) == 0:
334333
raise exc.LibTmuxException("No Windows")
335334

336335
def select_window(self, target_window: str) -> Window:

libtmux/window.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,23 @@ def _info(self, *args):
7474
attrs = {"window_id": self._window_id}
7575

7676
# from https://github.com/serkanyersen/underscore.py
77-
def by(val, *args):
78-
for key, value in attrs.items():
77+
def by(val) -> bool:
78+
for key in attrs.keys():
7979
try:
8080
if attrs[key] != val[key]:
8181
return False
8282
except KeyError:
8383
return False
84-
return True
84+
return True
8585

86-
ret = list(filter(by, self.server._windows))
86+
# TODO add type hint
87+
target_windows = list(filter(by, self.server._windows))
8788
# If a window_shell option was configured which results in
8889
# a short-lived process, the window id is @0. Use that instead of
8990
# self._window_id
90-
if len(ret) == 0 and self.server._windows[0]["window_id"] == "@0":
91-
ret = self.server._windows
92-
return ret[0]
91+
if len(target_windows) == 0 and self.server._windows[0]["window_id"] == "@0":
92+
target_windows = self.server._windows
93+
return target_windows[0]
9394

9495
def cmd(self, cmd, *args, **kwargs):
9596
"""Return :meth:`Server.cmd` defaulting ``target_window`` as target.
@@ -501,10 +502,9 @@ def attached_pane(self) -> t.Optional[Pane]:
501502
:class:`Pane`
502503
"""
503504
for pane in self._panes:
504-
if "pane_active" in pane:
505-
# for now pane_active is a unicode
506-
if pane.get("pane_active") == "1":
507-
return Pane(window=self, **pane)
505+
# for now pane_active is a unicode
506+
if "pane_active" in pane and pane.get("pane_active") == "1":
507+
return Pane(window=self, **pane)
508508

509509
def _list_panes(self) -> t.List[PaneDict]:
510510
panes = self.server._update_panes()._panes

0 commit comments

Comments
 (0)