[Python-checkins] CVS: python/nondist/sandbox/help htmldoc.py,NONE,1.1 inspect.py,NONE,1.1 pydoc.py,NONE,1.1 textdoc.py,NONE,1.1
Ka-Ping Yee
python-dev@python.org
Sat, 13 Jan 2001 18:27:12 -0800
Update of /cvsroot/python/python/nondist/sandbox/help
In directory usw-pr-cvs1:/tmp/cvs-serv2565/help
Added Files:
htmldoc.py inspect.py pydoc.py textdoc.py
Log Message:
Added a first pass at an interactive / shell-command / web-based doc system.
--- NEW FILE: htmldoc.py ---
#!/usr/bin/env python
"""Generate HTML documentation from live Python objects."""
__version__ = 'Ka-Ping Yee <ping@lfw.org>, 29 May 2000'
import sys, os, re, inspect
from string import join, replace, expandtabs, rstrip
# ---------------------------------------------------- formatting utilities
def serialize(stuff):
"""Combine a list containing strings and nested lists into a single
string. This lets us manipulate lists until the last moment, since
rearranging lists is faster than rearranging strings."""
if type(stuff) is type(''): return stuff
results = []
for item in stuff:
if type(item) is type(''): results.append(item)
else: results.append(serialize(item))
return join(results, '')
def htmlescape(text):
return replace(replace(replace(text, '&', '&'),
'<', '<'),
'>', '>')
def htmlrepr(object):
return htmlescape(repr(object))
def preformat(text):
text = htmlescape(expandtabs(text))
return replace(replace(replace(replace(text, '\n\n', '\n \n'),
'\n\n', '\n \n'),
' ', ' '),
'\n', '<br>\n')
def multicolumn(list, format, cols=4):
results = ['<table width="100%"><tr>']
rows = (len(list)+cols-1)/cols
for col in range(cols):
results.append('<td width="%d%%" valign=top>' % (100/cols))
for i in range(rows*col, rows*col+rows):
if i < len(list):
results.append(format(list[i]) + '<br>')
results.append('</td>')
results.append('</tr></table>')
return results
def heading(title, fgcol, bgcol, extras=''):
return ["""
<p><table width="100%%" cellspacing=0 cellpadding=0 border=0>
<tr bgcolor="%s"><td colspan=3 valign=bottom><small><small><br></small></small
><font color="%s" face="helvetica, arial"> %s</font></td
><td align=right valign=bottom
><font color="%s" face="helvetica, arial"> %s</font></td></tr>
""" % (bgcol, fgcol, title, fgcol, extras), '</table>']
def section(title, fgcol, bgcol, contents, width=20,
prelude='', marginalia=None, gap=' '):
if marginalia is None:
marginalia = ' ' * width
results = []
results.append("""
<p><table width="100%%" cellspacing=0 cellpadding=0 border=0>
<tr bgcolor="%s"><td colspan=3 valign=bottom><small><small><br></small></small
><font color="%s" face="helvetica, arial"> %s</font></td></tr>
""" % (bgcol, fgcol, title))
if prelude:
results.append("""
<tr><td bgcolor="%s">%s</td>
<td bgcolor="%s" colspan=2>%s</td></tr>
""" % (bgcol, marginalia, bgcol, prelude))
results.append("""
<tr><td bgcolor="%s">%s</td><td>%s</td>
""" % (bgcol, marginalia, gap))
# Alas, this horrible hack seems to be the only way to force Netscape
# to expand the main cell consistently to the maximum available width.
results.append('<td><small><small>' + ' '*100 + '</small></small\n>')
results.append(contents)
results.append('</td></tr></table>')
return results
def footer():
return """
<table width="100%"><tr><td align=right>
<font face="helvetica, arial"><small><small>generated with
<strong>htmldoc</strong> by Ka-Ping Yee</a></small></small></font>
</td></tr></table>
"""
# -------------------------------------------------------- automatic markup
def namelink(name, *dicts):
for dict in dicts:
if dict.has_key(name):
return '<a href="%s">%s</a>' % (dict[name], name)
return name
def classlink(object, modname, *dicts):
name = object.__name__
if object.__module__ != modname:
name = object.__module__ + '.' + name
for dict in dicts:
if dict.has_key(object):
return '<a href="%s">%s</a>' % (dict[object], name)
return name
def modulelink(object):
return '<a href="%s.html">%s</a>' % (object.__name__, object.__name__)
def markup(text, functions={}, classes={}, methods={}, escape=htmlescape):
"""Mark up some plain text, given a context of symbols to look for.
Each context dictionary maps object names to named anchor identifiers."""
results = []
here = 0
pattern = re.compile('(self\.)?(\w+)')
while 1:
match = pattern.search(text, here)
if not match: break
start, end = match.regs[2]
found, name = match.group(0), match.group(2)
results.append(escape(text[here:start]))
if text[end:end+1] == '(':
results.append(namelink(name, methods, functions, classes))
elif match.group(1):
results.append('<strong>%s</strong>' % name)
else:
results.append(namelink(name, classes))
here = end
results.append(text[here:])
return join(results, '')
def getdoc(object):
result = inspect.getdoc(object)
if not result:
try: result = inspect.getcomments(object)
except: pass
return result and rstrip(result) + '\n' or ''
# -------------------------------------------------- type-specific routines
def document_tree(tree, modname, classes={}, parent=None):
"""Produce HTML for a class tree as returned by inspect.getclasstree()."""
results = ['<dl>\n']
for entry in tree:
if type(entry) is type(()):
c, bases = entry
results.append('<dt><font face="helvetica, arial"><small>')
results.append(classlink(c, modname, classes))
if bases and bases != (parent,):
parents = []
for base in bases:
parents.append(classlink(base, modname, classes))
results.append('(' + join(parents, ', ') + ')')
results.append('\n</small></font></dt>')
elif type(entry) is type([]):
results.append('<dd>\n')
results.append(document_tree(entry, modname, classes, c))
results.append('</dd>\n')
results.append('</dl>\n')
return results
def document_module(object):
"""Produce HTML documentation for a given module object."""
name = object.__name__
results = []
head = '<br><big><big><strong> %s</strong></big></big>' % name
try:
file = inspect.getsourcefile(object)
filelink = '<a href="file:%s">%s</a>' % (file, file)
except TypeError:
filelink = '(built-in)'
if hasattr(object, '__version__'):
head = head + ' (version: %s)' % htmlescape(object.__version__)
results.append(heading(head, '#ffffff', '#7799ee', filelink))
cadr = lambda list: list[1]
modules = map(cadr, inspect.getmembers(object, inspect.ismodule))
classes, cdict = [], {}
for key, value in inspect.getmembers(object, inspect.isclass):
if (inspect.getmodule(value) or object) is object:
classes.append(value)
cdict[key] = cdict[value] = '#' + key
functions, fdict = [], {}
for key, value in inspect.getmembers(object, inspect.isroutine):
if inspect.isbuiltin(value) or inspect.getmodule(value) is object:
functions.append(value)
fdict[key] = '#-' + key
if inspect.isfunction(value): fdict[value] = fdict[key]
for c in classes:
for base in c.__bases__:
key, modname = base.__name__, base.__module__
if modname != name and sys.modules.has_key(modname):
module = sys.modules[modname]
if hasattr(module, key) and getattr(module, key) is base:
if not cdict.has_key(key):
cdict[key] = cdict[base] = modname + '.html#' + key
doc = markup(getdoc(object), fdict, cdict, escape=preformat)
if doc: doc = '<p><small><tt>' + doc + '</tt></small>\n\n'
else: doc = '<p><small><em>no doc string</em></small>\n'
results.append(doc)
if modules:
contents = multicolumn(modules, modulelink)
results.append(section('<big><strong>Modules</strong></big>',
'#fffff', '#aa55cc', contents))
if classes:
contents = document_tree(inspect.getclasstree(classes, 1), name, cdict)
for item in classes:
contents.append(document_class(item, fdict, cdict))
results.append(section('<big><strong>Classes</strong></big>',
'#ffffff', '#ee77aa', contents))
if functions:
contents = []
for item in functions:
contents.append(document_function(item, fdict, cdict))
results.append(section('<big><strong>Functions</strong></big>',
'#ffffff', '#eeaa77', contents))
return results
def document_class(object, functions={}, classes={}):
"""Produce HTML documentation for a given class object."""
name = object.__name__
bases = object.__bases__
results = []
methods, mdict = [], {}
for key, value in inspect.getmembers(object, inspect.ismethod):
methods.append(value)
mdict[key] = mdict[value] = '#' + name + '-' + key
if methods:
for item in methods:
results.append(document_method(
item, functions, classes, mdict, name))
title = '<a name="%s">class <strong>%s</strong></a>' % (name, name)
if bases:
parents = []
for base in bases:
parents.append(classlink(base, object.__module__, classes))
title = title + '(%s)' % join(parents, ', ')
doc = markup(getdoc(object), functions, classes, mdict, escape=preformat)
if doc: doc = '<small><tt>' + doc + '<br> </tt></small>'
else: doc = '<small><em>no doc string</em></small>'
return section(title, '#000000', '#ffc8d8', results, 10, doc)
def document_method(object, functions={}, classes={}, methods={}, clname=''):
"""Produce HTML documentation for a given method object."""
return document_function(
object.im_func, functions, classes, methods, clname)
def defaultformat(object):
return '<small><font color="#a0a0a0">=' + \
htmlrepr(object) + '</font></small>'
def document_function(object, functions={}, classes={}, methods={}, clname=''):
"""Produce HTML documentation for a given function object."""
try:
args, varargs, varkw, defaults = inspect.getargspec(object)
argspec = inspect.formatargspec(
args, varargs, varkw, defaults, defaultformat=defaultformat)
except TypeError:
argspec = '(<small>...</em></small>)'
if object.__name__ == '<lambda>':
decl = ['<em>lambda</em> ', argspec[1:-1]]
else:
anchor = clname + '-' + object.__name__
decl = ['<a name="%s"\n>' % anchor,
'<strong>%s</strong>' % object.__name__, argspec, '</a>\n']
doc = markup(getdoc(object), functions, classes, methods, escape=preformat)
if doc:
doc = replace(doc, '<br>\n', '</tt></small\n><dd><small><tt>')
doc = ['<dd><small><tt>', doc, '</tt></small>']
else:
doc = '<dd><small><em>no doc string</em></small>'
return ['<dl><dt>', decl, doc, '</dl>']
def document_builtin(object):
"""Produce HTML documentation for a given built-in function."""
return ('<strong>%s</strong>' % object.__name__ +
'(<small>...</small>)')
# --------------------------------------------------- main dispatch routine
def document(object):
"""Generate documentation for a given object."""
if inspect.ismodule(object): results = document_module(object)
elif inspect.isclass(object): results = document_class(object)
elif inspect.ismethod(object): results = document_method(object)
elif inspect.isfunction(object): results = document_function(object)
elif inspect.isbuiltin(object): results = document_builtin(object)
else: raise TypeError, 'don\'t know how to document this kind of object'
return serialize(results)
def index(dir):
modules = []
packages = []
for file in os.listdir(dir):
path = dir + '/' + file
if os.path.isfile(path):
if file[-3:] == '.py':
modules.append(file[:-3])
elif file[-11:] == 'module.so':
modules.append(file[:-11])
elif file[-13:] == 'module.so.1':
modules.append(file[:-13])
elif os.path.isdir(path):
if os.path.exists(path + '/__init__.py'):
packages.append(file)
def modulelink(name):
return '<a href="%s.html">%s</a>' % (name, name)
modules.sort()
contents = multicolumn(modules, modulelink)
results = section('<big><strong>%s</strong></big>' % dir,
'#ffffff', '#ee77aa', contents)
return serialize(results)
if __name__ == '__main__':
import os
modnames = []
for arg in sys.argv[1:]:
if os.path.isdir(arg):
for file in os.listdir(arg):
if file[-3:] == '.py':
modnames.append(file[:-3])
elif file[-9:] == 'module.so':
modnames.append(file[:-9])
else:
if arg[-3:] == '.py':
modnames.append(arg[:-3])
else:
modnames.append(arg)
for modname in modnames:
try:
module = __import__(modname)
except:
print 'failed to import %s' % modname
else:
file = open(modname + '.html', 'w')
file.write(
"""<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><title>%s</title><body bgcolor="#ffffff">
""" % modname)
file.write(document(module))
file.write('</body></html>')
file.close()
print 'wrote %s.html' % modname
--- NEW FILE: inspect.py ---
"""Get useful information from live Python objects.
This module encapsulates the interface provided by the internal special
attributes (func_*, co_*, im_*, tb_*, etc.) in a friendlier fashion.
It also provides some help for examining source code and class layout.
Here are some of the useful functions provided by this module:
getdoc(), getcomments() - get documentation on an object
getclasstree() - arrange classes so as to represent their hierarchy
getfile(), getsourcefile(), getsource() - find an object's source code
getargspec(), getargvalues() - get info about function arguments
formatargspec(), formatargvalues() - format an argument spec
stack(), trace() - get info about frames on the stack or in a traceback
"""
# This module is in the public domain. No warranties.
__version__ = 'Ka-Ping Yee <ping@lfw.org>, 1 Jan 2001'
import sys, types, string, dis, imp
# ----------------------------------------------------------- type-checking
def ismodule(object):
"""Return true if the object is a module.
Module objects provide these attributes:
__doc__ documentation string
__file__ filename (missing for built-in modules)"""
return type(object) is types.ModuleType
def isclass(object):
"""Return true if the object is a class.
Class objects provide these attributes:
__doc__ documentation string
__module__ name of module in which this class was defined"""
return type(object) is types.ClassType
def ismethod(object):
"""Return true if the object is an instance method.
Instance method objects provide these attributes:
__doc__ documentation string
__name__ name with which this method was defined
im_class class object in which this method belongs
im_func function object containing implementation of method
im_self instance to which this method is bound, or None"""
return type(object) is types.MethodType
def isfunction(object):
"""Return true if the object is a user-defined function.
Function objects provide these attributes:
__doc__ documentation string
__name__ name with which this function was defined
func_code code object containing compiled function bytecode
func_defaults tuple of any default values for arguments
func_doc (same as __doc__)
func_globals global namespace in which this function was defined
func_name (same as __name__)"""
return type(object) in [types.FunctionType, types.LambdaType]
def istraceback(object):
"""Return true if the object is a traceback.
Traceback objects provide these attributes:
tb_frame frame object at this level
tb_lasti index of last attempted instruction in bytecode
tb_lineno current line number in Python source code
tb_next next inner traceback object (called by this level)"""
return type(object) is types.TracebackType
def isframe(object):
"""Return true if the object is a frame object.
Frame objects provide these attributes:
f_back next outer frame object (this frame's caller)
f_builtins built-in namespace seen by this frame
f_code code object being executed in this frame
f_exc_traceback traceback if raised in this frame, or None
f_exc_type exception type if raised in this frame, or None
f_exc_value exception value if raised in this frame, or None
f_globals global namespace seen by this frame
f_lasti index of last attempted instruction in bytecode
f_lineno current line number in Python source code
f_locals local namespace seen by this frame
f_restricted 0 or 1 if frame is in restricted execution mode
f_trace tracing function for this frame, or None"""
return type(object) is types.FrameType
def iscode(object):
"""Return true if the object is a code object.
Code objects provide these attributes:
co_argcount number of arguments (not including * or ** args)
co_code string of raw compiled bytecode
co_consts tuple of constants used in the bytecode
co_filename name of file in which this code object was created
co_firstlineno number of first line in Python source code
co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
co_lnotab encoded mapping of line numbers to bytecode indices
co_name name with which this code object was defined
co_names tuple of names of local variables
co_nlocals number of local variables
co_stacksize virtual machine stack space required
co_varnames tuple of names of arguments and local variables"""
return type(object) is types.CodeType
def isbuiltin(object):
"""Return true if the object is a built-in function or method.
Built-in functions and methods provide these attributes:
__doc__ documentation string
__name__ original name of this function or method
__self__ instance to which a method is bound, or None"""
return type(object) in [types.BuiltinFunctionType,
types.BuiltinMethodType]
def isroutine(object):
"""Return true if the object is any kind of function or method."""
return type(object) in [types.FunctionType, types.LambdaType,
types.MethodType, types.BuiltinFunctionType,
types.BuiltinMethodType]
def getmembers(object, predicate=None):
"""Return all members of an object as (key, value) pairs sorted by key.
Optionally, only return members that satisfy a given predicate."""
results = []
for key in dir(object):
value = getattr(object, key)
if not predicate or predicate(value):
results.append((key, value))
results.sort()
return results
# -------------------------------------------------- source code extraction
def indentsize(line):
"""Return the indent size, in spaces, at the start of a line of text."""
expline = string.expandtabs(line)
return len(expline) - len(string.lstrip(expline))
def getdoc(object):
"""Get the documentation string for an object.
All tabs are expanded to spaces. To clean up docstrings that are
indented to line up with blocks of code, any whitespace than can be
uniformly removed from the second line onwards is removed."""
if hasattr(object, '__doc__') and object.__doc__:
lines = string.split(string.expandtabs(object.__doc__), '\n')
margin = None
for line in lines[1:]:
content = len(string.lstrip(line))
if not content: continue
indent = len(line) - content
if margin is None: margin = indent
else: margin = min(margin, indent)
if margin is not None:
for i in range(1, len(lines)): lines[i] = lines[i][margin:]
return string.join(lines, '\n')
def getfile(object):
"""Try to guess which (text or binary) file an object was defined in."""
if ismodule(object):
if hasattr(object, '__file__'):
return object.__file__
raise TypeError, 'arg is a built-in module'
if isclass(object):
object = sys.modules[object.__module__]
if hasattr(object, '__file__'):
return object.__file__
raise TypeError, 'arg is a built-in class'
if ismethod(object):
object = object.im_func
if isfunction(object):
object = object.func_code
if istraceback(object):
object = object.tb_frame
if isframe(object):
object = object.f_code
if iscode(object):
return object.co_filename
raise TypeError, 'arg is not a module, class, method, ' \
'function, traceback, frame, or code object'
modulesbyfile = {}
def getmodule(object):
"""Try to guess which module an object was defined in."""
try:
file = getsourcefile(object)
except TypeError:
return None
if modulesbyfile.has_key(file):
return sys.modules[modulesbyfile[file]]
for module in sys.modules.values():
if hasattr(module, '__file__'):
modulesbyfile[getsourcefile(module)] = module.__name__
if modulesbyfile.has_key(file):
return sys.modules[modulesbyfile[file]]
main = sys.modules['__main__']
try:
mainobject = getattr(main, object.__name__)
if mainobject is object: return main
except AttributeError: pass
builtin = sys.modules['__builtin__']
try:
builtinobject = getattr(builtin, object.__name__)
if builtinobject is object: return builtin
except AttributeError: pass
def getsourcefile(object):
"""Try to guess which Python source file an object was defined in."""
filename = getfile(object)
if filename[-4:] == '.pyc':
filename = filename[:-4] + '.py'
return filename
def findsource(object):
"""Find the first line of code corresponding to a given module, class,
method, function, traceback, frame, or code object; return the entire
contents of the source file and the starting line number. An IOError
exception is raised if the source code cannot be retrieved."""
try:
file = open(getsourcefile(object))
lines = file.readlines()
file.close()
except (TypeError, IOError):
raise IOError, 'could not get source code'
if ismodule(object):
return lines, 0
if isclass(object):
name = object.__name__
matches = (['class', name], ['class', name + ':'])
for i in range(len(lines)):
if string.split(lines[i])[:2] in matches:
return lines, i
else: raise IOError, 'could not find class definition'
if ismethod(object):
object = object.im_func
if isfunction(object):
object = object.func_code
if istraceback(object):
object = object.tb_frame
if isframe(object):
object = object.f_code
if iscode(object):
try:
lnum = object.co_firstlineno - 1
except AttributeError:
raise IOError, 'could not find function definition'
else:
while lnum > 0:
if string.split(lines[lnum])[:1] == ['def']: break
lnum = lnum - 1
return lines, lnum
def getcomments(object):
"""Get lines of comments immediately preceding an object's source code."""
try: lines, lnum = findsource(object)
except: return None
if ismodule(object):
# Look for a comment block at the top of the file.
start = 0
if lines[0][:2] == '#!': start = 1
while start < len(lines) and string.strip(lines[start]) in ['', '#']:
start = start + 1
if lines[start][:1] == '#':
comments = []
end = start
while end < len(lines) and lines[end][:1] == '#':
comments.append(string.expandtabs(lines[end]))
end = end + 1
return string.join(comments, '')
# Look for a preceding block of comments at the same indentation.
elif lnum > 0:
indent = indentsize(lines[lnum])
end = lnum - 1
if string.strip(lines[end]) == '':
while end >= 0 and string.strip(lines[end]) == '':
end = end - 1
else:
while string.lstrip(lines[end])[:1] != '#' and \
indentsize(lines[end]) == indent:
end = end - 1
if end >= 0 and string.lstrip(lines[end])[:1] == '#' and \
indentsize(lines[end]) == indent:
comments = [string.lstrip(string.expandtabs(lines[end]))]
if end > 0:
end = end - 1
comment = string.lstrip(string.expandtabs(lines[end]))
while comment[:1] == '#' and indentsize(lines[end]) == indent:
comments[:0] = [comment]
end = end - 1
if end < 0: break
comment = string.lstrip(string.expandtabs(lines[end]))
return string.join(comments, '')
import tokenize
class ListReader:
"""Provide a readline() method to return lines from a list of strings."""
def __init__(self, lines):
self.lines = lines
self.index = 0
def readline(self):
i = self.index
if i < len(self.lines):
self.index = i + 1
return self.lines[i]
else: return ''
class EndOfBlock(Exception): pass
class BlockFinder:
"""Provide a tokeneater() method to detect the end of a code block."""
def __init__(self):
self.indent = 0
self.started = 0
self.last = 0
def tokeneater(self, type, token, (srow, scol), (erow, ecol), line):
if not self.started:
if type == tokenize.NAME: self.started = 1
elif type == tokenize.NEWLINE:
self.last = srow
elif type == tokenize.INDENT:
self.indent = self.indent + 1
elif type == tokenize.DEDENT:
self.indent = self.indent - 1
if self.indent == 0: raise EndOfBlock, self.last
def getblock(lines):
"""Extract the block of code at the top of the given list of lines."""
try:
tokenize.tokenize(ListReader(lines).readline, BlockFinder().tokeneater)
except EndOfBlock, eob:
return lines[:eob.args[0]]
def getsourcelines(object):
"""Try to get the source code corresponding to a module, class, method,
function, traceback, frame, or code object. Return a list of lines and
the line number of the first line, or raise an IOError exception if the
source code cannot be retrieved."""
lines, lnum = findsource(object)
if ismodule(object): return lines, 0
else: return getblock(lines[lnum:]), lnum
def getsource(object):
"""Try to get the source code corresponding to a module, class, method,
function, traceback, frame, or code object. Return a string, or raise
an IOError exception if the source code cannot be retrieved."""
lines, lnum = getsourcelines(object)
return string.join(lines, '')
# --------------------------------------------------- class tree extraction
def walktree(classes, children, parent):
"""Recursive helper function for getclasstree()."""
results = []
classes.sort(lambda a, b: cmp(a.__name__, b.__name__))
for c in classes:
results.append((c, c.__bases__))
if children.has_key(c):
results.append(walktree(children[c], children, c))
return results
def getclasstree(classes, unique=0):
"""Arrange the given list of classes into a hierarchy of nested lists.
Where a nested list appears, it contains classes derived from the class
whose entry immediately precedes the list. Each entry is a 2-tuple
containing a class and a tuple of its base classes. If the 'unique'
argument is true, exactly one entry appears in the returned structure
for each class in the given list. Otherwise, classes that multiply
inherit, and their descendants, will appear multiple times."""
children = {}
roots = []
for c in classes:
if c.__bases__:
for parent in c.__bases__:
if not children.has_key(parent):
children[parent] = []
children[parent].append(c)
if unique and parent in classes: break
elif c not in roots:
roots.append(c)
for parent in children.keys():
if parent not in classes:
roots.append(parent)
return walktree(roots, children, None)
# ------------------------------------------------ argument list extraction
# These constants are from Python's compile.h.
CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 1, 2, 4, 8
def getargs(co):
"""Get information about the arguments accepted by a code object.
Three things are returned: (args, varargs, varkw), where 'args' is
a list of argument names (possibly containing nested lists), and
'varargs' and 'varkw' are the names of the * and ** arguments or None."""
if not iscode(co): raise TypeError, 'arg is not a code object'
code = co.co_code
nargs = co.co_argcount
names = co.co_varnames
args = list(names[:nargs])
step = 0
# The following acrobatics are for anonymous (tuple) arguments.
for i in range(nargs):
if args[i][:1] in ['', '.']:
stack, remain, count = [], [], []
while step < len(code):
op = ord(code[step])
step = step + 1
if op >= dis.HAVE_ARGUMENT:
opname = dis.opname[op]
value = ord(code[step]) + ord(code[step+1])*256
step = step + 2
if opname == 'UNPACK_TUPLE':
remain.append(value)
count.append(value)
elif opname == 'STORE_FAST':
stack.append(names[value])
remain[-1] = remain[-1] - 1
while remain[-1] == 0:
remain.pop()
size = count.pop()
stack[-size:] = [stack[-size:]]
if not remain: break
remain[-1] = remain[-1] - 1
if not remain: break
args[i] = stack[0]
varargs = None
if co.co_flags & CO_VARARGS:
varargs = co.co_varnames[nargs]
nargs = nargs + 1
varkw = None
if co.co_flags & CO_VARKEYWORDS:
varkw = co.co_varnames[nargs]
return args, varargs, varkw
def getargspec(func):
"""Get the names and default values of a function's arguments.
A tuple of four things is returned: (args, varargs, varkw, defaults).
'args' is a list of the argument names (it may contain nested lists).
'varargs' and 'varkw' are the names of the * and ** arguments or None.
'defaults' is an n-tuple of the default values of the last n arguments."""
if not isfunction(func): raise TypeError, 'arg is not a Python function'
args, varargs, varkw = getargs(func.func_code)
return args, varargs, varkw, func.func_defaults
def getargvalues(frame):
"""Get information about arguments passed into a particular frame.
A tuple of four things is returned: (args, varargs, varkw, locals).
'args' is a list of the argument names (it may contain nested lists).
'varargs' and 'varkw' are the names of the * and ** arguments or None.
'locals' is the locals dictionary of the given frame."""
args, varargs, varkw = getargs(frame.f_code)
return args, varargs, varkw, frame.f_locals
def strseq(object, convert=str):
"""Recursively walk a sequence, stringifying each element."""
if type(object) in [types.ListType, types.TupleType]:
results = map(lambda o, c=convert: strseq(o, c), object)
if len(results) == 1:
return '(' + results[0] + ',)'
else:
return '(' + string.join(results, ', ') + ')'
else:
return convert(object)
def formatargspec(args, varargs=None, varkw=None, defaults=None,
argformat=str, defaultformat=lambda x: '=' + repr(x),
varargsformat=lambda name: '*' + name,
varkwformat=lambda name: '**' + name):
"""Format a pretty argument spec from the 4-tuple returned by getargspec.
The arguments are (args, varargs, varkw, defaults)."""
specs = []
if defaults:
firstdefault = len(args) - len(defaults)
for i in range(len(args)):
spec = strseq(args[i], argformat)
if defaults and i >= firstdefault:
spec = spec + defaultformat(defaults[i - firstdefault])
specs.append(spec)
if varargs:
specs.append(varargsformat(varargs))
if varkw:
specs.append(varkwformat(varkw))
return '(' + string.join(specs, ', ') + ')'
def formatargvalues(args, varargs, varkw, locals,
argformat=str, valueformat=repr,
varargsformat=lambda name: '*' + name,
varkwformat=lambda name: '**' + name):
"""Format a pretty argument spec from the 4-tuple returned by getargvalues.
The arguments are (args, varargs, varkw, locals)."""
def convert(name, locals=locals,
argformat=argformat, valueformat=valueformat):
return argformat(name) + '=' + valueformat(locals[name])
specs = []
for i in range(len(args)):
specs.append(strseq(args[i], convert))
if varargs:
specs.append(varargsformat(varargs) + '=' +
valueformat(locals[varargs]))
if varkw:
specs.append(varkwformat(varkw) + '=' + valueformat(locals[varkw]))
return '(' + string.join(specs, ', ') + ')'
# -------------------------------------------------- stack frame extraction
def getframe(frame, context=1):
"""For a given frame or traceback object, return the filename, line
number, function name, a given number of lines of context from the
source code, and the index of the line within the lines of context."""
if istraceback(frame):
frame = frame.tb_frame
if not isframe(frame):
raise TypeError, 'arg is not a frame or traceback object'
filename = getsourcefile(frame)
if context > 0:
start = frame.f_lineno - 1 - context/2
try:
lines, lnum = findsource(frame)
start = max(start, 1)
start = min(start, len(lines) - context)
lines = lines[start:start+context]
index = frame.f_lineno - 1 - start
except:
lines = index = None
else:
lines = index = None
return (filename, frame.f_lineno, frame.f_code.co_name, lines, index)
def getouterframes(frame, context=1):
"""Get a list of records for a frame and all higher (calling) frames.
Each record contains a frame object, filename, line number, function
name, the requested amount of context, and index within the context."""
framelist = []
while frame:
framelist.append((frame,) + getframe(frame, context))
frame = frame.f_back
return framelist
def getinnerframes(traceback, context=1):
"""Get a list of records for a traceback's frame and all lower frames.
Each record contains a frame object, filename, line number, function
name, the requested amount of context, and index within the context."""
traceback = traceback.tb_next
framelist = []
while traceback:
framelist.append((traceback.tb_frame,) + getframe(traceback, context))
traceback = traceback.tb_next
return framelist
def currentframe():
"""Return the frame object for the caller's stack frame."""
try:
raise 'catch me'
except:
return sys.exc_traceback.tb_frame.f_back
if hasattr(sys, '_getframe'): currentframe = sys._getframe
def stack(context=1):
"""Return a list of records for the stack above the caller's frame."""
return getouterframes(currentframe().f_back, context)
def trace(context=1):
"""Return a list of records for the stack below the current exception."""
return getinnerframes(sys.exc_traceback, context)
--- NEW FILE: pydoc.py ---
#!/usr/bin/env python
"""Format Python documentation for interactive use.
At the shell command line outside of Python, run "pydoc <name>" to show
documentation on something. <name> may be the name of a Python function,
module, package, or a dotted reference to a class or function within a
module or module in a package.
Or, at the shell prompt, run "pydoc -k <keyword>" to search for a keyword
in the one-line descriptions of modules.
Or, at the shell prompt, run "pydoc -p <port>" to start an HTTP server
on a given port on the local machine to generate documentation web pages.
In the Python interpreter, do "from pydoc import help" to provide online
help. Calling help(thing) on a Python object documents the object."""
__author__ = "Ka-Ping Yee <ping@lfw.org>"
__version__ = "10 January 2001"
import sys, os, string, types, getopt
import inspect, htmldoc, textdoc
def precis(filename):
"""Get the one-line summary out of a module file."""
file = open(filename)
line = file.readline()
while line[:1] in ['#', '\n']:
line = file.readline()
if line[-2:] == '\\\n': line = line[:-2] + file.readline()
file.close()
line = string.strip(line)
if line[:3] == '"""':
i = string.find(line, '"""', 3)
if i >= 0: return line[3:i]
return line[3:]
identchars = string.letters + string.digits + '_'
def index(dir):
"""Return a list of (module-name, precis) pairs for a given directory."""
results = []
for entry in os.listdir(dir):
path = os.path.join(dir, entry)
init = os.path.join(path, '__init__.py')
if os.path.isdir(path) and os.path.exists(init):
results.extend(map(
lambda (m, s), pkg=entry: (pkg + '.' + m, s), index(path)))
elif os.path.isfile(path) and entry[-3:] == '.py':
results.append((entry[:-3], precis(path)))
return results
def pager(text):
"""The first time this is called, determine what kind of pager to use."""
global pager
pager = getpager()
pager(text)
def getpager():
"""Decide what method to use for paging through text."""
if not sys.stdin.isatty() or type(sys.stdout) is not types.FileType:
return plainpager
if os.environ.has_key('PAGER'):
return lambda text: pipepager(text, os.environ['PAGER'])
if hasattr(sys, 'winver'):
return lambda text: tempfilepager(text, 'more')
if hasattr(os, 'system') and os.system('less 2>/dev/null') == 0:
return lambda text: pipepager(text, 'less')
import tempfile
filename = tempfile.mktemp()
open(filename, 'w').close()
try:
if hasattr(os, 'system') and os.system('more %s' % filename) == 0:
return lambda text: pipepager(text, 'more')
else:
return ttypager
finally:
os.unlink(filename)
def pipepager(text, cmd):
"""Page through text by feeding it to another program."""
pipe = os.popen(cmd, 'w')
try:
pipe.write(text)
except IOError:
# Ignore broken pipes caused by quitting the pager program.
pass
pipe.close()
def tempfilepager(text, cmd):
"""Page through text by invoking a program on a temporary file."""
import tempfile
filename = tempfile.mktemp()
file = open(filename, 'w')
file.write(text)
file.close()
try:
os.system(cmd + ' ' + filename)
finally:
os.unlink(filename)
def plain(text):
"""Remove boldface formatting from text."""
import re
return re.sub('.\b', '', text)
def ttypager(text):
"""Page through text on a text terminal."""
lines = string.split(plain(text), '\n')
try:
import tty
except ImportError:
tty = None
try:
if tty:
fd = sys.stdin.fileno()
old = tty.tcgetattr(fd)
tty.setcbreak(fd)
getchar = lambda: sys.stdin.read(1)
else:
getchar = lambda: sys.stdin.readline()[:-1][:1]
r = inc = 24
sys.stdout.write(string.join(lines[:inc], '\n') + '\n')
while lines[r:]:
sys.stdout.write('-- more --')
sys.stdout.flush()
c = getchar()
if c in ['q', 'Q']:
sys.stdout.write('\r \r')
break
elif c in ['\r', '\n']:
sys.stdout.write('\r \r' + lines[r] + '\n')
r = r + 1
continue
if c in ['b', 'B', '\x1b']:
r = r - inc - inc
if r < 0: r = 0
sys.stdout.write('\n' + string.join(lines[r:r+inc], '\n') + '\n')
r = r + inc
finally:
if tty:
tty.tcsetattr(fd, tty.TCSAFLUSH, old)
def plainpager(text):
sys.stdout.write(plain(text))
def describe(thing):
"""Produce a short description of the given kind of thing."""
if inspect.ismodule(thing):
if thing.__name__ in sys.builtin_module_names:
return 'built-in module ' + thing.__name__
if hasattr(thing, '__path__'):
return 'package ' + thing.__name__
else:
return 'module ' + thing.__name__
if inspect.isbuiltin(thing):
return 'built-in function ' + thing.__name__
if inspect.isclass(thing):
return 'class ' + thing.__name__
if inspect.isfunction(thing):
return 'function ' + thing.__name__
if inspect.ismethod(thing):
return 'method ' + thing.__name__
return repr(thing)
def find(name):
"""Locate an object by name (or dotted path), importing as necessary."""
if hasattr(__builtins__, name):
return None, getattr(__builtins__, name)
parts = string.split(name, '.')
n = 1
while n <= len(parts):
name = string.join(parts[:n], '.')
try:
mod = __import__(name)
except ImportError:
break
try:
mod = reload(mod)
except ImportError:
pass
try:
x = mod
for p in parts[1:]:
x = getattr(x, p)
return string.join(parts[:-1], '.'), x
except AttributeError:
n = n + 1
continue
return None, None
# ------------------------------------------------------ external interface
def doc(thing):
"""Display documentation on an object (for interactive use)."""
if type(thing) is type(""):
path, x = find(thing)
if x:
thing = x
else:
print 'could not import or find %s' % thing
return
desc = describe(thing)
module = inspect.getmodule(thing)
if module and module is not thing:
desc = desc + ' in module ' + module.__name__
pager('Help on %s:\n\n' % desc + textdoc.document(thing))
class Helper:
def __repr__(self):
return """To get help on a Python object, call help(object).
To get help on a module or package, either import it before calling
help(module) or call help('modulename')."""
def __call__(self, object):
doc(object)
help = Helper()
def man(key):
"""Display documentation on an object in a form similar to man(1)."""
path, x = find(key)
if x:
title = 'Python Library Documentation: ' + describe(x)
if path: title = title + ' in ' + path
pager('\n' + title + '\n\n' + textdoc.document(x))
found = 1
else:
print 'no Python documentation found for %s' % key
def apropos(key):
for module in sys.builtin_module_names:
desc = __import__(module).__doc__
if desc:
desc = string.split(desc, '\n')[0]
if (string.find(string.lower(module), key) >= 0 or
desc and string.find(string.lower(desc), key) >= 0):
print module, '-', desc
seen = {}
for dir in sys.path:
for module, desc in index(dir or '.'):
desc = desc or '(no description)'
if not seen.has_key(module):
seen[module] = 1
if (string.find(string.lower(module), key) >= 0 or
desc and string.find(string.lower(desc), key) >= 0):
if module[-9:] == '.__init__':
print module[:-9], '(package) -', desc
else:
print module, '-', desc
def server(port):
print 'starting server on port', port
import BaseHTTPServer
class DocReqHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def send_document(self, title, contents):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write(
'''<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><title>Python: %s</title><body bgcolor="#ffffff">''' % title)
self.wfile.write(contents)
self.wfile.write('</body></html>')
def do_GET(self):
path = self.path
if path[-5:] == '.html': path = path[:-5]
if path[:1] == '/': path = path[1:]
if path:
p, x = find(path)
if x:
self.send_document(describe(x), htmldoc.document(x))
else:
self.send_document(path,
'There is no Python module or object named "%s".' % path)
else:
contents = htmldoc.heading(
'<br><big><big><strong> '
'Python: Index of Modules'
'</strong></big></big>',
'#ffffff', '#7799ee')
builtins = []
for name in sys.builtin_module_names:
builtins.append('<a href="%s.html">%s</a>' % (name, name))
contents.append('<p>Built-in modules: ')
contents.append(string.join(builtins, ', '))
contents.extend(map(htmldoc.index, sys.path))
contents = htmldoc.serialize(contents)
self.send_document('Index of Modules', contents)
def log_message(self, *args): pass
BaseHTTPServer.HTTPServer(('', port), DocReqHandler).serve_forever()
def usage():
print """%s <name> ...
Show documentation on something.
<name> may be the name of a Python function, module,
package, or a dotted reference to a class or function
within a module or module in a package.
%s -k <keyword>
Search for a keyword in the short descriptions of modules.
%s -p <port>
Start an HTTP server on the given port on the local machine.
""" % ((sys.argv[0],) * 3)
if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], 'k:p:')
except getopt.error:
usage()
else:
for opt, val in opts:
if opt == '-k':
apropos(string.lower(val))
break
if opt == '-p':
try:
port = int(val)
except ValueError:
usage()
else:
server(port)
break
else:
if args:
for arg in args:
man(arg)
else:
usage()
--- NEW FILE: textdoc.py ---
"""Generate text documentation from live Python objects."""
__version__ = 'Ka-Ping Yee <ping@lfw.org>, 10 Jan 2001'
import sys, inspect
from string import join, split, strip, rstrip
# ---------------------------------------------------- formatting utilities
def serialize(stuff):
"""Combine a list containing strings and nested lists into a single
string. This lets us manipulate lists until the last moment, since
rearranging lists is faster than rearranging strings."""
if type(stuff) is type(''): return stuff
results = []
for item in stuff:
if type(item) is type(''): results.append(item)
else: results.append(serialize(item))
return join(results, '')
def bold(text):
result = ''
for char in text:
result = result + char + '\b' + char
return result
def section(title, contents):
return bold(title) + '\n' + rstrip(indent(contents)) + '\n\n'
def indent(text, spaces=4):
"""Indent text a given number of spaces."""
if type(text) is type([]): text = serialize(text)
lines = split(text, '\n')
lines = map(lambda line, prefix=' ' * spaces: prefix + line, lines)
if lines: lines[-1] = rstrip(lines[-1])
return join(lines, '\n')
def classname(object, modname):
name = object.__name__
if object.__module__ != modname:
name = object.__module__ + '.' + name
return name
def getdoc(object):
result = inspect.getdoc(object)
if not result:
try: result = inspect.getcomments(object)
except: pass
return result and rstrip(result) + '\n' or ''
# -------------------------------------------------- type-specific routines
def document_tree(tree, modname, parent=None, prefix=''):
"""Render in text a class tree as returned by inspect.getclasstree()."""
results = []
for entry in tree:
if type(entry) is type(()):
c, bases = entry
results.append(prefix + classname(c, modname))
if bases and bases != (parent,):
parents = map(lambda c, m=modname: classname(c, m), bases)
results.append('(%s)' % join(parents, ', '))
results.append('\n')
elif type(entry) is type([]):
results.append(document_tree(entry, modname, c, prefix + ' '))
return results
def document_module(object):
"""Produce text documentation for a given module object."""
results = []
name = object.__name__
lines = split(strip(getdoc(object)), '\n')
if len(lines) == 1:
name = name + ' - ' + lines[0]
lines = []
elif len(lines) >= 2 and not rstrip(lines[1]):
name = name + ' - ' + lines[0]
lines = lines[2:]
results.append(section('NAME', name))
try: file = inspect.getsourcefile(object)
except TypeError: file = None
results.append(section('FILE', file or '(built-in)'))
if lines:
results.append(section('DESCRIPTION', join(lines, '\n')))
classes = []
for key, value in inspect.getmembers(object, inspect.isclass):
if (inspect.getmodule(value) or object) is object:
classes.append(value)
functions = []
for key, value in inspect.getmembers(object, inspect.isroutine):
if inspect.isbuiltin(value) or inspect.getmodule(value) is object:
functions.append(value)
if classes:
contents = document_tree(
inspect.getclasstree(classes, 1), object.__name__)
contents.append('\n')
for item in classes:
contents.append([document_class(item), '\n'])
results.append(section('CLASSES', contents))
if functions:
contents = []
for item in functions:
contents.append([document_function(item), '\n'])
results.append(section('FUNCTIONS', contents))
if hasattr(object, '__version__'):
version = object.__version__
if hasattr(object, '__date__'):
version = version + ', ' + object.__date__
results.append(section('VERSION', version))
if hasattr(object, '__author__'):
author = object.__author__
if hasattr(object, '__email__'):
author = author + ' <' + object.__email__ + '>'
results.append(section('AUTHOR', author))
return results
def document_class(object):
"""Produce text documentation for a given class object."""
name = object.__name__
bases = object.__bases__
doc = getdoc(object)
if doc:
results = [doc, '\n']
else:
results = [doc]
methods = map(lambda (key, value): value,
inspect.getmembers(object, inspect.ismethod))
for item in methods:
results.append([document_method(item), '\n'])
title = 'class ' + bold(name)
if bases:
parents = map(lambda c, m=object.__module__: classname(c, m), bases)
title = title + '(%s)' % join(parents, ', ')
return title + '\n' + rstrip(indent(results)) + '\n'
def document_method(object):
return document_function(object.im_func)
def document_function(object):
"""Produce text documentation for a given function object."""
try:
args, varargs, varkw, defaults = inspect.getargspec(object)
argspec = inspect.formatargspec(args, varargs, varkw, defaults)
except TypeError:
argspec = '(...)'
if object.__name__ == '<lambda>':
decl = ['<lambda> ', argspec[1:-1]]
else:
decl = bold(object.__name__) + argspec
doc = getdoc(object)
if doc:
return decl + '\n' + rstrip(indent(doc)) + '\n'
else:
return decl + '\n'
def document_builtin(object):
return [bold(object.__name__) + '(...)\n',
rstrip(indent(object.__doc__)) + '\n']
# --------------------------------------------------- main dispatch routine
def document(object):
"""Generate documentation for a given object."""
if inspect.ismodule(object): results = document_module(object)
elif inspect.isclass(object): results = document_class(object)
elif inspect.ismethod(object): results = document_method(object)
elif inspect.isfunction(object): results = document_function(object)
elif inspect.isbuiltin(object): results = document_builtin(object)
else: raise TypeError, 'don\'t know how to document this kind of object'
return serialize(results)