#1715 - Moved IPv4Address conversions to the NIC init. Mak wake_on_lan not optional. Ignored ANN002 and ANN003 in .flake8 so we don't get silly 'ANN002 Missing type annotation for *args' and 'ANN003 Missing type annotation for **kwargs' flake8 failures

This commit is contained in:
Chris McCarthy
2023-08-01 10:32:16 +01:00
parent 557caeaac4
commit 8785089a1c
3 changed files with 18 additions and 11 deletions

View File

@@ -9,6 +9,8 @@ extend-ignore =
E712 E712
D401 D401
F811 F811
ANN002
ANN003
ANN101 ANN101
ANN102 ANN102
exclude = exclude =

View File

@@ -5,7 +5,7 @@
Physical Layer Physical Layer
============== ==============
The physical layer components are mode of a ``NIC`` (Network Interface Card) and a ``Link``. These components allow The physical layer components are models of a ``NIC`` (Network Interface Card) and a ``Link``. These components allow
modelling of layer 1 (physical layer) in the OSI model. modelling of layer 1 (physical layer) in the OSI model.
NIC NIC

View File

@@ -58,11 +58,11 @@ class NIC(SimComponent):
:param dns_servers: List of IP addresses of DNS servers used for name resolution. :param dns_servers: List of IP addresses of DNS servers used for name resolution.
""" """
ip_address: Union[str, IPv4Address] ip_address: Union[IPv4Address]
"The IP address assigned to the NIC for communication on an IP-based network." "The IP address assigned to the NIC for communication on an IP-based network."
subnet_mask: str subnet_mask: str
"The subnet mask assigned to the NIC." "The subnet mask assigned to the NIC."
gateway: Union[str, IPv4Address] gateway: Union[IPv4Address]
"The default gateway IP address for forwarding network traffic to other networks. Randomly generated upon creation." "The default gateway IP address for forwarding network traffic to other networks. Randomly generated upon creation."
mac_address: str = generate_mac_address() mac_address: str = generate_mac_address()
"The MAC address of the NIC. Defaults to a randomly set MAC address." "The MAC address of the NIC. Defaults to a randomly set MAC address."
@@ -70,7 +70,7 @@ class NIC(SimComponent):
"The speed of the NIC in Mbps. Default is 100 Mbps." "The speed of the NIC in Mbps. Default is 100 Mbps."
mtu: Optional[int] = 1500 mtu: Optional[int] = 1500
"The Maximum Transmission Unit (MTU) of the NIC in Bytes. Default is 1500 B" "The Maximum Transmission Unit (MTU) of the NIC in Bytes. Default is 1500 B"
wake_on_lan: Optional[bool] = False wake_on_lan: bool = False
"Indicates if the NIC supports Wake-on-LAN functionality." "Indicates if the NIC supports Wake-on-LAN functionality."
dns_servers: List[IPv4Address] = [] dns_servers: List[IPv4Address] = []
"List of IP addresses of DNS servers used for name resolution." "List of IP addresses of DNS servers used for name resolution."
@@ -79,17 +79,22 @@ class NIC(SimComponent):
enabled: bool = False enabled: bool = False
"Indicates whether the NIC is enabled." "Indicates whether the NIC is enabled."
def model_post_init(self, __context: Any) -> None: def __init__(self, **kwargs):
""" """
Post init function converts string IPs to IPv$Address and checks for proper IP address and gateway config. NIC constructor.
Performs some type conversion the calls ``super().__init__()``. Then performs some checking on the ip_address
and gateway just to check that it's all been configured correctly.
:raises ValueError: When the ip_address and gateway are the same. And when the ip_address/subnet mask are a :raises ValueError: When the ip_address and gateway are the same. And when the ip_address/subnet mask are a
network address. network address.
""" """
if not isinstance(self.ip_address, IPv4Address): if not isinstance(kwargs["ip_address"], IPv4Address):
self.ip_address: IPv4Address = IPv4Address(self.ip_address) kwargs["ip_address"] = IPv4Address(kwargs["ip_address"])
if not isinstance(self.gateway, IPv4Address): if not isinstance(kwargs["gateway"], IPv4Address):
self.gateway: IPv4Address = IPv4Address(self.gateway) kwargs["gateway"] = IPv4Address(kwargs["gateway"])
super().__init__(**kwargs)
if self.ip_address == self.gateway: if self.ip_address == self.gateway:
msg = f"NIC ip address {self.ip_address} cannot be the same as the gateway {self.gateway}" msg = f"NIC ip address {self.ip_address} cannot be the same as the gateway {self.gateway}"
_LOGGER.error(msg) _LOGGER.error(msg)