Debian Codegraph @

asusrouter/modules/ip_address.py

raw
1 """IP address module."""
2
3 from enum import Enum
4 import re
5
6 from asusrouter.tools.converters import clean_string
7
8
9 class IPAddressType(str, Enum):
10 """IP address type class."""
11
12 DHCP = "dhcp"
13 PPPOE = "pppoe"
14 PPTP = "pptp"
15 STATIC = "static"
16 UNKNOWN = "unknown"
17
18
19 def read_ip_address_type(data: str | None) -> IPAddressType:
20 """Read IP address type from data string."""
21
22 data = clean_string(data)
23
24 if data:
25 data = data.lower()
26
27 # Find the IP address type in the IPAddressType enum by value
28 for ip_address_type in IPAddressType:
29 if ip_address_type.value == data:
30 return ip_address_type
31
32 # Some other values to convert
33 if data == "manual":
34 return IPAddressType.STATIC
35
36 # If the IP address type is not found, return the UNKNOWN type
37 return IPAddressType.UNKNOWN
38
39
40 def read_dns_ip_address(data: str) -> list[str]:
41 """Read DNS record and return the list of IP addresses."""
42
43 # Regex to find each IP address in a string of IP addresses
44 regex = r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
45 # Find all IP addresses in the string
46 return re.findall(regex, data)