Skip to content

Commit cd0210f

Browse files
committed
Python 2 and 3 with the same source!
* Fixes Python 3 compatibility issues so one codebase will work for python 2.6, 2.7, 3.2 and 3.3. * Adds six dependency. * Also adds python wheels support to the build/release fab script (softlayer#234)
1 parent 9a91ac5 commit cd0210f

24 files changed

Lines changed: 101 additions & 90 deletions

SoftLayer/API.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"""
88
import time
99

10-
from consts import API_PUBLIC_ENDPOINT, API_PRIVATE_ENDPOINT, USER_AGENT
11-
from transports import make_xml_rpc_api_call
12-
from auth import TokenAuthentication
13-
from config import get_client_settings
10+
from .consts import API_PUBLIC_ENDPOINT, API_PRIVATE_ENDPOINT, USER_AGENT
11+
from .transports import make_xml_rpc_api_call
12+
from .auth import TokenAuthentication
13+
from .config import get_client_settings
1414

1515

1616
__all__ = ['Client', 'TimedClient', 'API_PUBLIC_ENDPOINT',

SoftLayer/CLI/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949

5050
from SoftLayer import Client, TimedClient, SoftLayerError, SoftLayerAPIError
5151
from SoftLayer.consts import VERSION
52-
from helpers import CLIAbort, ArgumentError, format_output, KeyValueTable
53-
from environment import Environment, InvalidCommand, InvalidModule
52+
from .helpers import CLIAbort, ArgumentError, format_output, KeyValueTable
53+
from .environment import Environment, InvalidCommand, InvalidModule
5454

5555

5656
DEBUG_LOGGING_MAP = {

SoftLayer/CLI/environment.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import os.path
1313
import sys
1414

15+
import six
16+
1517
from SoftLayer.CLI.modules import get_module_list
1618
from SoftLayer import SoftLayerError
1719

@@ -90,7 +92,7 @@ def err(self, s, nl=True):
9092
self.stderr.write(os.linesep)
9193

9294
def input(self, prompt):
93-
return raw_input(prompt)
95+
return six.moves.input(prompt)
9496

9597
def getpass(self, prompt):
9698
return getpass.getpass(prompt)

SoftLayer/CLI/formatting.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import os
1111
import json
1212

13+
import six
14+
1315
from prettytable import PrettyTable, FRAME, NONE
1416

1517

@@ -20,7 +22,7 @@ def format_output(data, fmt='table'):
2022
SequentialOutput
2123
:param string fmt (optional): One of: table, raw, json, python
2224
"""
23-
if isinstance(data, basestring):
25+
if isinstance(data, six.string_types):
2426
if fmt == 'json':
2527
return json.dumps(data)
2628
return data
@@ -142,7 +144,7 @@ def active_txn(item):
142144

143145

144146
def valid_response(prompt, *valid):
145-
ans = raw_input(prompt).lower()
147+
ans = six.moves.input(prompt).lower()
146148

147149
if ans in valid:
148150
return True

SoftLayer/CLI/helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
from SoftLayer.utils import NestedDict
1010
from SoftLayer.CLI.environment import CLIRunnable
11-
from exceptions import CLIHalt, CLIAbort, ArgumentError
12-
from formatting import (
11+
from .exceptions import CLIHalt, CLIAbort, ArgumentError
12+
from .formatting import (
1313
Table, KeyValueTable, FormattedItem, SequentialOutput, confirm,
1414
no_going_back, mb_to_gb, gb, listing, blank, format_output,
1515
active_txn, valid_response)
16-
from template import update_with_template_args, export_to_template
16+
from .template import update_with_template_args, export_to_template
1717

1818
__all__ = [
1919
# Core/Misc

SoftLayer/CLI/modules/cci.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,13 @@ def execute(self, args):
245245
t.add_row(['datacenter', listing(datacenters, separator=',')])
246246

247247
if args['--cpu'] or show_all:
248-
standard_cpu = filter(
249-
lambda x: not x['template'].get(
250-
'dedicatedAccountHostOnlyFlag', False),
251-
result['processors'])
248+
standard_cpu = [x for x in result['processors']
249+
if not x['template'].get(
250+
'dedicatedAccountHostOnlyFlag', False)]
252251

253-
ded_cpu = filter(
254-
lambda x: x['template'].get(
255-
'dedicatedAccountHostOnlyFlag', False),
256-
result['processors'])
252+
ded_cpu = [x for x in result['processors']
253+
if x['template'].get('dedicatedAccountHostOnlyFlag',
254+
False)]
257255

258256
def cpus_row(c, name):
259257
cpus = []
@@ -284,19 +282,16 @@ def cpus_row(c, name):
284282
for summary in sorted(os_summary):
285283
t.add_row([
286284
'os (%s)' % summary,
287-
linesep.join(sorted(filter(
288-
lambda x: x[0:len(summary)] == summary, op_sys))
289-
)
285+
linesep.join(sorted([x for x in op_sys
286+
if x[0:len(summary)] == summary]))
290287
])
291288

292289
if args['--disk'] or show_all:
293-
local_disks = filter(
294-
lambda x: x['template'].get('localDiskFlag', False),
295-
result['blockDevices'])
290+
local_disks = [x for x in result['blockDevices']
291+
if x['template'].get('localDiskFlag', False)]
296292

297-
san_disks = filter(
298-
lambda x: not x['template'].get('localDiskFlag', False),
299-
result['blockDevices'])
293+
san_disks = [x for x in result['blockDevices']
294+
if not x['template'].get('localDiskFlag', False)]
300295

301296
def block_rows(blocks, name):
302297
simple = {}
@@ -877,7 +872,7 @@ def sync_a_record():
877872
instance['primaryIpAddress'],
878873
ttl=args['--ttl'])
879874
else:
880-
recs = filter(lambda x: x['type'].lower() == 'a', records)
875+
recs = [x for x in records if x['type'].lower() == 'a']
881876
if len(recs) != 1:
882877
raise CLIAbort("Aborting A record sync, found %d "
883878
"A record exists!" % len(recs))

SoftLayer/CLI/modules/config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
import os.path
1313

14+
import six
15+
1416
from SoftLayer import (
1517
Client, SoftLayerAPIError, API_PUBLIC_ENDPOINT, API_PRIVATE_ENDPOINT)
1618
from SoftLayer.CLI import (
1719
CLIRunnable, CLIAbort, KeyValueTable, confirm, format_output)
18-
import ConfigParser
1920

2021

2122
def get_settings_from_client(client):
@@ -144,19 +145,19 @@ def execute(self, args):
144145

145146
# Persist the config file. Read the target config file in before
146147
# setting the values to avoid clobbering settings
147-
config = ConfigParser.RawConfigParser()
148+
config = six.moves.configparser.RawConfigParser()
148149
config.read(config_path)
149150
try:
150151
config.add_section('softlayer')
151-
except ConfigParser.DuplicateSectionError:
152+
except six.moves.configparser.DuplicateSectionError:
152153
pass
153154

154155
config.set('softlayer', 'username', settings['username'])
155156
config.set('softlayer', 'api_key', settings['api_key'])
156157
config.set('softlayer', 'endpoint_url', settings['endpoint_url'])
157158

158159
f = os.fdopen(os.open(
159-
config_path, (os.O_WRONLY | os.O_CREAT), 0600), 'w')
160+
config_path, (os.O_WRONLY | os.O_CREAT), 0o600), 'w')
160161
try:
161162
config.write(f)
162163
finally:

SoftLayer/CLI/modules/server.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,8 @@ def execute(self, args):
268268
t.align['Reason'] = 'l'
269269

270270
mgr = HardwareManager(self.client)
271-
reasons = mgr.get_cancellation_reasons().iteritems()
272271

273-
for code, reason in reasons:
272+
for code, reason in mgr.get_cancellation_reasons().items():
274273
t.add_row([code, reason])
275274

276275
return t

SoftLayer/CLI/modules/summary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def execute(self, args):
3131
])
3232
t.sortby = args.get('--sortby') or 'datacenter'
3333

34-
for name, dc in datacenters.iteritems():
34+
for name, dc in datacenters.items():
3535
t.add_row([
3636
name,
3737
dc['vlanCount'],

SoftLayer/CLI/template.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
:license: MIT, see LICENSE for more details.
99
"""
1010
import os.path
11-
import ConfigParser
12-
import StringIO
11+
import six
1312

14-
from exceptions import ArgumentError
13+
from .exceptions import ArgumentError
1514

1615

1716
def update_with_template_args(args):
@@ -26,10 +25,10 @@ def update_with_template_args(args):
2625
'File does not exist [-t | --template] = %s'
2726
% template_path)
2827

29-
config = ConfigParser.ConfigParser()
28+
config = six.moves.configparser.ConfigParser()
3029
ini_str = '[settings]\n' + open(
3130
os.path.expanduser(template_path), 'r').read()
32-
ini_fp = StringIO.StringIO(ini_str)
31+
ini_fp = six.StringIO(ini_str)
3332
config.readfp(ini_fp)
3433

3534
# Merge template options with the options passed in

0 commit comments

Comments
 (0)