1
0
Fork 0
nemubot/modules/man.py

79 lines
2.3 KiB
Python
Raw Normal View History

2015-12-02 23:29:16 +00:00
"""Read manual pages on IRC"""
2015-12-02 23:29:16 +00:00
# PYTHON STUFFS #######################################################
2014-11-13 01:51:49 +00:00
import subprocess
import re
import os
from nemubot.hooks import hook
from nemubot.module.more import Response
2015-12-02 23:29:16 +00:00
# GLOBALS #############################################################
RGXP_s = re.compile(b'\x1b\\[[0-9]+m')
2014-11-13 01:51:49 +00:00
2015-12-02 23:29:16 +00:00
# MODULE INTERFACE ####################################################
@hook.command("MAN",
help="Show man pages",
help_usage={
"SUBJECT": "Display the default man page for SUBJECT",
"SECTION SUBJECT": "Display the man page in SECTION for SUBJECT"
})
def cmd_man(msg):
args = ["man"]
num = None
2015-07-10 21:09:54 +00:00
if len(msg.args) == 1:
args.append(msg.args[0])
elif len(msg.args) >= 2:
try:
2015-07-10 21:09:54 +00:00
num = int(msg.args[0])
args.append("%d" % num)
2015-07-10 21:09:54 +00:00
args.append(msg.args[1])
except ValueError:
2015-07-10 21:09:54 +00:00
args.append(msg.args[0])
os.unsetenv("LANG")
res = Response(channel=msg.channel)
2014-11-13 01:51:49 +00:00
with subprocess.Popen(args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as proc:
for line in proc.stdout.read().split(b"\n"):
(line, n) = RGXP_s.subn(b'', line)
res.append_message(line.decode())
if len(res.messages) <= 0:
if num is not None:
2014-11-13 01:51:49 +00:00
res.append_message("There is no entry %s in section %d." %
2015-07-10 21:09:54 +00:00
(msg.args[0], num))
else:
2015-07-10 21:09:54 +00:00
res.append_message("There is no man page for %s." % msg.args[0])
return res
2014-11-13 01:51:49 +00:00
2015-12-02 23:29:16 +00:00
@hook.command("man",
help="Show man pages synopsis (in one line)",
help_usage={
"SUBJECT": "Display man page synopsis for SUBJECT",
})
def cmd_whatis(msg):
2015-07-10 21:09:54 +00:00
args = ["whatis", " ".join(msg.args)]
res = Response(channel=msg.channel)
2014-11-13 01:51:49 +00:00
with subprocess.Popen(args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as proc:
for line in proc.stdout.read().split(b"\n"):
(line, n) = RGXP_s.subn(b'', line)
res.append_message(" ".join(line.decode().split()))
if len(res.messages) <= 0:
2015-10-29 23:22:52 +00:00
res.append_message("There is no man page for %s." % msg.args[0])
return res