Skip to content

Commit

Permalink
make python 3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Vacaliuc committed Dec 30, 2016
1 parent 22c90e7 commit f6a1a0b
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions host/rss2rtl-power/rss2rtl-power.py
Expand Up @@ -19,7 +19,7 @@
# http://stackoverflow.com/questions/13218506/how-to-get-system-timezone-setting-and-pass-it-to-pytz-timezone
from dateutil.tz import tzlocal

DEF_VERSION = '0.2.5.0' # x.y.z.* to match RASDRproc version
DEF_VERSION = '0.2.5.1' # x.y.z.* to match RASDRproc version

UNIX_EPOCH = 518400 # 00:00 GMT January 6, 1970
PKT_FMT = '!dfffII'
Expand All @@ -28,7 +28,7 @@

class connection(asyncore.dispatcher):
pktlen = 1024
data = ''
data = b''
state = 'Disconnected'
partial = False
def __init__(self,ip,port,name='connection'):
Expand All @@ -48,7 +48,7 @@ def __init__(self,ip,port,name='connection'):

def packetDiscard(self,_buf):
"""Drop remaining bytes in connection object."""
self.data = ''
self.data = b''

def parseConnectionString(self,buf):
"""
Expand All @@ -57,7 +57,7 @@ def parseConnectionString(self,buf):
@param buf: bytes received in connection string
@return: a dictionary with key/value pairs interpreted from buf
"""
d = buf.rpartition('|')
d = buf.decode('utf-8').rpartition('|')
connect = d[0].split('|')
# parse key/value pairs in connection string
conf = {}
Expand All @@ -77,7 +77,7 @@ def readable(self):
return not self.state.startswith('Disconnected')

def handle_error(self):
traceback.print_exc(sys.stderr)
traceback.print_exc(file=sys.stderr)
self.close()

def handle_connect(self):
Expand Down Expand Up @@ -195,8 +195,7 @@ def __init__(self,opts):
self.conn.OnConnectReceived = self.OnConnectReceived
self.conn.pktlen = 1024
self.conn.partial = True
data = '\xFE\xFE'
self.eos_marker = np.frombuffer(data,dtype=self.dtyp)
self.eos_marker = np.frombuffer(b'\xFE\xFE',dtype=self.dtyp)
self.opts = opts

def OnPacketReceived(self,buf):
Expand Down Expand Up @@ -235,10 +234,10 @@ def OnConnectReceived(self,buf):
conf = self.conn.parseConnectionString(buf)

# ensure canonical connection string
assert(conf.has_key('F'))
assert(conf.has_key('S'))
assert(conf.has_key('O'))
assert(conf.has_key('C'))
assert('F' in conf.keys())
assert('S' in conf.keys())
assert('O' in conf.keys())
assert('C' in conf.keys())

hz_low = float((conf['F'] - conf['S']/2) + conf['O'])
hz_step = float(conf['S']) / float(conf['C'])
Expand Down Expand Up @@ -266,8 +265,8 @@ def OnConnectReceived(self,buf):
self.stats = statistics()
sys.stderr.write(self.stats.header()+os.linesep)
# consume (variable) connection buffer leaving payload
d = buf.rpartition('|')
self.conn.data = d[2].strip()
d = buf.decode('utf-8').rpartition('|')
self.conn.data = bytes(d[2].strip(),'utf-8')
# state to receive payloads
self.conn.pktlen = (self.channels+1)*self.dtyp.itemsize
self.conn.partial = False
Expand Down Expand Up @@ -359,10 +358,10 @@ def OnConnectReceived(self,buf):
conf = self.conn.parseConnectionString(buf)

# ensure canonical connection string
assert(conf.has_key('CenterFrequencyHertz'))
assert(conf.has_key('BandwidthHertz'))
assert(conf.has_key('OffsetHertz'))
assert(conf.has_key('NumberOfChannels'))
assert('CenterFrequencyHertz' in conf.keys())
assert('BandwidthHertz' in conf.keys())
assert('OffsetHertz' in conf.keys())
assert('NumberOfChannels' in conf.keys())

if not self.opts.quiet: # diagnostic connection message
msg = 'CenterFrequencyHertz=%d\n'%conf['CenterFrequencyHertz']
Expand Down Expand Up @@ -434,5 +433,5 @@ def OnConnectReceived(self,buf):
try:
c = rssx(opts) if opts.extended else rss(opts)
asyncore.loop(timeout=2)
except Exception, e:
logger.error('rss2rtl-power', exc_info=True)
except Exception as e:
logger.error('rss2rtl-power: '+str(e), exc_info=True)

0 comments on commit f6a1a0b

Please sign in to comment.