Skip to content

Commit 1f58278

Browse files
committed
MAC and IP conversion more robust
1 parent a516b37 commit 1f58278

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/tengbe.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def str2mac(mac_str):
3737
"""
3838
mac = 0
3939
if mac_str.count(':') != 5:
40-
raise RuntimeError('A MAC address must be of the form xx:xx:xx:xx:xx:xx')
40+
raise RuntimeError('A MAC address must be of the form xx:xx:xx:xx:xx:xx, got %s' % mac_str)
4141
offset = 40
4242
for byte_str in mac_str.split(':'):
4343
value = int(byte_str, base=16)
@@ -54,6 +54,10 @@ def __init__(self, mac):
5454
mac_str = mac
5555
elif isinstance(mac, int):
5656
mac_int = mac
57+
if mac_str is not None:
58+
if mac_str.find(':') == -1:
59+
mac_int = int(mac_str)
60+
mac_str = None
5761
if (mac_str is None) and (mac_int is None):
5862
raise ValueError('Cannot make a MAC with no value.')
5963
elif mac_str is not None:
@@ -68,6 +72,10 @@ def from_roach_hostname(cls, hostname, port_num):
6872
"""
6973
Make a MAC address object from a ROACH hostname
7074
"""
75+
# HACK
76+
if hostname.startswith('cbf_oach'):
77+
hostname = hostname.replace('cbf_oach', 'roach')
78+
# /HACK
7179
if not hostname.startswith('roach'):
7280
raise RuntimeError('Only hostnames beginning with'
7381
'roach supported: %s' % hostname)
@@ -130,6 +138,10 @@ def __init__(self, ip):
130138
ip_str = ip
131139
elif isinstance(ip, int):
132140
ip_int = ip
141+
if ip_str is not None:
142+
if ip_str.find('.') == -1:
143+
ip_int = int(ip_str)
144+
ip_str = None
133145
if (ip_str is None) and (ip_int is None):
134146
raise ValueError('Cannot make an IP with no value.')
135147
elif ip_str is not None:

0 commit comments

Comments
 (0)