From b00ecf6e869577c9d1dd4997d5ad1ac63a72ee8d Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 21 Apr 2020 21:11:19 +0100 Subject: [PATCH] replace ParamLiteral with string literals --- ircrobots/ircv3.py | 6 +++--- ircrobots/matching.py | 22 ++++++++++------------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/ircrobots/ircv3.py b/ircrobots/ircv3.py index 112a068..835c697 100644 --- a/ircrobots/ircv3.py +++ b/ircrobots/ircv3.py @@ -5,7 +5,7 @@ from irctokens import build from ircstates.server import ServerDisconnectedException from .contexts import ServerContext -from .matching import Response, ResponseOr, ParamAny, ParamLiteral +from .matching import Response, ResponseOr, ParamAny from .interface import ICapability from .params import ConnectionParams, STSPolicy @@ -101,8 +101,8 @@ class CAPContext(ServerContext): while cap_names: line = await self.server.wait_for(ResponseOr( - Response("CAP", [ParamAny(), ParamLiteral("ACK")]), - Response("CAP", [ParamAny(), ParamLiteral("NAK")]) + Response("CAP", [ParamAny(), "ACK"]), + Response("CAP", [ParamAny(), "NAK"]) )) current_caps = line.params[2].split(" ") diff --git a/ircrobots/matching.py b/ircrobots/matching.py index cf5cf52..e977606 100644 --- a/ircrobots/matching.py +++ b/ircrobots/matching.py @@ -1,12 +1,13 @@ -from typing import List, Optional +from typing import List, Optional, Union from irctokens import Line, Hostmask from .interface import (IServer, IMatchResponse, IMatchResponseParam, IMatchResponseHostmask) +TYPE_PARAM = Union[str, IMatchResponseParam] class Responses(IMatchResponse): def __init__(self, commands: List[str], - params: List[IMatchResponseParam]=[], + params: List[TYPE_PARAM]=[], source: Optional[IMatchResponseHostmask]=None): self._commands = commands self._params = params @@ -23,7 +24,12 @@ class Responses(IMatchResponse): ))): 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])): break else: @@ -34,7 +40,7 @@ class Responses(IMatchResponse): class Response(Responses): def __init__(self, command: str, - params: List[IMatchResponseParam]=[]): + params: List[TYPE_PARAM]=[]): super().__init__([command], params) def __repr__(self) -> str: @@ -58,14 +64,6 @@ class ParamAny(IMatchResponseParam): def match(self, server: IServer, arg: str) -> bool: 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): def __init__(self, value: str): self._value = value