Skip to content

Commit faf3d71

Browse files
committed
win_apple_supported
1 parent 8954fb2 commit faf3d71

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

pythonProject/switch/utilities.py

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
11
import psutil
22
from PyQt5 import QtGui
33
import time
4+
import platform
45

56

67
def get_interfaces(): # Get the interfaces
8+
actual_system = platform.system()
9+
if actual_system == 'Windows':
10+
ports = get_interfaces_windows()
11+
return ports
12+
elif actual_system == 'Darwin':
13+
ports = get_interfaces_mac()
14+
return ports
15+
else:
16+
return 'Unsupported OS'
17+
18+
def get_interfaces_mac():
719
ports = {}
820
for intf, mac_addr in psutil.net_if_addrs().items(): # psutil for system information
921
for i in mac_addr:
1022
if i.family == psutil.AF_LINK: # if the family is a link - Ethernet
1123
ports[intf] = i.address # add the interface and the mac address to the dictionary
1224

13-
#keys_to_remove = [x for x in ports if "Ethernet" not in x or "vEthernet" in x or "en0" in x or "en1" in x]
14-
# -------------------------------------------------- debug -------------------------------------------------- #
15-
# Explanation: If the interface is not Ethernet, I need to test with the Wi-Fi interface
16-
# if 'Wi-Fi' in keys_to_remove:
17-
# keys_to_remove.remove('Wi-Fi')
18-
# keys_to_remove.remove('vEthernet (WSL (Hyper-V firewall))')
19-
# -------------------------------------------------- debug -------------------------------------------------- #
20-
#for key in keys_to_remove: # remove the keys that are not Ethernet
21-
# del ports[key]
22-
23-
if 'lo0' not in ports:
25+
if 'lo0' not in ports or len(ports) < 2:
2426
ports['lo0'] = '00:00:00:00:00:00' # Loopback interfaces do not have a MAC address
2527

2628
return ports
2729

30+
def get_interfaces_windows():
31+
ports = {}
32+
for intf, mac_addr in psutil.net_if_addrs().items(): # psutil for system information
33+
for i in mac_addr:
34+
if i.family == psutil.AF_LINK: # if the family is a link - Ethernet
35+
ports[intf] = i.address # add the interface and the mac address to the dictionary
36+
37+
keys_to_remove = [x for x in ports if "Ethernet" not in x or "vEthernet" in x or "en0" in x or "en1" in x]
38+
39+
if 'Wi-Fi' in keys_to_remove:
40+
keys_to_remove.remove('Wi-Fi')
41+
keys_to_remove.remove('vEthernet (WSL (Hyper-V firewall))')
42+
43+
for key in keys_to_remove: # remove the keys that are not Ethernet
44+
del ports[key]
45+
46+
return ports
47+
2848

2949
def update_table_view(table_view, stats): # Update the table view
3050
model = QtGui.QStandardItemModel()

0 commit comments

Comments
 (0)