Skip to content

Refactor: Cleaning up #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion libtmux/_compat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# flake8: NOQA
import sys
import typing as t
from collections.abc import MutableMapping

console_encoding = sys.__stdout__.encoding

Expand Down
22 changes: 12 additions & 10 deletions libtmux/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -62,7 +63,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)

Expand All @@ -83,7 +84,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)

Expand All @@ -103,7 +104,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)

Expand Down Expand Up @@ -336,19 +337,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 target_children

def get_by_id(self, id):
"""
Expand Down
12 changes: 7 additions & 5 deletions libtmux/pane.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> bool:
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.
Expand Down
25 changes: 12 additions & 13 deletions libtmux/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ def _info(self):

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

def by(val):
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_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)

Expand Down Expand Up @@ -316,21 +318,18 @@ 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

if len(active_windows) == int(1):
# 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) == 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:
Expand Down
22 changes: 11 additions & 11 deletions libtmux/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> bool:
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.
Expand Down Expand Up @@ -501,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
Expand Down