add matching.params.Regex

This commit is contained in:
jesopo 2020-04-21 21:59:20 +01:00
parent 2246236e83
commit 4286e75749
1 changed files with 17 additions and 7 deletions

View File

@ -1,4 +1,5 @@
from typing import Optional
from re import compile
from typing import Optional, Pattern
from irctokens import Hostmask
from ..interface import IMatchResponseParam, IMatchResponseHostmask, IServer
from .. import formatting
@ -18,6 +19,14 @@ class Literal(IMatchResponseParam):
def match(self, server: IServer, arg: str) -> bool:
return arg == self._value
class Not(IMatchResponseParam):
def __init__(self, param: IMatchResponseParam):
self._param = param
def __repr__(self) -> str:
return f"Not({self._param!r})"
def match(self, server: IServer, arg: str) -> bool:
return not self._param.match(server, arg)
class Folded(IMatchResponseParam):
def __init__(self, value: str):
self._value = value
@ -37,13 +46,14 @@ class Formatless(Literal):
strip = formatting.strip(arg)
return super().match(server, strip)
class Not(IMatchResponseParam):
def __init__(self, param: IMatchResponseParam):
self._param = param
def __repr__(self) -> str:
return f"Not({self._param!r})"
class Regex(IMatchResponseParam):
def __init__(self, value: str):
self._value = value
self._pattern: Optional[Pattern] = None
def match(self, server: IServer, arg: str) -> bool:
return not self._param.match(server, arg)
if self._pattern is None:
self._pattern = compile(self._value)
return bool(self._pattern.search(arg))
class Nickname(IMatchResponseHostmask):
def __init__(self, nickname: str):