From 8785089a1c0f8bd8edc32a28838036713ceaccbd Mon Sep 17 00:00:00 2001 From: Chris McCarthy Date: Tue, 1 Aug 2023 10:32:16 +0100 Subject: [PATCH] #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 --- .flake8 | 2 ++ .../network/physical_layer.rst | 2 +- .../simulator/network/physical_layer.py | 25 +++++++++++-------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/.flake8 b/.flake8 index 1f3c7065..c2d9e4bb 100644 --- a/.flake8 +++ b/.flake8 @@ -9,6 +9,8 @@ extend-ignore = E712 D401 F811 + ANN002 + ANN003 ANN101 ANN102 exclude = diff --git a/docs/source/simulation_components/network/physical_layer.rst b/docs/source/simulation_components/network/physical_layer.rst index 2d942847..1e87b72e 100644 --- a/docs/source/simulation_components/network/physical_layer.rst +++ b/docs/source/simulation_components/network/physical_layer.rst @@ -5,7 +5,7 @@ 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. NIC diff --git a/src/primaite/simulator/network/physical_layer.py b/src/primaite/simulator/network/physical_layer.py index ebc0c788..b709fd0a 100644 --- a/src/primaite/simulator/network/physical_layer.py +++ b/src/primaite/simulator/network/physical_layer.py @@ -58,11 +58,11 @@ class NIC(SimComponent): :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." subnet_mask: str "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." mac_address: str = generate_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." mtu: Optional[int] = 1500 "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." dns_servers: List[IPv4Address] = [] "List of IP addresses of DNS servers used for name resolution." @@ -79,17 +79,22 @@ class NIC(SimComponent): enabled: bool = False "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 - network address. + network address. """ - if not isinstance(self.ip_address, IPv4Address): - self.ip_address: IPv4Address = IPv4Address(self.ip_address) - if not isinstance(self.gateway, IPv4Address): - self.gateway: IPv4Address = IPv4Address(self.gateway) + if not isinstance(kwargs["ip_address"], IPv4Address): + kwargs["ip_address"] = IPv4Address(kwargs["ip_address"]) + if not isinstance(kwargs["gateway"], IPv4Address): + kwargs["gateway"] = IPv4Address(kwargs["gateway"]) + super().__init__(**kwargs) + if self.ip_address == self.gateway: msg = f"NIC ip address {self.ip_address} cannot be the same as the gateway {self.gateway}" _LOGGER.error(msg)