]> jfr.im git - erebus.git/commitdiff
added help module
authorzonidjan <redacted>
Sat, 20 May 2017 05:59:04 +0000 (00:59 -0500)
committerzonidjan <redacted>
Sat, 20 May 2017 05:59:04 +0000 (00:59 -0500)
config.py
modlib.py
modules/foo.py
modules/help.py [new file with mode: 0644]

index 5f350612efb3aeea941ce5734a8e5806c885f48b..6664f1cef6bf91005b782d15a6bd4ba7e9af9cab 100644 (file)
--- 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
index 6862b0e09f1dceeacc0b8453c274e86f488876af..1fb53ded550143c0b2161db7d40e359eba005d82 100644 (file)
--- 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("<user> <pass>", "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 <command>) will say "<command> - 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
index 526fdff1d6c9c405bcb5f6e83d6d1bb8b53c08d4..68420c8137bec5ac1655069935e02bffd33f0fd8 100644 (file)
@@ -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 (file)
index 0000000..85588b0
--- /dev/null
@@ -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('[<command>]', '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))