Skip to content

Commit

Permalink
Refactor ManagementFactory.get_instance()
Browse files Browse the repository at this point in the history
Make the fallback algorithm more explicit, as per code review
comments.
  • Loading branch information
lunkwill42 committed Dec 4, 2024
1 parent f7e798a commit b6c327d
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions python/nav/portadmin/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from nav.portadmin.snmp.hp import HP
from nav.portadmin.napalm.juniper import Juniper

SUPPORTED_HANDLERS = (Cisco, Dell, H3C, HP, Juniper, SNMPHandler)
SUPPORTED_HANDLERS = (Cisco, Dell, H3C, HP, Juniper)
FALLBACK_HANDLER = SNMPHandler


class ManagementFactory(object):
Expand All @@ -37,11 +38,9 @@ def get_instance(cls, netbox: manage.Netbox, **kwargs) -> ManagementHandler:
if not netbox.type:
raise NoNetboxTypeError()

for handler_class in SUPPORTED_HANDLERS:
if handler_class.can_handle(netbox):
break

return handler_class(netbox, **kwargs)
matched_handlers = (h for h in SUPPORTED_HANDLERS if h.can_handle(netbox))
chosen_handler = next(matched_handlers, FALLBACK_HANDLER)
return chosen_handler(netbox, **kwargs)

def __init__(self):
pass

0 comments on commit b6c327d

Please sign in to comment.