Skip to content

Commit 5036aa3

Browse files
author
marcusw
committed
Got rid of unconditional except in locator.py, tab fixing in bluesock.py.
1 parent b4acb04 commit 5036aa3

2 files changed

Lines changed: 47 additions & 58 deletions

File tree

nxt/bluesock.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,62 @@
1414

1515
import bluetooth
1616
import os
17-
from nxt.brick import Brick
17+
from .brick import Brick
1818

1919
class BlueSock(object):
2020

21-
bsize = 118 # Bluetooth socket block size
22-
PORT = 1 # Standard NXT rfcomm port
21+
bsize = 118 # Bluetooth socket block size
22+
PORT = 1 # Standard NXT rfcomm port
2323

24-
def __init__(self, host):
25-
self.host = host
26-
self.sock = None
27-
self.debug = False
24+
def __init__(self, host):
25+
self.host = host
26+
self.sock = None
27+
self.debug = False
2828

29-
def __str__(self):
30-
return 'Bluetooth (%s)' % self.host
29+
def __str__(self):
30+
return 'Bluetooth (%s)' % self.host
3131

32-
def connect(self):
33-
if self.debug:
34-
print 'Connecting via Bluetooth...'
35-
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
36-
sock.connect((self.host, BlueSock.PORT))
37-
self.sock = sock
38-
if self.debug:
39-
print 'Connected.'
40-
return Brick(self)
32+
def connect(self):
33+
if self.debug:
34+
print 'Connecting via Bluetooth...'
35+
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
36+
sock.connect((self.host, BlueSock.PORT))
37+
self.sock = sock
38+
if self.debug:
39+
print 'Connected.'
40+
return Brick(self)
4141

42-
def close(self):
43-
if self.debug:
44-
print 'Closing Bluetooth connection...'
45-
self.sock.close()
46-
if self.debug:
47-
print 'Bluetooth connection closed.'
42+
def close(self):
43+
if self.debug:
44+
print 'Closing Bluetooth connection...'
45+
self.sock.close()
46+
if self.debug:
47+
print 'Bluetooth connection closed.'
4848

49-
def send(self, data):
50-
if self.debug:
51-
print 'Send:',
52-
print ':'.join('%02x' % ord(c) for c in data)
53-
l0 = len(data) & 0xFF
54-
l1 = (len(data) >> 8) & 0xFF
55-
d = chr(l0) + chr(l1) + data
56-
self.sock.send(d)
49+
def send(self, data):
50+
if self.debug:
51+
print 'Send:',
52+
print ':'.join('%02x' % ord(c) for c in data)
53+
l0 = len(data) & 0xFF
54+
l1 = (len(data) >> 8) & 0xFF
55+
d = chr(l0) + chr(l1) + data
56+
self.sock.send(d)
5757

58-
def recv(self):
59-
data = self.sock.recv(2)
60-
l0 = ord(data[0])
61-
l1 = ord(data[1])
62-
plen = l0 + (l1 << 8)
63-
data = self.sock.recv(plen)
64-
if self.debug:
65-
print 'Recv:',
66-
print ':'.join('%02x' % ord(c) for c in data)
67-
return data
58+
def recv(self):
59+
data = self.sock.recv(2)
60+
l0 = ord(data[0])
61+
l1 = ord(data[1])
62+
plen = l0 + (l1 << 8)
63+
data = self.sock.recv(plen)
64+
if self.debug:
65+
print 'Recv:',
66+
print ':'.join('%02x' % ord(c) for c in data)
67+
return data
6868

6969
def _check_brick(arg, value):
70-
return arg is None or arg == value
70+
return arg is None or arg == value
7171

7272
def find_bricks(host=None, name=None):
73-
for h, n in bluetooth.discover_devices(lookup_names=True):
74-
if _check_brick(host, h) and _check_brick(name, n):
75-
yield BlueSock(h)
73+
for h, n in bluetooth.discover_devices(lookup_names=True):
74+
if _check_brick(host, h) and _check_brick(name, n):
75+
yield BlueSock(h)

nxt/locator.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,7 @@ def find_bricks(host=None, name=None):
3838
socks = bluesock.find_bricks(host, name)
3939
for s in socks:
4040
yield s
41-
except:
42-
#"except:" is dangerous and the code in the above try: block should
43-
#be treated with extreme caution. Any errors in it will be dropped
44-
#silently.
45-
#This is necessary to provide a higher level of abstraction above
46-
#the "bluetooth" module on linux/windows and the "lightblue" module
47-
#on mac, since catching bluetooth errors would involve a non-cross-
48-
#platform "import bluetooth", which also happens to break compata-
49-
#bility with lightblueglue even when used externally.
50-
#When testing modifications to the try: block, it may be helpful to
51-
#add "import sys; print sys.exc_info()" right below this message, to
52-
#print out any errors.
41+
except (bluesock.bluetooth.BluetoothError, IOError):
5342
pass
5443
except ImportError:
5544
import sys

0 commit comments

Comments
 (0)