add hostmask parsing (irctokens.Hostmask("n!u@h"))

This commit is contained in:
jesopo 2020-03-11 18:04:50 +00:00
parent 89cc0a3061
commit 2bc35c57f7
4 changed files with 40 additions and 1 deletions

View File

@ -1,2 +1,2 @@
from .protocol import Line, tokenise, format
from .protocol import Line, tokenise, format, Hostmask
from .stateful import StatefulDecoder, StatefulEncoder

View File

@ -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:

View File

@ -2,3 +2,4 @@ from .tokenise import *
from .format import *
from .stateful_decode import *
from .stateful_encode import *
from .hostmask import *

28
test/hostmask.py Normal file
View File

@ -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)