Skip to content

Commit 4324104

Browse files
refactor: Make SysUtil.link_info_find() more pythonic
The existing implementation of `link_info_find()` had several issues that made it error-prone and harder to maintain: - The refresh parameter was unused, adding unnecessary complexity. - MAC address matching logic was convoluted, with redundant variables and conditions. - The use of locals() for fallback results was unclear and could lead to subtle bugs. - The order of checks (MAC vs ifname) didn't prioritize the most reliable matching criteria. To address these issues, the following changes were made: - Removed the unused `refresh` argument to reduce complexity. - 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. Signed-off-by: Wen Liang <[email protected]>
1 parent 53be949 commit 4324104

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
@@ -95,6 +95,7 @@
9595

9696
DEFAULT_ACTIVATION_TIMEOUT = 90
9797
DEFAULT_TIMEOUT = 10
98+
NULL_MAC = "00:00:00:00:00:00"
9899

99100

100101
class CheckMode:
@@ -222,31 +223,28 @@ def link_infos(cls, refresh=False):
222223
return linkinfos
223224

224225
@classmethod
225-
def link_info_find(cls, refresh=False, mac=None, ifname=None):
226-
if mac is not None:
226+
def link_info_find(cls, mac=None, ifname=None):
227+
if mac:
227228
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)
231229

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
230+
result = None
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["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 mac == perm_address:
242+
result = linkinfo
243+
break
244+
elif mac == current_address:
245+
result = linkinfo
248246

249-
return None
247+
return result
250248

251249

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

0 commit comments

Comments
 (0)