Skip to content

Commit

Permalink
feat(#267): Added HostPort type
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Jul 21, 2024
1 parent cd72113 commit e5fa38d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pokete_classes/multiplayer/host_port.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e5fa38d

Please sign in to comment.