Skip to content

Commit 3a073f6

Browse files
committed
Syntax modernization
1 parent 73d2bc5 commit 3a073f6

19 files changed

+93
-136
lines changed

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ flake8:
3838

3939
watch_flake8:
4040
if command -v entr > /dev/null; then ${PY_FILES} | entr -c $(MAKE) flake8; else $(MAKE) flake8 entr_warn; fi
41+
42+
futurize:
43+
futurize --write --nobackups `${PY_FILES}`
44+
45+
pyupgrade:
46+
pyupgrade `${PY_FILES}` --py37-plus
47+
48+
autoflake:
49+
autoflake `${PY_FILES}` -i
50+
51+
2to3-fixer:
52+
2to3-3.8 -w -n -f $(fixer) `${PY_FILES}`

docs/conf.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
import inspect
32
import os
43
import sys
@@ -109,8 +108,8 @@ def setup(app):
109108
latex_documents = [
110109
(
111110
'index',
112-
'{0}.tex'.format(about['__package_name__']),
113-
'{0} Documentation'.format(about['__title__']),
111+
'{}.tex'.format(about['__package_name__']),
112+
'{} Documentation'.format(about['__title__']),
114113
about['__author__'],
115114
'manual',
116115
)
@@ -120,7 +119,7 @@ def setup(app):
120119
(
121120
'index',
122121
about['__package_name__'],
123-
'{0} Documentation'.format(about['__title__']),
122+
'{} Documentation'.format(about['__title__']),
124123
about['__author__'],
125124
1,
126125
)
@@ -129,8 +128,8 @@ def setup(app):
129128
texinfo_documents = [
130129
(
131130
'index',
132-
'{0}'.format(about['__package_name__']),
133-
'{0} Documentation'.format(about['__title__']),
131+
'{}'.format(about['__package_name__']),
132+
'{} Documentation'.format(about['__title__']),
134133
about['__author__'],
135134
about['__package_name__'],
136135
about['__description__'],
@@ -196,14 +195,14 @@ def linkcode_resolve(domain, info): # NOQA: C901
196195
fn = relpath(fn, start=dirname(libtmux.__file__))
197196

198197
if 'dev' in about['__version__']:
199-
return "%s/blob/master/%s/%s%s" % (
198+
return "{}/blob/master/{}/{}{}".format(
200199
about['__github__'],
201200
about['__package_name__'],
202201
fn,
203202
linespec,
204203
)
205204
else:
206-
return "%s/blob/v%s/%s/%s%s" % (
205+
return "{}/blob/v{}/{}/{}{}".format(
207206
about['__github__'],
208207
about['__version__'],
209208
about['__package_name__'],

libtmux/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
# flake8: NOQA
32
from .__about__ import (
43
__author__,

libtmux/_compat.py

+15-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# -*- coding: utf8 -*-
21
# flake8: NOQA
2+
from future import standard_library
3+
standard_library.install_aliases()
34
import sys
45

56
PY2 = sys.version_info[0] == 2
@@ -8,34 +9,26 @@
89

910

1011
if PY2:
11-
unichr = unichr
12-
text_type = unicode
13-
string_types = (str, unicode)
14-
integer_types = (int, long)
15-
from urllib import urlretrieve
12+
chr = chr
13+
text_type = str
14+
string_types = (str, str)
15+
integer_types = (int, int)
1616

1717
text_to_native = lambda s, enc: s.encode(enc)
1818

19-
iterkeys = lambda d: d.iterkeys()
20-
itervalues = lambda d: d.itervalues()
21-
iteritems = lambda d: d.iteritems()
19+
iterkeys = lambda d: iter(d.keys())
20+
itervalues = lambda d: iter(d.values())
21+
iteritems = lambda d: iter(d.items())
2222

23-
from itertools import imap, izip
23+
2424

25-
import ConfigParser as configparser
26-
import cPickle as pickle
27-
from cStringIO import StringIO as BytesIO
28-
from StringIO import StringIO
2925

3026
range_type = xrange
3127

3228
cmp = cmp
3329

3430
input = raw_input
35-
from collections import MutableMapping
36-
from string import lower as ascii_lowercase
3731

38-
import urlparse
3932

4033
exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')
4134

@@ -49,20 +42,17 @@ def console_to_str(s):
4942

5043

5144
else:
52-
unichr = chr
45+
chr = chr
5346
text_type = str
5447
string_types = (str,)
5548
integer_types = (int,)
5649

5750
text_to_native = lambda s, enc: s
5851

59-
iterkeys = lambda d: iter(d.keys())
60-
itervalues = lambda d: iter(d.values())
61-
iteritems = lambda d: iter(d.items())
52+
iterkeys = lambda d: iter(list(d.keys()))
53+
itervalues = lambda d: iter(list(d.values()))
54+
iteritems = lambda d: iter(list(d.items()))
6255

63-
import configparser
64-
import pickle
65-
from io import BytesIO, StringIO
6656

6757
izip = zip
6858
imap = map
@@ -71,16 +61,11 @@ def console_to_str(s):
7161
cmp = lambda a, b: (a > b) - (a < b)
7262

7363
input = input
74-
import urllib.parse as urllib
75-
import urllib.parse as urlparse
76-
from string import ascii_lowercase
77-
from urllib.request import urlretrieve
7864

7965
console_encoding = sys.__stdout__.encoding
8066

8167
implements_to_string = _identity
8268

83-
from collections.abc import MutableMapping
8469

8570
def console_to_str(s):
8671
""" From pypa/pip project, pip.backwardwardcompat. License MIT. """
@@ -91,7 +76,7 @@ def console_to_str(s):
9176

9277
def reraise(tp, value, tb=None):
9378
if value.__traceback__ is not tb:
94-
raise (value.with_traceback(tb))
79+
raise value
9580
raise value
9681

9782

libtmux/common.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# -*- coding: utf-8 -*-
21
# flake8: NOQA W605
32
"""Helper methods and mixins.
43
54
libtmux.common
65
~~~~~~~~~~~~~~
76
87
"""
9-
import collections
108
import logging
119
import os
1210
import re
@@ -27,7 +25,7 @@
2725
TMUX_MAX_VERSION = '2.4'
2826

2927

30-
class EnvironmentMixin(object):
28+
class EnvironmentMixin:
3129

3230
"""
3331
Mixin class for managing session and server level environment variables in
@@ -143,7 +141,7 @@ def show_environment(self, name=None):
143141
return vars_dict
144142

145143

146-
class tmux_cmd(object):
144+
class tmux_cmd:
147145

148146
"""
149147
:term:`tmux(1)` command via :py:mod:`subprocess`.
@@ -193,7 +191,7 @@ def __init__(self, *args, **kwargs):
193191
append_env_path=kwargs.get('append_env_path', True),
194192
)
195193
if not tmux_bin:
196-
raise (exc.TmuxCommandNotFound)
194+
raise exc
197195

198196
cmd = [tmux_bin]
199197
cmd += args # add the command arguments to cmd
@@ -208,23 +206,23 @@ def __init__(self, *args, **kwargs):
208206
stdout, stderr = self.process.communicate()
209207
returncode = self.process.returncode
210208
except Exception as e:
211-
logger.error('Exception for %s: \n%s' % (subprocess.list2cmdline(cmd), e))
209+
logger.error(f'Exception for {subprocess.list2cmdline(cmd)}: \n{e}')
212210

213211
self.returncode = returncode
214212

215213
self.stdout = console_to_str(stdout)
216214
self.stdout = self.stdout.split('\n')
217-
self.stdout = list(filter(None, self.stdout)) # filter empty values
215+
self.stdout = list(_f for _f in self.stdout if _f) # filter empty values
218216

219217
self.stderr = console_to_str(stderr)
220218
self.stderr = self.stderr.split('\n')
221-
self.stderr = list(filter(None, self.stderr)) # filter empty values
219+
self.stderr = list(_f for _f in self.stderr if _f) # filter empty values
222220

223221
if 'has-session' in cmd and len(self.stderr):
224222
if not self.stdout:
225223
self.stdout = self.stderr[0]
226224

227-
logger.debug('self.stdout for %s: \n%s' % (' '.join(cmd), self.stdout))
225+
logger.debug('self.stdout for {}: \n{}'.format(' '.join(cmd), self.stdout))
228226

229227

230228
class TmuxMappingObject(MutableMapping):
@@ -261,22 +259,22 @@ def __delitem__(self, key):
261259

262260
def keys(self):
263261
"""Return list of keys."""
264-
return self._info.keys()
262+
return list(self._info.keys())
265263

266264
def __iter__(self):
267265
return self._info.__iter__()
268266

269267
def __len__(self):
270-
return len(self._info.keys())
268+
return len(list(self._info.keys()))
271269

272270
def __getattr__(self, key):
273271
try:
274272
return self._info[self.formatter_prefix + key]
275273
except KeyError:
276-
raise AttributeError('%s has no property %s' % (self.__class__, key))
274+
raise AttributeError(f'{self.__class__} has no property {key}')
277275

278276

279-
class TmuxRelationalObject(object):
277+
class TmuxRelationalObject:
280278

281279
"""Base Class for managing tmux object child entities. .. # NOQA
282280
@@ -334,7 +332,7 @@ def where(self, attrs, first=False):
334332

335333
# from https://github.com/serkanyersen/underscore.py
336334
def by(val, *args):
337-
for key, value in attrs.items():
335+
for key, value in list(attrs.items()):
338336
try:
339337
if attrs[key] != val[key]:
340338
return False
@@ -429,8 +427,8 @@ def _is_executable_file_or_link(exe):
429427
if _is_executable_file_or_link(full_path):
430428
return full_path
431429
logger.info(
432-
'\'{0}\' could not be found in the following search path: '
433-
'\'{1}\''.format(exe, search_path)
430+
'\'{}\' could not be found in the following search path: '
431+
'\'{}\''.format(exe, search_path)
434432
)
435433

436434
return None

libtmux/exc.py

-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
# -*- coding: utf-8 -*-
21
"""libtmux exceptions.
32
43
libtmux.exc
54
~~~~~~~~~~~
65
76
"""
8-
from __future__ import absolute_import, unicode_literals, with_statement
97

108

119
class LibTmuxException(Exception):
@@ -17,53 +15,45 @@ class TmuxSessionExists(LibTmuxException):
1715

1816
"""Session does not exist in the server."""
1917

20-
pass
2118

2219

2320
class TmuxCommandNotFound(LibTmuxException):
2421

2522
"""Application binary for tmux not found."""
2623

27-
pass
2824

2925

3026
class VersionTooLow(LibTmuxException):
3127

3228
"""Raised if tmux below the minimum version to use libtmux."""
3329

34-
pass
3530

3631

3732
class BadSessionName(LibTmuxException):
3833

3934
"""Disallowed session name for tmux (empty, contains periods or colons)."""
4035

41-
pass
4236

4337

4438
class OptionError(LibTmuxException):
4539

4640
"""Root error for any error involving invalid, ambiguous or bad options."""
4741

48-
pass
4942

5043

5144
class UnknownOption(OptionError):
5245

5346
"""Option unknown to tmux show-option(s) or show-window-option(s)."""
5447

55-
pass
5648

5749

5850
class InvalidOption(OptionError):
5951

6052
"""Option invalid to tmux, introduced in tmux v2.4."""
6153

62-
pass
6354

6455

6556
class AmbiguousOption(OptionError):
6657

6758
"""Option that could potentially match more than one."""
6859

69-
pass

libtmux/formats.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""Format variables for tmux objects.
32
43
libtmux.formats
@@ -8,7 +7,6 @@
87
98
"""
109

11-
from __future__ import absolute_import, unicode_literals, with_statement
1210

1311
SESSION_FORMATS = [
1412
'session_name',

libtmux/pane.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# -*- coding: utf-8 -*-
21
# flake8: NOQA W605
32
"""Pythonization of the :ref:`tmux(1)` pane.
43
54
libtmux.pane
65
~~~~~~~~~~~~
76
87
"""
9-
from __future__ import absolute_import, unicode_literals, with_statement
108

119
import logging
1210

@@ -67,7 +65,7 @@ def _info(self, *args):
6765

6866
# from https://github.com/serkanyersen/underscore.py
6967
def by(val, *args):
70-
for key, value in attrs.items():
68+
for key, value in list(attrs.items()):
7169
try:
7270
if attrs[key] != val[key]:
7371
return False
@@ -279,4 +277,4 @@ def select_pane(self):
279277
return self.window.select_pane(self.get('pane_id'))
280278

281279
def __repr__(self):
282-
return "%s(%s %s)" % (self.__class__.__name__, self.get('pane_id'), self.window)
280+
return "{}({} {})".format(self.__class__.__name__, self.get('pane_id'), self.window)

0 commit comments

Comments
 (0)