2015-11-13 00:39:30 +00:00
|
|
|
"""Filter messages, displaying lines matching a pattern"""
|
|
|
|
|
|
|
|
# PYTHON STUFFS #######################################################
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from nemubot import context
|
|
|
|
from nemubot.exception import IMException
|
|
|
|
from nemubot.hooks import hook
|
|
|
|
from nemubot.message import Command, Text
|
|
|
|
|
|
|
|
from more import Response
|
|
|
|
|
|
|
|
|
|
|
|
# MODULE CORE #########################################################
|
|
|
|
|
2016-01-14 22:54:11 +00:00
|
|
|
def grep(fltr, cmd, msg, icase=False, only=False):
|
2015-11-13 00:39:30 +00:00
|
|
|
"""Perform a grep like on known nemubot structures
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
fltr -- The filter regexp
|
|
|
|
cmd -- The subcommand to execute
|
|
|
|
msg -- The original message
|
2016-01-12 15:50:49 +00:00
|
|
|
icase -- like the --ignore-case parameter of grep
|
2016-01-12 15:39:01 +00:00
|
|
|
only -- like the --only-matching parameter of grep
|
2015-11-13 00:39:30 +00:00
|
|
|
"""
|
|
|
|
|
2016-01-12 15:50:49 +00:00
|
|
|
fltr = re.compile(fltr, re.I if icase else 0)
|
2016-01-12 15:39:01 +00:00
|
|
|
|
2016-01-14 22:54:11 +00:00
|
|
|
for r in context.subtreat(context.subparse(msg, cmd)):
|
2015-11-13 00:39:30 +00:00
|
|
|
if isinstance(r, Response):
|
|
|
|
for i in range(len(r.messages) - 1, -1, -1):
|
|
|
|
if isinstance(r.messages[i], list):
|
|
|
|
for j in range(len(r.messages[i]) - 1, -1, -1):
|
2016-01-12 15:39:01 +00:00
|
|
|
res = fltr.match(r.messages[i][j])
|
|
|
|
if not res:
|
2015-11-13 00:39:30 +00:00
|
|
|
r.messages[i].pop(j)
|
2016-01-12 15:39:01 +00:00
|
|
|
elif only:
|
|
|
|
r.messages[i][j] = res.group(1) if fltr.groups else res.group(0)
|
2015-11-13 00:39:30 +00:00
|
|
|
if len(r.messages[i]) <= 0:
|
|
|
|
r.messages.pop(i)
|
2016-01-12 15:39:01 +00:00
|
|
|
elif isinstance(r.messages[i], str):
|
|
|
|
res = fltr.match(r.messages[i])
|
|
|
|
if not res:
|
|
|
|
r.messages.pop(i)
|
|
|
|
elif only:
|
|
|
|
r.messages[i] = res.group(1) if fltr.groups else res.group(0)
|
2015-11-13 00:39:30 +00:00
|
|
|
yield r
|
|
|
|
|
|
|
|
elif isinstance(r, Text):
|
2016-01-12 15:39:01 +00:00
|
|
|
res = fltr.match(r.message)
|
|
|
|
if res:
|
|
|
|
if only:
|
|
|
|
r.message = res.group(1) if fltr.groups else res.group(0)
|
2015-11-13 00:39:30 +00:00
|
|
|
yield r
|
|
|
|
|
|
|
|
else:
|
|
|
|
yield r
|
|
|
|
|
|
|
|
|
|
|
|
# MODULE INTERFACE ####################################################
|
|
|
|
|
|
|
|
@hook.command("grep",
|
|
|
|
help="Display only lines from a subcommand matching the given pattern",
|
2016-01-12 15:39:01 +00:00
|
|
|
help_usage={"PTRN !SUBCMD": "Filter SUBCMD command using the pattern PTRN"},
|
|
|
|
keywords={
|
2016-01-12 15:50:49 +00:00
|
|
|
"nocase": "Perform case-insensitive matching",
|
2016-01-12 15:39:01 +00:00
|
|
|
"only": "Print only the matched parts of a matching line",
|
|
|
|
})
|
2015-11-13 00:39:30 +00:00
|
|
|
def cmd_grep(msg):
|
|
|
|
if len(msg.args) < 2:
|
|
|
|
raise IMException("Please provide a filter and a command")
|
|
|
|
|
2016-01-12 15:39:01 +00:00
|
|
|
only = "only" in msg.kwargs
|
|
|
|
|
2016-01-12 15:49:51 +00:00
|
|
|
l = [m for m in grep(msg.args[0] if msg.args[0][0] == "^" else ".*?(" + msg.args[0] + ").*?",
|
2016-01-14 22:54:11 +00:00
|
|
|
" ".join(msg.args[1:]),
|
2016-01-12 15:49:51 +00:00
|
|
|
msg,
|
2016-01-12 15:50:49 +00:00
|
|
|
icase="nocase" in msg.kwargs,
|
2016-01-12 15:49:51 +00:00
|
|
|
only=only) if m is not None]
|
|
|
|
|
|
|
|
if len(l) <= 0:
|
|
|
|
raise IMException("Pattern not found in output")
|
|
|
|
|
|
|
|
return l
|