Fix passive command matching and add regex flag args

This commit is contained in:
Tulir Asokan 2019-02-05 12:08:12 +02:00
parent 76c9c40844
commit a57df919f5

View File

@ -326,10 +326,17 @@ def argument(name: str, label: str = None, *, required: bool = True, matches: Op
def passive(regex: Union[str, Pattern], *, msgtypes: Sequence[MessageType] = (MessageType.TEXT,), def passive(regex: Union[str, Pattern], *, msgtypes: Sequence[MessageType] = (MessageType.TEXT,),
field: Callable[[MaubotMessageEvent], str] = lambda evt: evt.content.body, field: Callable[[MaubotMessageEvent], str] = lambda evt: evt.content.body,
event_type: EventType = EventType.ROOM_MESSAGE, multiple: bool = False event_type: EventType = EventType.ROOM_MESSAGE, multiple: bool = False,
case_insensitive: bool = False, multiline: bool = False, dot_all: bool = False
) -> PassiveCommandHandlerDecorator: ) -> PassiveCommandHandlerDecorator:
if not isinstance(regex, Pattern): if not isinstance(regex, Pattern):
regex = re.compile(regex) regex = re.compile(regex)
if case_insensitive:
regex.flags |= re.IGNORECASE
if multiline:
regex.flags |= re.MULTILINE
if dot_all:
regex.flags |= re.DOTALL
def decorator(func: CommandHandlerFunc) -> CommandHandlerFunc: def decorator(func: CommandHandlerFunc) -> CommandHandlerFunc:
combine = None combine = None
@ -352,7 +359,7 @@ def passive(regex: Union[str, Pattern], *, msgtypes: Sequence[MessageType] = (Me
val = [(data[match.pos:match.endpos], *match.groups()) val = [(data[match.pos:match.endpos], *match.groups())
for match in regex.finditer(data)] for match in regex.finditer(data)]
else: else:
match = regex.match(data) match = regex.search(data)
if match: if match:
val = (data[match.pos:match.endpos], *match.groups()) val = (data[match.pos:match.endpos], *match.groups())
else: else: