]> jfr.im git - erebus.git/blame - modules/help.py
help: refactor logic around _genhelp
[erebus.git] / modules / help.py
CommitLineData
0f8352dd 1# Erebus IRC bot - Author: Erebus Team
2# help module
3# This file is released into the public domain; see http://unlicense.org/
4
5# module info
6modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
a62d0d18 9 'compatible': [1,2],
10 'depends': [],
11 'softdeps': [],
0f8352dd 12}
13
14# preamble
15import modlib
16lib = modlib.modlib(__name__)
17modstart = lib.modstart
18modstop = lib.modstop
19
20# module code
bc68cb5e 21import os.path
0f8352dd 22helps = {}
23cmds = {}
24
25# ! this is part of this module's API, called from modlib.help()
0f8352dd 26def reghelp(func, *args, **kwargs):
27 syntax = None
28 shorthelp = None
29 longhelps = []
30
31 if len(args) > 0:
32 syntax = args[0]
33 if len(args) > 1:
34 shorthelp = args[1]
35 if len(args) > 2:
36 longhelps = args[2:]
37
38 if 'syntax' in kwargs:
39 syntax = kwargs['syntax']
40 if 'shorthelp' in kwargs:
41 shorthelp = kwargs['shorthelp']
42 if 'longhelps' in kwargs:
43 longhelps = kwargs['longhelps']
44
45 if syntax is None: syntax = ""
46 if shorthelp is None: shorthelp = ""
47
48 func.syntax = syntax
49 func.shorthelp = shorthelp
50 func.longhelps = longhelps
51 helps[func] = func
52 for c in func.cmd:
53 cmds[c] = func
54
55def dereghelp(func, *args, **kwargs):
56 for c in func.cmd:
bc68cb5e 57 del cmds[c]
0f8352dd 58 del helps[func]
59
7d0de55e 60class HelpLine(object):
d58c924f 61 def __init__(self, cmd, syntax, shorthelp, admin, glevel, module, clevel):
7d0de55e 62 self.cmd = cmd
63 self.syntax = syntax
64 self.shorthelp = shorthelp
58401d09 65 self.admin = admin
d58c924f 66 self.glevel = glevel
58401d09 67 self.module = module
d58c924f 68 self.clevel = clevel
7d0de55e 69
70 def __cmp__(self, other):
d58c924f 71 if self.glevel == other.glevel:
7d0de55e 72 return cmp(self.cmd, other.cmd)
73 else:
d58c924f 74 return cmp(self.glevel, other.glevel)
7d0de55e 75
76
77 def __str__(self):
58401d09 78 if self.admin:
d58c924f 79 ret = "%-35s(%3s) - %-10s - " % (self.cmd+' '+self.syntax, self.glevel, self.module)
7d0de55e 80 else:
d58c924f 81 ret = "%-40s - " % (self.cmd+' '+self.syntax)
82 if self.clevel != 0:
83 ret += "(%s) " % (lib.clevs[self.clevel])
84 ret += str(self.shorthelp)
85 return ret
7d0de55e 86
bc68cb5e 87def _mkhelp(level, func):
88 lines = []
89 if level >= func.reqglevel:
d58c924f 90 lines.append(HelpLine(func.cmd[0], func.syntax, func.shorthelp, (level > 0), func.reqglevel, func.module, func.reqclevel))
bc68cb5e 91 if len(func.cmd) > 1:
92 for c in func.cmd[1:]:
d58c924f 93 lines.append(HelpLine(c, "", "Alias of %s" % (func.cmd[0]), (level > 0), func.reqglevel, func.module, func.reqclevel))
bc68cb5e 94 return lines
95
96def _genhelp(bot, user, chan, realtarget, *args):
0d93d7b4 97 filepath = bot.parent.cfg.get('help', 'path', default='./help/%d.txt')
98 for level in range(-1, 101):
99 filename = filepath % (level)
100 fo = open(filename, 'w')
101 lines = []
102 for func in helps.itervalues():
103 lines += _mkhelp(level, func)
104 for line in sorted(lines):
105 fo.write(str(line)+"\n")
bc68cb5e 106 return True
107
108@lib.hook(glevel=1, needchan=False)
109@lib.help(None, "generates help file", "default path: ./help/<level>.txt", "config as: [help]", "path = ./help/%d.txt")
bc68cb5e 110def genhelp(bot, user, chan, realtarget, *args):
0d93d7b4 111 try:
112 _genhelp(bot, user, chan, realtarget, *args)
113 except Exception as e:
114 bot.msg(user, "Failed writing help. %s" % (e))
115 return
116 bot.msg(user, "Help written.")
bc68cb5e 117
0f8352dd 118@lib.hook(needchan=False)
bc68cb5e 119@lib.help("<command>", "describes a command")
120@lib.argsGE(1)
121def help(bot, user, chan, realtarget, *args):
122 cmd = str(' '.join(args)).lower()
123 if cmd in cmds and user.glevel >= cmds[cmd].reqglevel:
124 func = cmds[cmd]
d58c924f 125 bot.slowmsg(user, str(HelpLine(func.cmd[0], func.syntax, func.shorthelp, (user.glevel > 0), func.reqglevel, func.module, func.reqclevel)))
bc68cb5e 126 for line in func.longhelps:
127 bot.slowmsg(user, " %s" % (line))
128 bot.slowmsg(user, "End of help for %s." % (func.cmd[0]))
129 if len(func.cmd) > 1:
130 bot.slowmsg(user, " Aliases: %s" % (' '.join(func.cmd[1:])))
131 else:
132 bot.slowmsg(user, "No help found for %s" % (cmd))
133
134@lib.hook(needchan=False)
135@lib.help(None, "provides command list")
136def showcommands(bot, user, chan, realtarget, *args):
137 if bool(int(bot.parent.cfg.get('help', 'autogen', default=0))):
0d93d7b4 138 try:
139 _genhelp(bot, user, chan, realtarget, *args)
140 except: pass
141
bc68cb5e 142 url = bot.parent.cfg.get('help', 'url', default=None)
143 if url is None:
144 try:
145 import urllib2
146 myip = urllib2.urlopen("https://api.ipify.org").read()
147 url = "http://%s/help/%%d.txt (maybe)" % (myip)
148 except: url = None
149 if url is not None:
150 url = url % (user.glevel)
151 bot.msg(user, "Help is at: %s" % (url))
152 else:
153 bot.msg(user, "I don't know where help is. Sorry. Contact my owner.")
154
155"""#DISABLED
156@lib.hook(needchan=False)
157@lib.help('[@<module>|<command>]', 'lists commands or describes a command', 'with @<module>, lists all commands in <module>')
163cc00a 158def help(bot, user, chan, realtarget, *args):
5f03d045 159 if len(args) == 0: # list commands
7d0de55e 160 lines = []
0f8352dd 161 for func in helps.itervalues():
bc68cb5e 162 lines += _mkhelp(user, func)
163 for line in sorted(lines):
164 bot.slowmsg(user, str(line))
165 bot.slowmsg(user, "End of command listing.")
166 elif args[0][0] == "@":
167 lines = []
168 mod = args[0][1:].lower()
169 for func in helps.itervalues():
170 if func.module == mod:
171 lines += _mkhelp(user, func)
7d0de55e 172 for line in sorted(lines):
173 bot.slowmsg(user, str(line))
954cae0b 174 bot.slowmsg(user, "End of command listing.")
5f03d045 175 else: # help for a specific command/topic
bc68cb5e 176 cmd = str(' '.join(args)).lower()
0f8352dd 177 if cmd in cmds and user.glevel >= cmds[cmd].reqglevel:
178 func = cmds[cmd]
bc68cb5e 179 bot.slowmsg(user, str(HelpLine(func.cmd[0], func.syntax, func.shorthelp, (user.glevel > 0), func.reqglevel, func.module)))
0f8352dd 180 for line in func.longhelps:
181 bot.slowmsg(user, " %s" % (line))
954cae0b 182 bot.slowmsg(user, "End of help for %s." % (func.cmd[0]))
0f8352dd 183
184 if len(func.cmd) > 1:
185 bot.slowmsg(user, " Aliases: %s" % (' '.join(func.cmd[1:])))
186 else:
187 bot.slowmsg(user, "No help found for %s" % (cmd))
bc68cb5e 188"""
189pass