Skip to content

Commit

Permalink
Got rid of unconditional except in locator.py, tab fixing in bluesock…
Browse files Browse the repository at this point in the history
….py.
  • Loading branch information
marcusw committed Jan 14, 2010
1 parent b4acb04 commit 5036aa3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 58 deletions.
92 changes: 46 additions & 46 deletions nxt/bluesock.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,62 @@

import bluetooth
import os
from nxt.brick import Brick
from .brick import Brick

class BlueSock(object):

bsize = 118 # Bluetooth socket block size
PORT = 1 # Standard NXT rfcomm port
bsize = 118 # Bluetooth socket block size
PORT = 1 # Standard NXT rfcomm port

def __init__(self, host):
self.host = host
self.sock = None
self.debug = False
def __init__(self, host):
self.host = host
self.sock = None
self.debug = False

def __str__(self):
return 'Bluetooth (%s)' % self.host
def __str__(self):
return 'Bluetooth (%s)' % self.host

def connect(self):
if self.debug:
print 'Connecting via Bluetooth...'
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((self.host, BlueSock.PORT))
self.sock = sock
if self.debug:
print 'Connected.'
return Brick(self)
def connect(self):
if self.debug:
print 'Connecting via Bluetooth...'
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((self.host, BlueSock.PORT))
self.sock = sock
if self.debug:
print 'Connected.'
return Brick(self)

def close(self):
if self.debug:
print 'Closing Bluetooth connection...'
self.sock.close()
if self.debug:
print 'Bluetooth connection closed.'
def close(self):
if self.debug:
print 'Closing Bluetooth connection...'
self.sock.close()
if self.debug:
print 'Bluetooth connection closed.'

def send(self, data):
if self.debug:
print 'Send:',
print ':'.join('%02x' % ord(c) for c in data)
l0 = len(data) & 0xFF
l1 = (len(data) >> 8) & 0xFF
d = chr(l0) + chr(l1) + data
self.sock.send(d)
def send(self, data):
if self.debug:
print 'Send:',
print ':'.join('%02x' % ord(c) for c in data)
l0 = len(data) & 0xFF
l1 = (len(data) >> 8) & 0xFF
d = chr(l0) + chr(l1) + data
self.sock.send(d)

def recv(self):
data = self.sock.recv(2)
l0 = ord(data[0])
l1 = ord(data[1])
plen = l0 + (l1 << 8)
data = self.sock.recv(plen)
if self.debug:
print 'Recv:',
print ':'.join('%02x' % ord(c) for c in data)
return data
def recv(self):
data = self.sock.recv(2)
l0 = ord(data[0])
l1 = ord(data[1])
plen = l0 + (l1 << 8)
data = self.sock.recv(plen)
if self.debug:
print 'Recv:',
print ':'.join('%02x' % ord(c) for c in data)
return data

def _check_brick(arg, value):
return arg is None or arg == value
return arg is None or arg == value

def find_bricks(host=None, name=None):
for h, n in bluetooth.discover_devices(lookup_names=True):
if _check_brick(host, h) and _check_brick(name, n):
yield BlueSock(h)
for h, n in bluetooth.discover_devices(lookup_names=True):
if _check_brick(host, h) and _check_brick(name, n):
yield BlueSock(h)
13 changes: 1 addition & 12 deletions nxt/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,7 @@ def find_bricks(host=None, name=None):
socks = bluesock.find_bricks(host, name)
for s in socks:
yield s
except:
#"except:" is dangerous and the code in the above try: block should
#be treated with extreme caution. Any errors in it will be dropped
#silently.
#This is necessary to provide a higher level of abstraction above
#the "bluetooth" module on linux/windows and the "lightblue" module
#on mac, since catching bluetooth errors would involve a non-cross-
#platform "import bluetooth", which also happens to break compata-
#bility with lightblueglue even when used externally.
#When testing modifications to the try: block, it may be helpful to
#add "import sys; print sys.exc_info()" right below this message, to
#print out any errors.
except (bluesock.bluetooth.BluetoothError, IOError):
pass
except ImportError:
import sys
Expand Down

0 comments on commit 5036aa3

Please sign in to comment.