From 7f2f8c392c25ff7b67c782092338a8a8d8889307 Mon Sep 17 00:00:00 2001 From: itdependsnetworks Date: Thu, 17 Aug 2023 22:47:12 -0400 Subject: [PATCH] Fix issue where interface abbreviation not working and update mac docs --- netutils/interface.py | 2 +- netutils/mac.py | 9 +++++++-- tests/unit/test_interface.py | 9 +++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/netutils/interface.py b/netutils/interface.py index ea18fe76..3466b271 100644 --- a/netutils/interface.py +++ b/netutils/interface.py @@ -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) diff --git a/netutils/mac.py b/netutils/mac.py index 1f29b86e..67f1b6d6 100644 --- a/netutils/mac.py +++ b/netutils/mac.py @@ -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. @@ -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"] diff --git a/tests/unit/test_interface.py b/tests/unit/test_interface.py index 75e624c4..92292b67 100644 --- a/tests/unit/test_interface.py +++ b/tests/unit/test_interface.py @@ -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 = [ @@ -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 = [