mirror of https://github.com/jesopo/irctokens
add Line.hostmask property to lazy-parse .source in to a Hostmask
This commit is contained in:
parent
23c92967c4
commit
9cd39ddcdc
|
@ -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:
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue