Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion netutils/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def split_interface(interface: str) -> t.Tuple[str, str]:
('Eth', '1')
>>>
"""
head = interface.rstrip(r"/\0123456789. ")
head = interface.rstrip(r"/\0123456789.: ")
tail = interface[len(head) :].lstrip() # noqa: E203
return (head, tail)

Expand Down
9 changes: 7 additions & 2 deletions netutils/mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ def is_valid_mac(mac: str) -> bool:
def mac_to_format(mac: str, frmt: str = "MAC_NO_SPECIAL") -> str:
"""Converts the MAC address to a specific format.

The `frmt` is a combination delimiter (COLON, DASH, DOT) and number
of characters (TWO, FOUR), e.g. f'MAC_{delimiter}_{char_num}' or if no
special characters as `MAC_NO_SPECIAL`.

Args:
mac: A MAC address in string format that matches one of the defined regex patterns.
frmt: A format in which the MAC address should be returned in.
frmt: A format in which the MAC address should be returned in, one of MAC_COLON_TWO, MAC_COLON_FOUR, MAC_DASH_TWO, MAC_DASH_FOUR, MAC_DOT_TWO, MAC_DOT_FOUR, MAC_NO_SPECIAL.

Returns:
A MAC address in the specified format.
Expand All @@ -65,7 +69,8 @@ def mac_to_format(mac: str, frmt: str = "MAC_NO_SPECIAL") -> str:
>>>
"""
if not MAC_CREATE.get(frmt):
raise ValueError(f"An invalid mac format was provided in: `{frmt}`")
format_choices = ", ".join(MAC_CREATE)
raise ValueError(f"An invalid mac format was provided in: `{frmt}`, not one of [{format_choices}]")
mac = mac_normalize(mac)
count = MAC_CREATE[frmt]["count"]
char = MAC_CREATE[frmt]["char"]
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"received": ("GigabitEthernet", "1/0/1"),
},
{"sent": {"interface": "Gi1/0/1"}, "received": ("Gi", "1/0/1")},
{"sent": {"interface": "Serial0/0/0:0"}, "received": ("Serial", "0/0/0:0")},
{"sent": {"interface": "VLAN101"}, "received": ("VLAN", "101")},
{"sent": {"interface": "Port-channel40"}, "received": ("Port-channel", "40")},
{"sent": {"interface": "Gi1/0/3.100"}, "received": ("Gi", "1/0/3.100")},
]

CANONICAL_INTERFACE_NAME = [
Expand Down Expand Up @@ -158,6 +162,11 @@
"received": "SupE1/0/1",
},
{"sent": {"interface": "Noninterface1/0/1"}, "received": "Noninterface1/0/1"},
{"sent": {"interface": "Noninterface1/0/1"}, "received": "Noninterface1/0/1"},
{"sent": {"interface": "Serial0/0/0:0"}, "received": "Se0/0/0:0"},
{"sent": {"interface": "VLAN101"}, "received": "Vl101"},
{"sent": {"interface": "Port-channel40"}, "received": "Po40"},
{"sent": {"interface": "GigabitEthernet1/0/3.100"}, "received": "Gi1/0/3.100"},
]

INTERFACE_EXPANSION = [
Expand Down