nft-update-addresses: parse lifetimes to absolute datetime

main
Felix Stupp 1 month ago
parent 7a09055ec3
commit ecff468750
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -12,6 +12,10 @@ from collections.abc import (
Mapping, Mapping,
Sequence, Sequence,
) )
from datetime import (
datetime,
timedelta,
)
from enum import ( from enum import (
Enum, Enum,
Flag, Flag,
@ -267,8 +271,8 @@ class IpAddressUpdate:
ip: IPInterface ip: IPInterface
scope: str scope: str
flags: IpFlag flags: IpFlag
valid_lft: int | Literal["forever"] valid_until: datetime
preferred_lft: int | Literal["forever"] preferred_until: datetime
@classmethod @classmethod
def parse_line(cls, line: str) -> IpAddressUpdate: def parse_line(cls, line: str) -> IpAddressUpdate:
@ -293,22 +297,20 @@ class IpAddressUpdate:
ip=ip, ip=ip,
scope=grp["scope"], scope=grp["scope"],
flags=flags, flags=flags,
valid_lft=cls.parse_lifetime(grp, "valid"), valid_until=cls.__parse_lifetime(grp, "valid_lft"),
preferred_lft=cls.parse_lifetime(grp, "preferred"), preferred_until=cls.__parse_lifetime(grp, "preferred_lft"),
) )
@staticmethod @staticmethod
def parse_lifetime( def __parse_lifetime(grp: Mapping[str, str | None], name: str) -> datetime:
grp: Mapping[str, str | None], name: str if grp[f"{name}_forever"] != None:
) -> int | Literal["forever"]: return datetime.now() + timedelta(days=30)
if grp[f"{name}_lft_forever"] != None: sec = grp[f"{name}_sec"]
return "forever" if sec is None:
sec = grp[f"{name}_lft_sec"]
if sec == None:
raise ValueError( raise ValueError(
"IP address update parse error: expected regex group for seconds != None (bug in code)" "IP address update parse error: expected regex group for seconds != None (bug in code)"
) )
return int(sec) # type: ignore[arg-type] return datetime.now() + timedelta(seconds=int(sec))
def kickoff_ip( def kickoff_ip(

Loading…
Cancel
Save