allow Numerics() with params use NUMERICS list from ircstates

This commit is contained in:
jesopo 2020-04-09 11:09:15 +01:00
parent 9265fd0c48
commit 9f832df2f1
4 changed files with 37 additions and 40 deletions

View file

@ -1,43 +1,55 @@
from typing import List, Optional from typing import List, Optional
from irctokens import Line from irctokens import Line
from .numerics import NUMERIC_NAMES from ircstates import NUMERIC_NAMES
from .interface import IServer, IMatchResponse, IMatchResponseParam from .interface import IServer, IMatchResponse, IMatchResponseParam
class Response(IMatchResponse): class Responses(IMatchResponse):
def __init__(self, def __init__(self,
command: str, commands: List[str],
params: List[IMatchResponseParam]): params: List[IMatchResponseParam]=[]):
self._command = command self._commands = commands
self._params = params self._params = params
def __repr__(self) -> str: def __repr__(self) -> str:
return f"Response({self._command}: {self._params!r})" return f"Responses({self._commands!r}: {self._params!r})"
def match(self, server: IServer, line: Line) -> bool: def match(self, server: IServer, line: Line) -> bool:
if line.command == self._command: for command in self._commands:
for i, param in enumerate(self._params): if line.command == command:
if (i >= len(line.params) or for i, param in enumerate(self._params):
not param.match(server, line.params[i])): if (i >= len(line.params) or
return False not param.match(server, line.params[i])):
else: continue
return True else:
return True
else: else:
return False return False
class Response(Responses):
def __init__(self,
command: str,
params: List[IMatchResponseParam]=[]):
super().__init__([command], params)
def __repr__(self) -> str:
return f"Response({self._commands[0]}: {self._params!r})"
class Numeric(Response): class Numeric(Response):
def __init__(self, def __init__(self,
name: str, name: str,
params: List[IMatchResponseParam]=[]): params: List[IMatchResponseParam]=[]):
super().__init__(NUMERIC_NAMES.get(name, name), params) super().__init__(NUMERIC_NAMES.get(name, name), params)
class Numerics(IMatchResponse): class Numerics(Responses):
def __init__(self, def __init__(self,
numerics: List[str]): numerics: List[str],
self._numerics = [NUMERIC_NAMES.get(n, n) for n in numerics] params: List[IMatchResponseParam]=[]):
def __repr__(self) -> str: self._numerics = numerics
return f"Numerics({self._numerics!r})" numerics = [NUMERIC_NAMES.get(n, n) for n in numerics]
super().__init__(numerics, params)
def match(self, server: IServer, line: Line): def __repr__(self) -> str:
return line.command in self._numerics return f"Numerics({self._numerics!r}: {self._params!r})"
class ResponseOr(IMatchResponse): class ResponseOr(IMatchResponse):
def __init__(self, *responses: IMatchResponse): def __init__(self, *responses: IMatchResponse):

View file

@ -1,16 +0,0 @@
NUMERIC_NUMBERS = {}
NUMERIC_NAMES = {}
def _numeric(number: str, name: str):
NUMERIC_NUMBERS[number] = name
NUMERIC_NAMES[name] = number
_numeric("001", "RPL_WELCOME")
_numeric("005", "RPL_ISUPPORT")
_numeric("903", "RPL_SASLSUCCESS")
_numeric("904", "ERR_SASLFAIL")
_numeric("905", "ERR_SASLTOOLONG")
_numeric("906", "ERR_SASLABORTED")
_numeric("907", "ERR_SASLALREADY")
_numeric("908", "RPL_SASLMECHS")

View file

@ -3,13 +3,14 @@ from typing import Awaitable, Deque, Dict, List, Optional, Set, Tuple
from collections import deque from collections import deque
from asyncio_throttle import Throttler from asyncio_throttle import Throttler
from ircstates import Emit, Channel from ircstates import Emit, Channel, NUMERIC_NAMES
from irctokens import build, Line, tokenise from irctokens import build, Line, tokenise
from .ircv3 import CAPContext, CAP_SASL from .ircv3 import CAPContext, CAP_SASL
from .sasl import SASLContext, SASLResult from .sasl import SASLContext, SASLResult
from .matching import Numeric, ParamAny, ParamFolded from .matching import ResponseOr, Numerics, Numeric, ParamAny, ParamFolded
from .asyncs import MaybeAwait from .asyncs import MaybeAwait
from .struct import Whois
from .interface import (ConnectionParams, ICapability, IServer, SentLine, from .interface import (ConnectionParams, ICapability, IServer, SentLine,
SendPriority, SASLParams, IMatchResponse) SendPriority, SASLParams, IMatchResponse)

View file

@ -1,4 +1,4 @@
anyio ==1.3.0 anyio ==1.3.0
asyncio-throttle ==1.0.1 asyncio-throttle ==1.0.1
dataclasses ==0.6 dataclasses ==0.6
ircstates ==0.8.3 ircstates ==0.8.7