add matching.params.Regex

This commit is contained in:
jesopo 2020-04-21 21:59:20 +01:00
parent 2246236e83
commit 4286e75749

View file

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