4
0
mirror of https://github.com/jesopo/ircrobots synced 2024-06-15 04:36:37 +00:00

replace ParamLiteral with string literals

This commit is contained in:
jesopo 2020-04-21 21:11:19 +01:00
parent f14f2b2e6b
commit b00ecf6e86
2 changed files with 13 additions and 15 deletions

View File

@ -5,7 +5,7 @@ from irctokens import build
from ircstates.server import ServerDisconnectedException from ircstates.server import ServerDisconnectedException
from .contexts import ServerContext from .contexts import ServerContext
from .matching import Response, ResponseOr, ParamAny, ParamLiteral from .matching import Response, ResponseOr, ParamAny
from .interface import ICapability from .interface import ICapability
from .params import ConnectionParams, STSPolicy from .params import ConnectionParams, STSPolicy
@ -101,8 +101,8 @@ class CAPContext(ServerContext):
while cap_names: while cap_names:
line = await self.server.wait_for(ResponseOr( line = await self.server.wait_for(ResponseOr(
Response("CAP", [ParamAny(), ParamLiteral("ACK")]), Response("CAP", [ParamAny(), "ACK"]),
Response("CAP", [ParamAny(), ParamLiteral("NAK")]) Response("CAP", [ParamAny(), "NAK"])
)) ))
current_caps = line.params[2].split(" ") current_caps = line.params[2].split(" ")

View File

@ -1,12 +1,13 @@
from typing import List, Optional from typing import List, Optional, Union
from irctokens import Line, Hostmask from irctokens import Line, Hostmask
from .interface import (IServer, IMatchResponse, IMatchResponseParam, from .interface import (IServer, IMatchResponse, IMatchResponseParam,
IMatchResponseHostmask) IMatchResponseHostmask)
TYPE_PARAM = Union[str, IMatchResponseParam]
class Responses(IMatchResponse): class Responses(IMatchResponse):
def __init__(self, def __init__(self,
commands: List[str], commands: List[str],
params: List[IMatchResponseParam]=[], params: List[TYPE_PARAM]=[],
source: Optional[IMatchResponseHostmask]=None): source: Optional[IMatchResponseHostmask]=None):
self._commands = commands self._commands = commands
self._params = params self._params = params
@ -23,7 +24,12 @@ class Responses(IMatchResponse):
))): ))):
for i, param in enumerate(self._params): for i, param in enumerate(self._params):
if (i >= len(line.params) or if i >= len(line.params):
break
elif (isinstance(param, str) and
not param == line.params[i]):
break
elif (isinstance(param, IMatchResponseParam) and
not param.match(server, line.params[i])): not param.match(server, line.params[i])):
break break
else: else:
@ -34,7 +40,7 @@ class Responses(IMatchResponse):
class Response(Responses): class Response(Responses):
def __init__(self, def __init__(self,
command: str, command: str,
params: List[IMatchResponseParam]=[]): params: List[TYPE_PARAM]=[]):
super().__init__([command], params) super().__init__([command], params)
def __repr__(self) -> str: def __repr__(self) -> str:
@ -58,14 +64,6 @@ class ParamAny(IMatchResponseParam):
def match(self, server: IServer, arg: str) -> bool: def match(self, server: IServer, arg: str) -> bool:
return True return True
class ParamLiteral(IMatchResponseParam):
def __init__(self, value: str):
self._value = value
def __repr__(self) -> str:
return f"Literal({self._value!r})"
def match(self, server: IServer, arg: str) -> bool:
return self._value == arg
class ParamFolded(IMatchResponseParam): class ParamFolded(IMatchResponseParam):
def __init__(self, value: str): def __init__(self, value: str):
self._value = value self._value = value