Skip to content

Commit 5fa2da0

Browse files
committed
_compat module for py2/py3. Transition. Reorganize imports on some modules.
1 parent 96b1d33 commit 5fa2da0

23 files changed

+137
-74
lines changed

tmuxp/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,3 @@
2020
from .pane import Pane
2121
from .workspacebuilder import WorkspaceBuilder
2222
from . import config, util, cli
23-
24-
import logging

tmuxp/_compat.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import sys
2+
3+
PY2 = sys.version_info[0] == 2
4+
5+
_identity = lambda x: x
6+
7+
8+
if not PY2:
9+
text_type = str
10+
string_types = (str,)
11+
integer_types = (int, )
12+
unichr = chr
13+
14+
text_to_native = lambda s, enc: s
15+
16+
iterkeys = lambda d: iter(d.keys())
17+
itervalues = lambda d: iter(d.values())
18+
iteritems = lambda d: iter(d.items())
19+
20+
from io import StringIO, BytesIO
21+
import pickle
22+
23+
izip = zip
24+
imap = map
25+
range_type = range
26+
27+
cmp = lambda a, b: (a > b) - (a < b)
28+
29+
input = input
30+
from string import ascii_lowercase
31+
import urllib.parse as urllib
32+
import urllib.parse as urlparse
33+
else:
34+
text_type = unicode
35+
string_types = (str, unicode)
36+
integer_types = (int, long)
37+
38+
text_to_native = lambda s, enc: s.encode(enc)
39+
unichr = unichr
40+
41+
iterkeys = lambda d: d.iterkeys()
42+
itervalues = lambda d: d.itervalues()
43+
iteritems = lambda d: d.iteritems()
44+
45+
from cStringIO import StringIO as BytesIO
46+
from StringIO import StringIO
47+
import cPickle as pickle
48+
49+
from itertools import izip, imap
50+
range_type = xrange
51+
52+
cmp = cmp
53+
54+
input = raw_input
55+
from string import lower as ascii_lowercase
56+
import urlparse
57+
58+
59+
number_types = integer_types + (float,)

tmuxp/cli.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212
import os
1313
import sys
1414
import argparse
15-
import argcomplete
15+
import re
1616
import logging
17+
18+
from distutils.util import strtobool
19+
20+
import argcomplete
1721
import kaptan
22+
1823
from . import log, util, exc, WorkspaceBuilder, Server, config
19-
from .util import ascii_lowercase, input
24+
from ._compat import ascii_lowercase, input
2025
from .workspacebuilder import freeze
21-
from distutils.util import strtobool
2226

2327

24-
import re
2528
VERSIONFILE = os.path.join(
2629
os.path.abspath(os.path.dirname(__file__)), '__init__.py'
2730
)

tmuxp/config.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
"""
1111

1212
from __future__ import absolute_import, division, print_function, with_statement
13+
1314
import os
1415
import copy
1516
import logging
17+
1618
from . import exc
17-
from .util import basestring
19+
from ._compat import string_types
1820

1921
logger = logging.getLogger(__name__)
2022

@@ -60,7 +62,7 @@ def is_config_file(filename, extensions=['.yml', '.yaml', '.json']):
6062
"""
6163

6264
extensions = [extensions] if isinstance(
63-
extensions, basestring) else extensions
65+
extensions, string_types) else extensions
6466
return any(filename.endswith(e) for e in extensions)
6567

6668

@@ -174,18 +176,17 @@ def expand(sconf, cwd=None):
174176
start_path = sconf['start_directory']
175177
if any(start_path.startswith(a) for a in ['.', './']):
176178
start_path = os.path.normpath(os.path.join(cwd, start_path))
177-
178179
sconf['start_directory'] = start_path
179180

180181
if (
181182
'shell_command' in sconf and
182-
isinstance(sconf['shell_command'], basestring)
183+
isinstance(sconf['shell_command'], string_types)
183184
):
184185
sconf['shell_command'] = [sconf['shell_command']]
185186

186187
if (
187188
'shell_command_before' in sconf and
188-
isinstance(sconf['shell_command_before'], basestring)
189+
isinstance(sconf['shell_command_before'], string_types)
189190
):
190191
sconf['shell_command_before'] = [sconf['shell_command_before']]
191192

@@ -201,7 +202,7 @@ def expand(sconf, cwd=None):
201202
p = copy.deepcopy(pconf)
202203
pconf = sconf['panes'][p_index] = {}
203204

204-
if isinstance(p, basestring):
205+
if isinstance(p, string_types):
205206
p = {
206207
'shell_command': [p]
207208
}
@@ -215,7 +216,7 @@ def expand(sconf, cwd=None):
215216
if 'shell_command' in p:
216217
cmd = p['shell_command']
217218

218-
if isinstance(p['shell_command'], basestring):
219+
if isinstance(p['shell_command'], string_types):
219220
cmd = [cmd]
220221

221222
if not cmd or any(a == cmd for a in [None, 'blank', 'pane']):
@@ -343,12 +344,12 @@ def import_tmuxinator(sconf):
343344
if 'pre' in sconf and 'pre_window' in sconf:
344345
tmuxp_config['shell_command'] = sconf['pre']
345346

346-
if isinstance(sconf['pre'], basestring):
347+
if isinstance(sconf['pre'], string_types):
347348
tmuxp_config['shell_command_before'] = [sconf['pre_window']]
348349
else:
349350
tmuxp_config['shell_command_before'] = sconf['pre_window']
350351
elif 'pre' in sconf:
351-
if isinstance(sconf['pre'], basestring):
352+
if isinstance(sconf['pre'], string_types):
352353
tmuxp_config['shell_command_before'] = [sconf['pre']]
353354
else:
354355
tmuxp_config['shell_command_before'] = sconf['pre']
@@ -367,7 +368,7 @@ def import_tmuxinator(sconf):
367368

368369
windowdict['window_name'] = k
369370

370-
if isinstance(v, basestring) or v is None:
371+
if isinstance(v, string_types) or v is None:
371372
windowdict['panes'] = [v]
372373
tmuxp_config['windows'].append(windowdict)
373374
continue

tmuxp/log.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"""
1111

1212
from __future__ import absolute_import, division, print_function, with_statement
13+
1314
import logging
1415
import time
16+
1517
from ._vendor.colorama import init
1618
from ._vendor.colorama import Fore, Back, Style
1719

tmuxp/pane.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
1212
"""
1313
from __future__ import absolute_import, division, print_function, with_statement
14-
from . import util, formats, exc
14+
1515
import logging
1616

17+
from . import util, formats, exc
18+
19+
1720
logger = logging.getLogger(__name__)
1821

1922

tmuxp/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
from __future__ import absolute_import, division, print_function, with_statement
1414

1515
import os
16+
import logging
17+
1618
from .util import tmux, TmuxRelationalObject
1719
from .session import Session
1820
from . import formats, exc
19-
import logging
2021

2122
logger = logging.getLogger(__name__)
2223

tmuxp/session.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
from __future__ import absolute_import, division, print_function, with_statement
1414

1515
import pipes
16+
import logging
17+
1618
from .window import Window
1719
from . import util, formats, exc
18-
import logging
20+
1921
logger = logging.getLogger(__name__)
2022

2123

tmuxp/testsuite/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212

1313
import time
1414
from random import randint
15-
15+
import logging
1616
try:
1717
import unittest2 as unittest
1818
except ImportError: # Python 2.7
1919
import unittest
20+
2021
from . import t
2122
from .. import Server, log, exc
22-
import logging
23+
2324
logger = logging.getLogger(__name__)
2425

2526
TEST_SESSION_PREFIX = 'tmuxp_'

tmuxp/testsuite/test_cli.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212

1313
import os
1414
import shutil
15-
import kaptan
1615
import tempfile
16+
import logging
17+
18+
import kaptan
19+
1720
from .. import config, cli
1821
from ..util import tmux
1922
from .helpers import TestCase
2023

21-
import logging
22-
2324
logger = logging.getLogger(__name__)
2425
TMUXP_DIR = os.path.join(os.path.dirname(__file__), '.tmuxp')
2526

0 commit comments

Comments
 (0)