Skip to content

Commit

Permalink
More changes for compatibility with both Python 2 and 3 (sonic-net#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jleveque authored May 3, 2018
1 parent ba614d3 commit 9adb75f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 72 deletions.
22 changes: 12 additions & 10 deletions sonic_eeprom/eeprom_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# 'burn' as a data name indicates the corresponding number of bytes are to
# be ignored

from __future__ import print_function

try:
import exceptions
import binascii
Expand All @@ -35,7 +37,7 @@ def __init__(self, path, format, start, status, readonly):
self.lock_file = None

def check_status(self):
if self.u <> '':
if self.u != '':
F = open(self.u, "r")
d = F.readline().rstrip()
F.close()
Expand Down Expand Up @@ -74,14 +76,14 @@ def encode_checksum(self, crc):
return struct.pack('>I', crc)
elif self.checksum_field_size() == 1:
return struct.pack('>B', crc)
print 'checksum type not yet supported'
print('checksum type not yet supported')
exit(1)

def compute_2s_complement(self, e, size):
crc = 0
loc = 0
end = len(e)
while loc <> end:
while loc != end:
crc += int('0x' + binascii.b2a_hex(e[loc:loc+size]), 0)
loc += size
T = 1 << (size * 8)
Expand Down Expand Up @@ -113,7 +115,7 @@ def calculate_checksum(self, e):

if self.checksum_type() == 'dell-crc':
return self.compute_dell_crc(e)
print 'checksum type not yet supported'
print('checksum type not yet supported')
exit(1)

def is_checksum_valid(self, e):
Expand Down Expand Up @@ -147,7 +149,7 @@ def decode_eeprom(self, e):
i = t
else:
i = self.decoder(I[0], t)
print "%-20s: %s" %(I[0], i)
print("%-20s: %s" %(I[0], i))

def set_eeprom(self, e, cmd_args):
line = ''
Expand All @@ -160,7 +162,7 @@ def set_eeprom(self, e, cmd_args):
k = k.strip()
v = v.strip()
if k not in fields:
print "Error: invalid field '%s'" %(k)
print("Error: invalid field '%s'" %(k))
exit(1)
ndict[k] = v

Expand All @@ -181,7 +183,7 @@ def set_eeprom(self, e, cmd_args):

if len(cmd_args) == 0:
if self.is_checksum_field(I):
print ("%-20s: %s " %(I[0], i))
print("%-20s: %s " %(I[0], i))
continue

# prompt for new value
Expand Down Expand Up @@ -284,7 +286,7 @@ def increment_mac(self, mac):
ret_mac = ret_mac + 1

if (ret_mac & 0xff000000):
print 'Error: increment carries into OUI'
print('Error: increment carries into OUI')
return ''

mac_octets[5] = hex(ret_mac & 0xff)[2:].zfill(2)
Expand Down Expand Up @@ -320,7 +322,7 @@ def base_mac_addr(self, e):
See also mgmtaddrstr() and switchaddrstr().
'''
print "ERROR: Platform did not implement base_mac_addr()"
print("ERROR: Platform did not implement base_mac_addr()")
raise NotImplementedError

def mgmtaddrstr(self, e):
Expand Down Expand Up @@ -352,7 +354,7 @@ def switchaddrrange(self, e):
# the platform specific import should have an override of this method
# to provide the allocated mac range from syseeprom or flash sector or
# wherever that platform stores this info
print "Platform did not indicate allocated mac address range"
print("Platform did not indicate allocated mac address range")
raise NotImplementedError

def serial_number_str(self, e):
Expand Down
24 changes: 13 additions & 11 deletions sonic_eeprom/eeprom_tlvinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# 'burn' as a data name indicates the corresponding number of bytes are to
# be ignored

from __future__ import print_function

try:
import exceptions
import binascii
Expand Down Expand Up @@ -84,27 +86,27 @@ def decode_eeprom(self, e):
'''
if self._TLV_HDR_ENABLED :
if not self.is_valid_tlvinfo_header(e):
print "EEPROM does not contain data in a valid TlvInfo format."
print("EEPROM does not contain data in a valid TlvInfo format.")
return

print "TlvInfo Header:"
print " Id String: %s" % (e[0:7],)
print " Version: %d" % (ord(e[8]),)
print("TlvInfo Header:")
print(" Id String: %s" % (e[0:7],))
print(" Version: %d" % (ord(e[8]),))
total_len = (ord(e[9]) << 8) | ord(e[10])
print " Total Length: %d" % (total_len,)
print(" Total Length: %d" % (total_len,))
tlv_index = self._TLV_INFO_HDR_LEN
tlv_end = self._TLV_INFO_HDR_LEN + total_len
else :
tlv_index = self.eeprom_start
tlv_end = self._TLV_INFO_MAX_LEN

print "TLV Name Code Len Value"
print "-------------------- ---- --- -----"
print("TLV Name Code Len Value")
print("-------------------- ---- --- -----")
while (tlv_index + 2) < len(e) and tlv_index < tlv_end:
if not self.is_valid_tlv(e[tlv_index:]):
print "Invalid TLV field starting at EEPROM offset %d" % (tlv_index,)
print("Invalid TLV field starting at EEPROM offset %d" % (tlv_index,))
return
print self.decoder(None, e[tlv_index:tlv_index + 2 + ord(e[tlv_index + 1])])
print(self.decoder(None, e[tlv_index:tlv_index + 2 + ord(e[tlv_index + 1])]))
if ord(e[tlv_index]) == self._TLV_CODE_QUANTA_CRC or \
ord(e[tlv_index]) == self._TLV_CODE_CRC_32:
return
Expand Down Expand Up @@ -175,7 +177,7 @@ def set_eeprom(self, e, cmd_args):
elif action in ['f', 'F']:
pass
else:
print "\nInvalid input, please enter 'a', 'm', 'd', or 'f'\n"
print("\nInvalid input, please enter 'a', 'm', 'd', or 'f'\n")

if self._TLV_HDR_ENABLED:
new_tlvs_len = len(new_tlvs) + 6
Expand All @@ -190,7 +192,7 @@ def set_eeprom(self, e, cmd_args):
elif self._TLV_CODE_QUANTA_CRC != self._TLV_CODE_UNDEFINED:
new_e = new_e + chr(self._TLV_CODE_QUANTA_CRC) + chr(2)
else:
print "\nFailed to formulate new eeprom\n"
print("\nFailed to formulate new eeprom\n")
exit
new_e += self.encode_checksum(self.calculate_checksum(new_e))
self.decode_eeprom(new_e)
Expand Down
24 changes: 15 additions & 9 deletions sonic_sfp/bcmshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#
#-------------------------------------------------------------------------------
#

from __future__ import print_function

try:
import sys
import os
Expand Down Expand Up @@ -346,7 +349,7 @@ def cmd(self, cmd):
s = self.run(cmd)
if 'Unknown command:' in s:
raise ValueError(s)
print s
print(s)


#---------------
Expand All @@ -360,20 +363,20 @@ def prettyprint(self, d, level=0):
if type(d) is dict:
for I in sorted(d, key=self.__name_conv__):
if type(d[I]) is int:
print "%s %30s: " % (s, I),
print("%s %30s: " % (s, I), end=" ")
else:
print "%s %s:" % (s, I)
print("%s %s:" % (s, I))
self.prettyprint(d[I], (level + 1))
elif type(d) is list:
for I in range(len(d)):
i = "[" + str(I) + "]"
if type(d[I]) is int or type(d[I]) is long:
print "%s %10s: " % (s, i),
print("%s %10s: " % (s, i), end=" ")
else:
print "%s %s:" % (s, i)
print("%s %s:" % (s, i))
self.prettyprint(d[I], (level + 1))
else:
print "%s" % (hex(d))
print("%s" % (hex(d)))


#---------------
Expand Down Expand Up @@ -403,7 +406,8 @@ def run(self, cmd):
self.__open__()
try:
self.socketobj.sendall(cmd + '\n')
except socket.error as (errno, errstr):
except socket.error as err:
(errno, errstr) = err.args
raise IOError("unable to send command \"%s\", %s" % (cmd, errstr))

self.buffer = ''
Expand Down Expand Up @@ -460,7 +464,8 @@ def __open__(self):
self.socketobj = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socketobj.settimeout(self.timeout)
self.socketobj.connect(self.socketname)
except socket.error as (errno, errstr):
except socket.error as err:
(errno, errstr) = err.args
if timeout == 0:
raise IOError("unable to open %s, %s" % (self.socketname, errstr))
time.sleep(1)
Expand Down Expand Up @@ -488,7 +493,8 @@ def __open__(self):
raise IOError("unable to flush %s for %d seconds" %
(self.socketname, self.timeout))

except socket.error as (errno, errstr):
except socket.error as err:
(errno, errstr) = err.args
raise IOError("Socket error: unable to flush %s on open: %s" % (self.socketname, errstr))
except IOError as e:
raise IOError("unable to flush %s on open: %s" % (self.socketname, e.message))
Expand Down
22 changes: 12 additions & 10 deletions sonic_sfp/sff8436.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# SFF-8436 QSFP+ 10 Gbs 4X PLUGGABLE TRANSCEIVER
#----------------------------------------------------------------------------

from __future__ import print_function

try:
import fcntl
import struct
Expand Down Expand Up @@ -378,7 +380,7 @@ def parse(self, eeprom_raw_data, start_pos):

def dump_pretty(self):
if self.interface_data == None:
print 'Object not initialized, nothing to print'
print('Object not initialized, nothing to print')
return
sffbase.dump_pretty(self, self.interface_data)

Expand Down Expand Up @@ -455,7 +457,7 @@ def calc_voltage(self, eeprom_data, offset, size):
# Internal Calibration

result = float(result * 0.0001)
#print indent, name, ' : %.4f' %result, 'Volts'
#print(indent, name, ' : %.4f' %result, 'Volts')
retval = '%.4f' %result + 'Volts'
elif cal_type == 2:

Expand All @@ -475,10 +477,10 @@ def calc_voltage(self, eeprom_data, offset, size):

result = v_slope * result + v_offset
result = float(result * 0.0001)
#print indent, name, ' : %.4f' %result, 'Volts'
#print(indent, name, ' : %.4f' %result, 'Volts')
retval = '%.4f' %result + 'Volts'
else:
#print indent, name, ' : Unknown'
#print(indent, name, ' : Unknown')
retval = 'Unknown'
except Exception as err:
retval = str(err)
Expand All @@ -498,7 +500,7 @@ def calc_bias(self, eeprom_data, offset, size):
# Internal Calibration

result = float(result * 0.002)
#print indent, name, ' : %.4f' %result, 'mA'
#print(indent, name, ' : %.4f' %result, 'mA')
retval = '%.4f' %result + 'mA'

elif cal_type == 2:
Expand All @@ -518,7 +520,7 @@ def calc_bias(self, eeprom_data, offset, size):

result = i_slope * result + i_offset
result = float(result * 0.002)
#print indent, name, ' : %.4f' %result, 'mA'
#print(indent, name, ' : %.4f' %result, 'mA')
retval = '%.4f' %result + 'mA'
else:
retval = 'Unknown'
Expand All @@ -539,7 +541,7 @@ def calc_tx_power(self, eeprom_data, offset, size):
if cal_type == 1:

result = float(result * 0.0001)
#print indent, name, ' : ', power_in_dbm_str(result)
#print(indent, name, ' : ', power_in_dbm_str(result))
retval = self.power_in_dbm_str(result)

elif cal_type == 2:
Expand Down Expand Up @@ -579,7 +581,7 @@ def calc_rx_power(self, eeprom_data, offset, size):

# Internal Calibration
result = float(result * 0.0001)
#print indent, name, " : ", power_in_dbm_str(result)
#print(indent, name, " : ", power_in_dbm_str(result))
retval = self.power_in_dbm_str(result)

elif cal_type == 2:
Expand Down Expand Up @@ -629,7 +631,7 @@ def calc_rx_power(self, eeprom_data, offset, size):
rx_pwr = (rx_pwr_4 * result) + (rx_pwr_3 * result) + (rx_pwr_2 * result) + (rx_pwr_1 * result) + rx_pwr_0

result = float(result * 0.0001)
#print indent, name, " : ", power_in_dbm_str(result)
#print(indent, name, " : ", power_in_dbm_str(result))
retval = self.power_in_dbm_str(result)
else:
retval = 'Unknown'
Expand Down Expand Up @@ -936,7 +938,7 @@ def parse(self, eeprom_raw_data, start_pos):

def dump_pretty(self):
if self.dom_data == None:
print 'Object not initialized, nothing to print'
print('Object not initialized, nothing to print')
return
sffbase.dump_pretty(self, self.dom_data)

Expand Down
Loading

0 comments on commit 9adb75f

Please sign in to comment.