Skip to content

Commit e723571

Browse files
committed
Updates to /tools for Python 3 compatibility
1 parent 30bab09 commit e723571

32 files changed

+107
-88
lines changed

tools/export/exporters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from tools.targets import TARGET_MAP
2929
from tools.utils import mkdir
3030
from tools.resources import FileType, FileRef
31+
from future.utils import with_metaclass
3132

3233
"""Just a template for subclassing"""
3334

@@ -57,14 +58,13 @@ def __init__(*args, **kwargs):
5758
CLS.NAME = "%s (DEPRECATED)" % old_name
5859
return CLS
5960

60-
class Exporter(object):
61+
class Exporter(with_metaclass(ABCMeta, object)):
6162
"""Exporter base class
6263
6364
This class is meant to be extended by individual exporters, and provides a
6465
few helper methods for implementing an exporter with either jinja2 or
6566
progen.
6667
"""
67-
__metaclass__ = ABCMeta
6868
TEMPLATE_DIR = dirname(__file__)
6969
DOT_IN_RELATIVE_PATH = False
7070
NAME = None

tools/flash_algo/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def format_algo_data(self, spaces, group_size, fmt):
134134
blob = self.algo_data[:]
135135
pad_size = 0 if len(blob) % 4 == 0 else 4 - len(blob) % 4
136136
blob = blob + "\x00" * pad_size
137-
integer_list = struct.unpack("<" + "L" * (len(blob) / 4), blob)
137+
integer_list = struct.unpack("<" + "L" * (len(blob) // 4), blob)
138138
line_list = []
139139
for pos in range(0, len(integer_list), group_size):
140140
group = ["0x%08x" % value for value in

tools/host_tests/echo_flow_control.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
from host_test import Test
17+
from .host_test import Test
1818

1919

2020
class EchoTest(Test):

tools/host_tests/hello_auto.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
class HelloTest():
19+
class HelloTest(object):
1920
HELLO_WORLD = "Hello World"
2021

2122
def test(self, selftest):

tools/host_tests/host_registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
limitations under the License.
1616
"""
1717

18-
class HostRegistry:
18+
class HostRegistry(object):
1919
""" Class stores registry with host tests and objects representing them
2020
"""
2121
HOST_TESTS = {} # host_test_name -> host_test_ojbect

tools/host_tests/host_test.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
# Check if 'serial' module is installed
1919
try:
2020
from serial import Serial
21-
except ImportError, e:
22-
print "Error: Can't import 'serial' module: %s"% e
21+
except ImportError as e:
22+
print("Error: Can't import 'serial' module: %s"% e)
2323
exit(-1)
2424

2525
import os
@@ -29,7 +29,7 @@
2929
from time import sleep, time
3030
from optparse import OptionParser
3131

32-
import host_tests_plugins
32+
from . import host_tests_plugins
3333

3434
# This is a little tricky. We need to add upper directory to path so
3535
# we can find packages we want from the same level as other files do
@@ -39,7 +39,7 @@
3939
from tools.test_api import get_module_avail
4040

4141

42-
class Mbed:
42+
class Mbed(object):
4343
""" Base class for a host driven test
4444
"""
4545
def __init__(self):
@@ -117,7 +117,7 @@ def __init__(self):
117117
self.serial_timeout = 1
118118

119119
self.timeout = self.DEFAULT_TOUT if self.options.timeout is None else self.options.timeout
120-
print 'MBED: Instrumentation: "%s" and disk: "%s"' % (self.port, self.disk)
120+
print('MBED: Instrumentation: "%s" and disk: "%s"' % (self.port, self.disk))
121121

122122
def init_serial_params(self, serial_baud=9600, serial_timeout=1):
123123
""" Initialize port parameters.
@@ -183,11 +183,11 @@ def pool_for_serial_init(self, serial_baud, serial_timeout, pooling_loops=40, in
183183
stdout.write('.')
184184
stdout.flush()
185185
else:
186-
print "...port ready!"
186+
print("...port ready!")
187187
result = True
188188
break
189189
if not result and last_error:
190-
print last_error
190+
print(last_error)
191191
return result
192192

193193
def set_serial_timeout(self, timeout):
@@ -221,7 +221,7 @@ def serial_readline(self, timeout=5):
221221
c = self.serial.read(1)
222222
result += c
223223
except Exception as e:
224-
print "MBED: %s"% str(e)
224+
print("MBED: %s"% str(e))
225225
result = None
226226
break
227227
if c == '\n':
@@ -298,7 +298,7 @@ def flush(self):
298298
return result
299299

300300

301-
class HostTestResults:
301+
class HostTestResults(object):
302302
""" Test results set by host tests
303303
"""
304304
def __init__(self):
@@ -389,8 +389,8 @@ def run(self):
389389
self.print_result(result)
390390
else:
391391
self.notify("HOST: Passive mode...")
392-
except Exception, e:
393-
print str(e)
392+
except Exception as e:
393+
print(str(e))
394394
self.print_result(self.RESULT_ERROR)
395395

396396
def setup(self):
@@ -406,7 +406,7 @@ def setup(self):
406406
def notify(self, message):
407407
""" On screen notification function
408408
"""
409-
print message
409+
print(message)
410410
stdout.flush()
411411

412412
def print_result(self, result):

tools/host_tests/mbedrpc.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
# >myled.write(1)
2828
# >
2929

30-
import serial, urllib2, time
30+
from future import standard_library
31+
standard_library.install_aliases()
32+
import serial, urllib.request, urllib.error, urllib.parse, time
3133

3234
# mbed super class
33-
class mbed:
35+
class mbed(object):
3436
def __init__(self):
3537
print("This will work as a demo but no transport mechanism has been selected")
3638

@@ -48,7 +50,7 @@ def rpc(self, name, method, args):
4850
# creates the command to be sent serially - /name/method arg1 arg2 arg3 ... argN
4951
str = "/" + name + "/" + method + " " + " ".join(args) + "\n"
5052
# prints the command being executed
51-
print str
53+
print(str)
5254
# writes the command to serial
5355
self.ser.write(str)
5456
# strips trailing characters from the line just written
@@ -61,12 +63,12 @@ def __init__(self, ip):
6163
self.host = "http://" + ip
6264

6365
def rpc(self, name, method, args):
64-
response = urllib2.urlopen(self.host + "/rpc/" + name + "/" + method + "%20" + "%20".join(args))
66+
response = urllib.request.urlopen(self.host + "/rpc/" + name + "/" + method + "%20" + "%20".join(args))
6567
return response.read().strip()
6668

6769

6870
# generic mbed interface super class
69-
class mbed_interface():
71+
class mbed_interface(object):
7072
# initialize an mbed interface with a transport mechanism and pin name
7173
def __init__(self, this_mbed, mpin):
7274
self.mbed = this_mbed
@@ -198,7 +200,7 @@ def read_us(self):
198200
return float(re.search('\d+\.*\d*', r).group(0))
199201

200202
# Serial
201-
class Serial():
203+
class Serial(object):
202204
def __init__(self, this_mbed, tx, rx=""):
203205
self.mbed = this_mbed
204206
if isinstance(tx, str):

tools/host_tests/net_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
from host_test import Test, Simple
17+
from .host_test import Test, Simple
1818
from sys import stdout
1919

2020
class NETTest(Simple):

tools/host_tests/rpc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
from host_test import Test
18-
from mbedrpc import SerialRPC, DigitalOut, DigitalIn, pin
17+
from .host_test import Test
18+
from .mbedrpc import SerialRPC, DigitalOut, DigitalIn, pin
1919

2020

2121
class RpcTest(Test):
@@ -30,7 +30,7 @@ def test(self):
3030

3131
if hasattr(self.mbed.options, 'micro'):
3232
if self.mbed.options.micro == 'M0+':
33-
print "Freedom Board: PTA12 <-> PTC4"
33+
print("Freedom Board: PTA12 <-> PTC4")
3434
p_out = pin("PTA12")
3535
p_in = pin("PTC4")
3636

tools/host_tests/serial_complete_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import string
2222
from sys import stdout
2323

24-
class SerialCompleteTest():
24+
class SerialCompleteTest(object):
2525

2626
def test(self, selftest):
2727
strip_chars = string.whitespace + "\0"

tools/host_tests/serial_nc_rx_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import string
2222
from sys import stdout
2323

24-
class SerialNCRXTest():
24+
class SerialNCRXTest(object):
2525

2626
def test(self, selftest):
2727
selftest.mbed.flush();

tools/host_tests/serial_nc_tx_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import string
2222
from sys import stdout
2323

24-
class SerialNCTXTest():
24+
class SerialNCTXTest(object):
2525

2626
def test(self, selftest):
2727
selftest.mbed.flush();

tools/host_tests/stdio_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import random
2020
from time import time
2121

22-
class StdioTest():
22+
class StdioTest(object):
2323
PATTERN_INT_VALUE = "Your value was: (-?\d+)"
2424
re_detect_int_value = re.compile(PATTERN_INT_VALUE)
2525

tools/host_tests/tcpecho_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
N_PACKETS = 5000
2727
TOT_BITS = float(LEN_PACKET * N_PACKETS * 8) * 2
2828
MEGA = float(1024 * 1024)
29-
UPDATE_STEP = (N_PACKETS/10)
29+
UPDATE_STEP = N_PACKETS // 10
3030

31-
class TCP_EchoClient:
31+
class TCP_EchoClient(object):
3232
def __init__(self, host):
3333
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3434
self.s.connect((host, ECHO_PORT))
@@ -44,10 +44,10 @@ def __packet(self):
4444
def test(self):
4545
start = time()
4646
for i in range(N_PACKETS):
47-
if (i % UPDATE_STEP) == 0: print '%.2f%%' % ((float(i)/float(N_PACKETS)) * 100.)
47+
if (i % UPDATE_STEP) == 0: print('%.2f%%' % ((float(i)/float(N_PACKETS)) * 100.))
4848
self.__packet()
4949
t = time() - start
50-
print 'Throughput: (%.2f)Mbits/s' % ((TOT_BITS / t)/MEGA)
50+
print('Throughput: (%.2f)Mbits/s' % ((TOT_BITS / t)/MEGA))
5151

5252
def __del__(self):
5353
self.s.close()

tools/host_tests/tcpecho_client_auto.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ def handle(self):
3535
if not data: break
3636
self.request.sendall(data)
3737
if '{{end}}' in str(data):
38-
print
38+
print()
3939
print(str(data))
4040
else:
4141
if not count % 10:
4242
sys.stdout.write('.')
4343
count += 1
4444
stdout.flush()
4545

46-
class TCPEchoClientTest():
46+
class TCPEchoClientTest(object):
4747
def send_server_ip_port(self, selftest, ip_address, port_no):
4848
""" Set up network host. Reset target and and send server IP via serial to Mbed
4949
"""

tools/host_tests/tcpecho_server.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
from SocketServer import BaseRequestHandler, TCPServer
17+
try:
18+
from SocketServer import BaseRequestHandler, TCPServer
19+
except ImportError:
20+
from socketserver import BaseRequestHandler, TCPServer
1821
from time import time
1922

2023
from mbed_settings import LOCALHOST
@@ -24,7 +27,7 @@
2427

2528
class TCP_EchoHandler(BaseRequestHandler):
2629
def handle(self):
27-
print "\nconnection received"
30+
print("\nconnection received")
2831
start = time()
2932
bytes = 0
3033
index = 0
@@ -35,16 +38,16 @@ def handle(self):
3538
bytes += len(data)
3639
for n in map(ord, data):
3740
if n != index:
38-
print "data error %d != %d" % (n , index)
41+
print("data error %d != %d" % (n , index))
3942
index += 1
4043
if index > MAX_INDEX:
4144
index = 0
4245

4346
self.request.sendall(data)
4447
t = time() - start
4548
b = float(bytes * 8) * 2
46-
print "Throughput: (%.2f)Mbits/s" % ((b/t)/MEGA)
49+
print("Throughput: (%.2f)Mbits/s" % ((b/t)/MEGA))
4750

4851
server = TCPServer((LOCALHOST, 7), TCP_EchoHandler)
49-
print "listening for connections"
52+
print("listening for connections")
5053
server.serve_forever()

tools/host_tests/tcpecho_server_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import socket
2323
from sys import stdout
2424

25-
class TCPEchoServerTest():
25+
class TCPEchoServerTest(object):
2626
ECHO_SERVER_ADDRESS = ""
2727
ECHO_PORT = 0
2828
ECHO_LOOPs = 100

tools/host_tests/tcpecho_server_loop.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@
2121
sys.path.insert(0, ROOT)
2222

2323
from mbed_settings import LOCALHOST
24-
from SocketServer import BaseRequestHandler, TCPServer
24+
try:
25+
from SocketServer import BaseRequestHandler, TCPServer
26+
except ImportError:
27+
from socketserver import BaseRequestHandler, TCPServer
2528

2629

2730
class TCP_EchoHandler(BaseRequestHandler):
2831
def handle(self):
29-
print "\nHandle connection from:", self.client_address
32+
print("\nHandle connection from:", self.client_address)
3033
while True:
3134
data = self.request.recv(1024)
3235
if not data: break
3336
self.request.sendall(data)
3437
self.request.close()
35-
print "socket closed"
38+
print("socket closed")
3639

3740
if __name__ == '__main__':
3841
server = TCPServer((LOCALHOST, 7), TCP_EchoHandler)
39-
print "listening for connections on:", (LOCALHOST, 7)
42+
print("listening for connections on:", (LOCALHOST, 7))
4043
server.serve_forever()

0 commit comments

Comments
 (0)