2001-02-27 10:43:46 -04:00
|
|
|
|
#!/usr/bin/env python
|
2001-02-28 20:24:32 -04:00
|
|
|
|
"""Generate Python documentation in HTML or text for interactive use.
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-01 09:55:20 -04:00
|
|
|
|
In the Python interpreter, do "from pydoc import help" to provide online
|
|
|
|
|
help. Calling help(thing) on a Python object documents the object.
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
Or, at the shell command line outside of Python:
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
Run "pydoc <name>" to show documentation on something. <name> may be
|
|
|
|
|
the name of a function, module, package, or a dotted reference to a
|
|
|
|
|
class or function within a module or module in a package. If the
|
|
|
|
|
argument contains a path segment delimiter (e.g. slash on Unix,
|
|
|
|
|
backslash on Windows) it is treated as the path to a Python source file.
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
Run "pydoc -k <keyword>" to search for a keyword in the synopsis lines
|
|
|
|
|
of all available modules.
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
Run "pydoc -p <port>" to start an HTTP server on a given port on the
|
|
|
|
|
local machine to generate documentation web pages.
|
2001-03-01 09:55:20 -04:00
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
For platforms without a command line, "pydoc -g" starts the HTTP server
|
|
|
|
|
and also pops up a little window for controlling it.
|
|
|
|
|
|
|
|
|
|
Run "pydoc -w <name>" to write out the HTML documentation for a module
|
|
|
|
|
to a file named "<name>.html".
|
2001-03-01 09:55:20 -04:00
|
|
|
|
"""
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
__author__ = "Ka-Ping Yee <ping@lfw.org>"
|
2001-02-27 18:42:36 -04:00
|
|
|
|
__date__ = "26 February 2001"
|
2001-02-27 18:43:48 -04:00
|
|
|
|
__version__ = "$Revision$"
|
2001-02-27 19:35:09 -04:00
|
|
|
|
__credits__ = """Guido van Rossum, for an excellent programming language.
|
|
|
|
|
Tommy Burnette, the original creator of manpy.
|
2001-02-27 18:42:36 -04:00
|
|
|
|
Paul Prescod, for all his work on onlinehelp.
|
|
|
|
|
Richard Chamberlain, for the first implementation of textdoc.
|
|
|
|
|
|
2001-02-27 19:35:09 -04:00
|
|
|
|
Mynd you, m<EFBFBD><EFBFBD>se bites Kan be pretty nasti..."""
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-01 09:55:20 -04:00
|
|
|
|
# Note: this module is designed to deploy instantly and run under any
|
|
|
|
|
# version of Python from 1.5 and up. That's why it's a single file and
|
|
|
|
|
# some 2.0 features (like string methods) are conspicuously avoided.
|
|
|
|
|
|
2001-02-27 10:43:46 -04:00
|
|
|
|
import sys, imp, os, stat, re, types, inspect
|
|
|
|
|
from repr import Repr
|
2001-03-23 09:17:50 -04:00
|
|
|
|
from string import expandtabs, find, join, lower, split, strip, rfind, rstrip
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------- common routines
|
|
|
|
|
|
|
|
|
|
def synopsis(filename, cache={}):
|
|
|
|
|
"""Get the one-line summary out of a module file."""
|
|
|
|
|
mtime = os.stat(filename)[stat.ST_MTIME]
|
|
|
|
|
lastupdate, result = cache.get(filename, (0, None))
|
2001-03-22 20:12:53 -04:00
|
|
|
|
# XXX what if ext is 'rb' type in imp_getsuffixes?
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if lastupdate < mtime:
|
|
|
|
|
file = open(filename)
|
|
|
|
|
line = file.readline()
|
|
|
|
|
while line[:1] == '#' or strip(line) == '':
|
|
|
|
|
line = file.readline()
|
|
|
|
|
if not line: break
|
|
|
|
|
if line[-2:] == '\\\n':
|
|
|
|
|
line = line[:-2] + file.readline()
|
|
|
|
|
line = strip(line)
|
|
|
|
|
if line[:3] == '"""':
|
|
|
|
|
line = line[3:]
|
|
|
|
|
while strip(line) == '':
|
|
|
|
|
line = file.readline()
|
|
|
|
|
if not line: break
|
2001-03-01 22:45:08 -04:00
|
|
|
|
result = strip(split(line, '"""')[0])
|
2001-02-27 10:43:46 -04:00
|
|
|
|
else: result = None
|
|
|
|
|
file.close()
|
|
|
|
|
cache[filename] = (mtime, result)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def pathdirs():
|
|
|
|
|
"""Convert sys.path into a list of absolute, existing, unique paths."""
|
|
|
|
|
dirs = []
|
2001-02-28 20:24:32 -04:00
|
|
|
|
normdirs = []
|
2001-02-27 10:43:46 -04:00
|
|
|
|
for dir in sys.path:
|
|
|
|
|
dir = os.path.abspath(dir or '.')
|
2001-02-28 20:24:32 -04:00
|
|
|
|
normdir = os.path.normcase(dir)
|
|
|
|
|
if normdir not in normdirs and os.path.isdir(dir):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
dirs.append(dir)
|
2001-02-28 20:24:32 -04:00
|
|
|
|
normdirs.append(normdir)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
return dirs
|
|
|
|
|
|
|
|
|
|
def getdoc(object):
|
|
|
|
|
"""Get the doc string or comments for an object."""
|
2001-03-23 09:17:50 -04:00
|
|
|
|
result = inspect.getdoc(object) or inspect.getcomments(object)
|
2001-03-01 22:45:08 -04:00
|
|
|
|
return result and re.sub('^ *\n', '', rstrip(result)) or ''
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def classname(object, modname):
|
|
|
|
|
"""Get a class name and qualify it with a module name if necessary."""
|
|
|
|
|
name = object.__name__
|
|
|
|
|
if object.__module__ != modname:
|
|
|
|
|
name = object.__module__ + '.' + name
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
def isconstant(object):
|
|
|
|
|
"""Check if an object is of a type that probably means it's a constant."""
|
|
|
|
|
return type(object) in [
|
|
|
|
|
types.FloatType, types.IntType, types.ListType, types.LongType,
|
|
|
|
|
types.StringType, types.TupleType, types.TypeType,
|
|
|
|
|
hasattr(types, 'UnicodeType') and types.UnicodeType or 0]
|
|
|
|
|
|
|
|
|
|
def replace(text, *pairs):
|
|
|
|
|
"""Do a series of global replacements on a string."""
|
|
|
|
|
for old, new in pairs:
|
|
|
|
|
text = join(split(text, old), new)
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
def cram(text, maxlen):
|
|
|
|
|
"""Omit part of a string if needed to make it fit in a maximum length."""
|
|
|
|
|
if len(text) > maxlen:
|
|
|
|
|
pre = max(0, (maxlen-3)/2)
|
|
|
|
|
post = max(0, maxlen-3-pre)
|
|
|
|
|
return text[:pre] + '...' + text[len(text)-post:]
|
|
|
|
|
return text
|
|
|
|
|
|
2001-02-28 20:24:32 -04:00
|
|
|
|
def stripid(text):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
"""Remove the hexadecimal id from a Python object representation."""
|
2001-02-28 20:24:32 -04:00
|
|
|
|
# The behaviour of %p is implementation-dependent, so we need an example.
|
|
|
|
|
for pattern in [' at 0x[0-9a-f]{6,}>$', ' at [0-9A-F]{8,}>$']:
|
|
|
|
|
if re.search(pattern, repr(Exception)):
|
|
|
|
|
return re.sub(pattern, '>', text)
|
|
|
|
|
return text
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def modulename(path):
|
|
|
|
|
"""Return the Python module name for a given path, or None."""
|
|
|
|
|
filename = os.path.basename(path)
|
2001-03-01 21:19:14 -04:00
|
|
|
|
suffixes = map(lambda (suffix, mode, kind): (len(suffix), suffix),
|
|
|
|
|
imp.get_suffixes())
|
|
|
|
|
suffixes.sort()
|
|
|
|
|
suffixes.reverse() # try longest suffixes first, in case they overlap
|
|
|
|
|
for length, suffix in suffixes:
|
|
|
|
|
if len(filename) > length and filename[-length:] == suffix:
|
|
|
|
|
return filename[:-length]
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-23 09:17:50 -04:00
|
|
|
|
def allmethods(cl):
|
|
|
|
|
methods = {}
|
|
|
|
|
for key, value in inspect.getmembers(cl, inspect.ismethod):
|
|
|
|
|
methods[key] = 1
|
|
|
|
|
for base in cl.__bases__:
|
|
|
|
|
methods.update(allmethods(base)) # all your base are belong to us
|
|
|
|
|
for key in methods.keys():
|
|
|
|
|
methods[key] = getattr(cl, key)
|
|
|
|
|
return methods
|
|
|
|
|
|
|
|
|
|
class ErrorDuringImport(Exception):
|
|
|
|
|
"""Errors that occurred while trying to import something to document it."""
|
|
|
|
|
def __init__(self, filename, (exc, value, tb)):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
self.filename = filename
|
2001-03-23 09:17:50 -04:00
|
|
|
|
self.exc = exc
|
2001-03-22 20:12:53 -04:00
|
|
|
|
self.value = value
|
|
|
|
|
self.tb = tb
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2001-03-23 09:17:50 -04:00
|
|
|
|
exc = self.exc
|
|
|
|
|
if type(exc) is types.ClassType:
|
|
|
|
|
exc = exc.__name__
|
|
|
|
|
return 'problem in %s - %s: %s' % (self.filename, exc, self.value)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def importfile(path):
|
|
|
|
|
"""Import a Python source file or compiled file given its path."""
|
|
|
|
|
magic = imp.get_magic()
|
|
|
|
|
file = open(path, 'r')
|
|
|
|
|
if file.read(len(magic)) == magic:
|
|
|
|
|
kind = imp.PY_COMPILED
|
|
|
|
|
else:
|
|
|
|
|
kind = imp.PY_SOURCE
|
|
|
|
|
file.close()
|
|
|
|
|
filename = os.path.basename(path)
|
|
|
|
|
name, ext = os.path.splitext(filename)
|
|
|
|
|
file = open(path, 'r')
|
|
|
|
|
try:
|
|
|
|
|
module = imp.load_module(name, file, path, (ext, 'r', kind))
|
|
|
|
|
except:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
raise ErrorDuringImport(path, sys.exc_info())
|
2001-02-27 10:43:46 -04:00
|
|
|
|
file.close()
|
|
|
|
|
return module
|
|
|
|
|
|
|
|
|
|
def ispackage(path):
|
|
|
|
|
"""Guess whether a path refers to a package directory."""
|
|
|
|
|
if os.path.isdir(path):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
for ext in ['.py', '.pyc', '.pyo']:
|
|
|
|
|
if os.path.isfile(os.path.join(path, '__init__' + ext)):
|
|
|
|
|
return 1
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------- formatter base class
|
|
|
|
|
|
|
|
|
|
class Doc:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def document(self, object, name=None, *args):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
"""Generate documentation for an object."""
|
2001-03-22 20:12:53 -04:00
|
|
|
|
args = (object, name) + args
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if inspect.ismodule(object): return apply(self.docmodule, args)
|
|
|
|
|
if inspect.isclass(object): return apply(self.docclass, args)
|
2001-03-01 09:55:20 -04:00
|
|
|
|
if inspect.isroutine(object): return apply(self.docroutine, args)
|
2001-03-22 20:12:53 -04:00
|
|
|
|
return apply(self.docother, args)
|
|
|
|
|
|
|
|
|
|
def fail(self, object, name=None, *args):
|
|
|
|
|
"""Raise an exception for unimplemented types."""
|
|
|
|
|
message = "don't know how to document object%s of type %s" % (
|
|
|
|
|
name and ' ' + repr(name), type(object).__name__)
|
|
|
|
|
raise TypeError, message
|
|
|
|
|
|
|
|
|
|
docmodule = docclass = docroutine = docother = fail
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
# -------------------------------------------- HTML documentation generator
|
|
|
|
|
|
|
|
|
|
class HTMLRepr(Repr):
|
|
|
|
|
"""Class for safely making an HTML representation of a Python object."""
|
|
|
|
|
def __init__(self):
|
|
|
|
|
Repr.__init__(self)
|
|
|
|
|
self.maxlist = self.maxtuple = self.maxdict = 10
|
2001-03-22 20:12:53 -04:00
|
|
|
|
self.maxstring = self.maxother = 100
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def escape(self, text):
|
|
|
|
|
return replace(text, ('&', '&'), ('<', '<'), ('>', '>'))
|
|
|
|
|
|
|
|
|
|
def repr(self, object):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
return Repr.repr(self, object)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def repr1(self, x, level):
|
|
|
|
|
methodname = 'repr_' + join(split(type(x).__name__), '_')
|
|
|
|
|
if hasattr(self, methodname):
|
|
|
|
|
return getattr(self, methodname)(x, level)
|
|
|
|
|
else:
|
2001-02-28 20:24:32 -04:00
|
|
|
|
return self.escape(cram(stripid(repr(x)), self.maxother))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def repr_string(self, x, level):
|
2001-03-01 21:19:14 -04:00
|
|
|
|
test = cram(x, self.maxstring)
|
|
|
|
|
testrepr = repr(test)
|
|
|
|
|
if '\\' in test and '\\' not in replace(testrepr, (r'\\', '')):
|
|
|
|
|
# Backslashes are only literal in the string and are never
|
|
|
|
|
# needed to make any special characters, so show a raw string.
|
|
|
|
|
return 'r' + testrepr[0] + self.escape(test) + testrepr[0]
|
|
|
|
|
return re.sub(r'((\\[\\abfnrtv\'"]|\\x..|\\u....)+)',
|
|
|
|
|
r'<font color="#c040c0">\1</font>',
|
|
|
|
|
self.escape(testrepr))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def repr_instance(self, x, level):
|
|
|
|
|
try:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
return self.escape(cram(stripid(repr(x)), self.maxstring))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
except:
|
|
|
|
|
return self.escape('<%s instance>' % x.__class__.__name__)
|
|
|
|
|
|
|
|
|
|
repr_unicode = repr_string
|
|
|
|
|
|
|
|
|
|
class HTMLDoc(Doc):
|
|
|
|
|
"""Formatter class for HTML documentation."""
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------- HTML formatting utilities
|
|
|
|
|
|
|
|
|
|
_repr_instance = HTMLRepr()
|
|
|
|
|
repr = _repr_instance.repr
|
|
|
|
|
escape = _repr_instance.escape
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def page(self, title, contents):
|
|
|
|
|
"""Format an HTML page."""
|
|
|
|
|
return '''
|
|
|
|
|
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
|
2001-03-23 09:17:50 -04:00
|
|
|
|
<html><head><title>Python: %s</title>
|
|
|
|
|
<style>TT { font-family: lucida console, lucida typewriter, courier }</style>
|
|
|
|
|
</head><body bgcolor="#f0f0f8">
|
2001-03-22 20:12:53 -04:00
|
|
|
|
%s
|
|
|
|
|
</body></html>''' % (title, contents)
|
|
|
|
|
|
|
|
|
|
def heading(self, title, fgcol, bgcol, extras=''):
|
|
|
|
|
"""Format a page heading."""
|
|
|
|
|
return '''
|
|
|
|
|
<table width="100%%" cellspacing=0 cellpadding=2 border=0>
|
|
|
|
|
<tr bgcolor="%s">
|
|
|
|
|
<td valign=bottom><small> <br></small
|
2001-03-23 09:35:45 -04:00
|
|
|
|
><font color="%s" face="helvetica, arial"> <br>%s</font></td
|
2001-03-22 20:12:53 -04:00
|
|
|
|
><td align=right valign=bottom
|
2001-03-23 09:35:45 -04:00
|
|
|
|
><font color="%s" face="helvetica, arial">%s</font></td></tr></table>
|
2001-03-22 20:12:53 -04:00
|
|
|
|
''' % (bgcol, fgcol, title, fgcol, extras or ' ')
|
|
|
|
|
|
|
|
|
|
def section(self, title, fgcol, bgcol, contents, width=20,
|
|
|
|
|
prelude='', marginalia=None, gap=' '):
|
|
|
|
|
"""Format a section with a heading."""
|
|
|
|
|
if marginalia is None:
|
|
|
|
|
marginalia = ' ' * width
|
|
|
|
|
result = '''
|
|
|
|
|
<p><table width="100%%" cellspacing=0 cellpadding=2 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:
|
|
|
|
|
result = result + '''
|
2001-03-23 09:35:45 -04:00
|
|
|
|
<tr bgcolor="%s"><td rowspan=2>%s</td>
|
|
|
|
|
<td colspan=2>%s</td></tr>
|
|
|
|
|
<tr><td>%s</td>''' % (bgcol, marginalia, prelude, gap)
|
|
|
|
|
else:
|
|
|
|
|
result = result + '''
|
2001-03-23 09:17:50 -04:00
|
|
|
|
<tr><td bgcolor="%s">%s</td><td>%s</td>''' % (bgcol, marginalia, gap)
|
2001-03-22 20:12:53 -04:00
|
|
|
|
|
|
|
|
|
return result + '<td width="100%%">%s</td></tr></table>' % contents
|
|
|
|
|
|
|
|
|
|
def bigsection(self, title, *args):
|
|
|
|
|
"""Format a section with a big heading."""
|
|
|
|
|
title = '<big><strong>%s</strong></big>' % title
|
|
|
|
|
return apply(self.section, (title,) + args)
|
|
|
|
|
|
2001-02-27 10:43:46 -04:00
|
|
|
|
def preformat(self, text):
|
|
|
|
|
"""Format literal preformatted text."""
|
|
|
|
|
text = self.escape(expandtabs(text))
|
|
|
|
|
return replace(text, ('\n\n', '\n \n'), ('\n\n', '\n \n'),
|
|
|
|
|
(' ', ' '), ('\n', '<br>\n'))
|
|
|
|
|
|
|
|
|
|
def multicolumn(self, list, format, cols=4):
|
|
|
|
|
"""Format a list of items into a multi-column list."""
|
|
|
|
|
result = ''
|
|
|
|
|
rows = (len(list)+cols-1)/cols
|
|
|
|
|
for col in range(cols):
|
|
|
|
|
result = result + '<td width="%d%%" valign=top>' % (100/cols)
|
|
|
|
|
for i in range(rows*col, rows*col+rows):
|
|
|
|
|
if i < len(list):
|
2001-03-23 09:17:50 -04:00
|
|
|
|
result = result + format(list[i]) + '<br>\n'
|
2001-02-27 10:43:46 -04:00
|
|
|
|
result = result + '</td>'
|
|
|
|
|
return '<table width="100%%"><tr>%s</tr></table>' % result
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def small(self, text): return '<small>%s</small>' % text
|
|
|
|
|
def grey(self, text): return '<font color="#909090">%s</font>' % text
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def namelink(self, name, *dicts):
|
|
|
|
|
"""Make a link for an identifier, given name-to-URL mappings."""
|
|
|
|
|
for dict in dicts:
|
|
|
|
|
if dict.has_key(name):
|
|
|
|
|
return '<a href="%s">%s</a>' % (dict[name], name)
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
def classlink(self, object, modname, *dicts):
|
|
|
|
|
"""Make a link for a class."""
|
|
|
|
|
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(self, object):
|
|
|
|
|
"""Make a link for a module."""
|
|
|
|
|
return '<a href="%s.html">%s</a>' % (object.__name__, object.__name__)
|
|
|
|
|
|
|
|
|
|
def modpkglink(self, (name, path, ispackage, shadowed)):
|
|
|
|
|
"""Make a link for a module or package to display in an index."""
|
|
|
|
|
if shadowed:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
return self.grey(name)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if path:
|
|
|
|
|
url = '%s.%s.html' % (path, name)
|
|
|
|
|
else:
|
|
|
|
|
url = '%s.html' % name
|
|
|
|
|
if ispackage:
|
|
|
|
|
text = '<strong>%s</strong> (package)' % name
|
|
|
|
|
else:
|
|
|
|
|
text = name
|
|
|
|
|
return '<a href="%s">%s</a>' % (url, text)
|
|
|
|
|
|
|
|
|
|
def markup(self, text, escape=None, funcs={}, classes={}, methods={}):
|
|
|
|
|
"""Mark up some plain text, given a context of symbols to look for.
|
|
|
|
|
Each context dictionary maps object names to anchor names."""
|
|
|
|
|
escape = escape or self.escape
|
|
|
|
|
results = []
|
|
|
|
|
here = 0
|
2001-03-23 09:17:50 -04:00
|
|
|
|
pattern = re.compile(r'\b((http|ftp)://\S+[\w/]|'
|
|
|
|
|
r'RFC[- ]?(\d+)|'
|
2001-02-27 10:43:46 -04:00
|
|
|
|
r'(self\.)?(\w+))\b')
|
|
|
|
|
while 1:
|
|
|
|
|
match = pattern.search(text, here)
|
|
|
|
|
if not match: break
|
|
|
|
|
start, end = match.span()
|
|
|
|
|
results.append(escape(text[here:start]))
|
|
|
|
|
|
2001-03-23 09:17:50 -04:00
|
|
|
|
all, scheme, rfc, selfdot, name = match.groups()
|
|
|
|
|
if scheme:
|
|
|
|
|
results.append('<a href="%s">%s</a>' % (all, escape(all)))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
elif rfc:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
url = 'http://www.rfc-editor.org/rfc/rfc%s.txt' % rfc
|
|
|
|
|
results.append('<a href="%s">%s</a>' % (url, escape(all)))
|
|
|
|
|
elif text[end:end+1] == '(':
|
|
|
|
|
results.append(self.namelink(name, methods, funcs, classes))
|
|
|
|
|
elif selfdot:
|
|
|
|
|
results.append('self.<strong>%s</strong>' % name)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
else:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
results.append(self.namelink(name, classes))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
here = end
|
|
|
|
|
results.append(escape(text[here:]))
|
|
|
|
|
return join(results, '')
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------- type-specific routines
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def formattree(self, tree, modname, classes={}, parent=None):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
"""Produce HTML for a class tree as given by inspect.getclasstree()."""
|
|
|
|
|
result = ''
|
|
|
|
|
for entry in tree:
|
|
|
|
|
if type(entry) is type(()):
|
|
|
|
|
c, bases = entry
|
|
|
|
|
result = result + '<dt><font face="helvetica, arial"><small>'
|
|
|
|
|
result = result + self.classlink(c, modname, classes)
|
|
|
|
|
if bases and bases != (parent,):
|
|
|
|
|
parents = []
|
|
|
|
|
for base in bases:
|
|
|
|
|
parents.append(self.classlink(base, modname, classes))
|
|
|
|
|
result = result + '(' + join(parents, ', ') + ')'
|
|
|
|
|
result = result + '\n</small></font></dt>'
|
|
|
|
|
elif type(entry) is type([]):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
result = result + '<dd>\n%s</dd>\n' % self.formattree(
|
|
|
|
|
entry, modname, classes, c)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
return '<dl>\n%s</dl>\n' % result
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def docmodule(self, object, name=None):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
"""Produce HTML documentation for a module object."""
|
2001-03-22 20:12:53 -04:00
|
|
|
|
name = object.__name__ # ignore the passed-in name
|
2001-03-01 09:55:20 -04:00
|
|
|
|
parts = split(name, '.')
|
|
|
|
|
links = []
|
|
|
|
|
for i in range(len(parts)-1):
|
|
|
|
|
links.append(
|
|
|
|
|
'<a href="%s.html"><font color="#ffffff">%s</font></a>' %
|
|
|
|
|
(join(parts[:i+1], '.'), parts[i]))
|
|
|
|
|
linkedname = join(links + parts[-1:], '.')
|
|
|
|
|
head = '<big><big><strong>%s</strong></big></big>' % linkedname
|
2001-02-27 10:43:46 -04:00
|
|
|
|
try:
|
2001-03-01 22:45:08 -04:00
|
|
|
|
path = inspect.getabsfile(object)
|
2001-02-28 20:24:32 -04:00
|
|
|
|
filelink = '<a href="file:%s">%s</a>' % (path, path)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
except TypeError:
|
|
|
|
|
filelink = '(built-in)'
|
2001-02-27 18:42:36 -04:00
|
|
|
|
info = []
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if hasattr(object, '__version__'):
|
2001-02-27 18:42:36 -04:00
|
|
|
|
version = str(object.__version__)
|
2001-02-27 18:46:01 -04:00
|
|
|
|
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
|
|
|
|
|
version = strip(version[11:-1])
|
2001-02-28 20:24:32 -04:00
|
|
|
|
info.append('version %s' % self.escape(version))
|
2001-02-27 18:42:36 -04:00
|
|
|
|
if hasattr(object, '__date__'):
|
|
|
|
|
info.append(self.escape(str(object.__date__)))
|
|
|
|
|
if info:
|
|
|
|
|
head = head + ' (%s)' % join(info, ', ')
|
2001-03-01 09:55:20 -04:00
|
|
|
|
result = self.heading(
|
2001-02-27 10:43:46 -04:00
|
|
|
|
head, '#ffffff', '#7799ee', '<a href=".">index</a><br>' + filelink)
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
modules = inspect.getmembers(object, inspect.ismodule)
|
|
|
|
|
|
|
|
|
|
if 0 and hasattr(object, '__all__'):
|
|
|
|
|
visible = lambda key, all=object.__all__: key in all
|
|
|
|
|
else:
|
|
|
|
|
visible = lambda key: key[:1] != '_'
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
classes, cdict = [], {}
|
|
|
|
|
for key, value in inspect.getmembers(object, inspect.isclass):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if visible(key) and (
|
|
|
|
|
inspect.getmodule(value) or object) is object:
|
|
|
|
|
classes.append((key, value))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
cdict[key] = cdict[value] = '#' + key
|
2001-03-22 20:12:53 -04:00
|
|
|
|
for key, value in classes:
|
|
|
|
|
for base in value.__bases__:
|
|
|
|
|
key, modname = base.__name__, base.__module__
|
|
|
|
|
module = sys.modules.get(modname)
|
|
|
|
|
if modname != name and module and hasattr(module, key):
|
|
|
|
|
if getattr(module, key) is base:
|
|
|
|
|
if not cdict.has_key(key):
|
|
|
|
|
cdict[key] = cdict[base] = modname + '.html#' + key
|
2001-02-27 10:43:46 -04:00
|
|
|
|
funcs, fdict = [], {}
|
|
|
|
|
for key, value in inspect.getmembers(object, inspect.isroutine):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if visible(key) and (inspect.isbuiltin(value) or
|
|
|
|
|
inspect.getmodule(value) is object):
|
|
|
|
|
funcs.append((key, value))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
fdict[key] = '#-' + key
|
|
|
|
|
if inspect.isfunction(value): fdict[value] = fdict[key]
|
|
|
|
|
constants = []
|
|
|
|
|
for key, value in inspect.getmembers(object, isconstant):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if visible(key):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
constants.append((key, value))
|
|
|
|
|
|
|
|
|
|
doc = self.markup(getdoc(object), self.preformat, fdict, cdict)
|
|
|
|
|
doc = doc and '<tt>%s</tt>' % doc
|
2001-03-22 20:12:53 -04:00
|
|
|
|
result = result + '<p>%s\n' % self.small(doc)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
if hasattr(object, '__path__'):
|
|
|
|
|
modpkgs = []
|
|
|
|
|
modnames = []
|
|
|
|
|
for file in os.listdir(object.__path__[0]):
|
|
|
|
|
if file[:1] != '_':
|
|
|
|
|
path = os.path.join(object.__path__[0], file)
|
|
|
|
|
modname = modulename(file)
|
|
|
|
|
if modname and modname not in modnames:
|
|
|
|
|
modpkgs.append((modname, name, 0, 0))
|
|
|
|
|
modnames.append(modname)
|
|
|
|
|
elif ispackage(path):
|
2001-02-28 04:26:44 -04:00
|
|
|
|
modpkgs.append((file, name, 1, 0))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
modpkgs.sort()
|
|
|
|
|
contents = self.multicolumn(modpkgs, self.modpkglink)
|
|
|
|
|
result = result + self.bigsection(
|
|
|
|
|
'Package Contents', '#ffffff', '#aa55cc', contents)
|
|
|
|
|
elif modules:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
contents = self.multicolumn(
|
|
|
|
|
modules, lambda (key, value), s=self: s.modulelink(value))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
result = result + self.bigsection(
|
|
|
|
|
'Modules', '#fffff', '#aa55cc', contents)
|
|
|
|
|
|
|
|
|
|
if classes:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
classlist = map(lambda (key, value): value, classes)
|
|
|
|
|
contents = [self.formattree(
|
|
|
|
|
inspect.getclasstree(classlist, 1), name, cdict)]
|
|
|
|
|
for key, value in classes:
|
|
|
|
|
contents.append(self.document(value, key, fdict, cdict))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
result = result + self.bigsection(
|
2001-03-22 20:12:53 -04:00
|
|
|
|
'Classes', '#ffffff', '#ee77aa', join(contents))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if funcs:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
contents = []
|
|
|
|
|
for key, value in funcs:
|
|
|
|
|
contents.append(self.document(value, key, fdict, cdict))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
result = result + self.bigsection(
|
2001-03-22 20:12:53 -04:00
|
|
|
|
'Functions', '#ffffff', '#eeaa77', join(contents))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if constants:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
contents = []
|
2001-02-27 10:43:46 -04:00
|
|
|
|
for key, value in constants:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
contents.append(self.document(value, key))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
result = result + self.bigsection(
|
2001-03-22 20:12:53 -04:00
|
|
|
|
'Constants', '#ffffff', '#55aa55', join(contents, '<br>'))
|
2001-02-27 18:42:36 -04:00
|
|
|
|
if hasattr(object, '__author__'):
|
|
|
|
|
contents = self.markup(str(object.__author__), self.preformat)
|
|
|
|
|
result = result + self.bigsection(
|
|
|
|
|
'Author', '#ffffff', '#7799ee', contents)
|
|
|
|
|
if hasattr(object, '__credits__'):
|
|
|
|
|
contents = self.markup(str(object.__credits__), self.preformat)
|
|
|
|
|
result = result + self.bigsection(
|
|
|
|
|
'Credits', '#ffffff', '#7799ee', contents)
|
|
|
|
|
|
2001-02-27 10:43:46 -04:00
|
|
|
|
return result
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def docclass(self, object, name=None, funcs={}, classes={}):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
"""Produce HTML documentation for a class object."""
|
2001-03-22 20:12:53 -04:00
|
|
|
|
realname = object.__name__
|
|
|
|
|
name = name or realname
|
2001-02-27 10:43:46 -04:00
|
|
|
|
bases = object.__bases__
|
|
|
|
|
contents = ''
|
|
|
|
|
|
|
|
|
|
methods, mdict = [], {}
|
2001-03-23 09:17:50 -04:00
|
|
|
|
for key, value in allmethods(object).items():
|
2001-03-22 20:12:53 -04:00
|
|
|
|
methods.append((key, value))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
mdict[key] = mdict[value] = '#' + name + '-' + key
|
2001-03-23 09:17:50 -04:00
|
|
|
|
methods.sort()
|
2001-03-22 20:12:53 -04:00
|
|
|
|
for key, value in methods:
|
2001-02-27 10:43:46 -04:00
|
|
|
|
contents = contents + self.document(
|
2001-03-23 09:17:50 -04:00
|
|
|
|
value, key, funcs, classes, mdict, object)
|
2001-03-22 20:12:53 -04:00
|
|
|
|
|
|
|
|
|
if name == realname:
|
|
|
|
|
title = '<a name="%s">class <strong>%s</strong></a>' % (
|
|
|
|
|
name, realname)
|
|
|
|
|
else:
|
|
|
|
|
title = '<strong>%s</strong> = <a name="%s">class %s</a>' % (
|
|
|
|
|
name, name, realname)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if bases:
|
|
|
|
|
parents = []
|
|
|
|
|
for base in bases:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
parents.append(
|
|
|
|
|
self.classlink(base, object.__module__, classes))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
title = title + '(%s)' % join(parents, ', ')
|
2001-03-22 20:12:53 -04:00
|
|
|
|
doc = self.markup(
|
|
|
|
|
getdoc(object), self.preformat, funcs, classes, mdict)
|
2001-03-23 09:35:45 -04:00
|
|
|
|
doc = self.small(doc and '<tt>%s<br> </tt>' % doc or '<tt> </tt>')
|
2001-02-27 10:43:46 -04:00
|
|
|
|
return self.section(title, '#000000', '#ffc8d8', contents, 10, doc)
|
|
|
|
|
|
|
|
|
|
def formatvalue(self, object):
|
|
|
|
|
"""Format an argument default value as text."""
|
2001-03-22 20:12:53 -04:00
|
|
|
|
return self.small(self.grey('=' + self.repr(object)))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def docroutine(self, object, name=None,
|
2001-03-23 09:17:50 -04:00
|
|
|
|
funcs={}, classes={}, methods={}, cl=None):
|
2001-03-01 09:55:20 -04:00
|
|
|
|
"""Produce HTML documentation for a function or method object."""
|
2001-03-22 20:12:53 -04:00
|
|
|
|
realname = object.__name__
|
|
|
|
|
name = name or realname
|
2001-03-23 09:17:50 -04:00
|
|
|
|
anchor = (cl and cl.__name__ or '') + '-' + name
|
2001-03-22 20:12:53 -04:00
|
|
|
|
note = ''
|
2001-03-23 09:17:50 -04:00
|
|
|
|
skipdocs = 0
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if inspect.ismethod(object):
|
2001-03-23 09:17:50 -04:00
|
|
|
|
if cl:
|
|
|
|
|
if not cl.__dict__.has_key(name):
|
|
|
|
|
base = object.im_class
|
|
|
|
|
url = '#%s-%s' % (base.__name__, name)
|
|
|
|
|
basename = base.__name__
|
|
|
|
|
if base.__module__ != cl.__module__:
|
|
|
|
|
url = base.__module__ + '.html' + url
|
|
|
|
|
basename = base.__module__ + '.' + basename
|
|
|
|
|
note = ' from <a href="%s">%s</a>' % (url, basename)
|
|
|
|
|
skipdocs = 1
|
|
|
|
|
else:
|
|
|
|
|
note = (object.im_self and
|
2001-03-23 09:35:45 -04:00
|
|
|
|
' method of ' + self.repr(object.im_self) or
|
2001-03-23 09:17:50 -04:00
|
|
|
|
' unbound %s method' % object.im_class.__name__)
|
2001-03-22 20:12:53 -04:00
|
|
|
|
object = object.im_func
|
|
|
|
|
|
|
|
|
|
if name == realname:
|
|
|
|
|
title = '<a name="%s"><strong>%s</strong></a>' % (anchor, realname)
|
|
|
|
|
else:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
if (cl and cl.__dict__.has_key(realname) and
|
|
|
|
|
cl.__dict__[realname] is object):
|
|
|
|
|
reallink = '<a href="%s">%s</a>' % (
|
|
|
|
|
cl.__name__ + '-' + realname, realname)
|
|
|
|
|
skipdocs = 1
|
|
|
|
|
else:
|
|
|
|
|
reallink = realname
|
|
|
|
|
title = '<a name="%s"><strong>%s</strong></a> = %s' % (
|
|
|
|
|
anchor, name, reallink)
|
2001-03-01 09:55:20 -04:00
|
|
|
|
if inspect.isbuiltin(object):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
argspec = '(...)'
|
2001-02-27 10:43:46 -04:00
|
|
|
|
else:
|
2001-03-01 09:55:20 -04:00
|
|
|
|
args, varargs, varkw, defaults = inspect.getargspec(object)
|
|
|
|
|
argspec = inspect.formatargspec(
|
|
|
|
|
args, varargs, varkw, defaults, formatvalue=self.formatvalue)
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if realname == '<lambda>':
|
|
|
|
|
decl = '<em>lambda</em>'
|
|
|
|
|
argspec = argspec[1:-1] # remove parentheses
|
2001-03-01 09:55:20 -04:00
|
|
|
|
|
2001-03-23 09:35:45 -04:00
|
|
|
|
decl = title + argspec + (note and self.small(self.grey(
|
|
|
|
|
'<font face="helvetica, arial">%s</font>' % note)))
|
2001-03-22 20:12:53 -04:00
|
|
|
|
|
2001-03-23 09:17:50 -04:00
|
|
|
|
if skipdocs:
|
2001-03-23 09:35:45 -04:00
|
|
|
|
return '<dl><dt>%s</dl>\n' % decl
|
2001-03-23 09:17:50 -04:00
|
|
|
|
else:
|
|
|
|
|
doc = self.markup(
|
|
|
|
|
getdoc(object), self.preformat, funcs, classes, methods)
|
|
|
|
|
doc = doc and '<tt>%s</tt>' % doc
|
2001-03-23 09:35:45 -04:00
|
|
|
|
return '<dl><dt>%s<dd>%s</dl>\n' % (decl, self.small(doc))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def docother(self, object, name=None):
|
|
|
|
|
"""Produce HTML documentation for a data object."""
|
|
|
|
|
return '<strong>%s</strong> = %s' % (name, self.repr(object))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def index(self, dir, shadowed=None):
|
|
|
|
|
"""Generate an HTML index for a directory of modules."""
|
|
|
|
|
modpkgs = []
|
|
|
|
|
if shadowed is None: shadowed = {}
|
|
|
|
|
seen = {}
|
|
|
|
|
files = os.listdir(dir)
|
|
|
|
|
|
|
|
|
|
def found(name, ispackage,
|
|
|
|
|
modpkgs=modpkgs, shadowed=shadowed, seen=seen):
|
|
|
|
|
if not seen.has_key(name):
|
|
|
|
|
modpkgs.append((name, '', ispackage, shadowed.has_key(name)))
|
|
|
|
|
seen[name] = 1
|
|
|
|
|
shadowed[name] = 1
|
|
|
|
|
|
|
|
|
|
# Package spam/__init__.py takes precedence over module spam.py.
|
|
|
|
|
for file in files:
|
|
|
|
|
path = os.path.join(dir, file)
|
|
|
|
|
if ispackage(path): found(file, 1)
|
|
|
|
|
for file in files:
|
|
|
|
|
path = os.path.join(dir, file)
|
2001-02-28 04:26:44 -04:00
|
|
|
|
if file[:1] != '_' and os.path.isfile(path):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
modname = modulename(file)
|
|
|
|
|
if modname: found(modname, 0)
|
|
|
|
|
|
|
|
|
|
modpkgs.sort()
|
|
|
|
|
contents = self.multicolumn(modpkgs, self.modpkglink)
|
|
|
|
|
return self.bigsection(dir, '#ffffff', '#ee77aa', contents)
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------- text documentation generator
|
|
|
|
|
|
|
|
|
|
class TextRepr(Repr):
|
|
|
|
|
"""Class for safely making a text representation of a Python object."""
|
|
|
|
|
def __init__(self):
|
|
|
|
|
Repr.__init__(self)
|
|
|
|
|
self.maxlist = self.maxtuple = self.maxdict = 10
|
2001-03-22 20:12:53 -04:00
|
|
|
|
self.maxstring = self.maxother = 100
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def repr1(self, x, level):
|
|
|
|
|
methodname = 'repr_' + join(split(type(x).__name__), '_')
|
|
|
|
|
if hasattr(self, methodname):
|
|
|
|
|
return getattr(self, methodname)(x, level)
|
|
|
|
|
else:
|
2001-02-28 20:24:32 -04:00
|
|
|
|
return cram(stripid(repr(x)), self.maxother)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-01 21:19:14 -04:00
|
|
|
|
def repr_string(self, x, level):
|
|
|
|
|
test = cram(x, self.maxstring)
|
|
|
|
|
testrepr = repr(test)
|
|
|
|
|
if '\\' in test and '\\' not in replace(testrepr, (r'\\', '')):
|
|
|
|
|
# Backslashes are only literal in the string and are never
|
|
|
|
|
# needed to make any special characters, so show a raw string.
|
|
|
|
|
return 'r' + testrepr[0] + test + testrepr[0]
|
|
|
|
|
return testrepr
|
|
|
|
|
|
2001-02-27 10:43:46 -04:00
|
|
|
|
def repr_instance(self, x, level):
|
|
|
|
|
try:
|
2001-02-28 20:24:32 -04:00
|
|
|
|
return cram(stripid(repr(x)), self.maxstring)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
except:
|
|
|
|
|
return '<%s instance>' % x.__class__.__name__
|
|
|
|
|
|
|
|
|
|
class TextDoc(Doc):
|
|
|
|
|
"""Formatter class for text documentation."""
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------- text formatting utilities
|
|
|
|
|
|
|
|
|
|
_repr_instance = TextRepr()
|
|
|
|
|
repr = _repr_instance.repr
|
|
|
|
|
|
|
|
|
|
def bold(self, text):
|
|
|
|
|
"""Format a string in bold by overstriking."""
|
|
|
|
|
return join(map(lambda ch: ch + '\b' + ch, text), '')
|
|
|
|
|
|
|
|
|
|
def indent(self, text, prefix=' '):
|
|
|
|
|
"""Indent text by prepending a given prefix to each line."""
|
|
|
|
|
if not text: return ''
|
|
|
|
|
lines = split(text, '\n')
|
|
|
|
|
lines = map(lambda line, prefix=prefix: prefix + line, lines)
|
|
|
|
|
if lines: lines[-1] = rstrip(lines[-1])
|
|
|
|
|
return join(lines, '\n')
|
|
|
|
|
|
|
|
|
|
def section(self, title, contents):
|
|
|
|
|
"""Format a section with a given heading."""
|
|
|
|
|
return self.bold(title) + '\n' + rstrip(self.indent(contents)) + '\n\n'
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------- type-specific routines
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def formattree(self, tree, modname, parent=None, prefix=''):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
"""Render in text a class tree as returned by inspect.getclasstree()."""
|
|
|
|
|
result = ''
|
|
|
|
|
for entry in tree:
|
|
|
|
|
if type(entry) is type(()):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
c, bases = entry
|
|
|
|
|
result = result + prefix + classname(c, modname)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if bases and bases != (parent,):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
parents = map(lambda c, m=modname: classname(c, m), bases)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
result = result + '(%s)' % join(parents, ', ')
|
|
|
|
|
result = result + '\n'
|
|
|
|
|
elif type(entry) is type([]):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
result = result + self.formattree(
|
|
|
|
|
entry, modname, c, prefix + ' ')
|
2001-02-27 10:43:46 -04:00
|
|
|
|
return result
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def docmodule(self, object, name=None):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
"""Produce text documentation for a given module object."""
|
2001-03-22 20:12:53 -04:00
|
|
|
|
name = object.__name__ # ignore the passed-in name
|
2001-03-23 09:17:50 -04:00
|
|
|
|
namesec = name
|
2001-02-27 10:43:46 -04:00
|
|
|
|
lines = split(strip(getdoc(object)), '\n')
|
|
|
|
|
if len(lines) == 1:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
if lines[0]: namesec = namesec + ' - ' + lines[0]
|
2001-02-27 10:43:46 -04:00
|
|
|
|
lines = []
|
|
|
|
|
elif len(lines) >= 2 and not rstrip(lines[1]):
|
2001-03-23 09:17:50 -04:00
|
|
|
|
if lines[0]: namesec = namesec + ' - ' + lines[0]
|
2001-02-27 10:43:46 -04:00
|
|
|
|
lines = lines[2:]
|
2001-03-23 09:17:50 -04:00
|
|
|
|
result = self.section('NAME', namesec)
|
2001-03-22 20:12:53 -04:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
file = inspect.getabsfile(object)
|
|
|
|
|
except TypeError:
|
|
|
|
|
file = '(built-in)'
|
2001-03-01 22:45:08 -04:00
|
|
|
|
result = result + self.section('FILE', file)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if lines:
|
|
|
|
|
result = result + self.section('DESCRIPTION', join(lines, '\n'))
|
|
|
|
|
|
|
|
|
|
classes = []
|
|
|
|
|
for key, value in inspect.getmembers(object, inspect.isclass):
|
|
|
|
|
if (inspect.getmodule(value) or object) is object:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
classes.append((key, value))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
funcs = []
|
|
|
|
|
for key, value in inspect.getmembers(object, inspect.isroutine):
|
|
|
|
|
if inspect.isbuiltin(value) or inspect.getmodule(value) is object:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
funcs.append((key, value))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
constants = []
|
|
|
|
|
for key, value in inspect.getmembers(object, isconstant):
|
|
|
|
|
if key[:1] != '_':
|
|
|
|
|
constants.append((key, value))
|
|
|
|
|
|
|
|
|
|
if hasattr(object, '__path__'):
|
|
|
|
|
modpkgs = []
|
|
|
|
|
for file in os.listdir(object.__path__[0]):
|
|
|
|
|
if file[:1] != '_':
|
|
|
|
|
path = os.path.join(object.__path__[0], file)
|
|
|
|
|
modname = modulename(file)
|
|
|
|
|
if modname and modname not in modpkgs:
|
|
|
|
|
modpkgs.append(modname)
|
|
|
|
|
elif ispackage(path):
|
|
|
|
|
modpkgs.append(file + ' (package)')
|
|
|
|
|
modpkgs.sort()
|
|
|
|
|
result = result + self.section(
|
|
|
|
|
'PACKAGE CONTENTS', join(modpkgs, '\n'))
|
|
|
|
|
|
|
|
|
|
if classes:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
classlist = map(lambda (key, value): value, classes)
|
|
|
|
|
contents = [self.formattree(
|
|
|
|
|
inspect.getclasstree(classlist, 1), name)]
|
|
|
|
|
for key, value in classes:
|
|
|
|
|
contents.append(self.document(value, key))
|
|
|
|
|
result = result + self.section('CLASSES', join(contents, '\n'))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
if funcs:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
contents = []
|
|
|
|
|
for key, value in funcs:
|
|
|
|
|
contents.append(self.document(value, key))
|
|
|
|
|
result = result + self.section('FUNCTIONS', join(contents, '\n'))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
if constants:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
contents = []
|
2001-02-27 10:43:46 -04:00
|
|
|
|
for key, value in constants:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
contents.append(self.docother(value, key, 70))
|
|
|
|
|
result = result + self.section('CONSTANTS', join(contents, '\n'))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
if hasattr(object, '__version__'):
|
|
|
|
|
version = str(object.__version__)
|
2001-02-28 20:24:32 -04:00
|
|
|
|
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
|
|
|
|
|
version = strip(version[11:-1])
|
2001-02-27 10:43:46 -04:00
|
|
|
|
result = result + self.section('VERSION', version)
|
2001-02-27 18:42:36 -04:00
|
|
|
|
if hasattr(object, '__date__'):
|
|
|
|
|
result = result + self.section('DATE', str(object.__date__))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if hasattr(object, '__author__'):
|
2001-02-27 18:42:36 -04:00
|
|
|
|
result = result + self.section('AUTHOR', str(object.__author__))
|
|
|
|
|
if hasattr(object, '__credits__'):
|
|
|
|
|
result = result + self.section('CREDITS', str(object.__credits__))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
return result
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def docclass(self, object, name=None):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
"""Produce text documentation for a given class object."""
|
2001-03-22 20:12:53 -04:00
|
|
|
|
realname = object.__name__
|
|
|
|
|
name = name or realname
|
2001-02-27 10:43:46 -04:00
|
|
|
|
bases = object.__bases__
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if name == realname:
|
|
|
|
|
title = 'class ' + self.bold(realname)
|
|
|
|
|
else:
|
|
|
|
|
title = self.bold(name) + ' = class ' + realname
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if bases:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
def makename(c, m=object.__module__): return classname(c, m)
|
|
|
|
|
parents = map(makename, bases)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
title = title + '(%s)' % join(parents, ', ')
|
|
|
|
|
|
|
|
|
|
doc = getdoc(object)
|
|
|
|
|
contents = doc and doc + '\n'
|
2001-03-23 09:17:50 -04:00
|
|
|
|
methods = allmethods(object).items()
|
|
|
|
|
methods.sort()
|
|
|
|
|
for key, value in methods:
|
|
|
|
|
contents = contents + '\n' + self.document(value, key, object)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
if not contents: return title + '\n'
|
|
|
|
|
return title + '\n' + self.indent(rstrip(contents), ' | ') + '\n'
|
|
|
|
|
|
|
|
|
|
def formatvalue(self, object):
|
|
|
|
|
"""Format an argument default value as text."""
|
|
|
|
|
return '=' + self.repr(object)
|
|
|
|
|
|
2001-03-23 09:17:50 -04:00
|
|
|
|
def docroutine(self, object, name=None, cl=None):
|
2001-03-01 09:55:20 -04:00
|
|
|
|
"""Produce text documentation for a function or method object."""
|
2001-03-22 20:12:53 -04:00
|
|
|
|
realname = object.__name__
|
|
|
|
|
name = name or realname
|
|
|
|
|
note = ''
|
2001-03-23 09:17:50 -04:00
|
|
|
|
skipdocs = 0
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if inspect.ismethod(object):
|
2001-03-23 09:17:50 -04:00
|
|
|
|
if cl:
|
|
|
|
|
if not cl.__dict__.has_key(name):
|
|
|
|
|
base = object.im_class
|
|
|
|
|
basename = base.__name__
|
|
|
|
|
if base.__module__ != cl.__module__:
|
|
|
|
|
basename = base.__module__ + '.' + basename
|
|
|
|
|
note = ' from %s' % basename
|
|
|
|
|
skipdocs = 1
|
|
|
|
|
else:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if object.im_self:
|
|
|
|
|
note = ' method of %s' % self.repr(object.im_self)
|
|
|
|
|
else:
|
|
|
|
|
note = ' unbound %s method' % object.im_class.__name__
|
|
|
|
|
object = object.im_func
|
|
|
|
|
|
|
|
|
|
if name == realname:
|
|
|
|
|
title = self.bold(realname)
|
|
|
|
|
else:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
if (cl and cl.__dict__.has_key(realname) and
|
|
|
|
|
cl.__dict__[realname] is object):
|
|
|
|
|
skipdocs = 1
|
2001-03-22 20:12:53 -04:00
|
|
|
|
title = self.bold(name) + ' = ' + realname
|
2001-03-01 09:55:20 -04:00
|
|
|
|
if inspect.isbuiltin(object):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
argspec = '(...)'
|
2001-03-01 09:55:20 -04:00
|
|
|
|
else:
|
2001-02-27 10:43:46 -04:00
|
|
|
|
args, varargs, varkw, defaults = inspect.getargspec(object)
|
|
|
|
|
argspec = inspect.formatargspec(
|
|
|
|
|
args, varargs, varkw, defaults, formatvalue=self.formatvalue)
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if realname == '<lambda>':
|
|
|
|
|
title = 'lambda'
|
|
|
|
|
argspec = argspec[1:-1] # remove parentheses
|
|
|
|
|
decl = title + argspec + note
|
|
|
|
|
|
2001-03-23 09:17:50 -04:00
|
|
|
|
if skipdocs:
|
2001-02-27 10:43:46 -04:00
|
|
|
|
return decl + '\n'
|
2001-03-23 09:17:50 -04:00
|
|
|
|
else:
|
|
|
|
|
doc = getdoc(object) or ''
|
|
|
|
|
return decl + '\n' + (doc and rstrip(self.indent(doc)) + '\n')
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def docother(self, object, name=None, maxlen=None):
|
|
|
|
|
"""Produce text documentation for a data object."""
|
|
|
|
|
repr = self.repr(object)
|
|
|
|
|
if maxlen:
|
|
|
|
|
line = name + ' = ' + repr
|
|
|
|
|
chop = maxlen - len(line)
|
|
|
|
|
if chop < 0: repr = repr[:chop] + '...'
|
|
|
|
|
line = self.bold(name) + ' = ' + repr
|
|
|
|
|
return line
|
|
|
|
|
|
2001-02-27 10:43:46 -04:00
|
|
|
|
# --------------------------------------------------------- user interfaces
|
|
|
|
|
|
|
|
|
|
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 type(sys.stdout) is not types.FileType:
|
|
|
|
|
return plainpager
|
|
|
|
|
if not sys.stdin.isatty() or not sys.stdout.isatty():
|
|
|
|
|
return plainpager
|
|
|
|
|
if os.environ.has_key('PAGER'):
|
|
|
|
|
return lambda a: pipepager(a, os.environ['PAGER'])
|
2001-03-02 01:54:35 -04:00
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
|
return lambda a: tempfilepager(a, 'more <')
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if hasattr(os, 'system') and os.system('less 2>/dev/null') == 0:
|
|
|
|
|
return lambda a: pipepager(a, '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)
|
|
|
|
|
pipe.close()
|
|
|
|
|
except IOError:
|
|
|
|
|
# Ignore broken pipes caused by quitting the pager program.
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
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:
|
2001-03-02 01:54:35 -04:00
|
|
|
|
os.system(cmd + ' ' + filename)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
finally:
|
|
|
|
|
os.unlink(filename)
|
|
|
|
|
|
|
|
|
|
def plain(text):
|
|
|
|
|
"""Remove boldface formatting from text."""
|
|
|
|
|
return re.sub('.\b', '', text)
|
|
|
|
|
|
|
|
|
|
def ttypager(text):
|
|
|
|
|
"""Page through text on a text terminal."""
|
|
|
|
|
lines = split(plain(text), '\n')
|
|
|
|
|
try:
|
|
|
|
|
import tty
|
|
|
|
|
fd = sys.stdin.fileno()
|
|
|
|
|
old = tty.tcgetattr(fd)
|
|
|
|
|
tty.setcbreak(fd)
|
|
|
|
|
getchar = lambda: sys.stdin.read(1)
|
2001-02-27 19:36:29 -04:00
|
|
|
|
except (ImportError, AttributeError):
|
2001-02-27 10:43:46 -04:00
|
|
|
|
tty = None
|
|
|
|
|
getchar = lambda: sys.stdin.readline()[:-1][:1]
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
r = inc = os.environ.get('LINES', 25) - 1
|
|
|
|
|
sys.stdout.write(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' + join(lines[r:r+inc], '\n') + '\n')
|
|
|
|
|
r = r + inc
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
if tty:
|
|
|
|
|
tty.tcsetattr(fd, tty.TCSAFLUSH, old)
|
|
|
|
|
|
|
|
|
|
def plainpager(text):
|
|
|
|
|
"""Simply print unformatted text. This is the ultimate fallback."""
|
|
|
|
|
sys.stdout.write(plain(text))
|
|
|
|
|
|
|
|
|
|
def describe(thing):
|
2001-03-23 09:17:50 -04:00
|
|
|
|
"""Produce a short description of the given thing."""
|
2001-02-27 10:43:46 -04:00
|
|
|
|
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__
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if type(thing) is types.InstanceType:
|
|
|
|
|
return 'instance of ' + thing.__class__.__name__
|
|
|
|
|
return type(thing).__name__
|
|
|
|
|
|
2001-03-23 09:17:50 -04:00
|
|
|
|
def freshimport(name, cache={}):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
"""Import a module, reloading it if the source file has changed."""
|
2001-03-23 09:17:50 -04:00
|
|
|
|
topmodule = __import__(name)
|
|
|
|
|
module = None
|
|
|
|
|
for component in split(name, '.'):
|
|
|
|
|
if module == None:
|
|
|
|
|
module = topmodule
|
|
|
|
|
path = split(name, '.')[0]
|
|
|
|
|
else:
|
|
|
|
|
module = getattr(module, component)
|
|
|
|
|
path = path + '.' + component
|
|
|
|
|
if hasattr(module, '__file__'):
|
|
|
|
|
file = module.__file__
|
|
|
|
|
if os.path.exists(file):
|
|
|
|
|
info = (file, os.path.getmtime(file), os.path.getsize(file))
|
|
|
|
|
if cache.has_key(path) and cache[path] != info:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
module = reload(module)
|
2001-03-23 09:17:50 -04:00
|
|
|
|
file = module.__file__
|
|
|
|
|
if os.path.exists(file):
|
|
|
|
|
info = (file, os.path.getmtime(file), os.path.getsize(file))
|
|
|
|
|
cache[path] = info
|
2001-03-22 20:12:53 -04:00
|
|
|
|
return module
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def locate(path):
|
|
|
|
|
"""Locate an object by name (or dotted path), importing as necessary."""
|
|
|
|
|
if not path: # special case: imp.find_module('') strangely succeeds
|
2001-03-23 09:17:50 -04:00
|
|
|
|
return None
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if type(path) is not types.StringType:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
return path
|
2001-02-27 10:43:46 -04:00
|
|
|
|
parts = split(path, '.')
|
2001-03-23 09:17:50 -04:00
|
|
|
|
n = len(parts)
|
|
|
|
|
while n > 0:
|
2001-02-27 10:43:46 -04:00
|
|
|
|
path = join(parts[:n], '.')
|
|
|
|
|
try:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
module = freshimport(path)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
except:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
# Did the error occur before or after the module was found?
|
|
|
|
|
(exc, value, tb) = info = sys.exc_info()
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if sys.modules.has_key(path):
|
2001-03-23 09:17:50 -04:00
|
|
|
|
# An error occured while executing the imported module.
|
|
|
|
|
raise ErrorDuringImport(sys.modules[path].__file__, info)
|
|
|
|
|
elif exc is SyntaxError:
|
|
|
|
|
# A SyntaxError occurred before we could execute the module.
|
|
|
|
|
raise ErrorDuringImport(value.filename, info)
|
|
|
|
|
elif exc is ImportError and \
|
|
|
|
|
split(lower(str(value)))[:2] == ['no', 'module']:
|
|
|
|
|
# The module was not found.
|
|
|
|
|
n = n - 1
|
|
|
|
|
continue
|
2001-02-27 10:43:46 -04:00
|
|
|
|
else:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
# Some other error occurred before executing the module.
|
|
|
|
|
raise DocImportError(filename, sys.exc_info())
|
2001-02-27 10:43:46 -04:00
|
|
|
|
try:
|
|
|
|
|
x = module
|
2001-03-23 09:17:50 -04:00
|
|
|
|
for p in parts[n:]:
|
2001-02-27 10:43:46 -04:00
|
|
|
|
x = getattr(x, p)
|
2001-03-23 09:17:50 -04:00
|
|
|
|
return x
|
2001-02-27 10:43:46 -04:00
|
|
|
|
except AttributeError:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
n = n - 1
|
2001-02-27 10:43:46 -04:00
|
|
|
|
continue
|
2001-03-01 09:55:20 -04:00
|
|
|
|
if hasattr(__builtins__, path):
|
2001-03-23 09:17:50 -04:00
|
|
|
|
return getattr(__builtins__, path)
|
|
|
|
|
return None
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
# --------------------------------------- interactive interpreter interface
|
|
|
|
|
|
|
|
|
|
text = TextDoc()
|
|
|
|
|
html = HTMLDoc()
|
|
|
|
|
|
|
|
|
|
def doc(thing):
|
|
|
|
|
"""Display documentation on an object (for interactive use)."""
|
|
|
|
|
if type(thing) is type(""):
|
|
|
|
|
try:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
object = locate(thing)
|
|
|
|
|
except ErrorDuringImport, value:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
print value
|
2001-02-27 10:43:46 -04:00
|
|
|
|
return
|
2001-03-23 09:17:50 -04:00
|
|
|
|
if object:
|
|
|
|
|
thing = object
|
2001-02-27 10:43:46 -04:00
|
|
|
|
else:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
print 'no Python documentation found for %s' % repr(thing)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
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 + text.document(thing))
|
|
|
|
|
|
|
|
|
|
def writedoc(key):
|
|
|
|
|
"""Write HTML documentation to a file in the current directory."""
|
2001-03-22 20:12:53 -04:00
|
|
|
|
try:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
object = locate(key)
|
|
|
|
|
except ErrorDuringImport, value:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
print value
|
|
|
|
|
else:
|
|
|
|
|
if object:
|
|
|
|
|
page = html.page('Python: ' + describe(object),
|
|
|
|
|
html.document(object, object.__name__))
|
|
|
|
|
file = open(key + '.html', 'w')
|
|
|
|
|
file.write(page)
|
|
|
|
|
file.close()
|
|
|
|
|
print 'wrote', key + '.html'
|
|
|
|
|
else:
|
|
|
|
|
print 'no Python documentation found for %s' % repr(key)
|
|
|
|
|
|
|
|
|
|
def writedocs(dir, pkgpath='', done={}):
|
|
|
|
|
"""Write out HTML documentation for all modules in a directory tree."""
|
|
|
|
|
for file in os.listdir(dir):
|
|
|
|
|
path = os.path.join(dir, file)
|
|
|
|
|
if ispackage(path):
|
|
|
|
|
writedocs(path, pkgpath + file + '.')
|
|
|
|
|
elif os.path.isfile(path):
|
|
|
|
|
modname = modulename(path)
|
|
|
|
|
if modname:
|
|
|
|
|
modname = pkgpath + modname
|
|
|
|
|
if not done.has_key(modname):
|
|
|
|
|
done[modname] = 1
|
|
|
|
|
writedoc(modname)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
class Helper:
|
|
|
|
|
def __repr__(self):
|
2001-03-22 20:12:53 -04:00
|
|
|
|
return '''To get help on a Python object, call help(object).
|
2001-02-27 10:43:46 -04:00
|
|
|
|
To get help on a module or package, either import it before calling
|
2001-03-22 20:12:53 -04:00
|
|
|
|
help(module) or call help('modulename').'''
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def __call__(self, *args):
|
|
|
|
|
if args:
|
|
|
|
|
doc(args[0])
|
|
|
|
|
else:
|
|
|
|
|
print repr(self)
|
|
|
|
|
|
|
|
|
|
help = Helper()
|
|
|
|
|
|
|
|
|
|
def man(key):
|
|
|
|
|
"""Display documentation on an object in a form similar to man(1)."""
|
2001-03-23 09:17:50 -04:00
|
|
|
|
object = locate(key)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if object:
|
|
|
|
|
title = 'Python Library Documentation: ' + describe(object)
|
2001-03-23 09:17:50 -04:00
|
|
|
|
lastdot = rfind(key, '.')
|
|
|
|
|
if lastdot > 0: title = title + ' in ' + key[:lastdot]
|
2001-03-22 20:12:53 -04:00
|
|
|
|
pager('\n' + title + '\n\n' + text.document(object, key))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
found = 1
|
|
|
|
|
else:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
print 'no Python documentation found for %s' % repr(key)
|
2001-03-01 09:55:20 -04:00
|
|
|
|
|
|
|
|
|
class Scanner:
|
|
|
|
|
"""A generic tree iterator."""
|
|
|
|
|
def __init__(self, roots, children, recurse):
|
|
|
|
|
self.roots = roots[:]
|
|
|
|
|
self.state = []
|
|
|
|
|
self.children = children
|
|
|
|
|
self.recurse = recurse
|
|
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
|
if not self.state:
|
|
|
|
|
if not self.roots:
|
|
|
|
|
return None
|
|
|
|
|
root = self.roots.pop(0)
|
|
|
|
|
self.state = [(root, self.children(root))]
|
|
|
|
|
node, children = self.state[-1]
|
|
|
|
|
if not children:
|
|
|
|
|
self.state.pop()
|
|
|
|
|
return self.next()
|
|
|
|
|
child = children.pop(0)
|
|
|
|
|
if self.recurse(child):
|
|
|
|
|
self.state.append((child, self.children(child)))
|
|
|
|
|
return child
|
|
|
|
|
|
|
|
|
|
class ModuleScanner(Scanner):
|
|
|
|
|
"""An interruptible scanner that searches module synopses."""
|
|
|
|
|
def __init__(self):
|
|
|
|
|
roots = map(lambda dir: (dir, ''), pathdirs())
|
|
|
|
|
Scanner.__init__(self, roots, self.submodules, self.ispackage)
|
|
|
|
|
|
|
|
|
|
def submodules(self, (dir, package)):
|
|
|
|
|
children = []
|
|
|
|
|
for file in os.listdir(dir):
|
|
|
|
|
path = os.path.join(dir, file)
|
2001-03-16 04:29:48 -04:00
|
|
|
|
if ispackage(path):
|
2001-03-01 09:55:20 -04:00
|
|
|
|
children.append((path, package + (package and '.') + file))
|
|
|
|
|
else:
|
|
|
|
|
children.append((path, package))
|
|
|
|
|
children.sort()
|
|
|
|
|
return children
|
|
|
|
|
|
|
|
|
|
def ispackage(self, (dir, package)):
|
|
|
|
|
return ispackage(dir)
|
|
|
|
|
|
|
|
|
|
def run(self, key, callback, completer=None):
|
|
|
|
|
self.quit = 0
|
|
|
|
|
seen = {}
|
|
|
|
|
|
|
|
|
|
for modname in sys.builtin_module_names:
|
2001-03-01 22:45:08 -04:00
|
|
|
|
if modname != '__main__':
|
|
|
|
|
seen[modname] = 1
|
2001-03-23 09:17:50 -04:00
|
|
|
|
desc = split(freshimport(modname).__doc__ or '', '\n')[0]
|
2001-03-01 22:45:08 -04:00
|
|
|
|
if find(lower(modname + ' - ' + desc), lower(key)) >= 0:
|
|
|
|
|
callback(None, modname, desc)
|
2001-03-01 09:55:20 -04:00
|
|
|
|
|
|
|
|
|
while not self.quit:
|
|
|
|
|
node = self.next()
|
|
|
|
|
if not node: break
|
|
|
|
|
path, package = node
|
|
|
|
|
modname = modulename(path)
|
|
|
|
|
if os.path.isfile(path) and modname:
|
|
|
|
|
modname = package + (package and '.') + modname
|
|
|
|
|
if not seen.has_key(modname):
|
|
|
|
|
seen[modname] = 1
|
|
|
|
|
desc = synopsis(path) or ''
|
|
|
|
|
if find(lower(modname + ' - ' + desc), lower(key)) >= 0:
|
|
|
|
|
callback(path, modname, desc)
|
|
|
|
|
if completer: completer()
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def apropos(key):
|
|
|
|
|
"""Print all the one-line module summaries that contain a substring."""
|
2001-03-01 09:55:20 -04:00
|
|
|
|
def callback(path, modname, desc):
|
|
|
|
|
if modname[-9:] == '.__init__':
|
|
|
|
|
modname = modname[:-9] + ' (package)'
|
|
|
|
|
print modname, '-', desc or '(no description)'
|
|
|
|
|
ModuleScanner().run(key, callback)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
# --------------------------------------------------- web browser interface
|
|
|
|
|
|
2001-03-01 09:55:20 -04:00
|
|
|
|
def serve(port, callback=None):
|
|
|
|
|
import BaseHTTPServer, mimetools, select
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
# Patch up mimetools.Message so it doesn't break if rfc822 is reloaded.
|
|
|
|
|
class Message(mimetools.Message):
|
|
|
|
|
def __init__(self, fp, seekable=1):
|
|
|
|
|
Message = self.__class__
|
|
|
|
|
Message.__bases__[0].__bases__[0].__init__(self, fp, seekable)
|
|
|
|
|
self.encodingheader = self.getheader('content-transfer-encoding')
|
|
|
|
|
self.typeheader = self.getheader('content-type')
|
|
|
|
|
self.parsetype()
|
|
|
|
|
self.parseplist()
|
|
|
|
|
|
|
|
|
|
class DocHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|
|
|
|
def send_document(self, title, contents):
|
2001-03-01 09:55:20 -04:00
|
|
|
|
try:
|
|
|
|
|
self.send_response(200)
|
|
|
|
|
self.send_header('Content-Type', 'text/html')
|
|
|
|
|
self.end_headers()
|
2001-03-22 20:12:53 -04:00
|
|
|
|
self.wfile.write(html.page(title, contents))
|
2001-03-01 09:55:20 -04:00
|
|
|
|
except IOError: pass
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
|
path = self.path
|
|
|
|
|
if path[-5:] == '.html': path = path[:-5]
|
|
|
|
|
if path[:1] == '/': path = path[1:]
|
|
|
|
|
if path and path != '.':
|
|
|
|
|
try:
|
2001-03-23 09:17:50 -04:00
|
|
|
|
obj = locate(path)
|
|
|
|
|
except ErrorDuringImport, value:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
self.send_document(path, html.escape(str(value)))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
return
|
2001-03-23 09:17:50 -04:00
|
|
|
|
if obj:
|
|
|
|
|
self.send_document(describe(obj), html.document(obj, path))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
else:
|
|
|
|
|
self.send_document(path,
|
2001-03-22 20:12:53 -04:00
|
|
|
|
'no Python documentation found for %s' % repr(path))
|
2001-02-27 10:43:46 -04:00
|
|
|
|
else:
|
|
|
|
|
heading = html.heading(
|
2001-03-01 09:55:20 -04:00
|
|
|
|
'<big><big><strong>Python: Index of Modules</strong></big></big>',
|
|
|
|
|
'#ffffff', '#7799ee')
|
2001-03-01 22:45:08 -04:00
|
|
|
|
def bltinlink(name):
|
|
|
|
|
return '<a href="%s.html">%s</a>' % (name, name)
|
2001-03-23 09:17:50 -04:00
|
|
|
|
names = filter(lambda x: x != '__main__',
|
|
|
|
|
sys.builtin_module_names)
|
2001-03-01 22:45:08 -04:00
|
|
|
|
contents = html.multicolumn(names, bltinlink)
|
|
|
|
|
indices = ['<p>' + html.bigsection(
|
|
|
|
|
'Built-in Modules', '#ffffff', '#ee77aa', contents)]
|
|
|
|
|
|
2001-02-27 10:43:46 -04:00
|
|
|
|
seen = {}
|
|
|
|
|
for dir in pathdirs():
|
|
|
|
|
indices.append(html.index(dir, seen))
|
2001-03-22 20:12:53 -04:00
|
|
|
|
contents = heading + join(indices) + '''<p align=right>
|
2001-03-01 09:55:20 -04:00
|
|
|
|
<small><small><font color="#909090" face="helvetica, arial"><strong>
|
2001-03-22 20:12:53 -04:00
|
|
|
|
pydoc</strong> by Ka-Ping Yee <ping@lfw.org></font></small></small>'''
|
2001-03-01 09:55:20 -04:00
|
|
|
|
self.send_document('Index of Modules', contents)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def log_message(self, *args): pass
|
|
|
|
|
|
|
|
|
|
class DocServer(BaseHTTPServer.HTTPServer):
|
2001-03-01 09:55:20 -04:00
|
|
|
|
def __init__(self, port, callback):
|
2001-03-02 01:58:17 -04:00
|
|
|
|
host = (sys.platform == 'mac') and '127.0.0.1' or 'localhost'
|
2001-03-22 20:12:53 -04:00
|
|
|
|
self.address = ('', port)
|
2001-03-02 01:58:17 -04:00
|
|
|
|
self.url = 'http://%s:%d/' % (host, port)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
self.callback = callback
|
2001-03-01 09:55:20 -04:00
|
|
|
|
self.base.__init__(self, self.address, self.handler)
|
|
|
|
|
|
|
|
|
|
def serve_until_quit(self):
|
|
|
|
|
import select
|
|
|
|
|
self.quit = 0
|
|
|
|
|
while not self.quit:
|
|
|
|
|
rd, wr, ex = select.select([self.socket.fileno()], [], [], 1)
|
|
|
|
|
if rd: self.handle_request()
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
def server_activate(self):
|
|
|
|
|
self.base.server_activate(self)
|
2001-03-01 09:55:20 -04:00
|
|
|
|
if self.callback: self.callback(self)
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
DocServer.base = BaseHTTPServer.HTTPServer
|
|
|
|
|
DocServer.handler = DocHandler
|
|
|
|
|
DocHandler.MessageClass = Message
|
|
|
|
|
try:
|
2001-03-01 09:55:20 -04:00
|
|
|
|
DocServer(port, callback).serve_until_quit()
|
|
|
|
|
except (KeyboardInterrupt, select.error):
|
|
|
|
|
pass
|
|
|
|
|
print 'server stopped'
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------- graphical interface
|
|
|
|
|
|
|
|
|
|
def gui():
|
|
|
|
|
"""Graphical interface (starts web server and pops up a control window)."""
|
|
|
|
|
class GUI:
|
|
|
|
|
def __init__(self, window, port=7464):
|
|
|
|
|
self.window = window
|
|
|
|
|
self.server = None
|
|
|
|
|
self.scanner = None
|
|
|
|
|
|
|
|
|
|
import Tkinter
|
|
|
|
|
self.server_frm = Tkinter.Frame(window)
|
|
|
|
|
self.title_lbl = Tkinter.Label(self.server_frm,
|
|
|
|
|
text='Starting server...\n ')
|
|
|
|
|
self.open_btn = Tkinter.Button(self.server_frm,
|
|
|
|
|
text='open browser', command=self.open, state='disabled')
|
|
|
|
|
self.quit_btn = Tkinter.Button(self.server_frm,
|
|
|
|
|
text='quit serving', command=self.quit, state='disabled')
|
|
|
|
|
|
|
|
|
|
self.search_frm = Tkinter.Frame(window)
|
|
|
|
|
self.search_lbl = Tkinter.Label(self.search_frm, text='Search for')
|
|
|
|
|
self.search_ent = Tkinter.Entry(self.search_frm)
|
|
|
|
|
self.search_ent.bind('<Return>', self.search)
|
|
|
|
|
self.stop_btn = Tkinter.Button(self.search_frm,
|
|
|
|
|
text='stop', pady=0, command=self.stop, state='disabled')
|
|
|
|
|
if sys.platform == 'win32':
|
2001-03-02 01:54:35 -04:00
|
|
|
|
# Trying to hide and show this button crashes under Windows.
|
2001-03-01 09:55:20 -04:00
|
|
|
|
self.stop_btn.pack(side='right')
|
|
|
|
|
|
|
|
|
|
self.window.title('pydoc')
|
|
|
|
|
self.window.protocol('WM_DELETE_WINDOW', self.quit)
|
|
|
|
|
self.title_lbl.pack(side='top', fill='x')
|
|
|
|
|
self.open_btn.pack(side='left', fill='x', expand=1)
|
|
|
|
|
self.quit_btn.pack(side='right', fill='x', expand=1)
|
|
|
|
|
self.server_frm.pack(side='top', fill='x')
|
|
|
|
|
|
|
|
|
|
self.search_lbl.pack(side='left')
|
|
|
|
|
self.search_ent.pack(side='right', fill='x', expand=1)
|
|
|
|
|
self.search_frm.pack(side='top', fill='x')
|
|
|
|
|
self.search_ent.focus_set()
|
|
|
|
|
|
2001-03-02 01:54:35 -04:00
|
|
|
|
font = ('helvetica', sys.platform == 'win32' and 8 or 10)
|
2001-03-01 22:45:08 -04:00
|
|
|
|
self.result_lst = Tkinter.Listbox(window, font=font, height=6)
|
2001-03-01 09:55:20 -04:00
|
|
|
|
self.result_lst.bind('<Button-1>', self.select)
|
|
|
|
|
self.result_lst.bind('<Double-Button-1>', self.goto)
|
|
|
|
|
self.result_scr = Tkinter.Scrollbar(window,
|
|
|
|
|
orient='vertical', command=self.result_lst.yview)
|
|
|
|
|
self.result_lst.config(yscrollcommand=self.result_scr.set)
|
|
|
|
|
|
|
|
|
|
self.result_frm = Tkinter.Frame(window)
|
|
|
|
|
self.goto_btn = Tkinter.Button(self.result_frm,
|
|
|
|
|
text='go to selected', command=self.goto)
|
|
|
|
|
self.hide_btn = Tkinter.Button(self.result_frm,
|
|
|
|
|
text='hide results', command=self.hide)
|
|
|
|
|
self.goto_btn.pack(side='left', fill='x', expand=1)
|
|
|
|
|
self.hide_btn.pack(side='right', fill='x', expand=1)
|
|
|
|
|
|
|
|
|
|
self.window.update()
|
|
|
|
|
self.minwidth = self.window.winfo_width()
|
|
|
|
|
self.minheight = self.window.winfo_height()
|
|
|
|
|
self.bigminheight = (self.server_frm.winfo_reqheight() +
|
|
|
|
|
self.search_frm.winfo_reqheight() +
|
|
|
|
|
self.result_lst.winfo_reqheight() +
|
|
|
|
|
self.result_frm.winfo_reqheight())
|
|
|
|
|
self.bigwidth, self.bigheight = self.minwidth, self.bigminheight
|
|
|
|
|
self.expanded = 0
|
|
|
|
|
self.window.wm_geometry('%dx%d' % (self.minwidth, self.minheight))
|
|
|
|
|
self.window.wm_minsize(self.minwidth, self.minheight)
|
|
|
|
|
|
|
|
|
|
import threading
|
|
|
|
|
threading.Thread(target=serve, args=(port, self.ready)).start()
|
|
|
|
|
|
|
|
|
|
def ready(self, server):
|
|
|
|
|
self.server = server
|
|
|
|
|
self.title_lbl.config(
|
|
|
|
|
text='Python documentation server at\n' + server.url)
|
|
|
|
|
self.open_btn.config(state='normal')
|
|
|
|
|
self.quit_btn.config(state='normal')
|
|
|
|
|
|
2001-03-01 22:45:08 -04:00
|
|
|
|
def open(self, event=None, url=None):
|
|
|
|
|
url = url or self.server.url
|
|
|
|
|
try:
|
|
|
|
|
import webbrowser
|
|
|
|
|
webbrowser.open(url)
|
|
|
|
|
except ImportError: # pre-webbrowser.py compatibility
|
2001-03-02 01:54:35 -04:00
|
|
|
|
if sys.platform == 'win32':
|
2001-03-01 22:45:08 -04:00
|
|
|
|
os.system('start "%s"' % url)
|
|
|
|
|
elif sys.platform == 'mac':
|
|
|
|
|
try:
|
|
|
|
|
import ic
|
|
|
|
|
ic.launchurl(url)
|
|
|
|
|
except ImportError: pass
|
|
|
|
|
else:
|
|
|
|
|
rc = os.system('netscape -remote "openURL(%s)" &' % url)
|
|
|
|
|
if rc: os.system('netscape "%s" &' % url)
|
2001-03-01 09:55:20 -04:00
|
|
|
|
|
|
|
|
|
def quit(self, event=None):
|
|
|
|
|
if self.server:
|
|
|
|
|
self.server.quit = 1
|
|
|
|
|
self.window.quit()
|
|
|
|
|
|
|
|
|
|
def search(self, event=None):
|
|
|
|
|
key = self.search_ent.get()
|
|
|
|
|
self.stop_btn.pack(side='right')
|
|
|
|
|
self.stop_btn.config(state='normal')
|
|
|
|
|
self.search_lbl.config(text='Searching for "%s"...' % key)
|
|
|
|
|
self.search_ent.forget()
|
|
|
|
|
self.search_lbl.pack(side='left')
|
|
|
|
|
self.result_lst.delete(0, 'end')
|
|
|
|
|
self.goto_btn.config(state='disabled')
|
|
|
|
|
self.expand()
|
|
|
|
|
|
|
|
|
|
import threading
|
|
|
|
|
if self.scanner:
|
|
|
|
|
self.scanner.quit = 1
|
|
|
|
|
self.scanner = ModuleScanner()
|
|
|
|
|
threading.Thread(target=self.scanner.run,
|
|
|
|
|
args=(key, self.update, self.done)).start()
|
|
|
|
|
|
|
|
|
|
def update(self, path, modname, desc):
|
|
|
|
|
if modname[-9:] == '.__init__':
|
|
|
|
|
modname = modname[:-9] + ' (package)'
|
|
|
|
|
self.result_lst.insert('end',
|
|
|
|
|
modname + ' - ' + (desc or '(no description)'))
|
|
|
|
|
|
|
|
|
|
def stop(self, event=None):
|
|
|
|
|
if self.scanner:
|
|
|
|
|
self.scanner.quit = 1
|
|
|
|
|
self.scanner = None
|
|
|
|
|
|
|
|
|
|
def done(self):
|
|
|
|
|
self.scanner = None
|
|
|
|
|
self.search_lbl.config(text='Search for')
|
|
|
|
|
self.search_lbl.pack(side='left')
|
|
|
|
|
self.search_ent.pack(side='right', fill='x', expand=1)
|
|
|
|
|
if sys.platform != 'win32': self.stop_btn.forget()
|
|
|
|
|
self.stop_btn.config(state='disabled')
|
|
|
|
|
|
|
|
|
|
def select(self, event=None):
|
|
|
|
|
self.goto_btn.config(state='normal')
|
|
|
|
|
|
|
|
|
|
def goto(self, event=None):
|
|
|
|
|
selection = self.result_lst.curselection()
|
|
|
|
|
if selection:
|
|
|
|
|
modname = split(self.result_lst.get(selection[0]))[0]
|
2001-03-01 22:45:08 -04:00
|
|
|
|
self.open(url=self.server.url + modname + '.html')
|
2001-03-01 09:55:20 -04:00
|
|
|
|
|
|
|
|
|
def collapse(self):
|
|
|
|
|
if not self.expanded: return
|
|
|
|
|
self.result_frm.forget()
|
|
|
|
|
self.result_scr.forget()
|
|
|
|
|
self.result_lst.forget()
|
|
|
|
|
self.bigwidth = self.window.winfo_width()
|
|
|
|
|
self.bigheight = self.window.winfo_height()
|
|
|
|
|
self.window.wm_geometry('%dx%d' % (self.minwidth, self.minheight))
|
|
|
|
|
self.window.wm_minsize(self.minwidth, self.minheight)
|
|
|
|
|
self.expanded = 0
|
|
|
|
|
|
|
|
|
|
def expand(self):
|
|
|
|
|
if self.expanded: return
|
|
|
|
|
self.result_frm.pack(side='bottom', fill='x')
|
|
|
|
|
self.result_scr.pack(side='right', fill='y')
|
|
|
|
|
self.result_lst.pack(side='top', fill='both', expand=1)
|
|
|
|
|
self.window.wm_geometry('%dx%d' % (self.bigwidth, self.bigheight))
|
|
|
|
|
self.window.wm_minsize(self.minwidth, self.bigminheight)
|
|
|
|
|
self.expanded = 1
|
|
|
|
|
|
|
|
|
|
def hide(self, event=None):
|
|
|
|
|
self.stop()
|
|
|
|
|
self.collapse()
|
|
|
|
|
|
|
|
|
|
import Tkinter
|
|
|
|
|
try:
|
|
|
|
|
gui = GUI(Tkinter.Tk())
|
|
|
|
|
Tkinter.mainloop()
|
2001-02-27 10:43:46 -04:00
|
|
|
|
except KeyboardInterrupt:
|
2001-03-01 09:55:20 -04:00
|
|
|
|
pass
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
# -------------------------------------------------- command-line interface
|
|
|
|
|
|
2001-03-22 20:12:53 -04:00
|
|
|
|
def ispath(x):
|
|
|
|
|
return type(x) is types.StringType and find(x, os.sep) >= 0
|
|
|
|
|
|
2001-02-28 20:24:32 -04:00
|
|
|
|
def cli():
|
2001-03-01 09:55:20 -04:00
|
|
|
|
"""Command-line interface (looks at sys.argv to decide what to do)."""
|
2001-02-27 10:43:46 -04:00
|
|
|
|
import getopt
|
|
|
|
|
class BadUsage: pass
|
|
|
|
|
|
2001-03-23 09:17:50 -04:00
|
|
|
|
# Scripts don't get the current directory in their path by default.
|
|
|
|
|
sys.path.insert(0, '.')
|
2001-03-01 09:55:20 -04:00
|
|
|
|
|
2001-03-23 09:17:50 -04:00
|
|
|
|
try:
|
2001-03-01 09:55:20 -04:00
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 'gk:p:w')
|
2001-02-27 10:43:46 -04:00
|
|
|
|
writing = 0
|
|
|
|
|
|
|
|
|
|
for opt, val in opts:
|
2001-03-01 09:55:20 -04:00
|
|
|
|
if opt == '-g':
|
|
|
|
|
gui()
|
|
|
|
|
return
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if opt == '-k':
|
2001-03-01 09:55:20 -04:00
|
|
|
|
apropos(val)
|
|
|
|
|
return
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if opt == '-p':
|
|
|
|
|
try:
|
|
|
|
|
port = int(val)
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise BadUsage
|
2001-03-01 09:55:20 -04:00
|
|
|
|
def ready(server):
|
|
|
|
|
print 'server ready at %s' % server.url
|
|
|
|
|
serve(port, ready)
|
|
|
|
|
return
|
2001-02-27 10:43:46 -04:00
|
|
|
|
if opt == '-w':
|
|
|
|
|
writing = 1
|
2001-03-01 09:55:20 -04:00
|
|
|
|
|
|
|
|
|
if not args: raise BadUsage
|
|
|
|
|
for arg in args:
|
|
|
|
|
try:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if ispath(arg) and os.path.isfile(arg):
|
2001-03-01 09:55:20 -04:00
|
|
|
|
arg = importfile(arg)
|
2001-03-22 20:12:53 -04:00
|
|
|
|
if writing:
|
|
|
|
|
if ispath(arg) and os.path.isdir(arg):
|
|
|
|
|
writedocs(arg)
|
|
|
|
|
else:
|
|
|
|
|
writedoc(arg)
|
|
|
|
|
else:
|
|
|
|
|
man(arg)
|
2001-03-23 09:17:50 -04:00
|
|
|
|
except ErrorDuringImport, value:
|
2001-03-22 20:12:53 -04:00
|
|
|
|
print value
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
except (getopt.error, BadUsage):
|
2001-03-01 09:55:20 -04:00
|
|
|
|
cmd = sys.argv[0]
|
|
|
|
|
print """pydoc - the Python documentation tool
|
|
|
|
|
|
|
|
|
|
%s <name> ...
|
|
|
|
|
Show text documentation on something. <name> may be the name of a
|
|
|
|
|
function, module, or package, or a dotted reference to a class or
|
|
|
|
|
function within a module or module in a package. If <name> contains
|
|
|
|
|
a '%s', it is used as the path to a Python source file to document.
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
%s -k <keyword>
|
2001-03-01 09:55:20 -04:00
|
|
|
|
Search for a keyword in the synopsis lines of all available modules.
|
2001-02-27 10:43:46 -04:00
|
|
|
|
|
|
|
|
|
%s -p <port>
|
|
|
|
|
Start an HTTP server on the given port on the local machine.
|
|
|
|
|
|
2001-03-01 09:55:20 -04:00
|
|
|
|
%s -g
|
|
|
|
|
Pop up a graphical interface for serving and finding documentation.
|
|
|
|
|
|
|
|
|
|
%s -w <name> ...
|
|
|
|
|
Write out the HTML documentation for a module to a file in the current
|
|
|
|
|
directory. If <name> contains a '%s', it is treated as a filename.
|
|
|
|
|
""" % (cmd, os.sep, cmd, cmd, cmd, cmd, os.sep)
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': cli()
|