mirror of https://github.com/jesopo/irctokens
add hostmask parsing (irctokens.Hostmask("n!u@h"))
This commit is contained in:
parent
89cc0a3061
commit
2bc35c57f7
|
@ -1,2 +1,2 @@
|
|||
from .protocol import Line, tokenise, format
|
||||
from .protocol import Line, tokenise, format, Hostmask
|
||||
from .stateful import StatefulDecoder, StatefulEncoder
|
||||
|
|
|
@ -12,6 +12,16 @@ def _escape_tag(value: str):
|
|||
value = value.replace(char, TAG_UNESCAPE[i])
|
||||
return value
|
||||
|
||||
class Hostmask(object):
|
||||
def __init__(self, source: str):
|
||||
self._raw = source
|
||||
username, _, hostname = source.partition("@")
|
||||
self.nickname, _, username = username.partition("!")
|
||||
self.username = username or None
|
||||
self.hostname = hostname or None
|
||||
def __str__(self) -> str:
|
||||
return self._raw
|
||||
|
||||
class Line(object):
|
||||
def __init__(self,
|
||||
tags:
|
||||
|
|
|
@ -2,3 +2,4 @@ from .tokenise import *
|
|||
from .format import *
|
||||
from .stateful_decode import *
|
||||
from .stateful_encode import *
|
||||
from .hostmask import *
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import unittest
|
||||
import irctokens
|
||||
|
||||
class HostmaskTestAll(unittest.TestCase):
|
||||
def test_all(self):
|
||||
hostmask = irctokens.Hostmask("nick!user@host")
|
||||
self.assertEqual(hostmask.nickname, "nick")
|
||||
self.assertEqual(hostmask.username, "user")
|
||||
self.assertEqual(hostmask.hostname, "host")
|
||||
|
||||
def test_no_hostname(self):
|
||||
hostmask = irctokens.Hostmask("nick!user")
|
||||
self.assertEqual(hostmask.nickname, "nick")
|
||||
self.assertEqual(hostmask.username, "user")
|
||||
self.assertIsNone(hostmask.hostname)
|
||||
|
||||
def test_no_ident(self):
|
||||
hostmask = irctokens.Hostmask("nick@host")
|
||||
self.assertEqual(hostmask.nickname, "nick")
|
||||
self.assertIsNone(hostmask.username)
|
||||
self.assertEqual(hostmask.hostname, "host")
|
||||
|
||||
def test_only_nickname(self):
|
||||
hostmask = irctokens.Hostmask("nick")
|
||||
self.assertEqual(hostmask.nickname, "nick")
|
||||
self.assertIsNone(hostmask.username)
|
||||
self.assertIsNone(hostmask.hostname)
|
||||
|
Loading…
Reference in New Issue