From 4c54c800f3af1e6e870b8ce9b5782bdea0999194 Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 15:48:22 -0400 Subject: [PATCH 1/9] Refactor session _info method --- libtmux/session.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libtmux/session.py b/libtmux/session.py index 4dd982a02..b7a56f89e 100644 --- a/libtmux/session.py +++ b/libtmux/session.py @@ -66,16 +66,18 @@ def _info(self): attrs = {"session_id": str(self._session_id)} def by(val): - for key, value in attrs.items(): + for key in attrs.keys(): try: if attrs[key] != val[key]: return False except KeyError: return False - return True + return True + # TODO add type hint + target_sessions = list(filter(by, self.server._sessions)) try: - return list(filter(by, self.server._sessions))[0] + return target_sessions[0] except IndexError as e: logger.error(e) From f974449afe151a02351439a7f3f432657c0eb375 Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 15:52:07 -0400 Subject: [PATCH 2/9] Refactor window _info method --- libtmux/window.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libtmux/window.py b/libtmux/window.py index c8c3ce9b7..a70e94bdf 100644 --- a/libtmux/window.py +++ b/libtmux/window.py @@ -74,22 +74,23 @@ def _info(self, *args): attrs = {"window_id": self._window_id} # from https://github.com/serkanyersen/underscore.py - def by(val, *args): - for key, value in attrs.items(): + def by(val): + for key in attrs.keys(): try: if attrs[key] != val[key]: return False except KeyError: return False - return True + return True - ret = list(filter(by, self.server._windows)) + # TODO add type hint + target_windows = list(filter(by, self.server._windows)) # If a window_shell option was configured which results in # a short-lived process, the window id is @0. Use that instead of # self._window_id - if len(ret) == 0 and self.server._windows[0]["window_id"] == "@0": - ret = self.server._windows - return ret[0] + if len(target_windows) == 0 and self.server._windows[0]["window_id"] == "@0": + target_windows = self.server._windows + return target_windows[0] def cmd(self, cmd, *args, **kwargs): """Return :meth:`Server.cmd` defaulting ``target_window`` as target. From c522878b608e871450d2f3f374a28e60a2c7d5f1 Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 15:54:25 -0400 Subject: [PATCH 3/9] Refactor pane _info method --- libtmux/pane.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libtmux/pane.py b/libtmux/pane.py index 8ccf354fd..cf8d8cf8a 100644 --- a/libtmux/pane.py +++ b/libtmux/pane.py @@ -58,21 +58,23 @@ def __init__(self, window=None, **kwargs): self.server._update_panes() @property - def _info(self, *args): + def _info(self): attrs = {"pane_id": self._pane_id} # from https://github.com/serkanyersen/underscore.py - def by(val, *args): - for key, value in attrs.items(): + def by(val): + for key in attrs.keys(): try: if attrs[key] != val[key]: return False except KeyError: return False - return True + return True - return list(filter(by, self.server._panes))[0] + # TODO add type hint + target_panes = list(filter(by, self.server._panes)) + return target_panes[0] def cmd(self, cmd, *args, **kwargs): """Return :meth:`Server.cmd` defaulting to ``target_pane`` as target. From c5bdb0169d2eecf5b9ce309e697c85c2cbc6ba72 Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 15:57:10 -0400 Subject: [PATCH 4/9] Refactor common `where` method --- libtmux/common.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/libtmux/common.py b/libtmux/common.py index b71ca4a0a..f3b01f317 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -336,19 +336,20 @@ def where(self, attrs, first=False): """ # from https://github.com/serkanyersen/underscore.py - def by(val, *args): - for key, value in attrs.items(): + def by(val) -> bool: + for key in attrs.keys(): try: if attrs[key] != val[key]: return False except KeyError: return False - return True + return True + # TODO add type hint + target_children = list(filter(by, self.children)) if first: - return list(filter(by, self.children))[0] - else: - return list(filter(by, self.children)) + return target_children[0] + return return target_children def get_by_id(self, id): """ From 821a453c15583860177593bbdad28557ce8ed39f Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 16:01:12 -0400 Subject: [PATCH 5/9] Simplify nested iteration in session `attached_window` method --- libtmux/session.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/libtmux/session.py b/libtmux/session.py index b7a56f89e..958be0522 100644 --- a/libtmux/session.py +++ b/libtmux/session.py @@ -318,12 +318,9 @@ def attached_window(self) -> Window: """ active_windows = [] for window in self._windows: - if "window_active" in window: - # for now window_active is a unicode - if window.get("window_active") == "1": - active_windows.append(Window(session=self, **window)) - else: - continue + # for now window_active is a unicode + if "window_active" in window and window.get("window_active") == "1": + active_windows.append(Window(session=self, **window)) if len(active_windows) == int(1): return active_windows[0] From bcdbbd6c8c629ec2515f584bc5d21cdfee5798de Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 16:02:57 -0400 Subject: [PATCH 6/9] Simplify nested iteration in window `attached_pane` method --- libtmux/common.py | 2 +- libtmux/window.py | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/libtmux/common.py b/libtmux/common.py index f3b01f317..dcc54eaee 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -349,7 +349,7 @@ def by(val) -> bool: target_children = list(filter(by, self.children)) if first: return target_children[0] - return return target_children + return target_children def get_by_id(self, id): """ diff --git a/libtmux/window.py b/libtmux/window.py index a70e94bdf..1e602b5d4 100644 --- a/libtmux/window.py +++ b/libtmux/window.py @@ -502,10 +502,9 @@ def attached_pane(self) -> t.Optional[Pane]: :class:`Pane` """ for pane in self._panes: - if "pane_active" in pane: - # for now pane_active is a unicode - if pane.get("pane_active") == "1": - return Pane(window=self, **pane) + # for now pane_active is a unicode + if "pane_active" in pane and pane.get("pane_active") == "1": + return Pane(window=self, **pane) def _list_panes(self) -> t.List[PaneDict]: panes = self.server._update_panes()._panes From e21b80c45fd7c8bc2edb7a46836e398269e9fb24 Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 16:06:20 -0400 Subject: [PATCH 7/9] Remove redundant casts to int --- libtmux/common.py | 6 +++--- libtmux/session.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libtmux/common.py b/libtmux/common.py index dcc54eaee..b12df792a 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -62,7 +62,7 @@ def set_environment(self, name, value): proc = self.cmd(*args) if proc.stderr: - if isinstance(proc.stderr, list) and len(proc.stderr) == int(1): + if isinstance(proc.stderr, list) and len(proc.stderr) == 1: proc.stderr = proc.stderr[0] raise ValueError("tmux set-environment stderr: %s" % proc.stderr) @@ -83,7 +83,7 @@ def unset_environment(self, name): proc = self.cmd(*args) if proc.stderr: - if isinstance(proc.stderr, list) and len(proc.stderr) == int(1): + if isinstance(proc.stderr, list) and len(proc.stderr) == 1: proc.stderr = proc.stderr[0] raise ValueError("tmux set-environment stderr: %s" % proc.stderr) @@ -103,7 +103,7 @@ def remove_environment(self, name): proc = self.cmd(*args) if proc.stderr: - if isinstance(proc.stderr, list) and len(proc.stderr) == int(1): + if isinstance(proc.stderr, list) and len(proc.stderr) == 1: proc.stderr = proc.stderr[0] raise ValueError("tmux set-environment stderr: %s" % proc.stderr) diff --git a/libtmux/session.py b/libtmux/session.py index 958be0522..b2260b9be 100644 --- a/libtmux/session.py +++ b/libtmux/session.py @@ -322,14 +322,14 @@ def attached_window(self) -> Window: if "window_active" in window and window.get("window_active") == "1": active_windows.append(Window(session=self, **window)) - if len(active_windows) == int(1): + if len(active_windows) == 1: return active_windows[0] else: raise exc.LibTmuxException( "multiple active windows found. %s" % active_windows ) - if len(self._windows) == int(0): + if len(self._windows) == 0: raise exc.LibTmuxException("No Windows") def select_window(self, target_window: str) -> Window: From e393113af7f903099a6af9a8d918941cc5667daa Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 16:07:21 -0400 Subject: [PATCH 8/9] Move MutableMapping import from _compat to common --- libtmux/_compat.py | 1 - libtmux/common.py | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libtmux/_compat.py b/libtmux/_compat.py index a1c5bfe99..d16df9fb7 100644 --- a/libtmux/_compat.py +++ b/libtmux/_compat.py @@ -1,7 +1,6 @@ # flake8: NOQA import sys import typing as t -from collections.abc import MutableMapping console_encoding = sys.__stdout__.encoding diff --git a/libtmux/common.py b/libtmux/common.py index b12df792a..05b85229f 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -11,10 +11,11 @@ import subprocess import sys import typing as t +from collections.abc import MutableMapping from distutils.version import LooseVersion from . import exc -from ._compat import MutableMapping, console_to_str, str_from_console +from ._compat import console_to_str, str_from_console logger = logging.getLogger(__name__) From f937e685a1f71ee80fd1cb7a2491ab23de31edf7 Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 16:10:33 -0400 Subject: [PATCH 9/9] Add type hinting to `by` return value --- libtmux/pane.py | 2 +- libtmux/session.py | 2 +- libtmux/window.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libtmux/pane.py b/libtmux/pane.py index cf8d8cf8a..e35a70f65 100644 --- a/libtmux/pane.py +++ b/libtmux/pane.py @@ -63,7 +63,7 @@ def _info(self): attrs = {"pane_id": self._pane_id} # from https://github.com/serkanyersen/underscore.py - def by(val): + def by(val) -> bool: for key in attrs.keys(): try: if attrs[key] != val[key]: diff --git a/libtmux/session.py b/libtmux/session.py index b2260b9be..e3a390ec0 100644 --- a/libtmux/session.py +++ b/libtmux/session.py @@ -65,7 +65,7 @@ def _info(self): attrs = {"session_id": str(self._session_id)} - def by(val): + def by(val) -> bool: for key in attrs.keys(): try: if attrs[key] != val[key]: diff --git a/libtmux/window.py b/libtmux/window.py index 1e602b5d4..386310005 100644 --- a/libtmux/window.py +++ b/libtmux/window.py @@ -74,7 +74,7 @@ def _info(self, *args): attrs = {"window_id": self._window_id} # from https://github.com/serkanyersen/underscore.py - def by(val): + def by(val) -> bool: for key in attrs.keys(): try: if attrs[key] != val[key]: