From 6589275ce21a0039d9ec4fd1a50426a75d8d66de Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 5 Mar 2022 15:09:25 -0600 Subject: [PATCH] pyupgrade pip install pyupgrade autoflake; \ pyupgrade --py37 tests/**/*.py docs/**/*.py libtmux/**/*.py; \ poetry run autoflake tests/**/*.py docs/**/*.py libtmux/**/*.py -i --ignore-init-module-imports --exclude libtmux/_compat.py; \ make black isort --- docs/conf.py | 4 ++-- libtmux/_compat.py | 1 - libtmux/common.py | 17 ++++++++--------- libtmux/exc.py | 16 ---------------- libtmux/pane.py | 4 +++- libtmux/server.py | 26 ++++++++++++-------------- libtmux/session.py | 7 +++---- libtmux/test.py | 2 +- libtmux/window.py | 14 +++++++------- tests/conftest.py | 1 - tests/test_common.py | 8 ++++---- 11 files changed, 40 insertions(+), 60 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index ca9d2faf5..4926d296b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -187,14 +187,14 @@ def linkcode_resolve(domain, info): # NOQA: C901 fn = relpath(fn, start=dirname(libtmux.__file__)) if "dev" in about["__version__"]: - return "%s/blob/master/%s/%s%s" % ( + return "{}/blob/master/{}/{}{}".format( about["__github__"], about["__package_name__"], fn, linespec, ) else: - return "%s/blob/v%s/%s/%s%s" % ( + return "{}/blob/v{}/{}/{}{}".format( about["__github__"], about["__version__"], about["__package_name__"], diff --git a/libtmux/_compat.py b/libtmux/_compat.py index e735bed9d..7c17c0bc1 100644 --- a/libtmux/_compat.py +++ b/libtmux/_compat.py @@ -1,4 +1,3 @@ -# -*- coding: utf8 -*- # flake8: NOQA import sys from collections.abc import MutableMapping diff --git a/libtmux/common.py b/libtmux/common.py index f0dd9c2f6..81ed05767 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -5,7 +5,6 @@ ~~~~~~~~~~~~~~ """ -import collections import logging import os import re @@ -26,7 +25,7 @@ TMUX_MAX_VERSION = "2.4" -class EnvironmentMixin(object): +class EnvironmentMixin: """ Mixin class for managing session and server level environment variables in @@ -142,7 +141,7 @@ def show_environment(self, name=None): return vars_dict -class tmux_cmd(object): +class tmux_cmd: """ :term:`tmux(1)` command via :py:mod:`subprocess`. @@ -207,7 +206,7 @@ def __init__(self, *args, **kwargs): stdout, stderr = self.process.communicate() returncode = self.process.returncode except Exception as e: - logger.error("Exception for %s: \n%s" % (subprocess.list2cmdline(cmd), e)) + logger.error(f"Exception for {subprocess.list2cmdline(cmd)}: \n{e}") self.returncode = returncode @@ -223,7 +222,7 @@ def __init__(self, *args, **kwargs): if not self.stdout: self.stdout = self.stderr[0] - logger.debug("self.stdout for %s: \n%s" % (" ".join(cmd), self.stdout)) + logger.debug("self.stdout for {}: \n{}".format(" ".join(cmd), self.stdout)) class TmuxMappingObject(MutableMapping): @@ -272,10 +271,10 @@ def __getattr__(self, key): try: return self._info[self.formatter_prefix + key] except KeyError: - raise AttributeError("%s has no property %s" % (self.__class__, key)) + raise AttributeError(f"{self.__class__} has no property {key}") -class TmuxRelationalObject(object): +class TmuxRelationalObject: """Base Class for managing tmux object child entities. .. # NOQA @@ -428,8 +427,8 @@ def _is_executable_file_or_link(exe): if _is_executable_file_or_link(full_path): return full_path logger.info( - "'{0}' could not be found in the following search path: " - "'{1}'".format(exe, search_path) + "'{}' could not be found in the following search path: " + "'{}'".format(exe, search_path) ) return None diff --git a/libtmux/exc.py b/libtmux/exc.py index 68a8cddb3..541bcbca2 100644 --- a/libtmux/exc.py +++ b/libtmux/exc.py @@ -15,53 +15,37 @@ class TmuxSessionExists(LibTmuxException): """Session does not exist in the server.""" - pass - class TmuxCommandNotFound(LibTmuxException): """Application binary for tmux not found.""" - pass - class VersionTooLow(LibTmuxException): """Raised if tmux below the minimum version to use libtmux.""" - pass - class BadSessionName(LibTmuxException): """Disallowed session name for tmux (empty, contains periods or colons).""" - pass - class OptionError(LibTmuxException): """Root error for any error involving invalid, ambiguous or bad options.""" - pass - class UnknownOption(OptionError): """Option unknown to tmux show-option(s) or show-window-option(s).""" - pass - class InvalidOption(OptionError): """Option invalid to tmux, introduced in tmux v2.4.""" - pass - class AmbiguousOption(OptionError): """Option that could potentially match more than one.""" - - pass diff --git a/libtmux/pane.py b/libtmux/pane.py index c229f8fb0..510da1d32 100644 --- a/libtmux/pane.py +++ b/libtmux/pane.py @@ -276,4 +276,6 @@ def select_pane(self): return self.window.select_pane(self.get("pane_id")) def __repr__(self): - return "%s(%s %s)" % (self.__class__.__name__, self.get("pane_id"), self.window) + return "{}({} {})".format( + self.__class__.__name__, self.get("pane_id"), self.window + ) diff --git a/libtmux/server.py b/libtmux/server.py index ac62ad546..8129260f7 100644 --- a/libtmux/server.py +++ b/libtmux/server.py @@ -75,7 +75,7 @@ def __init__( socket_path=None, config_file=None, colors=None, - **kwargs + **kwargs, ): EnvironmentMixin.__init__(self, "-g") self._windows = [] @@ -110,11 +110,11 @@ def cmd(self, *args, **kwargs): args = list(args) if self.socket_name: - args.insert(0, "-L{}".format(self.socket_name)) + args.insert(0, f"-L{self.socket_name}") if self.socket_path: - args.insert(0, "-S{}".format(self.socket_path)) + args.insert(0, f"-S{self.socket_path}") if self.config_file: - args.insert(0, "-f{}".format(self.config_file)) + args.insert(0, f"-f{self.config_file}") if self.colors: if self.colors == 256: args.insert(0, "-2") @@ -160,9 +160,7 @@ def _list_sessions(self): ] # clear up empty dict - sessions = [ - dict((k, v) for k, v in session.items() if v) for session in sessions - ] + sessions = [{k: v for k, v in session.items() if v} for session in sessions] return sessions @@ -228,7 +226,7 @@ def _list_windows(self): ] # clear up empty dict - windows = [dict((k, v) for k, v in window.items() if v) for window in windows] + windows = [{k: v for k, v in window.items() if v} for window in windows] # tmux < 1.8 doesn't have window_id, use window_name for w in windows: @@ -299,9 +297,9 @@ def _list_panes(self): # clear up empty dict panes = [ - dict( - (k, v) for k, v in window.items() if v or k == "pane_current_path" - ) # preserve pane_current_path, in case it entered a new process + { + k: v for k, v in window.items() if v or k == "pane_current_path" + } # preserve pane_current_path, in case it entered a new process # where we may not get a cwd from. for window in panes ] @@ -372,7 +370,7 @@ def has_session(self, target_session, exact=True): session_check_name(target_session) if exact and has_gte_version("2.1"): - target_session = "={}".format(target_session) + target_session = f"={target_session}" proc = self.cmd("has-session", "-t%s" % target_session) @@ -464,7 +462,7 @@ def new_session( window_name=None, window_command=None, *args, - **kwargs + **kwargs, ): """ Return :class:`Session` from ``$ tmux new-session``. @@ -570,7 +568,7 @@ def new_session( session = dict(zip(sformats, session.split(formats.FORMAT_SEPARATOR))) # clear up empty dict - session = dict((k, v) for k, v in session.items() if v) + session = {k: v for k, v in session.items() if v} session = Session(server=self, **session) diff --git a/libtmux/session.py b/libtmux/session.py index ffacd1f30..0b3c09b3e 100644 --- a/libtmux/session.py +++ b/libtmux/session.py @@ -159,7 +159,6 @@ def rename_session(self, new_name): - https://www.mail-archive.com/tech@openbsd.org/msg45186.html - https://marc.info/?l=openbsd-cvs&m=152183263526828&w=2 """ - pass else: raise exc.LibTmuxException(proc.stderr) @@ -242,7 +241,7 @@ def new_window( window = dict(zip(wformats, window.split(formats.FORMAT_SEPARATOR))) # clear up empty dict - window = dict((k, v) for k, v in window.items() if v) + window = {k: v for k, v in window.items() if v} window = Window(session=self, **window) self.server._update_windows() @@ -358,7 +357,7 @@ def select_window(self, target_window): # Note that we also provide the session ID here, since cmd() # will not automatically add it as there is already a '-t' # argument provided. - target = "-t%s:%s" % (self._session_id, target_window) + target = f"-t{self._session_id}:{target_window}" proc = self.cmd("select-window", target) @@ -511,4 +510,4 @@ def show_option(self, option, _global=False): return option[1] def __repr__(self): - return "%s(%s %s)" % (self.__class__.__name__, self.id, self.name) + return f"{self.__class__.__name__}({self.id} {self.name})" diff --git a/libtmux/test.py b/libtmux/test.py index 6cafb7f3c..73f10eb83 100644 --- a/libtmux/test.py +++ b/libtmux/test.py @@ -194,7 +194,7 @@ def temp_window(session, *args, **kwargs): return -class EnvironmentVarGuard(object): +class EnvironmentVarGuard: """Mock environmental variables safetly. diff --git a/libtmux/window.py b/libtmux/window.py index a06d68f06..dbd3052df 100644 --- a/libtmux/window.py +++ b/libtmux/window.py @@ -54,7 +54,7 @@ def __init__(self, session=None, **kwargs): self._window_id = kwargs["window_id"] def __repr__(self): - return "%s(%s %s:%s, %s)" % ( + return "{}({} {}:{}, {})".format( self.__class__.__name__, self.id, self.index, @@ -137,7 +137,7 @@ def select_layout(self, layout=None): 'custom' custom dimensions (see :term:`tmux(1)` manpages). """ - cmd = ["select-layout", "-t%s:%s" % (self.get("session_id"), self.index)] + cmd = ["select-layout", "-t{}:{}".format(self.get("session_id"), self.index)] if layout: # tmux allows select-layout without args cmd.append(layout) @@ -174,7 +174,7 @@ def set_window_option(self, option, value): cmd = self.cmd( "set-window-option", - "-t%s:%s" % (self.get("session_id"), self.index), + "-t{}:{}".format(self.get("session_id"), self.index), # '-t%s' % self.id, option, value, @@ -302,7 +302,7 @@ def kill_window(self): proc = self.cmd( "kill-window", # '-t:%s' % self.id - "-t%s:%s" % (self.get("session_id"), self.index), + "-t{}:{}".format(self.get("session_id"), self.index), ) if proc.stderr: @@ -326,8 +326,8 @@ def move_window(self, destination="", session=None): session = session or self.get("session_id") proc = self.cmd( "move-window", - "-s%s:%s" % (self.get("session_id"), self.index), - "-t%s:%s" % (session, destination), + "-s{}:{}".format(self.get("session_id"), self.index), + f"-t{session}:{destination}", ) if proc.stderr: @@ -481,7 +481,7 @@ def split_window( pane = dict(zip(pformats, pane.split(formats.FORMAT_SEPARATOR))) # clear up empty dict - pane = dict((k, v) for k, v in pane.items() if v) + pane = {k: v for k, v in pane.items() if v} return Pane(window=self, **pane) diff --git a/tests/conftest.py b/tests/conftest.py index 0ce651b5e..fad000af7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -79,7 +79,6 @@ def session(request, server): """ try: server.switch_client(session.get("session_id")) - pass except exc.LibTmuxException: # server.attach_session(session.get('session_id')) pass diff --git a/tests/test_common.py b/tests/test_common.py index 9d529f4b8..a86da3bb0 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -29,7 +29,7 @@ def test_allows_master_version(monkeypatch): def mock_tmux_cmd(param): - class Hi(object): + class Hi: stdout = ["tmux master"] stderr = None @@ -47,7 +47,7 @@ class Hi(object): def test_allows_next_version(monkeypatch): def mock_tmux_cmd(param): - class Hi(object): + class Hi: stdout = ["tmux next-2.9"] stderr = None @@ -63,7 +63,7 @@ class Hi(object): def test_get_version_openbsd(monkeypatch): def mock_tmux_cmd(param): - class Hi(object): + class Hi: stderr = ["tmux: unknown option -- V"] return Hi() @@ -80,7 +80,7 @@ class Hi(object): def test_get_version_too_low(monkeypatch): def mock_tmux_cmd(param): - class Hi(object): + class Hi: stderr = ["tmux: unknown option -- V"] return Hi()