ircrobots/ircrobots/asyncs.py

34 lines
1.0 KiB
Python
Raw Normal View History

from asyncio import Future
from typing import (Any, Awaitable, Callable, Generator, Generic, Optional,
TypeVar)
from irctokens import Line
from .matching import IMatchResponse
from .interface import IServer
from .ircv3 import TAG_LABEL
TEvent = TypeVar("TEvent")
class MaybeAwait(Generic[TEvent]):
def __init__(self, func: Callable[[], Awaitable[TEvent]]):
self._func = func
def __await__(self) -> Generator[Any, None, TEvent]:
coro = self._func()
return coro.__await__()
2020-04-23 14:22:30 +00:00
class WaitFor(object):
def __init__(self,
response: IMatchResponse,
2021-02-18 14:50:01 +00:00
label: Optional[str]=None):
self.response = response
2021-02-18 14:50:01 +00:00
self._label = label
2020-04-23 14:22:30 +00:00
def match(self, server: IServer, line: Line):
if (self._label is not None and
line.tags is not None):
label = TAG_LABEL.get(line.tags)
if (label is not None and
label == self._label):
return True
return self.response.match(server, line)