add Line.hostmask property to lazy-parse .source in to a Hostmask

This commit is contained in:
jesopo 2020-03-11 20:17:56 +00:00
parent 23c92967c4
commit 9cd39ddcdc
2 changed files with 21 additions and 2 deletions

View File

@ -19,8 +19,14 @@ class Hostmask(object):
self.nickname, _, username = username.partition("!")
self.username = username or None
self.hostname = hostname or None
def __str__(self) -> str:
return self._raw
def __eq__(self, other) -> bool:
if isinstance(other, Hostmask):
return str(self) == str(other)
else:
return False
class Line(object):
def __init__(self,
@ -34,12 +40,18 @@ class Line(object):
self.command = command
self.params = params
def __eq__(self, other):
def __eq__(self, other) -> bool:
if isinstance(other, Line):
return self.format() == other.format()
else:
return False
_hostmask: typing.Optional[Hostmask] = None
@property
def hostmask(self):
self._hostmask = self._hostmask or Hostmask(self.source)
return self._hostmask
def format(self) -> str:
outs: typing.List[str] = []
if self.tags:

View File

@ -1,7 +1,7 @@
import unittest
import irctokens
class HostmaskTestAll(unittest.TestCase):
class HostmaskTest(unittest.TestCase):
def test_all(self):
hostmask = irctokens.Hostmask("nick!user@host")
self.assertEqual(hostmask.nickname, "nick")
@ -26,3 +26,10 @@ class HostmaskTestAll(unittest.TestCase):
self.assertIsNone(hostmask.username)
self.assertIsNone(hostmask.hostname)
def test_line(self):
line = irctokens.tokenise(":nick!user@host PRIVMSG #channel hello")
hostmask = irctokens.Hostmask("nick!user@host")
self.assertEqual(line.hostmask, hostmask)
self.assertEqual(line.hostmask.nickname, "nick")
self.assertEqual(line.hostmask.username, "user")
self.assertEqual(line.hostmask.hostname, "host")