Refactored a bit and allow 'pydoc copy'.

This commit is contained in:
Jeremy Fincher 2003-11-04 06:26:24 +00:00
parent 3f8c149047
commit 8e45f35946
2 changed files with 48 additions and 36 deletions

View File

@ -102,26 +102,11 @@ class Python(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable):
""" """
def normalize(s): def normalize(s):
return utils.normalizeWhitespace(s.replace('\n\n', '.')) return utils.normalizeWhitespace(s.replace('\n\n', '.'))
funcname = privmsgs.getArgs(args) def getModule(name, path=pythonPath):
if funcname.translate(string.ascii, self.modulechars) != '': if name in sys.modules:
irc.error('That\'s not a valid module or function name.') return sys.modules[name]
return
if '.' in funcname:
parts = funcname.split('.')
if len(parts) == 2 and parts[0] in __builtins__:
(objectname, methodname) = parts
obj = __builtins__[objectname]
if hasattr(obj, methodname):
obj = getattr(obj, methodname)
if hasattr(obj, '__doc__'):
irc.reply(msg, normalize(obj.__doc__))
else:
irc.reply(msg, '%s has no documentation' % funcname)
else:
irc.reply(msg, '%s has no method %s' % (parts[0],parts[1]))
else: else:
functionName = parts.pop() parts = name.split('.')
path = pythonPath
for name in parts: for name in parts:
try: try:
info = imp.find_module(name, path) info = imp.find_module(name, path)
@ -131,31 +116,57 @@ class Python(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable):
info[0].close() info[0].close()
except ImportError: except ImportError:
if parts == ['os', 'path']: if parts == ['os', 'path']:
newmodule = os.path return os.path
else: else:
irc.error(msg, 'No such module %s exists.' % name) return None
return return newmodule
if hasattr(newmodule, functionName): name = privmsgs.getArgs(args)
f = getattr(newmodule, functionName) if name.translate(string.ascii, self.modulechars) != '':
if hasattr(f, '__doc__'): irc.error('That\'s not a valid module or function name.')
s = f.__doc__.replace('\n\n', '. ') return
s = utils.normalizeWhitespace(s) if '.' in name:
irc.reply(msg, s) (moduleName, funcName) = rsplit(name, '.', 1)
if moduleName in __builtins__:
obj = __builtins__[moduleName]
if hasattr(obj, funcName):
obj = getattr(obj, funcName)
if hasattr(obj, '__doc__'):
irc.reply(msg, normalize(obj.__doc__))
else: else:
irc.error(msg, 'That function has no documentation.') irc.reply(msg, '%s has no documentation' % name)
else: else:
irc.error(msg, 'That function doesn\'t exist.') s = '%s has no method %s' % (moduleName, funcName)
irc.reply(msg, s)
elif moduleName:
newmodule = getModule(moduleName)
if newmodule is None:
irc.error(msg, 'No module %s exists.' % moduleName)
else:
if hasattr(newmodule, funcName):
f = getattr(newmodule, funcName)
if hasattr(f, '__doc__'):
s = normalize(f.__doc__)
irc.reply(msg, s)
else:
irc.error(msg, '%s has no documentation.' % name)
else:
s = '%s has no function %s' % (moduleName, funcName)
irc.error(msg, s)
else: else:
try: if name in sys.modules:
f = __builtins__[funcname] newmodule = sys.modules[name]
if hasattr(newmodule, '__doc__') and newmodule.__doc__:
irc.reply(msg, normalize(newmodule.__doc__))
else:
irc.reply(msg, 'Module %s has no documentation.' % name)
elif name in __builtins__:
f = __builtins__[name]
if hasattr(f, '__doc__'): if hasattr(f, '__doc__'):
irc.reply(msg, normalize(f.__doc__)) irc.reply(msg, normalize(f.__doc__))
else: else:
irc.error(msg, 'That function has no documentation.') irc.error(msg, 'That function has no documentation.')
except SyntaxError: else:
irc.error(msg, 'That\'s not a function!') irc.error(msg, 'No function or module %s exists.' % name)
except KeyError:
irc.error(msg, 'That function doesn\'t exist.')
_these = [str(s) for s in this.s.decode('rot13').splitlines() if s] _these = [str(s) for s in this.s.decode('rot13').splitlines() if s]
_these.pop(0) # Initial line (The Zen of Python...) _these.pop(0) # Initial line (The Zen of Python...)

View File

@ -39,6 +39,7 @@ class PythonTestCase(PluginTestCase, PluginDocumentation):
self.assertError('pydoc foobar') self.assertError('pydoc foobar')
self.assertError('pydoc assert') self.assertError('pydoc assert')
self.assertNotError('pydoc str') self.assertNotError('pydoc str')
self.assertNotError('pydoc copy')
self.assertNotError('pydoc list.reverse') self.assertNotError('pydoc list.reverse')
if os.name == 'posix': if os.name == 'posix':
self.assertNotRegexp('pydoc crypt.crypt', 'NameError') self.assertNotRegexp('pydoc crypt.crypt', 'NameError')