From e5fa38de02a48806da813c32e771c4a2c98ed1af Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Sun, 21 Jul 2024 18:38:02 +0200 Subject: [PATCH] feat(#267): Added HostPort type --- pokete_classes/multiplayer/host_port.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 pokete_classes/multiplayer/host_port.py diff --git a/pokete_classes/multiplayer/host_port.py b/pokete_classes/multiplayer/host_port.py new file mode 100644 index 00000000..c38eb45c --- /dev/null +++ b/pokete_classes/multiplayer/host_port.py @@ -0,0 +1,24 @@ +class HostPortParseException(Exception): + pass + + +class HostPort: + def __init__(self, host: str, port: int | None = None): + self.host: str = host + self.port: int | None = port + + @classmethod + def parse(cls, s: str) -> "HostPort": + splid = s.split(":") + if len(splid) == 1: + return cls(splid[0]) + elif len(splid) == 2: + try: + return cls(splid[0], int(splid[1])) + except ValueError: + HostPortParseException("Invalid port") + else: + raise HostPortParseException("Invalid format") + + def __str__(self): + return f"{self.host}:{self.port}" if self.port is not None else self.host