# -*- coding: Latin-1 -*-
"""Generate Python documentation in HTML or text for interactive use.
In the Python interpreter, do "from pydoc import help" to provide online
help. Calling help(thing) on a Python object documents the object.
Or, at the shell command line outside of Python:
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.
Run "pydoc -k <keyword>" to search for a keyword in the synopsis lines
of all available modules.
Run "pydoc -p <port>" to start an HTTP server on a given port on the
local machine to generate documentation web pages.
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".
Module docs for core modules are assumed to be in
http://www.python.org/doc/current/lib/
This can be overridden by setting the PYTHONDOCS environment variable
to a different URL or to a local directory containing the Library
__author__
= "Ka-Ping Yee <ping@lfw.org>"
__date__
= "26 February 2001"
__version__
= "$Revision: 1.100.2.4 $"
__credits__
= """Guido van Rossum, for an excellent programming language.
Tommy Burnette, the original creator of manpy.
Paul Prescod, for all his work on onlinehelp.
Richard Chamberlain, for the first implementation of textdoc.
# Known bugs that can't be fixed here:
# - imp.load_module() cannot be prevented from clobbering existing
# loaded modules, so calling synopsis() on a binary module file
# changes the contents of any existing module with the same name.
# - If the __file__ attribute on a module is a relative path and
# the current directory is changed with os.chdir(), an incorrect
# path will be displayed.
import sys
, imp
, os
, re
, types
, inspect
, __builtin__
from string
import expandtabs
, find
, join
, lower
, split
, strip
, rfind
, rstrip
from collections
import deque
# --------------------------------------------------------- common routines
"""Convert sys.path into a list of absolute, existing, unique paths."""
dir = os
.path
.abspath(dir or '.')
normdir
= os
.path
.normcase(dir)
if normdir
not in normdirs
and os
.path
.isdir(dir):
"""Get the doc string or comments for an object."""
result
= inspect
.getdoc(object) or inspect
.getcomments(object)
return result
and re
.sub('^ *\n', '', rstrip(result
)) or ''
"""Split a doc string into a synopsis line (if any) and the rest."""
lines
= split(strip(doc
), '\n')
elif len(lines
) >= 2 and not rstrip(lines
[1]):
return lines
[0], join(lines
[2:], '\n')
return '', join(lines
, '\n')
def classname(object, modname
):
"""Get a class name and qualify it with a module name if necessary."""
if object.__module
__ != modname
:
name
= object.__module
__ + '.' + name
"""Check if an object is of a type that probably means it's data."""
return not (inspect
.ismodule(object) or inspect
.isclass(object) or
inspect
.isroutine(object) or inspect
.isframe(object) or
inspect
.istraceback(object) or inspect
.iscode(object))
def replace(text
, *pairs
):
"""Do a series of global replacements on a string."""
text
= join(split(text
, pairs
[0]), pairs
[1])
"""Omit part of a string if needed to make it fit in a maximum length."""
pre
= max(0, (maxlen
-3)//2)
post
= max(0, maxlen
-3-pre
)
return text
[:pre
] + '...' + text
[len(text
)-post
:]
_re_stripid
= re
.compile(r
' at 0x[0-9a-f]{6,16}(>+)$', re
.IGNORECASE
)
"""Remove the hexadecimal id from a Python object representation."""
# The behaviour of %p is implementation-dependent in terms of case.
if _re_stripid
.search(repr(Exception)):
return _re_stripid
.sub(r
'\1', text
)
def _is_some_method(obj
):
return inspect
.ismethod(obj
) or inspect
.ismethoddescriptor(obj
)
for key
, value
in inspect
.getmembers(cl
, _is_some_method
):
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
)
def _split_list(s
, predicate
):
"""Split sequence s via predicate, and return pair ([true], [false]).
The return value is a 2-tuple of lists,
([x for x in s if predicate(x)],
[x for x in s if not predicate(x)])
def visiblename(name
, all
=None):
"""Decide whether to show documentation on a variable."""
# Certain special names are redundant.
if name
in ['__builtins__', '__doc__', '__file__', '__path__',
'__module__', '__name__']: return 0
# Private names are hidden, but special names are displayed.
if name
.startswith('__') and name
.endswith('__'): return 1
# only document that which the programmer exported in __all__
return not name
.startswith('_')
# ----------------------------------------------------- module manipulation
"""Guess whether a path refers to a package directory."""
for ext
in ['.py', '.pyc', '.pyo']:
if os
.path
.isfile(os
.path
.join(path
, '__init__' + ext
)):
def synopsis(filename
, cache
={}):
"""Get the one-line summary out of a module file."""
mtime
= os
.stat(filename
).st_mtime
lastupdate
, result
= cache
.get(filename
, (0, None))
info
= inspect
.getmoduleinfo(filename
)
if info
and 'b' in info
[2]: # binary modules have to be imported
try: module
= imp
.load_module('__temp__', file, filename
, info
[1:])
result
= split(module
.__doc
__ or '', '\n')[0]
del sys
.modules
['__temp__']
else: # text modules can be directly examined
while line
[:1] == '#' or not strip(line
):
if line
[:4] == 'r"""': line
= line
[1:]
if line
[-1:] == '\\': line
= line
[:-1]
result
= strip(split(line
, '"""')[0])
cache
[filename
] = (mtime
, result
)
class ErrorDuringImport(Exception):
"""Errors that occurred while trying to import something to document it."""
def __init__(self
, filename
, (exc
, value
, tb
)):
if type(exc
) is types
.ClassType
:
return 'problem in %s - %s: %s' % (self
.filename
, exc
, self
.value
)
"""Import a Python source file or compiled file given its path."""
if file.read(len(magic
)) == magic
:
filename
= os
.path
.basename(path
)
name
, ext
= os
.path
.splitext(filename
)
module
= imp
.load_module(name
, file, path
, (ext
, 'r', kind
))
raise ErrorDuringImport(path
, sys
.exc_info())
def safeimport(path
, forceload
=0, cache
={}):
"""Import a module; handle errors; return None if the module isn't found.
If the module *is* found but an exception occurs, it's wrapped in an
ErrorDuringImport exception and reraised. Unlike __import__, if a
package path is specified, the module at the end of the path is returned,
not the package at the beginning. If the optional 'forceload' argument
is 1, we reload the module from disk (unless it's a dynamic extension)."""
if forceload
and path
in sys
.modules
:
# This is the only way to be sure. Checking the mtime of the file
# isn't good enough (e.g. what if the module contains a class that
# inherits from another module that has changed?).
if path
not in sys
.builtin_module_names
:
# Python never loads a dynamic extension a second time from the
# same path, even if the file is changed or missing. Deleting
# the entry in sys.modules doesn't help for dynamic extensions,
# so we're not even going to try to keep them up to date.
info
= inspect
.getmoduleinfo(sys
.modules
[path
].__file
__)
if info
[3] != imp
.C_EXTENSION
:
cache
[path
] = sys
.modules
[path
] # prevent module from clearing
module
= __import__(path
)
# Did the error occur before or after the module was found?
(exc
, value
, tb
) = info
= sys
.exc_info()
# An error occured while executing the imported module.
raise ErrorDuringImport(sys
.modules
[path
].__file
__, info
)
# 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.
# Some other error occurred during the importing process.
raise ErrorDuringImport(path
, sys
.exc_info())
for part
in split(path
, '.')[1:]:
try: module
= getattr(module
, part
)
except AttributeError: return None
# ---------------------------------------------------- formatter base class
def document(self
, object, name
=None, *args
):
"""Generate documentation for an object."""
args
= (object, name
) + args
# 'try' clause is to attempt to handle the possibility that inspect
# identifies something in a way that pydoc itself has issues handling;
# think 'super' and how it is a descriptor (which raises the exception
# by lacking a __name__ attribute) and an instance.
if inspect
.ismodule(object): return self
.docmodule(*args
)
if inspect
.isclass(object): return self
.docclass(*args
)
if inspect
.isroutine(object): return self
.docroutine(*args
)
if isinstance(object, property): return self
.docproperty(*args
)
return 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
__)
docmodule
= docclass
= docroutine
= docother
= fail
def getdocloc(self
, object):
"""Return the location of module docs or None"""
file = inspect
.getabsfile(object)
docloc
= os
.environ
.get("PYTHONDOCS",
"http://www.python.org/doc/current/lib")
basedir
= os
.path
.join(sys
.exec_prefix
, "lib",
"python"+sys
.version
[0:3])
if (isinstance(object, type(os
)) and
(object.__name
__ in ('errno', 'exceptions', 'gc', 'imp',
'marshal', 'posix', 'signal', 'sys',
'thread', 'zipimport') or
(file.startswith(basedir
) and
not file.startswith(os
.path
.join(basedir
, 'site-packages'))))):
htmlfile
= "module-%s.html" % object.__name
__
if docloc
.startswith("http://"):
docloc
= "%s/%s" % (docloc
.rstrip("/"), htmlfile
)
docloc
= os
.path
.join(docloc
, htmlfile
)
# -------------------------------------------- HTML documentation generator
"""Class for safely making an HTML representation of a Python object."""
self
.maxlist
= self
.maxtuple
= 20
self
.maxstring
= self
.maxother
= 100
return replace(text
, '&', '&', '<', '<', '>', '>')
return Repr
.repr(self
, object)
def repr1(self
, x
, level
):
if hasattr(type(x
), '__name__'):
methodname
= 'repr_' + join(split(type(x
).__name
__), '_')
if hasattr(self
, methodname
):
return getattr(self
, methodname
)(x
, level
)
return self
.escape(cram(stripid(repr(x
)), self
.maxother
))
def repr_string(self
, x
, level
):
test
= cram(x
, self
.maxstring
)
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\'"]|\\[0-9]..|\\x..|\\u....)+)',
r'<font color="#c040c0">\1</font>',
def repr_instance(self
, x
, level
):
return self
.escape(cram(stripid(repr(x
)), self
.maxstring
))
return self
.escape('<%s instance>' % x
.__class
__.__name
__)
repr_unicode
= repr_string
"""Formatter class for HTML documentation."""
# ------------------------------------------- HTML formatting utilities
_repr_instance
= HTMLRepr()
repr = _repr_instance
.repr
escape
= _repr_instance
.escape
def page(self
, title
, contents
):
"""Format an HTML page."""
<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: %s</title>
</head><body bgcolor="#f0f0f8">
</body></html>''' % (title
, contents
)
def heading(self
, title
, fgcol
, bgcol
, extras
=''):
"""Format a page heading."""
<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<td valign=bottom> <br>
<font color="%s" face="helvetica, arial"> <br>%s</font></td
><td align=right valign=bottom
><font color="%s" face="helvetica, arial">%s</font></td></tr></table>
''' % (bgcol
, fgcol
, title
, fgcol
, extras
or ' ')
def section(self
, title
, fgcol
, bgcol
, contents
, width
=6,
prelude
='', marginalia
=None, gap
=' '):
"""Format a section with a heading."""
marginalia
= '<tt>' + ' ' * width
+ '</tt>'
<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
<td colspan=3 valign=bottom> <br>
<font color="%s" face="helvetica, arial">%s</font></td></tr>
''' % (bgcol
, fgcol
, title
)
<tr bgcolor="%s"><td rowspan=2>%s</td>
<td colspan=2>%s</td></tr>
<tr><td>%s</td>''' % (bgcol
, marginalia
, prelude
, gap
)
<tr><td bgcolor="%s">%s</td><td>%s</td>''' % (bgcol
, marginalia
, gap
)
return result
+ '\n<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 self
.section(title
, *args
)
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."""
rows
= (len(list)+cols
-1)/cols
result
= result
+ '<td width="%d%%" valign=top>' % (100/cols
)
for i
in range(rows
*col
, rows
*col
+rows
):
result
= result
+ format(list[i
]) + '<br>\n'
result
= result
+ '</td>'
return '<table width="100%%" summary="list"><tr>%s</tr></table>' % result
def grey(self
, text
): return '<font color="#909090">%s</font>' % text
def namelink(self
, name
, *dicts
):
"""Make a link for an identifier, given name-to-URL mappings."""
return '<a href="%s">%s</a>' % (dict[name
], name
)
def classlink(self
, object, modname
):
"""Make a link for a class."""
name
, module
= object.__name
__, sys
.modules
.get(object.__module
__)
if hasattr(module
, name
) and getattr(module
, name
) is object:
return '<a href="%s.html#%s">%s</a>' % (
module
.__name
__, name
, classname(object, modname
))
return classname(object, modname
)
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."""
url
= '%s.%s.html' % (path
, name
)
text
= '<strong>%s</strong> (package)' % 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
pattern
= re
.compile(r
'\b((http|ftp)://\S+[\w/]|'
match
= pattern
.search(text
, here
)
start
, end
= match
.span()
results
.append(escape(text
[here
:start
]))
all
, scheme
, rfc
, pep
, selfdot
, name
= match
.groups()
url
= escape(all
).replace('"', '"')
results
.append('<a href="%s">%s</a>' % (url
, url
))
url
= 'http://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc
)
results
.append('<a href="%s">%s</a>' % (url
, escape(all
)))
url
= 'http://www.python.org/peps/pep-%04d.html' % int(pep
)
results
.append('<a href="%s">%s</a>' % (url
, escape(all
)))
elif text
[end
:end
+1] == '(':
results
.append(self
.namelink(name
, methods
, funcs
, classes
))
results
.append('self.<strong>%s</strong>' % name
)
results
.append(self
.namelink(name
, classes
))
results
.append(escape(text
[here
:]))
# ---------------------------------------------- type-specific routines
def formattree(self
, tree
, modname
, parent
=None):
"""Produce HTML for a class tree as given by inspect.getclasstree()."""
if type(entry
) is type(()):
result
= result
+ '<dt><font face="helvetica, arial">'
result
= result
+ self
.classlink(c
, modname
)
if bases
and bases
!= (parent
,):
parents
.append(self
.classlink(base
, modname
))
result
= result
+ '(' + join(parents
, ', ') + ')'
result
= result
+ '\n</font></dt>'
elif type(entry
) is type([]):
result
= result
+ '<dd>\n%s</dd>\n' % self
.formattree(
return '<dl>\n%s</dl>\n' % result
def docmodule(self
, object, name
=None, mod
=None, *ignored
):
"""Produce HTML documentation for a module object."""
name
= object.__name
__ # ignore the passed-in name
for i
in range(len(parts
)-1):
'<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
path
= inspect
.getabsfile(object)
if sys
.platform
== 'win32':
url
= nturl2path
.pathname2url(path
)
filelink
= '<a href="file:%s">%s</a>' % (url
, path
)
if hasattr(object, '__version__'):
version
= str(object.__version
__)
if version
[:11] == '$' + 'Revision: ' and version
[-1:] == '$':
version
= strip(version
[11:-1])
info
.append('version %s' % self
.escape(version
))
if hasattr(object, '__date__'):
info
.append(self
.escape(str(object.__date
__)))
head
= head
+ ' (%s)' % join(info
, ', ')
docloc
= self
.getdocloc(object)
docloc
= '<br><a href="%(docloc)s">Module Docs</a>' % locals()
head
, '#ffffff', '#7799ee',
'<a href=".">index</a><br>' + filelink
+ docloc
)
modules
= inspect
.getmembers(object, inspect
.ismodule
)
for key
, value
in inspect
.getmembers(object, inspect
.isclass
):
# if __all__ exists, believe it. Otherwise use old heuristic.
(inspect
.getmodule(value
) or object) is object):
if visiblename(key
, all
):
classes
.append((key
, value
))
cdict
[key
] = cdict
[value
] = '#' + key
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
:
cdict
[key
] = cdict
[base
] = modname
+ '.html#' + key
for key
, value
in inspect
.getmembers(object, inspect
.isroutine
):
# if __all__ exists, believe it. Otherwise use old heuristic.
inspect
.isbuiltin(value
) or inspect
.getmodule(value
) is object):
if visiblename(key
, all
):
funcs
.append((key
, value
))
if inspect
.isfunction(value
): fdict
[value
] = fdict
[key
]
for key
, value
in inspect
.getmembers(object, isdata
):
if visiblename(key
, all
):
data
.append((key
, value
))
doc
= self
.markup(getdoc(object), self
.preformat
, fdict
, cdict
)
doc
= doc
and '<tt>%s</tt>' % doc
result
= result
+ '<p>%s</p>\n' % doc
if hasattr(object, '__path__'):
for file in os
.listdir(object.__path
__[0]):
path
= os
.path
.join(object.__path
__[0], file)
modname
= inspect
.getmodulename(file)
if modname
!= '__init__':
if modname
and modname
not in modnames
:
modpkgs
.append((modname
, name
, 0, 0))
modpkgs
.append((file, name
, 1, 0))
contents
= self
.multicolumn(modpkgs
, self
.modpkglink
)
result
= result
+ self
.bigsection(
'Package Contents', '#ffffff', '#aa55cc', contents
)
contents
= self
.multicolumn(
modules
, lambda (key
, value
), s
=self
: s
.modulelink(value
))
result
= result
+ self
.bigsection(
'Modules', '#fffff', '#aa55cc', contents
)
classlist
= map(lambda (key
, value
): value
, classes
)
self
.formattree(inspect
.getclasstree(classlist
, 1), name
)]
for key
, value
in classes
:
contents
.append(self
.document(value
, key
, name
, fdict
, cdict
))
result
= result
+ self
.bigsection(
'Classes', '#ffffff', '#ee77aa', join(contents
))
contents
.append(self
.document(value
, key
, name
, fdict
, cdict
))
result
= result
+ self
.bigsection(
'Functions', '#ffffff', '#eeaa77', join(contents
))
contents
.append(self
.document(value
, key
))
result
= result
+ self
.bigsection(
'Data', '#ffffff', '#55aa55', join(contents
, '<br>\n'))
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
)
def docclass(self
, object, name
=None, mod
=None, funcs
={}, classes
={},
"""Produce HTML documentation for a class object."""
realname
= object.__name
__
# Cute little class to pump out a horizontal rule between sections.
# List the mro, if non-trivial.
mro
= deque(inspect
.getmro(object))
push('<dl><dt>Method resolution order:</dt>\n')
push('<dd>%s</dd>\n' % self
.classlink(base
,
def spill(msg
, attrs
, predicate
):
ok
, attrs
= _split_list(attrs
, predicate
)
for name
, kind
, homecls
, value
in ok
:
push(self
.document(getattr(object, name
), name
, mod
,
funcs
, classes
, mdict
, object))
def spillproperties(msg
, attrs
, predicate
):
ok
, attrs
= _split_list(attrs
, predicate
)
for name
, kind
, homecls
, value
in ok
:
push(self
._docproperty
(name
, value
, mod
))
def spilldata(msg
, attrs
, predicate
):
ok
, attrs
= _split_list(attrs
, predicate
)
for name
, kind
, homecls
, value
in ok
:
base
= self
.docother(getattr(object, name
), name
, mod
)
if callable(value
) or inspect
.isdatadescriptor(value
):
doc
= getattr(value
, "__doc__", None)
push('<dl><dt>%s</dl>\n' % base
)
doc
= self
.markup(getdoc(value
), self
.preformat
,
doc
= '<dd><tt>%s</tt>' % doc
push('<dl><dt>%s%s</dl>\n' % (base
, doc
))
attrs
= filter(lambda (name
, kind
, cls
, value
): visiblename(name
),
inspect
.classify_class_attrs(object))
for key
, kind
, homecls
, value
in attrs
:
mdict
[key
] = anchor
= '#' + name
+ '-' + key
value
= getattr(object, key
)
# The value may not be hashable (e.g., a data attr with
thisclass
= mro
.popleft()
attrs
, inherited
= _split_list(attrs
, lambda t
: t
[2] is thisclass
)
if thisclass
is __builtin__
.object:
elif thisclass
is object:
tag
= 'inherited from %s' % self
.classlink(thisclass
,
attrs
.sort(key
=lambda t
: t
[0])
# Pump out the attrs, segregated by kind.
attrs
= spill('Methods %s' % tag
, attrs
,
lambda t
: t
[1] == 'method')
attrs
= spill('Class methods %s' % tag
, attrs
,
lambda t
: t
[1] == 'class method')
attrs
= spill('Static methods %s' % tag
, attrs
,
lambda t
: t
[1] == 'static method')
attrs
= spillproperties('Properties %s' % tag
, attrs
,
lambda t
: t
[1] == 'property')
attrs
= spilldata('Data and other attributes %s' % tag
, attrs
,
lambda t
: t
[1] == 'data')
contents
= ''.join(contents
)
title
= '<a name="%s">class <strong>%s</strong></a>' % (
title
= '<strong>%s</strong> = <a name="%s">class %s</a>' % (
parents
.append(self
.classlink(base
, object.__module
__))
title
= title
+ '(%s)' % join(parents
, ', ')
doc
= self
.markup(getdoc(object), self
.preformat
, funcs
, classes
, mdict
)
doc
= doc
and '<tt>%s<br> </tt>' % doc
return self
.section(title
, '#000000', '#ffc8d8', contents
, 3, doc
)
def formatvalue(self
, object):
"""Format an argument default value as text."""
return self
.grey('=' + self
.repr(object))
def docroutine(self
, object, name
=None, mod
=None,
funcs
={}, classes
={}, methods
={}, cl
=None):
"""Produce HTML documentation for a function or method object."""
realname
= object.__name
__
anchor
= (cl
and cl
.__name
__ or '') + '-' + name
if inspect
.ismethod(object):
imclass
= object.im_class
note
= ' from ' + self
.classlink(imclass
, mod
)
note
= ' method of %s instance' % self
.classlink(
object.im_self
.__class
__, mod
)
note
= ' unbound %s method' % self
.classlink(imclass
,mod
)
title
= '<a name="%s"><strong>%s</strong></a>' % (anchor
, realname
)
if (cl
and realname
in cl
.__dict
__ and
cl
.__dict
__[realname
] is object):
reallink
= '<a href="#%s">%s</a>' % (
cl
.__name
__ + '-' + realname
, realname
)
title
= '<a name="%s"><strong>%s</strong></a> = %s' % (
if inspect
.isfunction(object):
args
, varargs
, varkw
, defaults
= inspect
.getargspec(object)
argspec
= inspect
.formatargspec(
args
, varargs
, varkw
, defaults
, formatvalue
=self
.formatvalue
)
if realname
== '<lambda>':
title
= '<strong>%s</strong> <em>lambda</em> ' % name
argspec
= argspec
[1:-1] # remove parentheses
decl
= title
+ argspec
+ (note
and self
.grey(
'<font face="helvetica, arial">%s</font>' % note
))
return '<dl><dt>%s</dt></dl>\n' % decl
getdoc(object), self
.preformat
, funcs
, classes
, methods
)
doc
= doc
and '<dd><tt>%s</tt></dd>' % doc
return '<dl><dt>%s</dt>%s</dl>\n' % (decl
, doc
)
def _docproperty(self
, name
, value
, mod
):
push('<dl><dt><strong>%s</strong></dt>\n' % name
)
if value
.__doc
__ is not None:
doc
= self
.markup(getdoc(value
), self
.preformat
)
push('<dd><tt>%s</tt></dd>\n' % doc
)
for attr
, tag
in [('fget', '<em>get</em>'),
('fset', '<em>set</em>'),
('fdel', '<em>delete</em>')]:
func
= getattr(value
, attr
)
base
= self
.document(func
, tag
, mod
)
push('<dd>%s</dd>\n' % base
)
def docproperty(self
, object, name
=None, mod
=None, cl
=None):
"""Produce html documentation for a property."""
return self
._docproperty
(name
, object, mod
)
def docother(self
, object, name
=None, mod
=None, *ignored
):
"""Produce HTML documentation for a data object."""
lhs
= name
and '<strong>%s</strong> = ' % name
or ''
return lhs
+ self
.repr(object)
def index(self
, dir, shadowed
=None):
"""Generate an HTML index for a directory of modules."""
if shadowed
is None: shadowed
= {}
def found(name
, ispackage
,
modpkgs
=modpkgs
, shadowed
=shadowed
, seen
=seen
):
modpkgs
.append((name
, '', ispackage
, name
in shadowed
))
# Package spam/__init__.py takes precedence over module spam.py.
path
= os
.path
.join(dir, file)
if ispackage(path
): found(file, 1)
path
= os
.path
.join(dir, file)
modname
= inspect
.getmodulename(file)
if modname
: found(modname
, 0)
contents
= self
.multicolumn(modpkgs
, self
.modpkglink
)
return self
.bigsection(dir, '#ffffff', '#ee77aa', contents
)
# -------------------------------------------- text documentation generator
"""Class for safely making a text representation of a Python object."""
self
.maxlist
= self
.maxtuple
= 20
self
.maxstring
= self
.maxother
= 100
def repr1(self
, x
, level
):
if hasattr(type(x
), '__name__'):
methodname
= 'repr_' + join(split(type(x
).__name
__), '_')
if hasattr(self
, methodname
):
return getattr(self
, methodname
)(x
, level
)
return cram(stripid(repr(x
)), self
.maxother
)
def repr_string(self
, x
, level
):
test
= cram(x
, self
.maxstring
)
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]
def repr_instance(self
, x
, level
):
return cram(stripid(repr(x
)), self
.maxstring
)
return '<%s instance>' % x
.__class
__.__name
__
"""Formatter class for text documentation."""
# ------------------------------------------- text formatting utilities
_repr_instance
= TextRepr()
repr = _repr_instance
.repr
"""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."""
lines
= split(text
, '\n')
lines
= map(lambda line
, prefix
=prefix
: prefix
+ line
, lines
)
if lines
: lines
[-1] = rstrip(lines
[-1])
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
def formattree(self
, tree
, modname
, parent
=None, prefix
=''):
"""Render in text a class tree as returned by inspect.getclasstree()."""
if type(entry
) is type(()):
result
= result
+ prefix
+ classname(c
, modname
)
if bases
and bases
!= (parent
,):
parents
= map(lambda c
, m
=modname
: classname(c
, m
), bases
)
result
= result
+ '(%s)' % join(parents
, ', ')
elif type(entry
) is type([]):
result
= result
+ self
.formattree(
entry
, modname
, c
, prefix
+ ' ')
def docmodule(self
, object, name
=None, mod
=None):
"""Produce text documentation for a given module object."""
name
= object.__name
__ # ignore the passed-in name
synop
, desc
= splitdoc(getdoc(object))
result
= self
.section('NAME', name
+ (synop
and ' - ' + synop
))
file = inspect
.getabsfile(object)
result
= result
+ self
.section('FILE', file)
docloc
= self
.getdocloc(object)
result
= result
+ self
.section('MODULE DOCS', docloc
)
result
= result
+ self
.section('DESCRIPTION', desc
)
for key
, value
in inspect
.getmembers(object, inspect
.isclass
):
# if __all__ exists, believe it. Otherwise use old heuristic.
or (inspect
.getmodule(value
) or object) is object):
if visiblename(key
, all
):
classes
.append((key
, value
))
for key
, value
in inspect
.getmembers(object, inspect
.isroutine
):
# if __all__ exists, believe it. Otherwise use old heuristic.
inspect
.isbuiltin(value
) or inspect
.getmodule(value
) is object):
if visiblename(key
, all
):
funcs
.append((key
, value
))
for key
, value
in inspect
.getmembers(object, isdata
):
if visiblename(key
, all
):
data
.append((key
, value
))
if hasattr(object, '__path__'):
for file in os
.listdir(object.__path
__[0]):
path
= os
.path
.join(object.__path
__[0], file)
modname
= inspect
.getmodulename(file)
if modname
!= '__init__':
if modname
and modname
not in modpkgs
:
modpkgs
.append(file + ' (package)')
result
= result
+ self
.section(
'PACKAGE CONTENTS', join(modpkgs
, '\n'))
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
, name
))
result
= result
+ self
.section('CLASSES', join(contents
, '\n'))
contents
.append(self
.document(value
, key
, name
))
result
= result
+ self
.section('FUNCTIONS', join(contents
, '\n'))
contents
.append(self
.docother(value
, key
, name
, 70))
result
= result
+ self
.section('DATA', join(contents
, '\n'))
if hasattr(object, '__version__'):
version
= str(object.__version
__)
if version
[:11] == '$' + 'Revision: ' and version
[-1:] == '$':
version
= strip(version
[11:-1])
result
= result
+ self
.section('VERSION', version
)
if hasattr(object, '__date__'):
result
= result
+ self
.section('DATE', str(object.__date
__))
if hasattr(object, '__author__'):
result
= result
+ self
.section('AUTHOR', str(object.__author
__))
if hasattr(object, '__credits__'):
result
= result
+ self
.section('CREDITS', str(object.__credits
__))
def docclass(self
, object, name
=None, mod
=None):
"""Produce text documentation for a given class object."""
realname
= object.__name
__
def makename(c
, m
=object.__module
__):
title
= 'class ' + self
.bold(realname
)
title
= self
.bold(name
) + ' = class ' + realname
parents
= map(makename
, bases
)
title
= title
+ '(%s)' % join(parents
, ', ')
contents
= doc
and [doc
+ '\n'] or []
# List the mro, if non-trivial.
mro
= deque(inspect
.getmro(object))
push("Method resolution order:")
push(' ' + makename(base
))
# Cute little class to pump out a horizontal rule between sections.
def spill(msg
, attrs
, predicate
):
ok
, attrs
= _split_list(attrs
, predicate
)
for name
, kind
, homecls
, value
in ok
:
push(self
.document(getattr(object, name
),
def spillproperties(msg
, attrs
, predicate
):
ok
, attrs
= _split_list(attrs
, predicate
)
for name
, kind
, homecls
, value
in ok
:
push(self
._docproperty
(name
, value
, mod
))
def spilldata(msg
, attrs
, predicate
):
ok
, attrs
= _split_list(attrs
, predicate
)
for name
, kind
, homecls
, value
in ok
:
if callable(value
) or inspect
.isdatadescriptor(value
):
push(self
.docother(getattr(object, name
),
name
, mod
, 70, doc
) + '\n')
attrs
= filter(lambda (name
, kind
, cls
, value
): visiblename(name
),
inspect
.classify_class_attrs(object))
thisclass
= mro
.popleft()
attrs
, inherited
= _split_list(attrs
, lambda t
: t
[2] is thisclass
)
if thisclass
is __builtin__
.object:
elif thisclass
is object:
tag
= "inherited from %s" % classname(thisclass
,
filter(lambda t
: not t
[0].startswith('_'), attrs
)
# Pump out the attrs, segregated by kind.
attrs
= spill("Methods %s:\n" % tag
, attrs
,
lambda t
: t
[1] == 'method')
attrs
= spill("Class methods %s:\n" % tag
, attrs
,
lambda t
: t
[1] == 'class method')
attrs
= spill("Static methods %s:\n" % tag
, attrs
,
lambda t
: t
[1] == 'static method')
attrs
= spillproperties("Properties %s:\n" % tag
, attrs
,
lambda t
: t
[1] == 'property')
attrs
= spilldata("Data and other attributes %s:\n" % tag
, attrs
,
lambda t
: t
[1] == 'data')
contents
= '\n'.join(contents
)
return title
+ '\n' + self
.indent(rstrip(contents
), ' | ') + '\n'
def formatvalue(self
, object):
"""Format an argument default value as text."""
return '=' + self
.repr(object)
def docroutine(self
, object, name
=None, mod
=None, cl
=None):
"""Produce text documentation for a function or method object."""
realname
= object.__name
__
if inspect
.ismethod(object):
imclass
= object.im_class
note
= ' from ' + classname(imclass
, mod
)
note
= ' method of %s instance' % classname(
object.im_self
.__class
__, mod
)
note
= ' unbound %s method' % classname(imclass
,mod
)
title
= self
.bold(realname
)
if (cl
and realname
in cl
.__dict
__ and
cl
.__dict
__[realname
] is object):
title
= self
.bold(name
) + ' = ' + realname
if inspect
.isfunction(object):
args
, varargs
, varkw
, defaults
= inspect
.getargspec(object)
argspec
= inspect
.formatargspec(
args
, varargs
, varkw
, defaults
, formatvalue
=self
.formatvalue
)
if realname
== '<lambda>':
argspec
= argspec
[1:-1] # remove parentheses
decl
= title
+ argspec
+ note
doc
= getdoc(object) or ''
return decl
+ '\n' + (doc
and rstrip(self
.indent(doc
)) + '\n')
def _docproperty(self
, name
, value
, mod
):
doc
= getdoc(value
) or ''
for attr
, tag
in [('fget', '<get>'),
func
= getattr(value
, attr
)
base
= self
.document(func
, tag
, mod
)
return '\n'.join(results
)
def docproperty(self
, object, name
=None, mod
=None, cl
=None):
"""Produce text documentation for a property."""
return self
._docproperty
(name
, object, mod
)
def docother(self
, object, name
=None, mod
=None, maxlen
=None, doc
=None):
"""Produce text documentation for a data object."""
line
= (name
and name
+ ' = ' or '') + repr
chop
= maxlen
- len(line
)
if chop
< 0: repr = repr[:chop
] + '...'
line
= (name
and self
.bold(name
) + ' = ' or '') + repr
line
+= '\n' + self
.indent(str(doc
))
# --------------------------------------------------------- user interfaces
"""The first time this is called, determine what kind of pager to use."""
"""Decide what method to use for paging through text."""
if type(sys
.stdout
) is not types
.FileType
:
if not sys
.stdin
.isatty() or not sys
.stdout
.isatty():
if os
.environ
.get('TERM') in ['dumb', 'emacs']:
if 'PAGER' in os
.environ
:
if sys
.platform
== 'win32': # pipes completely broken in Windows
return lambda text
: tempfilepager(plain(text
), os
.environ
['PAGER'])
elif os
.environ
.get('TERM') in ['dumb', 'emacs']:
return lambda text
: pipepager(plain(text
), os
.environ
['PAGER'])
return lambda text
: pipepager(text
, os
.environ
['PAGER'])
if sys
.platform
== 'win32' or sys
.platform
.startswith('os2'):
return lambda text
: tempfilepager(plain(text
), 'more <')
if hasattr(os
, 'system') and os
.system('(less) 2>/dev/null') == 0:
return lambda text
: pipepager(text
, 'less')
(fd
, filename
) = tempfile
.mkstemp()
if hasattr(os
, 'system') and os
.system('more %s' % filename
) == 0:
return lambda text
: pipepager(text
, 'more')
"""Remove boldface formatting from text."""
return re
.sub('.\b', '', text
)
def pipepager(text
, cmd
):
"""Page through text by feeding it to another program."""
pipe
= os
.popen(cmd
, 'w')
pass # Ignore broken pipes caused by quitting the pager program.
def tempfilepager(text
, cmd
):
"""Page through text by invoking a program on a temporary file."""
filename
= tempfile
.mktemp()
file = open(filename
, 'w')
os
.system(cmd
+ ' ' + filename
)
"""Page through text on a text terminal."""
lines
= split(plain(text
), '\n')
getchar
= lambda: sys
.stdin
.read(1)
except (ImportError, AttributeError):
getchar
= lambda: sys
.stdin
.readline()[:-1][:1]
r
= inc
= os
.environ
.get('LINES', 25) - 1
sys
.stdout
.write(join(lines
[:inc
], '\n') + '\n')
sys
.stdout
.write('-- more --')
sys
.stdout
.write('\r \r')
sys
.stdout
.write('\r \r' + lines
[r
] + '\n')
if c
in ['b', 'B', '\x1b']:
sys
.stdout
.write('\n' + join(lines
[r
:r
+inc
], '\n') + '\n')
tty
.tcsetattr(fd
, tty
.TCSAFLUSH
, old
)
"""Simply print unformatted text. This is the ultimate fallback."""
sys
.stdout
.write(plain(text
))
"""Produce a short description of the given 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
__
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
__
if type(thing
) is types
.InstanceType
:
return 'instance of ' + thing
.__class
__.__name
__
return type(thing
).__name
__
def locate(path
, forceload
=0):
"""Locate an object by name or dotted path, importing as necessary."""
parts
= [part
for part
in split(path
, '.') if part
]
nextmodule
= safeimport(join(parts
[:n
+1], '.'), forceload
)
if nextmodule
: module
, n
= nextmodule
, n
+ 1
try: object = getattr(object, part
)
except AttributeError: return None
if hasattr(__builtin__
, path
):
return getattr(__builtin__
, path
)
# --------------------------------------- interactive interpreter interface
def resolve(thing
, forceload
=0):
"""Given an object or a path to an object, get the object and its name."""
if isinstance(thing
, str):
object = locate(thing
, forceload
)
raise ImportError, 'no Python documentation found for %r' % thing
return thing
, getattr(thing
, '__name__', None)
def doc(thing
, title
='Python Library Documentation: %s', forceload
=0):
"""Display text documentation, given an object or a path to an object."""
object, name
= resolve(thing
, forceload
)
module
= inspect
.getmodule(object)
desc
+= ' in ' + name
[:name
.rfind('.')]
elif module
and module
is not object:
desc
+= ' in module ' + module
.__name
__
if not (inspect
.ismodule(object) or
inspect
.isclass(object) or
inspect
.isroutine(object) or
isinstance(object, property)):
# If the passed object is a piece of data or an instance,
# document its available methods instead of its value.
pager(title
% desc
+ '\n\n' + text
.document(object, name
))
except (ImportError, ErrorDuringImport
), value
:
def writedoc(thing
, forceload
=0):
"""Write HTML documentation to a file in the current directory."""
object, name
= resolve(thing
, forceload
)
page
= html
.page(describe(object), html
.document(object, name
))
file = open(name
+ '.html', 'w')
print 'wrote', name
+ '.html'
except (ImportError, ErrorDuringImport
), value
:
def writedocs(dir, pkgpath
='', done
=None):
"""Write out HTML documentation for all modules in a directory tree."""
if done
is None: done
= {}
for file in os
.listdir(dir):
path
= os
.path
.join(dir, file)
writedocs(path
, pkgpath
+ file + '.', done
)
elif os
.path
.isfile(path
):
modname
= inspect
.getmodulename(path
)
if modname
== '__init__':
modname
= pkgpath
[:-1] # remove trailing period
modname
= pkgpath
+ modname
'assert': ('ref/assert', ''),
'break': ('ref/break', 'while for'),
'class': ('ref/class', 'CLASSES SPECIALMETHODS'),
'continue': ('ref/continue', 'while for'),
'def': ('ref/function', ''),
'del': ('ref/del', 'BASICMETHODS'),
'else': ('ref/if', 'while for'),
'exec': ('ref/exec', ''),
'for': ('ref/for', 'break continue while'),
'global': ('ref/global', 'NAMESPACES'),
'if': ('ref/if', 'TRUTHVALUE'),
'import': ('ref/import', 'MODULES'),
'in': ('ref/comparisons', 'SEQUENCEMETHODS2'),
'lambda': ('ref/lambdas', 'FUNCTIONS'),
'pass': ('ref/pass', ''),
'print': ('ref/print', ''),
'raise': ('ref/raise', 'EXCEPTIONS'),
'return': ('ref/return', 'FUNCTIONS'),
'try': ('ref/try', 'EXCEPTIONS'),
'while': ('ref/while', 'break continue if TRUTHVALUE'),
'yield': ('ref/yield', ''),
'TYPES': ('ref/types', 'STRINGS UNICODE NUMBERS SEQUENCES MAPPINGS FUNCTIONS CLASSES MODULES FILES inspect'),
'STRINGS': ('ref/strings', 'str UNICODE SEQUENCES STRINGMETHODS FORMATTING TYPES'),
'STRINGMETHODS': ('lib/string-methods', 'STRINGS FORMATTING'),
'FORMATTING': ('lib/typesseq-strings', 'OPERATORS'),
'UNICODE': ('ref/strings', 'encodings unicode SEQUENCES STRINGMETHODS FORMATTING TYPES'),
'NUMBERS': ('ref/numbers', 'INTEGER FLOAT COMPLEX TYPES'),
'INTEGER': ('ref/integers', 'int range'),
'FLOAT': ('ref/floating', 'float math'),
'COMPLEX': ('ref/imaginary', 'complex cmath'),
'SEQUENCES': ('lib/typesseq', 'STRINGMETHODS FORMATTING xrange LISTS'),
'MAPPINGS': 'DICTIONARIES',
'FUNCTIONS': ('lib/typesfunctions', 'def TYPES'),
'METHODS': ('lib/typesmethods', 'class def CLASSES TYPES'),
'CODEOBJECTS': ('lib/bltin-code-objects', 'compile FUNCTIONS TYPES'),
'TYPEOBJECTS': ('lib/bltin-type-objects', 'types TYPES'),
'NONE': ('lib/bltin-null-object', ''),
'ELLIPSIS': ('lib/bltin-ellipsis-object', 'SLICINGS'),
'FILES': ('lib/bltin-file-objects', ''),
'SPECIALATTRIBUTES': ('lib/specialattrs', ''),
'CLASSES': ('ref/types', 'class SPECIALMETHODS PRIVATENAMES'),
'MODULES': ('lib/typesmodules', 'import'),
'EXPRESSIONS': ('ref/summary', 'lambda or and not in is BOOLEAN COMPARISON BITWISE SHIFTING BINARY FORMATTING POWER UNARY ATTRIBUTES SUBSCRIPTS SLICINGS CALLS TUPLES LISTS DICTIONARIES BACKQUOTES'),
'OPERATORS': 'EXPRESSIONS',
'PRECEDENCE': 'EXPRESSIONS',
'OBJECTS': ('ref/objects', 'TYPES'),
'SPECIALMETHODS': ('ref/specialnames', 'BASICMETHODS ATTRIBUTEMETHODS CALLABLEMETHODS SEQUENCEMETHODS1 MAPPINGMETHODS SEQUENCEMETHODS2 NUMBERMETHODS CLASSES'),
'BASICMETHODS': ('ref/customization', 'cmp hash repr str SPECIALMETHODS'),
'ATTRIBUTEMETHODS': ('ref/attribute-access', 'ATTRIBUTES SPECIALMETHODS'),
'CALLABLEMETHODS': ('ref/callable-types', 'CALLS SPECIALMETHODS'),
'SEQUENCEMETHODS1': ('ref/sequence-types', 'SEQUENCES SEQUENCEMETHODS2 SPECIALMETHODS'),
'SEQUENCEMETHODS2': ('ref/sequence-methods', 'SEQUENCES SEQUENCEMETHODS1 SPECIALMETHODS'),
'MAPPINGMETHODS': ('ref/sequence-types', 'MAPPINGS SPECIALMETHODS'),
'NUMBERMETHODS': ('ref/numeric-types', 'NUMBERS AUGMENTEDASSIGNMENT SPECIALMETHODS'),
'EXECUTION': ('ref/execmodel', 'NAMESPACES DYNAMICFEATURES EXCEPTIONS'),
'NAMESPACES': ('ref/naming', 'global ASSIGNMENT DELETION DYNAMICFEATURES'),
'DYNAMICFEATURES': ('ref/dynamic-features', ''),
'EXCEPTIONS': ('ref/exceptions', 'try except finally raise'),
'COERCIONS': ('ref/coercion-rules','CONVERSIONS'),
'CONVERSIONS': ('ref/conversions', 'COERCIONS'),
'IDENTIFIERS': ('ref/identifiers', 'keywords SPECIALIDENTIFIERS'),
'SPECIALIDENTIFIERS': ('ref/id-classes', ''),
'PRIVATENAMES': ('ref/atom-identifiers', ''),
'LITERALS': ('ref/atom-literals', 'STRINGS BACKQUOTES NUMBERS TUPLELITERALS LISTLITERALS DICTIONARYLITERALS'),
'TUPLELITERALS': ('ref/exprlists', 'TUPLES LITERALS'),
'LISTS': ('lib/typesseq-mutable', 'LISTLITERALS'),
'LISTLITERALS': ('ref/lists', 'LISTS LITERALS'),
'DICTIONARIES': ('lib/typesmapping', 'DICTIONARYLITERALS'),
'DICTIONARYLITERALS': ('ref/dict', 'DICTIONARIES LITERALS'),
'BACKQUOTES': ('ref/string-conversions', 'repr str STRINGS LITERALS'),
'ATTRIBUTES': ('ref/attribute-references', 'getattr hasattr setattr ATTRIBUTEMETHODS'),
'SUBSCRIPTS': ('ref/subscriptions', 'SEQUENCEMETHODS1'),
'SLICINGS': ('ref/slicings', 'SEQUENCEMETHODS2'),
'CALLS': ('ref/calls', 'EXPRESSIONS'),
'POWER': ('ref/power', 'EXPRESSIONS'),
'UNARY': ('ref/unary', 'EXPRESSIONS'),
'BINARY': ('ref/binary', 'EXPRESSIONS'),
'SHIFTING': ('ref/shifting', 'EXPRESSIONS'),
'BITWISE': ('ref/bitwise', 'EXPRESSIONS'),
'COMPARISON': ('ref/comparisons', 'EXPRESSIONS BASICMETHODS'),
'BOOLEAN': ('ref/Booleans', 'EXPRESSIONS TRUTHVALUE'),
'ASSIGNMENT': ('ref/assignment', 'AUGMENTEDASSIGNMENT'),
'AUGMENTEDASSIGNMENT': ('ref/augassign', 'NUMBERMETHODS'),
'LOOPING': ('ref/compound', 'for while break continue'),
'TRUTHVALUE': ('lib/truth', 'if while and or not BASICMETHODS'),
'DEBUGGING': ('lib/module-pdb', 'pdb'),
def __init__(self
, input, output
):
execdir
= os
.path
.dirname(sys
.executable
)
homedir
= os
.environ
.get('PYTHONHOME')
for dir in [os
.environ
.get('PYTHONDOCS'),
homedir
and os
.path
.join(homedir
, 'doc'),
os
.path
.join(execdir
, 'doc'),
'/usr/doc/python-docs-' + split(sys
.version
)[0],
'/usr/doc/python-' + split(sys
.version
)[0],
'/usr/doc/python-docs-' + sys
.version
[:3],
'/usr/doc/python-' + sys
.version
[:3],
os
.path
.join(sys
.prefix
, 'Resources/English.lproj/Documentation')]:
if dir and os
.path
.isdir(os
.path
.join(dir, 'lib')):
if inspect
.stack()[1][3] == '?':
return '<pydoc.Helper instance>'
def __call__(self
, request
=None):
You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)". Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.
request
= self
.getline('help> ')
except (KeyboardInterrupt, EOFError):
request
= strip(replace(request
, '"', '', "'", ''))
if lower(request
) in ['q', 'quit']: break
def getline(self
, prompt
):
"""Read one line, using raw_input when available."""
if self
.input is sys
.stdin
:
self
.output
.write(prompt
)
return self
.input.readline()
if type(request
) is type(''):
if request
== 'help': self
.intro()
elif request
== 'keywords': self
.listkeywords()
elif request
== 'topics': self
.listtopics()
elif request
== 'modules': self
.listmodules()
elif request
[:8] == 'modules ':
self
.listmodules(split(request
)[1])
elif request
in self
.keywords
: self
.showtopic(request
)
elif request
in self
.topics
: self
.showtopic(request
)
elif request
: doc(request
, 'Help on %s:')
elif isinstance(request
, Helper
): self()
else: doc(request
, 'Help on %s:')
Welcome to Python %s! This is the online help utility.
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://www.python.org/doc/tut/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".
def list(self
, items
, columns
=4, width
=80):
rows
= (len(items
) + columns
- 1) / columns
for col
in range(columns
):
self
.output
.write(items
[i
])
self
.output
.write(' ' + ' ' * (colw
-1 - len(items
[i
])))
Here is a list of the Python keywords. Enter any keyword to get more help.
self
.list(self
.keywords
.keys())
Here is a list of available topics. Enter any topic name to get more help.
self
.list(self
.topics
.keys())
def showtopic(self
, topic
):
Sorry, topic and keyword documentation is not available because the Python
HTML documentation files could not be found. If you have installed them,
please set the environment variable PYTHONDOCS to indicate their location.
target
= self
.topics
.get(topic
, self
.keywords
.get(topic
))
self
.output
.write('no documentation found for %s\n' % repr(topic
))
if type(target
) is type(''):
return self
.showtopic(target
)
filename
= self
.docdir
+ '/' + filename
+ '.html'
self
.output
.write('could not read docs from %s\n' % filename
)
divpat
= re
.compile('<div[^>]*navigat.*?</div.*?>', re
.I | re
.S
)
addrpat
= re
.compile('<address.*?>.*?</address.*?>', re
.I | re
.S
)
document
= re
.sub(addrpat
, '', re
.sub(divpat
, '', file.read()))
import htmllib
, formatter
, StringIO
buffer = StringIO
.StringIO()
parser
= htmllib
.HTMLParser(
formatter
.AbstractFormatter(formatter
.DumbWriter(buffer)))
parser
.start_table
= parser
.do_p
parser
.end_table
= lambda parser
=parser
: parser
.do_p({})
parser
.start_tr
= parser
.do_br
parser
.start_td
= parser
.start_th
= lambda a
, b
=buffer: b
.write('\t')
buffer = replace(buffer.getvalue(), '\xa0', ' ', '\n', '\n ')
pager(' ' + strip(buffer) + '\n')
buffer = StringIO
.StringIO()
formatter
.DumbWriter(buffer).send_flowing_data(
'Related help topics: ' + join(split(xrefs
), ', ') + '\n')
self
.output
.write('\n%s\n' % buffer.getvalue())
def listmodules(self
, key
=''):
Here is a list of matching modules. Enter any module name to get more help.
Please wait a moment while I gather a list of all available modules...
def callback(path
, modname
, desc
, modules
=modules
):
if modname
and modname
[-9:] == '.__init__':
modname
= modname
[:-9] + ' (package)'
if find(modname
, '.') < 0:
ModuleScanner().run(callback
)
self
.list(modules
.keys())
Enter any module name to get more help. Or, type "modules spam" to search
for modules whose descriptions contain the word "spam".
help = Helper(sys
.stdin
, sys
.stdout
)
"""A generic tree iterator."""
def __init__(self
, roots
, children
, descendp
):
self
.state
= [(root
, self
.children(root
))]
node
, children
= self
.state
[-1]
self
.state
.append((child
, self
.children(child
)))
class ModuleScanner(Scanner
):
"""An interruptible scanner that searches module synopses."""
roots
= map(lambda dir: (dir, ''), pathdirs())
Scanner
.__init
__(self
, roots
, self
.submodules
, self
.isnewpackage
)
self
.inodes
= map(lambda (dir, pkg
): os
.stat(dir).st_ino
, roots
)
def submodules(self
, (dir, package
)):
for file in os
.listdir(dir):
path
= os
.path
.join(dir, file)
children
.append((path
, package
+ (package
and '.') + file))
children
.append((path
, package
))
children
.sort() # so that spam.py comes before spam.pyc or spam.pyo
def isnewpackage(self
, (dir, package
)):
inode
= os
.path
.exists(dir) and os
.stat(dir).st_ino
if not (os
.path
.islink(dir) and inode
in self
.inodes
):
self
.inodes
.append(inode
) # detect circular symbolic links
def run(self
, callback
, key
=None, completer
=None):
for modname
in sys
.builtin_module_names
:
if modname
!= '__main__':
callback(None, modname
, '')
desc
= split(__import__(modname
).__doc
__ or '', '\n')[0]
if find(lower(modname
+ ' - ' + desc
), key
) >= 0:
callback(None, modname
, desc
)
modname
= inspect
.getmodulename(path
)
if os
.path
.isfile(path
) and modname
:
modname
= package
+ (package
and '.') + modname
seen
[modname
] = 1 # if we see spam.py, skip spam.pyc
callback(path
, modname
, '')
desc
= synopsis(path
) or ''
if find(lower(modname
+ ' - ' + desc
), key
) >= 0:
callback(path
, modname
, desc
)
if completer
: completer()
"""Print all the one-line module summaries that contain a substring."""
def callback(path
, modname
, desc
):
if modname
[-9:] == '.__init__':
modname
= modname
[:-9] + ' (package)'
print modname
, desc
and '- ' + desc
else: warnings
.filterwarnings('ignore') # ignore problems during import
ModuleScanner().run(callback
, key
)
# --------------------------------------------------- web browser interface
def serve(port
, callback
=None, completer
=None):
import BaseHTTPServer
, mimetools
, select
# Patch up mimetools.Message so it doesn't break if rfc822 is reloaded.
class Message(mimetools
.Message
):
def __init__(self
, fp
, seekable
=1):
Message
.__bases
__[0].__bases
__[0].__init
__(self
, fp
, seekable
)
self
.encodingheader
= self
.getheader('content-transfer-encoding')
self
.typeheader
= self
.getheader('content-type')
class DocHandler(BaseHTTPServer
.BaseHTTPRequestHandler
):
def send_document(self
, title
, contents
):
self
.send_header('Content-Type', 'text/html')
self
.wfile
.write(html
.page(title
, contents
))
if path
[-5:] == '.html': path
= path
[:-5]
if path
[:1] == '/': path
= path
[1:]
obj
= locate(path
, forceload
=1)
except ErrorDuringImport
, value
:
self
.send_document(path
, html
.escape(str(value
)))
self
.send_document(describe(obj
), html
.document(obj
, path
))
'no Python documentation found for %s' % repr(path
))
'<big><big><strong>Python: Index of Modules</strong></big></big>',
return '<a href="%s.html">%s</a>' % (name
, name
)
names
= filter(lambda x
: x
!= '__main__',
sys
.builtin_module_names
)
contents
= html
.multicolumn(names
, bltinlink
)
indices
= ['<p>' + html
.bigsection(
'Built-in Modules', '#ffffff', '#ee77aa', contents
)]
indices
.append(html
.index(dir, seen
))
contents
= heading
+ join(indices
) + '''<p align=right>
<font color="#909090" face="helvetica, arial"><strong>
pydoc</strong> by Ka-Ping Yee <ping@lfw.org></font>'''
self
.send_document('Index of Modules', contents
)
def log_message(self
, *args
): pass
class DocServer(BaseHTTPServer
.HTTPServer
):
def __init__(self
, port
, callback
):
host
= (sys
.platform
== 'mac') and '127.0.0.1' or 'localhost'
self
.address
= ('', port
)
self
.url
= 'http://%s:%d/' % (host
, port
)
self
.base
.__init
__(self
, self
.address
, self
.handler
)
def serve_until_quit(self
):
rd
, wr
, ex
= select
.select([self
.socket
.fileno()], [], [], 1)
if rd
: self
.handle_request()
def server_activate(self
):
self
.base
.server_activate(self
)
if self
.callback
: self
.callback(self
)
DocServer
.base
= BaseHTTPServer
.HTTPServer
DocServer
.handler
= DocHandler
DocHandler
.MessageClass
= Message
DocServer(port
, callback
).serve_until_quit()
except (KeyboardInterrupt, select
.error
):
if completer
: completer()
# ----------------------------------------------------- graphical interface
"""Graphical interface (starts web server and pops up a control window)."""
def __init__(self
, window
, port
=7464):
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':
# Trying to hide and show this button crashes under Windows.
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()
font
= ('helvetica', sys
.platform
== 'win32' and 8 or 10)
self
.result_lst
= Tkinter
.Listbox(window
, font
=font
, height
=6)
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
.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
.window
.wm_geometry('%dx%d' % (self
.minwidth
, self
.minheight
))
self
.window
.wm_minsize(self
.minwidth
, self
.minheight
)
self
.window
.tk
.willdispatch()
target
=serve
, args
=(port
, self
.ready
, self
.quit
)).start()
text
='Python documentation server at\n' + server
.url
)
self
.open_btn
.config(state
='normal')
self
.quit_btn
.config(state
='normal')
def open(self
, event
=None, url
=None):
url
= url
or self
.server
.url
except ImportError: # pre-webbrowser.py compatibility
if sys
.platform
== 'win32':
os
.system('start "%s"' % url
)
elif sys
.platform
== 'mac':
rc
= os
.system('netscape -remote "openURL(%s)" &' % url
)
if rc
: os
.system('netscape "%s" &' % url
)
def quit(self
, event
=None):
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_lbl
.pack(side
='left')
self
.result_lst
.delete(0, 'end')
self
.goto_btn
.config(state
='disabled')
self
.scanner
= ModuleScanner()
threading
.Thread(target
=self
.scanner
.run
,
args
=(self
.update
, key
, 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):
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()
modname
= split(self
.result_lst
.get(selection
[0]))[0]
self
.open(url
=self
.server
.url
+ modname
+ '.html')
if not self
.expanded
: return
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
.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
)
def hide(self
, event
=None):
# Tk will crash if pythonw.exe has an XP .manifest
# file and the root has is not destroyed explicitly.
# If the problem is ever fixed in Tk, the explicit
except KeyboardInterrupt:
# -------------------------------------------------- command-line interface
return isinstance(x
, str) and find(x
, os
.sep
) >= 0
"""Command-line interface (looks at sys.argv to decide what to do)."""
# Scripts don't get the current directory in their path by default.
scriptdir
= os
.path
.dirname(sys
.argv
[0])
if scriptdir
in sys
.path
:
sys
.path
.remove(scriptdir
)
opts
, args
= getopt
.getopt(sys
.argv
[1:], 'gk:p:w')
print 'pydoc server ready at %s' % server
.url
print 'pydoc server stopped'
serve(port
, ready
, stopped
)
if not args
: raise BadUsage
if ispath(arg
) and not os
.path
.exists(arg
):
print 'file %r does not exist' % arg
if ispath(arg
) and os
.path
.isfile(arg
):
if ispath(arg
) and os
.path
.isdir(arg
):
except ErrorDuringImport
, value
:
except (getopt
.error
, BadUsage
):
cmd
= os
.path
.basename(sys
.argv
[0])
print """pydoc - the Python documentation tool
Show text documentation on something. <name> may be the name of a
Python keyword, topic, 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. If name is 'keywords', 'topics',
or 'modules', a listing of these things is displayed.
Search for a keyword in the synopsis lines of all available modules.
Start an HTTP server on the given port on the local machine.
Pop up a graphical interface for finding and serving documentation.
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; if
it names a directory, documentation is written for all the contents.
""" % (cmd
, os
.sep
, cmd
, cmd
, cmd
, cmd
, os
.sep
)
if __name__
== '__main__': cli()