Skip to content

Commit 627518a

Browse files
refactor: Rewrite the function SysUtil.link_info_find() in a more pythonic way
- Removed the unnecessary `refresh` argument since it wasn't used. - Used `None` checks more idiomatically with `if mac` instead of `is not None`. - Eliminated redundant variables and conditions to improve readability. - Avoided using `locals()` by explicitly storing fallback results. - Made `ifname` matching take priority before checking MAC addresses. - Ensured that the function returns early when a definitive match is found.
1 parent e890ab5 commit 627518a

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

library/network_connections.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -222,31 +222,29 @@ def link_infos(cls, refresh=False):
222222
return linkinfos
223223

224224
@classmethod
225-
def link_info_find(cls, refresh=False, mac=None, ifname=None):
226-
if mac is not None:
225+
def link_info_find(cls, mac=None, ifname=None):
226+
if mac:
227227
mac = Util.mac_norm(mac)
228-
for linkinfo in cls.link_infos(refresh).values():
229-
perm_address = linkinfo.get("perm-address", None)
230-
current_address = linkinfo.get("address", None)
231228

232-
# Match by perm-address (prioritized)
233-
if mac is not None and perm_address not in [None, "00:00:00:00:00:00"]:
234-
if mac == perm_address:
235-
return linkinfo
229+
result = None
230+
null_mac = "00:00:00:00:00:00"
236231

237-
# Fallback to match by address
238-
if mac is not None and (perm_address in [None, "00:00:00:00:00:00"]):
239-
if mac == current_address:
240-
matched_by_address = linkinfo # Save for potential fallback
232+
for linkinfo in cls.link_infos().values():
233+
perm_address = linkinfo.get("perm-address", null_mac)
234+
current_address = linkinfo.get("address", null_mac)
241235

242-
if ifname is not None and ifname == linkinfo.get("ifname", None):
243-
return linkinfo
236+
if ifname and ifname == linkinfo.get("ifname"):
237+
result = linkinfo
238+
break
244239

245-
# Return fallback match by address if no perm-address match found
246-
if "matched_by_address" in locals():
247-
return matched_by_address
240+
if mac:
241+
if perm_address != null_mac and mac == perm_address:
242+
result = linkinfo
243+
break
244+
elif perm_address == null_mac and mac == current_address:
245+
result = linkinfo # Save for fallback, but don't break
248246

249-
return None
247+
return result
250248

251249

252250
###############################################################################

0 commit comments

Comments
 (0)