add IRCBuffer.find_all()

This commit is contained in:
jesopo 2020-01-30 14:57:24 +00:00
parent b296a2df05
commit 73aeb6bde4

View file

@ -38,8 +38,11 @@ class Buffer(object):
def __init__(self, bot: "IRCBot.Bot", server: "IRCServer.Server"): def __init__(self, bot: "IRCBot.Bot", server: "IRCServer.Server"):
self.bot = bot self.bot = bot
self.server = server self.server = server
self._lines = collections.deque(maxlen=MAX_LINES self._lines: typing.Deque[BufferLine] = collections.deque(
) # type: typing.Deque[BufferLine] maxlen=MAX_LINES)
def __len__(self) -> int:
return len(self._lines)
def add(self, line: BufferLine): def add(self, line: BufferLine):
self._lines.appendleft(line) self._lines.appendleft(line)
@ -62,10 +65,10 @@ class Buffer(object):
for line in self._lines: for line in self._lines:
yield line yield line
def find(self, pattern: typing.Union[str, typing.Pattern[str]], def find_all(self, pattern: typing.Union[str, typing.Pattern[str]],
not_pattern: typing.Union[str, typing.Pattern[str]]=None, not_pattern: typing.Union[str, typing.Pattern[str]]=None,
from_self=True, for_user: str=None, deleted=False from_self=True, for_user: str=None, deleted=False
) -> typing.Optional[BufferLineMatch]: ) -> typing.Generator[BufferLineMatch, None, None]:
if for_user: if for_user:
for_user = self.server.irc_lower(for_user) for_user = self.server.irc_lower(for_user)
@ -82,8 +85,11 @@ class Buffer(object):
continue continue
if line.deleted and not deleted: if line.deleted and not deleted:
continue continue
return BufferLineMatch(line, match.group(0)) yield BufferLineMatch(line, match.group(0))
return None return None
def find(self, pattern: typing.Union[str, typing.Pattern[str]]
) -> typing.Optional[BufferLineMatch]:
return next(self.find_all(pattern), None)
def find_id(self, id: str) -> typing.Optional[BufferLine]: def find_id(self, id: str) -> typing.Optional[BufferLine]:
for line in self._lines: for line in self._lines: