Skip to content

Commit

Permalink
Use socket directly.
Browse files Browse the repository at this point in the history
Remove aliases to the socket converter functions from utils module.
  • Loading branch information
denpamusic committed Oct 17, 2023
1 parent 46edcad commit 07c50d6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 67 deletions.
5 changes: 3 additions & 2 deletions pyplumio/helpers/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
import socket
from typing import Type

from pyplumio import util
Expand Down Expand Up @@ -246,7 +247,7 @@ class IPv4(DataType):
@property
def value(self) -> str:
"""A data value."""
return util.ip4_from_bytes(self._data)
return socket.inet_ntoa(self._data)

@property
def size(self) -> int:
Expand All @@ -260,7 +261,7 @@ class IPv6(DataType):
@property
def value(self) -> str:
"""A data value."""
return util.ip6_from_bytes(self._data)
return socket.inet_ntop(socket.AF_INET6, self._data)

@property
def size(self) -> int:
Expand Down
29 changes: 13 additions & 16 deletions pyplumio/structures/network_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from dataclasses import dataclass, field
import socket
from typing import Final

from pyplumio import util
Expand Down Expand Up @@ -51,13 +52,13 @@ def encode(self, data: EventDataType) -> bytearray:
message = bytearray()
message += b"\x01"
network_info = data[ATTR_NETWORK] if ATTR_NETWORK in data else NetworkInfo()
message += util.ip4_to_bytes(network_info.eth.ip)
message += util.ip4_to_bytes(network_info.eth.netmask)
message += util.ip4_to_bytes(network_info.eth.gateway)
message += socket.inet_aton(network_info.eth.ip)
message += socket.inet_aton(network_info.eth.netmask)
message += socket.inet_aton(network_info.eth.gateway)
message.append(network_info.eth.status)
message += util.ip4_to_bytes(network_info.wlan.ip)
message += util.ip4_to_bytes(network_info.wlan.netmask)
message += util.ip4_to_bytes(network_info.wlan.gateway)
message += socket.inet_aton(network_info.wlan.ip)
message += socket.inet_aton(network_info.wlan.netmask)
message += socket.inet_aton(network_info.wlan.gateway)
message.append(network_info.server_status)
message.append(network_info.wlan.encryption)
message.append(network_info.wlan.signal_quality)
Expand All @@ -78,21 +79,17 @@ def decode(
{
ATTR_NETWORK: NetworkInfo(
eth=EthernetParameters(
ip=util.ip4_from_bytes(message[offset : offset + 4]),
netmask=util.ip4_from_bytes(
message[offset + 4 : offset + 8]
),
gateway=util.ip4_from_bytes(
message[offset + 8 : offset + 12]
),
ip=socket.inet_ntoa(message[offset : offset + 4]),
netmask=socket.inet_ntoa(message[offset + 4 : offset + 8]),
gateway=socket.inet_ntoa(message[offset + 8 : offset + 12]),
status=bool(message[offset + 13]),
),
wlan=WirelessParameters(
ip=util.ip4_from_bytes(message[offset + 13 : offset + 17]),
netmask=util.ip4_from_bytes(
ip=socket.inet_ntoa(message[offset + 13 : offset + 17]),
netmask=socket.inet_ntoa(
message[offset + 17 : offset + 21]
),
gateway=util.ip4_from_bytes(
gateway=socket.inet_ntoa(
message[offset + 21 : offset + 25]
),
encryption=EncryptionType(int(message[offset + 26])),
Expand Down
21 changes: 0 additions & 21 deletions pyplumio/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

import functools
import socket
import struct

unpack_float = struct.Struct("<f").unpack
Expand Down Expand Up @@ -34,26 +33,6 @@ def unpack_string(data: bytearray, offset: int = 0) -> str:
return data[offset : offset + strlen + 1].decode()


def ip4_to_bytes(address: str) -> bytes:
"""Convert an IPv4 address to bytes."""
return socket.inet_aton(address)


def ip4_from_bytes(data: bytes) -> str:
"""Convert bytes to an IPv4 address."""
return socket.inet_ntoa(data)


def ip6_to_bytes(address: str) -> bytes:
"""Convert an IPv6 address to bytes."""
return socket.inet_pton(socket.AF_INET6, address)


def ip6_from_bytes(data: bytes) -> str:
"""Convert bytes to an IPv6 address."""
return socket.inet_ntop(socket.AF_INET6, data)


def to_camelcase(text: str, overrides: dict[str, str] = None) -> str:
"""Convert snake_case to CamelCase."""
if overrides is None:
Expand Down
28 changes: 0 additions & 28 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,6 @@ def test_unpack_ushort() -> None:
assert util.unpack_ushort(b"\x0a\x00") == 10


def test_ip4_to_bytes() -> None:
"""Test conversion from IPv4 to bytes."""
assert util.ip4_to_bytes("127.0.0.1") == b"\x7f\x00\x00\x01"


def test_ip4_from_bytes() -> None:
"""Test conversion from bytes to IPv4."""
assert util.ip4_from_bytes(b"\x7f\x00\x00\x01") == "127.0.0.1"


def test_ip6_from_bytes() -> None:
"""Test conversion from IPv6 to bytes."""
assert (
util.ip6_from_bytes(
b"\xfe\xed\xde\xad\xbe\xef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"
)
== "feed:dead:beef::1"
)


def test_ip6_to_bytes() -> None:
"""Test conversion from bytes to IPv6."""
assert (
util.ip6_to_bytes("feed:dead:beef::1")
== b"\xfe\xed\xde\xad\xbe\xef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"
)


def test_to_camelcase() -> None:
"""Test string to camelcase converter."""
assert util.to_camelcase("make_love_not_war") == "MakeLoveNotWar"
Expand Down

0 comments on commit 07c50d6

Please sign in to comment.