From 952b7b6517c0a82fd305811e30137e1c76077b54 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Wed, 2 Apr 2003 07:27:00 +0000 Subject: [PATCH] Added pydoc command --- plugins/FunCommands.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/plugins/FunCommands.py b/plugins/FunCommands.py index bad75dfc8..81a611a02 100644 --- a/plugins/FunCommands.py +++ b/plugins/FunCommands.py @@ -56,6 +56,7 @@ Commands include: last lastfrom lithp + pydoc """ from baseplugin import * @@ -63,6 +64,8 @@ from baseplugin import * import os import gc import re +import imp +import sys import new import md5 import sha @@ -425,6 +428,42 @@ class FunCommands(callbacks.Privmsg): irc.reply(msg, '<%s> %s' % (nick, m.args[1])) return irc.error(msg, 'I don\'t remember a message from that person.') + + modulechars = '%s%s%s' % (string.ascii_letters, string.digits, '_.') + def pydoc(self, irc, msg, args): + """ + + Returns the __doc__ string for a given Python function. + """ + funcname = privmsgs.getArgs(args) + if funcname.translate(string.ascii, self.modulechars) != '': + irc.error('That\'s not a valid module or function name.') + return + if '.' in funcname: + parts = funcname.split('.') + module = '.'.join(parts[:-1]) + if module not in sys.modules: + path = os.path.dirname(os.__file__) + for name in parts[:-1]: + try: + info = imp.find_module(name, path) + newmodule = imp.load_module(name, *info) + path = newmodule.__path__ + info[1].close() + except ImportError: + irc.error(msg, 'No such module %s exists.' % module) + return + try: + s = eval(funcname + '.__doc__') + s = s.replace('\n\n', '. ') + s = s.replace('\n', ' ') + except NameError: + s = 'No such function exists.' + except AttributeError: + s = 'That function has no documentation.' + irc.reply(msg, s) + + Class = FunCommands