From: zonidjan Date: Sat, 20 May 2017 05:59:04 +0000 (-0500) Subject: added help module X-Git-Url: https://jfr.im/git/erebus.git/commitdiff_plain/0f8352dd7b9e7437ece92f72db01da146aabb51f added help module --- diff --git a/config.py b/config.py index 5f35061..6664f1c 100644 --- a/config.py +++ b/config.py @@ -46,5 +46,8 @@ if __name__ == '__main__': import sys cfg = Config(sys.argv[1], False) - for k, v in cfg.items(): - print k, '=', v + for s in cfg.config.sections(): + for k, v in cfg.items(s): + print s+'.'+k, '=', v +# for k, v in cfg.items(): +# print 'erebus.'+k, '=', v diff --git a/modlib.py b/modlib.py index 6862b0e..1fb53de 100644 --- a/modlib.py +++ b/modlib.py @@ -36,6 +36,7 @@ class modlib(object): self.hooks = {} self.numhooks = {} self.chanhooks = {} + self.helps = [] self.parent = None self.name = name @@ -49,6 +50,12 @@ class modlib(object): self.parent.hooknum(num, func) for chan, func in self.chanhooks.iteritems(): self.parent.hookchan(chan, func) + + for func, args, kwargs in self.helps: + try: + self.mod('help').reghelp(func, *args, **kwargs) + except: + pass return True def modstop(self, parent): for cmd, func in self.hooks.iteritems(): @@ -58,6 +65,12 @@ class modlib(object): self.parent.unhooknum(num, func) for chan, func in self.chanhooks.iteritems(): self.parent.unhookchan(chan, func) + + for func, args, kwargs in self.helps: + try: + self.mod('help').dereghelp(func, *args, **kwargs) + except: + pass return True def hooknum(self, num): @@ -82,13 +95,14 @@ class modlib(object): cmd = _cmd #...and restore it if cmd is None: cmd = func.__name__ # default to function name + if isinstance(cmd, basestring): + cmd = (cmd,) func.needchan = needchan func.reqglevel = glevel func.reqclevel = clevel + func.cmd = cmd - if isinstance(cmd, basestring): - cmd = (cmd,) for c in cmd: self.hooks[c] = func if self.parent is not None: @@ -126,7 +140,7 @@ class modlib(object): return realhook def help(self, *args, **kwargs): - """help(syntax, shorthelp, longhelp, more lines longhelp, cmd=...?) + """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?) Example: help(" ", "login") ^ Help will only be one line. Command name determined based on function name. @@ -136,7 +150,12 @@ class modlib(object): ^ Command takes no args. Short description (in overall HELP listing) is "do stuff". Long description (HELP ) will say " - do stuff", newline, "This command is really complicated." """ - try: - self.mod('help').reghelp(*args, **kwargs) - except: - pass + def realhook(func): + if self.parent is not None: + try: + self.mod('help').reghelp(func, *args, **kwargs) + except: + pass + self.helps.append((func,args,kwargs)) + return func + return realhook diff --git a/modules/foo.py b/modules/foo.py index 526fdff..68420c8 100644 --- a/modules/foo.py +++ b/modules/foo.py @@ -26,6 +26,6 @@ def test(bot, user, chan, realtarget, *args): bot.msg(replyto, "You said: %s" % (' '.join([str(arg) for arg in args]))) @lib.hook(('foo', 'bar'), needchan=False) #hooks !foo and !bar as aliases -@lib.help(None, 'replies with nonsense.', cmd=('foo', 'bar')) +@lib.help(None, 'replies with nonsense.', "it's a very non-sensical command", "more lines") def foobar(bot, user, chan, realtarget, *args): bot.msg(user, "Foo bar baz.") diff --git a/modules/help.py b/modules/help.py new file mode 100644 index 0000000..85588b0 --- /dev/null +++ b/modules/help.py @@ -0,0 +1,86 @@ +# Erebus IRC bot - Author: Erebus Team +# help module +# This file is released into the public domain; see http://unlicense.org/ + +# module info +modinfo = { + 'author': 'Erebus Team', + 'license': 'public domain', + 'compatible': [1], # compatible module API versions + 'depends': [], # other modules required to work properly? +} + +# preamble +import modlib +lib = modlib.modlib(__name__) +modstart = lib.modstart +modstop = lib.modstop + +# module code +helps = {} +cmds = {} + +# ! this is part of this module's API, called from modlib.help() +# this function only handles the command name and aliases - the rest is passed directly to _reghelp() +def reghelp(func, *args, **kwargs): + syntax = None + shorthelp = None + longhelps = [] + + if len(args) > 0: + syntax = args[0] + if len(args) > 1: + shorthelp = args[1] + if len(args) > 2: + longhelps = args[2:] + + if 'syntax' in kwargs: + syntax = kwargs['syntax'] + if 'shorthelp' in kwargs: + shorthelp = kwargs['shorthelp'] + if 'longhelps' in kwargs: + longhelps = kwargs['longhelps'] + + if syntax is None: syntax = "" + if shorthelp is None: shorthelp = "" + + func.syntax = syntax + func.shorthelp = shorthelp + func.longhelps = longhelps + helps[func] = func + for c in func.cmd: + cmds[c] = func + +def dereghelp(func, *args, **kwargs): + for c in func.cmd: + del cmds[cmd] + del helps[func] + +@lib.hook(needchan=False) +@lib.help('[]', 'lists commands or describes a command') +def help(bot, user, chan, realtarget, *args): + if len(args) == 0: + for func in helps.itervalues(): + if user.glevel >= func.reqglevel: + if func.reqglevel <= 0: + bot.slowmsg(user, "%-40s - %-50s" % (func.cmd[0]+' '+func.syntax, func.shorthelp)) + else: + bot.slowmsg(user, "%-40s - %-50s (%5s)" % (func.cmd[0]+' '+func.syntax, func.shorthelp, func.reqglevel)) + if len(func.cmd) > 1: + for c in func.cmd[1:]: + bot.slowmsg(user, "%-40s - Alias of %s" % (c, func.cmd[0])) + else: + cmd = str(' '.join(args)) + if cmd in cmds and user.glevel >= cmds[cmd].reqglevel: + func = cmds[cmd] + if func.reqglevel <= 0: + bot.slowmsg(user, "%-40s - %-50s" % (func.cmd[0]+' '+func.syntax, func.shorthelp)) + else: + bot.slowmsg(user, "%-40s - %-50s (%5s)" % (func.cmd[0]+' '+func.syntax, func.shorthelp, func.reqglevel)) + for line in func.longhelps: + bot.slowmsg(user, " %s" % (line)) + + if len(func.cmd) > 1: + bot.slowmsg(user, " Aliases: %s" % (' '.join(func.cmd[1:]))) + else: + bot.slowmsg(user, "No help found for %s" % (cmd))