Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The generated preamble has the version filled in
executable file
2856 lines (2481 sloc)
108 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3.8 | |
"""COPY OF pydoc.py FROM THE PYTHON UPSTREAM | |
This is /usr/lib/python3.8/pydoc.py from the | |
libpython3.8-stdlib package version 3.8.4-1 on Debian | |
License: | |
PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 | |
-------------------------------------------- | |
1. This LICENSE AGREEMENT is between the Python Software Foundation | |
("PSF"), and the Individual or Organization ("Licensee") accessing and | |
otherwise using this software ("Python") in source or binary form and | |
its associated documentation. | |
2. Subject to the terms and conditions of this License Agreement, PSF | |
hereby grants Licensee a nonexclusive, royalty-free, world-wide | |
license to reproduce, analyze, test, perform and/or display publicly, | |
prepare derivative works, distribute, and otherwise use Python alone | |
or in any derivative version, provided, however, that PSF's License | |
Agreement and PSF's notice of copyright, i.e., "Copyright (c) 2001, | |
2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, | |
2013, 2014 Python Software Foundation; All Rights Reserved" are | |
retained in Python alone or in any derivative version prepared by | |
Licensee. | |
3. In the event Licensee prepares a derivative work that is based on | |
or incorporates Python or any part thereof, and wants to make | |
the derivative work available to others as provided herein, then | |
Licensee hereby agrees to include in any such work a brief summary of | |
the changes made to Python. | |
4. PSF is making Python available to Licensee on an "AS IS" | |
basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR | |
IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND | |
DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS | |
FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT | |
INFRINGE ANY THIRD PARTY RIGHTS. | |
5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON | |
FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS | |
A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, | |
OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. | |
6. This License Agreement will automatically terminate upon a material | |
breach of its terms and conditions. | |
7. Nothing in this License Agreement shall be deemed to create any | |
relationship of agency, partnership, or joint venture between PSF and | |
Licensee. This License Agreement does not grant permission to use PSF | |
trademarks or trade name in a trademark sense to endorse or promote | |
products or services of Licensee, or any third party. | |
8. By copying, installing or otherwise using Python, Licensee | |
agrees to be bound by the terms and conditions of this License | |
Agreement. | |
I needed to make minor changes to this tool to produce reference HTML | |
documentation for mrcal. The modifications are in the mrcal version control. | |
Broadly: | |
- removed the "index" links at the top-right | |
- remoded the "modules" section. This was mostly telling the reader about all | |
the modules that the module being documented imports, which is irrelevant | |
- I don't list any "package contents". Those just list the submodules, which I | |
don't want to do | |
- set all the submodule links (mrcal.submodule.function) to work as if it was | |
mrcal.function. That's how the api looks to the user | |
- pydoc output to stdout: for easier integration into the Makefile | |
- I don't document builtins | |
- Added links to external projects I use (gp,nps,np,cv2) | |
- Using my css style and my preamble | |
- I don't swallow failures due to a broken import | |
Generate Python documentation in HTML or text for interactive use. | |
At the Python interactive prompt, calling help(thing) on a Python object | |
documents the object, and calling help() starts up an interactive | |
help session. | |
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 -n <hostname>" to start an HTTP server with the given | |
hostname (default: localhost) on the local machine. | |
Run "pydoc -p <port>" to start an HTTP server on the given port on the | |
local machine. Port number 0 can be used to get an arbitrary unused port. | |
Run "pydoc -b" to start an HTTP server on an arbitrary unused port and | |
open a Web browser to interactively browse documentation. Combine with | |
the -n and -p options to control the hostname and port used. | |
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 | |
/usr/share/doc/pythonX.Y/html/library | |
if the pythonX.Y-doc package is installed or in | |
https://docs.python.org/X.Y/library/ | |
This can be overridden by setting the PYTHONDOCS environment variable | |
to a different URL or to a local directory containing the Library | |
Reference Manual pages. | |
""" | |
__all__ = ['help'] | |
__author__ = "Ka-Ping Yee <ping@lfw.org>" | |
__date__ = "26 February 2001" | |
__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: | |
# - synopsis() cannot be prevented from clobbering existing | |
# loaded modules. | |
# - 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 builtins | |
import importlib._bootstrap | |
import importlib._bootstrap_external | |
import importlib.machinery | |
import importlib.util | |
import inspect | |
import io | |
import os | |
import pkgutil | |
import platform | |
import re | |
import sys | |
import sysconfig | |
import time | |
import tokenize | |
import urllib.parse | |
import warnings | |
from collections import deque | |
from reprlib import Repr | |
from traceback import format_exception_only | |
extranames = \ | |
dict( gp = "https://www.github.com/dkogan/gnuplotlib", | |
gnuplotlib = "https://www.github.com/dkogan/gnuplotlib", | |
nps = "https://www.github.com/dkogan/numpysane", | |
numpysane = "https://www.github.com/dkogan/numpysane", | |
vnlog = "https://www.github.com/dkogan/vnlog", | |
np = "https://numpy.org/doc/stable/reference/index.html", | |
numpy = "https://numpy.org/doc/stable/reference/index.html", | |
cv2 = "https://docs.opencv.org/master/") | |
# --------------------------------------------------------- common routines | |
def pathdirs(): | |
"""Convert sys.path into a list of absolute, existing, unique paths.""" | |
dirs = [] | |
normdirs = [] | |
for dir in sys.path: | |
dir = os.path.abspath(dir or '.') | |
normdir = os.path.normcase(dir) | |
if normdir not in normdirs and os.path.isdir(dir): | |
dirs.append(dir) | |
normdirs.append(normdir) | |
return dirs | |
def getdoc(object): | |
"""Get the doc string or comments for an object.""" | |
result = inspect.getdoc(object) or inspect.getcomments(object) | |
return result and re.sub('^ *\n', '', result.rstrip()) or '' | |
def splitdoc(doc): | |
"""Split a doc string into a synopsis line (if any) and the rest.""" | |
lines = doc.strip().split('\n') | |
if len(lines) == 1: | |
return lines[0], '' | |
elif len(lines) >= 2 and not lines[1].rstrip(): | |
return lines[0], '\n'.join(lines[2:]) | |
return '', '\n'.join(lines) | |
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 isdata(object): | |
"""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.""" | |
while pairs: | |
text = pairs[1].join(text.split(pairs[0])) | |
pairs = pairs[2:] | |
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 | |
_re_stripid = re.compile(r' at 0x[0-9a-f]{6,16}(>+)$', re.IGNORECASE) | |
def stripid(text): | |
"""Remove the hexadecimal id from a Python object representation.""" | |
# The behaviour of %p is implementation-dependent in terms of case. | |
return _re_stripid.sub(r'\1', text) | |
def _is_bound_method(fn): | |
""" | |
Returns True if fn is a bound method, regardless of whether | |
fn was implemented in Python or in C. | |
""" | |
if inspect.ismethod(fn): | |
return True | |
if inspect.isbuiltin(fn): | |
self = getattr(fn, '__self__', None) | |
return not (inspect.ismodule(self) or (self is None)) | |
return False | |
def allmethods(cl): | |
methods = {} | |
for key, value in inspect.getmembers(cl, inspect.isroutine): | |
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 | |
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)]) | |
""" | |
yes = [] | |
no = [] | |
for x in s: | |
if predicate(x): | |
yes.append(x) | |
else: | |
no.append(x) | |
return yes, no | |
def visiblename(name, all=None, obj=None): | |
"""Decide whether to show documentation on a variable.""" | |
# Certain special names are redundant or internal. | |
# XXX Remove __initializing__? | |
if name in {'__author__', '__builtins__', '__cached__', '__credits__', | |
'__date__', '__doc__', '__file__', '__spec__', | |
'__loader__', '__module__', '__name__', '__package__', | |
'__path__', '__qualname__', '__slots__', '__version__'}: | |
return 0 | |
# Private names are hidden, but special names are displayed. | |
if name.startswith('__') and name.endswith('__'): return 1 | |
# Namedtuples have public fields and methods with a single leading underscore | |
if name.startswith('_') and hasattr(obj, '_fields'): | |
return True | |
if all is not None: | |
# only document that which the programmer exported in __all__ | |
return name in all | |
else: | |
return not name.startswith('_') | |
def classify_class_attrs(object): | |
"""Wrap inspect.classify_class_attrs, with fixup for data descriptors.""" | |
results = [] | |
for (name, kind, cls, value) in inspect.classify_class_attrs(object): | |
if inspect.isdatadescriptor(value): | |
kind = 'data descriptor' | |
if isinstance(value, property) and value.fset is None: | |
kind = 'readonly property' | |
results.append((name, kind, cls, value)) | |
return results | |
def sort_attributes(attrs, object): | |
'Sort the attrs list in-place by _fields and then alphabetically by name' | |
# This allows data descriptors to be ordered according | |
# to a _fields attribute if present. | |
fields = getattr(object, '_fields', []) | |
try: | |
field_order = {name : i-len(fields) for (i, name) in enumerate(fields)} | |
except TypeError: | |
field_order = {} | |
keyfunc = lambda attr: (field_order.get(attr[0], 0), attr[0]) | |
attrs.sort(key=keyfunc) | |
# ----------------------------------------------------- module manipulation | |
def ispackage(path): | |
"""Guess whether a path refers to a package directory.""" | |
if os.path.isdir(path): | |
for ext in ('.py', '.pyc'): | |
if os.path.isfile(os.path.join(path, '__init__' + ext)): | |
return True | |
return False | |
def source_synopsis(file): | |
line = file.readline() | |
while line[:1] == '#' or not line.strip(): | |
line = file.readline() | |
if not line: break | |
line = line.strip() | |
if line[:4] == 'r"""': line = line[1:] | |
if line[:3] == '"""': | |
line = line[3:] | |
if line[-1:] == '\\': line = line[:-1] | |
while not line.strip(): | |
line = file.readline() | |
if not line: break | |
result = line.split('"""')[0].strip() | |
else: result = None | |
return result | |
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, (None, None)) | |
if lastupdate is None or lastupdate < mtime: | |
# Look for binary suffixes first, falling back to source. | |
if filename.endswith(tuple(importlib.machinery.BYTECODE_SUFFIXES)): | |
loader_cls = importlib.machinery.SourcelessFileLoader | |
elif filename.endswith(tuple(importlib.machinery.EXTENSION_SUFFIXES)): | |
loader_cls = importlib.machinery.ExtensionFileLoader | |
else: | |
loader_cls = None | |
# Now handle the choice. | |
if loader_cls is None: | |
# Must be a source file. | |
try: | |
file = tokenize.open(filename) | |
except OSError: | |
# module can't be opened, so skip it | |
return None | |
# text modules can be directly examined | |
with file: | |
result = source_synopsis(file) | |
else: | |
# Must be a binary module, which has to be imported. | |
loader = loader_cls('__temp__', filename) | |
# XXX We probably don't need to pass in the loader here. | |
spec = importlib.util.spec_from_file_location('__temp__', filename, | |
loader=loader) | |
try: | |
module = importlib._bootstrap._load(spec) | |
except: | |
return None | |
del sys.modules['__temp__'] | |
result = module.__doc__.splitlines()[0] if module.__doc__ else None | |
# Cache the result. | |
cache[filename] = (mtime, result) | |
return result | |
def importfile(path): | |
"""Import a Python source file or compiled file given its path.""" | |
magic = importlib.util.MAGIC_NUMBER | |
with open(path, 'rb') as file: | |
is_bytecode = magic == file.read(len(magic)) | |
filename = os.path.basename(path) | |
name, ext = os.path.splitext(filename) | |
if is_bytecode: | |
loader = importlib._bootstrap_external.SourcelessFileLoader(name, path) | |
else: | |
loader = importlib._bootstrap_external.SourceFileLoader(name, path) | |
# XXX We probably don't need to pass in the loader here. | |
spec = importlib.util.spec_from_file_location(name, path, loader=loader) | |
return importlib._bootstrap._load(spec) | |
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).""" | |
try: | |
# If forceload is 1 and the module has been previously loaded from | |
# disk, we always have to reload the module. Checking the file's | |
# mtime isn't good enough (e.g. the module could contain a class | |
# that inherits from another module that has changed). | |
if forceload and path in sys.modules: | |
if path not in sys.builtin_module_names: | |
# Remove the module from sys.modules and re-import to try | |
# and avoid problems with partially loaded modules. | |
# Also remove any submodules because they won't appear | |
# in the newly loaded module's namespace if they're already | |
# in sys.modules. | |
subs = [m for m in sys.modules if m.startswith(path + '.')] | |
for key in [path] + subs: | |
# Prevent garbage collection. | |
cache[key] = sys.modules[key] | |
del sys.modules[key] | |
module = __import__(path) | |
except: | |
# Did the error occur before or after the module was found? | |
(exc, value, tb) = info = sys.exc_info() | |
if path in sys.modules: | |
# An error occurred 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 issubclass(exc, ImportError) and value.name == path: | |
# No such module in the path. | |
return None | |
else: | |
# Some other error occurred during the importing process. | |
raise ErrorDuringImport(path, sys.exc_info()) | |
for part in path.split('.')[1:]: | |
try: module = getattr(module, part) | |
except AttributeError: return None | |
return module | |
# ---------------------------------------------------- formatter base class | |
class Doc: | |
PYTHONDOCS = os.environ.get("PYTHONDOCS", | |
"https://docs.python.org/%d.%d/library" | |
% sys.version_info[:2]) | |
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. | |
try: | |
if inspect.ismodule(object): return self.docmodule(*args) | |
if inspect.isclass(object): return self.docclass(*args) | |
if inspect.isroutine(object): return self.docroutine(*args) | |
except AttributeError: | |
pass | |
if inspect.isdatadescriptor(object): return self.docdata(*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__) | |
raise TypeError(message) | |
docmodule = docclass = docroutine = docother = docproperty = docdata = fail | |
def getdocloc(self, object, basedir=sysconfig.get_path('stdlib')): | |
"""Return the location of module docs or None""" | |
try: | |
file = inspect.getabsfile(object) | |
except TypeError: | |
file = '(built-in)' | |
docloc = os.environ.get("PYTHONDOCS", self.PYTHONDOCS) | |
basedir = os.path.normcase(basedir) | |
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, 'dist-packages')) and | |
not file.startswith(os.path.join(basedir, 'site-packages')))) and | |
object.__name__ not in ('xml.etree', 'test.pydoc_mod')): | |
if docloc.startswith(("http://", "https://")): | |
docloc = "%s/%s" % (docloc.rstrip("/"), object.__name__.lower()) | |
else: | |
docloc = os.path.join(docloc, object.__name__.lower() + ".html") | |
else: | |
docloc = None | |
return docloc | |
# -------------------------------------------- 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 = 20 | |
self.maxdict = 10 | |
self.maxstring = self.maxother = 100 | |
def escape(self, text): | |
return replace(text, '&', '&', '<', '<', '>', '>') | |
def repr(self, object): | |
return Repr.repr(self, object) | |
def repr1(self, x, level): | |
if hasattr(type(x), '__name__'): | |
methodname = 'repr_' + '_'.join(type(x).__name__.split()) | |
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) | |
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\'"]|\\[0-9]..|\\x..|\\u....)+)', | |
r'<font color="#c040c0">\1</font>', | |
self.escape(testrepr)) | |
repr_str = repr_string | |
def repr_instance(self, x, level): | |
try: | |
return self.escape(cram(stripid(repr(x)), self.maxstring)) | |
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 | |
def page(self, title, contents): | |
"""Format an HTML page.""" | |
with open("doc/mrcal-preamble-GENERATED.html", "r") as f: | |
preamble = f.read() | |
return '''\ | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | |
<html><head><title>Python: %s</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<link rel="stylesheet" type="text/css" href="mrcal.css"/> | |
</head> | |
<body bgcolor="#f0f0f8"> | |
%s | |
%s | |
</body></html>''' % (title, preamble, contents) | |
def heading(self, title, fgcol, bgcol, extras=''): | |
"""Format a page heading.""" | |
return ''' | |
<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> | |
<tr bgcolor="%s"> | |
<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.""" | |
if marginalia is None: | |
marginalia = '<tt>' + ' ' * width + '</tt>' | |
result = '''<p> | |
<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |
<tr bgcolor="%s"> | |
<td colspan=3 valign=bottom> <br> | |
<font color="%s" face="helvetica, arial">%s</font></td></tr> | |
''' % (bgcol, fgcol, title) | |
if prelude: | |
result = result + ''' | |
<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 + ''' | |
<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(text.expandtabs()) | |
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): | |
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.""" | |
for dict in dicts: | |
if name in dict: | |
if re.match('builtins\.', dict[name]): | |
return name | |
return '<a href="%s">%s</a>' % (dict[name], name) | |
return 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 and not re.match('builtins\.',classname(object, modname)): | |
def strip_submodule(m): | |
s = "[a-zA-Z0-9_]+" | |
return re.sub(rf"({s})\.{s}\.({s})", r"\1.\2", m) | |
return '<a href="#%s">%s</a>' % ( | |
name, | |
strip_submodule(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, modpkginfo): | |
"""Make a link for a module or package to display in an index.""" | |
name, path, ispackage, shadowed = modpkginfo | |
if shadowed: | |
return self.grey(name) | |
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 filelink(self, url, path): | |
"""Make a link to source file.""" | |
return '<a href="file:%s">%s</a>' % (url, path) | |
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 | |
pattern = re.compile(r'\b((http|ftp)://\S+[\w/]|' | |
r'RFC[- ]?(\d+)|' | |
r'PEP[- ]?(\d+)|' | |
r'(self\.)?(\w+))') | |
while True: | |
match = pattern.search(text, here) | |
if not match: break | |
start, end = match.span() | |
results.append(escape(text[here:start])) | |
all, scheme, rfc, pep, selfdot, name = match.groups() | |
if scheme: | |
url = escape(all).replace('"', '"') | |
results.append('<a href="%s">%s</a>' % (url, url)) | |
elif rfc: | |
url = 'http://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc) | |
results.append('<a href="%s">%s</a>' % (url, escape(all))) | |
elif pep: | |
url = 'http://www.python.org/dev/peps/pep-%04d/' % int(pep) | |
results.append('<a href="%s">%s</a>' % (url, escape(all))) | |
elif selfdot: | |
# Create a link for methods like 'self.method(...)' | |
# and use <strong> for attributes like 'self.attr' | |
if text[end:end+1] == '(': | |
results.append('self.' + self.namelink(name, methods, extranames)) | |
else: | |
results.append('self.<strong>%s</strong>' % name) | |
elif text[end:end+1] == '(': | |
results.append(self.namelink(name, methods, funcs, classes, extranames)) | |
else: | |
results.append(self.namelink(name, classes, extranames)) | |
here = end | |
results.append(escape(text[here:])) | |
return ''.join(results) | |
# ---------------------------------------------- type-specific routines | |
def formattree(self, tree, modname, parent=None): | |
"""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">' | |
result = result + self.classlink(c, modname) | |
if bases and bases != (parent,): | |
parents = [] | |
for base in bases: | |
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( | |
entry, modname, c) | |
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 | |
try: | |
all = object.__all__ | |
except AttributeError: | |
all = None | |
parts = name.split('.') | |
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 | |
try: | |
path = inspect.getabsfile(object) | |
url = urllib.parse.quote(path) | |
filelink = self.filelink(url, path) | |
except TypeError: | |
filelink = '(built-in)' | |
info = [] | |
if hasattr(object, '__version__'): | |
version = str(object.__version__) | |
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$': | |
version = version[11:-1].strip() | |
info.append('version %s' % self.escape(version)) | |
if hasattr(object, '__date__'): | |
info.append(self.escape(str(object.__date__))) | |
if info: | |
head = head + ' (%s)' % ', '.join(info) | |
docloc = self.getdocloc(object) | |
if docloc is not None: | |
docloc = '<br><a href="%(docloc)s">Module Reference</a>' % locals() | |
else: | |
docloc = '' | |
result = self.heading( | |
head, '#ffffff', '#7799ee') | |
modules = None | |
classes, cdict = [], {} | |
for key, value in inspect.getmembers(object, inspect.isclass): | |
# if __all__ exists, believe it. Otherwise use old heuristic. | |
if True: | |
if visiblename(key, all, object): | |
classes.append((key, value)) | |
cdict[key] = cdict[value] = '#' + key | |
for key, value in classes: | |
for base in value.__bases__: | |
if base == builtins.object: | |
continue | |
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 key in cdict: | |
cdict[key] = cdict[base] = modname + '.html#' + key | |
funcs, fdict = [], {} | |
for key, value in inspect.getmembers(object, inspect.isroutine): | |
# if __all__ exists, believe it. Otherwise use old heuristic. | |
if True: | |
if visiblename(key, all, object): | |
funcs.append((key, value)) | |
fdict[key] = '#-' + key | |
if inspect.isfunction(value): fdict[value] = fdict[key] | |
data = [] | |
for key, value in inspect.getmembers(object, isdata): | |
if visiblename(key, all, object): | |
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 modules: | |
contents = self.multicolumn( | |
modules, lambda t: self.modulelink(t[1])) | |
result = result + self.bigsection( | |
'Modules', '#ffffff', '#aa55cc', contents) | |
if classes: | |
classlist = [value for (key, value) in classes] | |
contents = [ | |
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)) | |
if funcs: | |
contents = [] | |
for key, value in funcs: | |
contents.append(self.document(value, key, name, fdict, cdict)) | |
result = result + self.bigsection( | |
'Functions', '#ffffff', '#eeaa77', ' '.join(contents)) | |
if data: | |
contents = [] | |
for key, value in data: | |
contents.append(self.document(value, key)) | |
result = result + self.bigsection( | |
'Data', '#ffffff', '#55aa55', '<br>\n'.join(contents)) | |
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) | |
return result | |
def docclass(self, object, name=None, mod=None, funcs={}, classes={}, | |
*ignored): | |
"""Produce HTML documentation for a class object.""" | |
realname = object.__name__ | |
name = name or realname | |
bases = object.__bases__ | |
contents = [] | |
push = contents.append | |
# Cute little class to pump out a horizontal rule between sections. | |
class HorizontalRule: | |
def __init__(self): | |
self.needone = 0 | |
def maybe(self): | |
if self.needone: | |
push('<hr>\n') | |
self.needone = 1 | |
hr = HorizontalRule() | |
# List the mro, if non-trivial. | |
mro = deque(inspect.getmro(object)) | |
if len(mro) > 2: | |
hr.maybe() | |
push('<dl><dt>Method resolution order:</dt>\n') | |
for base in mro: | |
push('<dd>%s</dd>\n' % self.classlink(base, | |
object.__module__)) | |
push('</dl>\n') | |
def spill(msg, attrs, predicate): | |
ok, attrs = _split_list(attrs, predicate) | |
if ok: | |
hr.maybe() | |
push(msg) | |
for name, kind, homecls, value in ok: | |
try: | |
value = getattr(object, name) | |
except Exception: | |
# Some descriptors may meet a failure in their __get__. | |
# (bug #1785) | |
push(self.docdata(value, name, mod)) | |
else: | |
push(self.document(value, name, mod, | |
funcs, classes, mdict, object)) | |
push('\n') | |
return attrs | |
def spilldescriptors(msg, attrs, predicate): | |
ok, attrs = _split_list(attrs, predicate) | |
if ok: | |
hr.maybe() | |
push(msg) | |
for name, kind, homecls, value in ok: | |
push(self.docdata(value, name, mod)) | |
return attrs | |
def spilldata(msg, attrs, predicate): | |
ok, attrs = _split_list(attrs, predicate) | |
if ok: | |
hr.maybe() | |
push(msg) | |
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) | |
else: | |
doc = None | |
if doc is None: | |
push('<dl><dt>%s</dl>\n' % base) | |
else: | |
doc = self.markup(getdoc(value), self.preformat, | |
funcs, classes, mdict) | |
doc = '<dd><tt>%s</tt>' % doc | |
push('<dl><dt>%s%s</dl>\n' % (base, doc)) | |
push('\n') | |
return attrs | |
attrs = [(name, kind, cls, value) | |
for name, kind, cls, value in classify_class_attrs(object) | |
if visiblename(name, obj=object)] | |
mdict = {} | |
for key, kind, homecls, value in attrs: | |
mdict[key] = anchor = '#' + name + '-' + key | |
try: | |
value = getattr(object, name) | |
except Exception: | |
# Some descriptors may meet a failure in their __get__. | |
# (bug #1785) | |
pass | |
try: | |
# The value may not be hashable (e.g., a data attr with | |
# a dict or list value). | |
mdict[value] = anchor | |
except TypeError: | |
pass | |
while attrs: | |
if mro: | |
thisclass = mro.popleft() | |
else: | |
thisclass = attrs[0][2] | |
attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass) | |
if object is not builtins.object and thisclass is builtins.object: | |
attrs = inherited | |
continue | |
elif thisclass is object: | |
tag = 'defined here' | |
else: | |
tag = 'inherited from %s' % self.classlink(thisclass, | |
object.__module__) | |
tag += ':<br>\n' | |
sort_attributes(attrs, object) | |
# 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 = spilldescriptors("Readonly properties %s" % tag, attrs, | |
lambda t: t[1] == 'readonly property') | |
attrs = spilldescriptors('Data descriptors %s' % tag, attrs, | |
lambda t: t[1] == 'data descriptor') | |
attrs = spilldata('Data and other attributes %s' % tag, attrs, | |
lambda t: t[1] == 'data') | |
assert attrs == [] | |
attrs = inherited | |
contents = ''.join(contents) | |
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) | |
if bases: | |
parents = [] | |
for base in bases: | |
parents.append(self.classlink(base, object.__module__)) | |
title = title + '(%s)' % ', '.join(parents) | |
decl = '' | |
try: | |
signature = inspect.signature(object) | |
except (ValueError, TypeError): | |
signature = None | |
if signature: | |
argspec = str(signature) | |
if argspec and argspec != '()': | |
decl = name + self.escape(argspec) + '\n\n' | |
doc = getdoc(object) | |
if decl: | |
doc = decl + (doc or '') | |
doc = self.markup(doc, 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__ | |
name = name or realname | |
anchor = (cl and cl.__name__ or '') + '-' + name | |
note = '' | |
skipdocs = 0 | |
if _is_bound_method(object): | |
imclass = object.__self__.__class__ | |
if cl: | |
if imclass is not cl: | |
note = ' from ' + self.classlink(imclass, mod) | |
else: | |
if object.__self__ is not None: | |
note = ' method of %s instance' % self.classlink( | |
object.__self__.__class__, mod) | |
else: | |
note = ' unbound %s method' % self.classlink(imclass,mod) | |
if (inspect.iscoroutinefunction(object) or | |
inspect.isasyncgenfunction(object)): | |
asyncqualifier = 'async ' | |
else: | |
asyncqualifier = '' | |
if name == realname: | |
title = '<a name="%s"><strong>%s</strong></a>' % (anchor, realname) | |
else: | |
if cl and inspect.getattr_static(cl, 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) | |
argspec = None | |
if inspect.isroutine(object): | |
try: | |
signature = inspect.signature(object) | |
except (ValueError, TypeError): | |
signature = None | |
if signature: | |
argspec = str(signature) | |
if realname == '<lambda>': | |
title = '<strong>%s</strong> <em>lambda</em> ' % name | |
# XXX lambda's won't usually have func_annotations['return'] | |
# since the syntax doesn't support but it is possible. | |
# So removing parentheses isn't truly safe. | |
argspec = argspec[1:-1] # remove parentheses | |
if not argspec: | |
argspec = '(...)' | |
decl = asyncqualifier + title + self.escape(argspec) + (note and | |
self.grey('<font face="helvetica, arial">%s</font>' % note)) | |
if skipdocs: | |
return '<dl><dt>%s</dt></dl>\n' % decl | |
else: | |
doc = self.markup( | |
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 docdata(self, object, name=None, mod=None, cl=None): | |
"""Produce html documentation for a data descriptor.""" | |
results = [] | |
push = results.append | |
if name: | |
push('<dl><dt><strong>%s</strong></dt>\n' % name) | |
doc = self.markup(getdoc(object), self.preformat) | |
if doc: | |
push('<dd><tt>%s</tt></dd>\n' % doc) | |
push('</dl>\n') | |
return ''.join(results) | |
docproperty = docdata | |
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.""" | |
modpkgs = [] | |
if shadowed is None: shadowed = {} | |
for importer, name, ispkg in pkgutil.iter_modules([dir]): | |
if any((0xD800 <= ord(ch) <= 0xDFFF) for ch in name): | |
# ignore a module if its name contains a surrogate character | |
continue | |
modpkgs.append((name, '', ispkg, name in shadowed)) | |
shadowed[name] = 1 | |
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 = 20 | |
self.maxdict = 10 | |
self.maxstring = self.maxother = 100 | |
def repr1(self, x, level): | |
if hasattr(type(x), '__name__'): | |
methodname = 'repr_' + '_'.join(type(x).__name__.split()) | |
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) | |
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 | |
repr_str = repr_string | |
def repr_instance(self, x, level): | |
try: | |
return cram(stripid(repr(x)), self.maxstring) | |
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(ch + '\b' + ch for ch in text) | |
def indent(self, text, prefix=' '): | |
"""Indent text by prepending a given prefix to each line.""" | |
if not text: return '' | |
lines = [prefix + line for line in text.split('\n')] | |
if lines: lines[-1] = lines[-1].rstrip() | |
return '\n'.join(lines) | |
def section(self, title, contents): | |
"""Format a section with a given heading.""" | |
clean_contents = self.indent(contents).rstrip() | |
return self.bold(title) + '\n' + clean_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().""" | |
result = '' | |
for entry in tree: | |
if type(entry) is type(()): | |
c, bases = entry | |
result = result + prefix + classname(c, modname) | |
if bases and bases != (parent,): | |
parents = (classname(c, modname) for c in bases) | |
result = result + '(%s)' % ', '.join(parents) | |
result = result + '\n' | |
elif type(entry) is type([]): | |
result = result + self.formattree( | |
entry, modname, c, prefix + ' ') | |
return result | |
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)) | |
all = getattr(object, '__all__', None) | |
docloc = self.getdocloc(object) | |
if docloc is not None: | |
result = result + self.section('MODULE REFERENCE', docloc + """ | |
The following documentation is automatically generated from the Python | |
source files. It may be incomplete, incorrect or include features that | |
are considered implementation detail and may vary between Python | |
implementations. When in doubt, consult the module reference at the | |
location listed above. | |
""") | |
if desc: | |
result = result + self.section('DESCRIPTION', desc) | |
classes = [] | |
for key, value in inspect.getmembers(object, inspect.isclass): | |
# if __all__ exists, believe it. Otherwise use old heuristic. | |
if True: | |
if visiblename(key, all, object): | |
classes.append((key, value)) | |
funcs = [] | |
for key, value in inspect.getmembers(object, inspect.isroutine): | |
# if __all__ exists, believe it. Otherwise use old heuristic. | |
if True: | |
if visiblename(key, all, object): | |
funcs.append((key, value)) | |
data = [] | |
for key, value in inspect.getmembers(object, isdata): | |
if visiblename(key, all, object): | |
data.append((key, value)) | |
modpkgs = [] | |
modpkgs_names = set() | |
if hasattr(object, '__path__'): | |
for importer, modname, ispkg in pkgutil.iter_modules(object.__path__): | |
modpkgs_names.add(modname) | |
if ispkg: | |
modpkgs.append(modname + ' (package)') | |
else: | |
modpkgs.append(modname) | |
modpkgs.sort() | |
result = result + self.section( | |
'PACKAGE CONTENTS', '\n'.join(modpkgs)) | |
# Detect submodules as sometimes created by C extensions | |
submodules = [] | |
for key, value in inspect.getmembers(object, inspect.ismodule): | |
if value.__name__.startswith(name + '.') and key not in modpkgs_names: | |
submodules.append(key) | |
if submodules: | |
submodules.sort() | |
result = result + self.section( | |
'SUBMODULES', '\n'.join(submodules)) | |
if classes: | |
classlist = [value for key, value in 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', '\n'.join(contents)) | |
if funcs: | |
contents = [] | |
for key, value in funcs: | |
contents.append(self.document(value, key, name)) | |
result = result + self.section('FUNCTIONS', '\n'.join(contents)) | |
if data: | |
contents = [] | |
for key, value in data: | |
contents.append(self.docother(value, key, name, maxlen=70)) | |
result = result + self.section('DATA', '\n'.join(contents)) | |
if hasattr(object, '__version__'): | |
version = str(object.__version__) | |
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$': | |
version = version[11:-1].strip() | |
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__)) | |
try: | |
file = inspect.getabsfile(object) | |
except TypeError: | |
file = '(built-in)' | |
result = result + self.section('FILE', file) | |
return result | |
def docclass(self, object, name=None, mod=None, *ignored): | |
"""Produce text documentation for a given class object.""" | |
realname = object.__name__ | |
name = name or realname | |
bases = object.__bases__ | |
def makename(c, m=object.__module__): | |
return classname(c, m) | |
if name == realname: | |
title = 'class ' + self.bold(realname) | |
else: | |
title = self.bold(name) + ' = class ' + realname | |
if bases: | |
parents = map(makename, bases) | |
title = title + '(%s)' % ', '.join(parents) | |
contents = [] | |
push = contents.append | |
try: | |
signature = inspect.signature(object) | |
except (ValueError, TypeError): | |
signature = None | |
if signature: | |
argspec = str(signature) | |
if argspec and argspec != '()': | |
push(name + argspec + '\n') | |
doc = getdoc(object) | |
if doc: | |
push(doc + '\n') | |
# List the mro, if non-trivial. | |
mro = deque(inspect.getmro(object)) | |
if len(mro) > 2: | |
push("Method resolution order:") | |
for base in mro: | |
push(' ' + makename(base)) | |
push('') | |
# List the built-in subclasses, if any: | |
subclasses = sorted( | |
(str(cls.__name__) for cls in type.__subclasses__(object) | |
if not cls.__name__.startswith("_") and cls.__module__ == "builtins"), | |
key=str.lower | |
) | |
no_of_subclasses = len(subclasses) | |
MAX_SUBCLASSES_TO_DISPLAY = 4 | |
if subclasses: | |
push("Built-in subclasses:") | |
for subclassname in subclasses[:MAX_SUBCLASSES_TO_DISPLAY]: | |
push(' ' + subclassname) | |
if no_of_subclasses > MAX_SUBCLASSES_TO_DISPLAY: | |
push(' ... and ' + | |
str(no_of_subclasses - MAX_SUBCLASSES_TO_DISPLAY) + | |
' other subclasses') | |
push('') | |
# Cute little class to pump out a horizontal rule between sections. | |
class HorizontalRule: | |
def __init__(self): | |
self.needone = 0 | |
def maybe(self): | |
if self.needone: | |
push('-' * 70) | |
self.needone = 1 | |
hr = HorizontalRule() | |
def spill(msg, attrs, predicate): | |
ok, attrs = _split_list(attrs, predicate) | |
if ok: | |
hr.maybe() | |
push(msg) | |
for name, kind, homecls, value in ok: | |
try: | |
value = getattr(object, name) | |
except Exception: | |
# Some descriptors may meet a failure in their __get__. | |
# (bug #1785) | |
push(self.docdata(value, name, mod)) | |
else: | |
push(self.document(value, | |
name, mod, object)) | |
return attrs | |
def spilldescriptors(msg, attrs, predicate): | |
ok, attrs = _split_list(attrs, predicate) | |
if ok: | |
hr.maybe() | |
push(msg) | |
for name, kind, homecls, value in ok: | |
push(self.docdata(value, name, mod)) | |
return attrs | |
def spilldata(msg, attrs, predicate): | |
ok, attrs = _split_list(attrs, predicate) | |
if ok: | |
hr.maybe() | |
push(msg) | |
for name, kind, homecls, value in ok: | |
if callable(value) or inspect.isdatadescriptor(value): | |
doc = getdoc(value) | |
else: | |
doc = None | |
try: | |
obj = getattr(object, name) | |
except AttributeError: | |
obj = homecls.__dict__[name] | |
push(self.docother(obj, name, mod, maxlen=70, doc=doc) + | |
'\n') | |
return attrs | |
attrs = [(name, kind, cls, value) | |
for name, kind, cls, value in classify_class_attrs(object) | |
if visiblename(name, obj=object)] | |
while attrs: | |
if mro: | |
thisclass = mro.popleft() | |
else: | |
thisclass = attrs[0][2] | |
attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass) | |
if object is not builtins.object and thisclass is builtins.object: | |
attrs = inherited | |
continue | |
elif thisclass is object: | |
tag = "defined here" | |
else: | |
tag = "inherited from %s" % classname(thisclass, | |
object.__module__) | |
sort_attributes(attrs, object) | |
# 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 = spilldescriptors("Readonly properties %s:\n" % tag, attrs, | |
lambda t: t[1] == 'readonly property') | |
attrs = spilldescriptors("Data descriptors %s:\n" % tag, attrs, | |
lambda t: t[1] == 'data descriptor') | |
attrs = spilldata("Data and other attributes %s:\n" % tag, attrs, | |
lambda t: t[1] == 'data') | |
assert attrs == [] | |
attrs = inherited | |
contents = '\n'.join(contents) | |
if not contents: | |
return title + '\n' | |
return title + '\n' + self.indent(contents.rstrip(), ' | ') + '\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__ | |
name = name or realname | |
note = '' | |
skipdocs = 0 | |
if _is_bound_method(object): | |
imclass = object.__self__.__class__ | |
if cl: | |
if imclass is not cl: | |
note = ' from ' + classname(imclass, mod) | |
else: | |
if object.__self__ is not None: | |
note = ' method of %s instance' % classname( | |
object.__self__.__class__, mod) | |
else: | |
note = ' unbound %s method' % classname(imclass,mod) | |
if (inspect.iscoroutinefunction(object) or | |
inspect.isasyncgenfunction(object)): | |
asyncqualifier = 'async ' | |
else: | |
asyncqualifier = '' | |
if name == realname: | |
title = self.bold(realname) | |
else: | |
if cl and inspect.getattr_static(cl, realname, []) is object: | |
skipdocs = 1 | |
title = self.bold(name) + ' = ' + realname | |
argspec = None | |
if inspect.isroutine(object): | |
try: | |
signature = inspect.signature(object) | |
except (ValueError, TypeError): | |
signature = None | |
if signature: | |
argspec = str(signature) | |
if realname == '<lambda>': | |
title = self.bold(name) + ' lambda ' | |
# XXX lambda's won't usually have func_annotations['return'] | |
# since the syntax doesn't support but it is possible. | |
# So removing parentheses isn't truly safe. | |
argspec = argspec[1:-1] # remove parentheses | |
if not argspec: | |
argspec = '(...)' | |
decl = asyncqualifier + title + argspec + note | |
if skipdocs: | |
return decl + '\n' | |
else: | |
doc = getdoc(object) or '' | |
return decl + '\n' + (doc and self.indent(doc).rstrip() + '\n') | |
def docdata(self, object, name=None, mod=None, cl=None): | |
"""Produce text documentation for a data descriptor.""" | |
results = [] | |
push = results.append | |
if name: | |
push(self.bold(name)) | |
push('\n') | |
doc = getdoc(object) or '' | |
if doc: | |
push(self.indent(doc)) | |
push('\n') | |
return ''.join(results) | |
docproperty = docdata | |
def docother(self, object, name=None, mod=None, parent=None, maxlen=None, doc=None): | |
"""Produce text documentation for a data object.""" | |
repr = self.repr(object) | |
if maxlen: | |
line = (name and name + ' = ' or '') + repr | |
chop = maxlen - len(line) | |
if chop < 0: repr = repr[:chop] + '...' | |
line = (name and self.bold(name) + ' = ' or '') + repr | |
if doc is not None: | |
line += '\n' + self.indent(str(doc)) | |
return line | |
class _PlainTextDoc(TextDoc): | |
"""Subclass of TextDoc which overrides string styling""" | |
def bold(self, text): | |
return text | |
# --------------------------------------------------------- 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 not hasattr(sys.stdin, "isatty"): | |
return plainpager | |
if not hasattr(sys.stdout, "isatty"): | |
return plainpager | |
if not sys.stdin.isatty() or not sys.stdout.isatty(): | |
return plainpager | |
use_pager = os.environ.get('MANPAGER') or os.environ.get('PAGER') | |
if use_pager: | |
if sys.platform == 'win32': # pipes completely broken in Windows | |
return lambda text: tempfilepager(plain(text), use_pager) | |
elif os.environ.get('TERM') in ('dumb', 'emacs'): | |
return lambda text: pipepager(plain(text), use_pager) | |
else: | |
return lambda text: pipepager(text, use_pager) | |
if os.environ.get('TERM') in ('dumb', 'emacs'): | |
return plainpager | |
if sys.platform == 'win32': | |
return lambda text: tempfilepager(plain(text), 'more <') | |
if hasattr(os, 'system') and os.system('(pager) 2>/dev/null') == 0: | |
return lambda text: pipepager(text, 'pager') | |
if hasattr(os, 'system') and os.system('(less) 2>/dev/null') == 0: | |
return lambda text: pipepager(text, 'less') | |
import tempfile | |
(fd, filename) = tempfile.mkstemp() | |
os.close(fd) | |
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 plain(text): | |
"""Remove boldface formatting from text.""" | |
return re.sub('.\b', '', text) | |
def pipepager(text, cmd): | |
"""Page through text by feeding it to another program.""" | |
import subprocess | |
proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE) | |
try: | |
with io.TextIOWrapper(proc.stdin, errors='backslashreplace') as pipe: | |
try: | |
pipe.write(text) | |
except KeyboardInterrupt: | |
# We've hereby abandoned whatever text hasn't been written, | |
# but the pager is still in control of the terminal. | |
pass | |
except OSError: | |
pass # Ignore broken pipes caused by quitting the pager program. | |
while True: | |
try: | |
proc.wait() | |
break | |
except KeyboardInterrupt: | |
# Ignore ctl-c like the pager itself does. Otherwise the pager is | |
# left running and the terminal is in raw mode and unusable. | |
pass | |
def tempfilepager(text, cmd): | |
"""Page through text by invoking a program on a temporary file.""" | |
import tempfile | |
filename = tempfile.mktemp() | |
with open(filename, 'w', errors='backslashreplace') as file: | |
file.write(text) | |
try: | |
os.system(cmd + ' "' + filename + '"') | |
finally: | |
os.unlink(filename) | |
def _escape_stdout(text): | |
# Escape non-encodable characters to avoid encoding errors later | |
encoding = getattr(sys.stdout, 'encoding', None) or 'utf-8' | |
return text.encode(encoding, 'backslashreplace').decode(encoding) | |
def ttypager(text): | |
"""Page through text on a text terminal.""" | |
lines = plain(_escape_stdout(text)).split('\n') | |
try: | |
import tty | |
fd = sys.stdin.fileno() | |
old = tty.tcgetattr(fd) | |
tty.setcbreak(fd) | |
getchar = lambda: sys.stdin.read(1) | |
except (ImportError, AttributeError, io.UnsupportedOperation): | |
tty = None | |
getchar = lambda: sys.stdin.readline()[:-1][:1] | |
try: | |
try: | |
h = int(os.environ.get('LINES', 0)) | |
except ValueError: | |
h = 0 | |
if h <= 1: | |
h = 25 | |
r = inc = h - 1 | |
sys.stdout.write('\n'.join(lines[:inc]) + '\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' + '\n'.join(lines[r:r+inc]) + '\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(_escape_stdout(text))) | |
def describe(thing): | |
"""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__ | |
else: | |
return 'module ' + thing.__name__ | |
if inspect.isbuiltin(thing): | |
return 'built-in function ' + thing.__name__ | |
if inspect.isgetsetdescriptor(thing): | |
return 'getset descriptor %s.%s.%s' % ( | |
thing.__objclass__.__module__, thing.__objclass__.__name__, | |
thing.__name__) | |
if inspect.ismemberdescriptor(thing): | |
return 'member descriptor %s.%s.%s' % ( | |
thing.__objclass__.__module__, thing.__objclass__.__name__, | |
thing.__name__) | |
if inspect.isclass(thing): | |
return 'class ' + thing.__name__ | |
if inspect.isfunction(thing): | |
return 'function ' + thing.__name__ | |
if inspect.ismethod(thing): | |
return 'method ' + thing.__name__ | |
return type(thing).__name__ | |
def locate(path, forceload=0): | |
"""Locate an object by name or dotted path, importing as necessary.""" | |
parts = [part for part in path.split('.') if part] | |
module, n = None, 0 | |
while n < len(parts): | |
nextmodule = safeimport('.'.join(parts[:n+1]), forceload) | |
if nextmodule: module, n = nextmodule, n + 1 | |
else: break | |
if module: | |
object = module | |
else: | |
object = builtins | |
for part in parts[n:]: | |
try: | |
object = getattr(object, part) | |
except AttributeError: | |
return None | |
return object | |
# --------------------------------------- interactive interpreter interface | |
text = TextDoc() | |
plaintext = _PlainTextDoc() | |
html = HTMLDoc() | |
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) | |
if object is None: | |
raise ImportError('''\ | |
No Python documentation found for %r. | |
Use help() to get the interactive help utility. | |
Use help(str) for help on the str class.''' % thing) | |
return object, thing | |
else: | |
name = getattr(thing, '__name__', None) | |
return thing, name if isinstance(name, str) else None | |
def render_doc(thing, title='Python Library Documentation: %s', forceload=0, | |
renderer=None): | |
"""Render text documentation, given an object or a path to an object.""" | |
if renderer is None: | |
renderer = text | |
object, name = resolve(thing, forceload) | |
desc = describe(object) | |
module = inspect.getmodule(object) | |
if name and '.' in name: | |
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 | |
inspect.isdatadescriptor(object)): | |
# If the passed object is a piece of data or an instance, | |
# document its available methods instead of its value. | |
object = type(object) | |
desc += ' object' | |
return title % desc + '\n\n' + renderer.document(object, name) | |
def doc(thing, title='Python Library Documentation: %s', forceload=0, | |
output=None): | |
"""Display text documentation, given an object or a path to an object.""" | |
if output is None: | |
pager(render_doc(thing, title, forceload)) | |
else: | |
output.write(render_doc(thing, title, forceload, plaintext)) | |
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)) | |
sys.stdout.write(page) | |
def writedocs(dir, pkgpath='', done=None): | |
"""Write out HTML documentation for all modules in a directory tree.""" | |
if done is None: done = {} | |
for importer, modname, ispkg in pkgutil.walk_packages([dir], pkgpath): | |
writedoc(modname) | |
return | |
class Helper: | |
# These dictionaries map a topic name to either an alias, or a tuple | |
# (label, seealso-items). The "label" is the label of the corresponding | |
# section in the .rst file under Doc/ and an index into the dictionary | |
# in pydoc_data/topics.py. | |
# | |
# CAUTION: if you change one of these dictionaries, be sure to adapt the | |
# list of needed labels in Doc/tools/extensions/pyspecific.py and | |
# regenerate the pydoc_data/topics.py file by running | |
# make pydoc-topics | |
# in Doc/ and copying the output file into the Lib/ directory. | |
keywords = { | |
'False': '', | |
'None': '', | |
'True': '', | |
'and': 'BOOLEAN', | |
'as': 'with', | |
'assert': ('assert', ''), | |
'async': ('async', ''), | |
'await': ('await', ''), | |
'break': ('break', 'while for'), | |
'class': ('class', 'CLASSES SPECIALMETHODS'), | |
'continue': ('continue', 'while for'), | |
'def': ('function', ''), | |
'del': ('del', 'BASICMETHODS'), | |
'elif': 'if', | |
'else': ('else', 'while for'), | |
'except': 'try', | |
'finally': 'try', | |
'for': ('for', 'break continue while'), | |
'from': 'import', | |
'global': ('global', 'nonlocal NAMESPACES'), | |
'if': ('if', 'TRUTHVALUE'), | |
'import': ('import', 'MODULES'), | |
'in': ('in', 'SEQUENCEMETHODS'), | |
'is': 'COMPARISON', | |
'lambda': ('lambda', 'FUNCTIONS'), | |
'nonlocal': ('nonlocal', 'global NAMESPACES'), | |
'not': 'BOOLEAN', | |
'or': 'BOOLEAN', | |
'pass': ('pass', ''), | |
'raise': ('raise', 'EXCEPTIONS'), | |
'return': ('return', 'FUNCTIONS'), | |
'try': ('try', 'EXCEPTIONS'), | |
'while': ('while', 'break continue if TRUTHVALUE'), | |
'with': ('with', 'CONTEXTMANAGERS EXCEPTIONS yield'), | |
'yield': ('yield', ''), | |
} | |
# Either add symbols to this dictionary or to the symbols dictionary | |
# directly: Whichever is easier. They are merged later. | |
_strprefixes = [p + q for p in ('b', 'f', 'r', 'u') for q in ("'", '"')] | |
_symbols_inverse = { | |
'STRINGS' : ("'", "'''", '"', '"""', *_strprefixes), | |
'OPERATORS' : ('+', '-', '*', '**', '/', '//', '%', '<<', '>>', '&', | |
'|', '^', '~', '<', '>', '<=', '>=', '==', '!=', '<>'), | |
'COMPARISON' : ('<', '>', '<=', '>=', '==', '!=', '<>'), | |
'UNARY' : ('-', '~'), | |
'AUGMENTEDASSIGNMENT' : ('+=', '-=', '*=', '/=', '%=', '&=', '|=', | |
'^=', '<<=', '>>=', '**=', '//='), | |
'BITWISE' : ('<<', '>>', '&', '|', '^', '~'), | |
'COMPLEX' : ('j', 'J') | |
} | |
symbols = { | |
'%': 'OPERATORS FORMATTING', | |
'**': 'POWER', | |
',': 'TUPLES LISTS FUNCTIONS', | |
'.': 'ATTRIBUTES FLOAT MODULES OBJECTS', | |
'...': 'ELLIPSIS', | |
':': 'SLICINGS DICTIONARYLITERALS', | |
'@': 'def class', | |
'\\': 'STRINGS', | |
'_': 'PRIVATENAMES', | |
'__': 'PRIVATENAMES SPECIALMETHODS', | |
'`': 'BACKQUOTES', | |
'(': 'TUPLES FUNCTIONS CALLS', | |
')': 'TUPLES FUNCTIONS CALLS', | |
'[': 'LISTS SUBSCRIPTS SLICINGS', | |
']': 'LISTS SUBSCRIPTS SLICINGS' | |
} | |
for topic, symbols_ in _symbols_inverse.items(): | |
for symbol in symbols_: | |
topics = symbols.get(symbol, topic) | |
if topic not in topics: | |
topics = topics + ' ' + topic | |
symbols[symbol] = topics | |
topics = { | |
'TYPES': ('types', 'STRINGS UNICODE NUMBERS SEQUENCES MAPPINGS ' | |
'FUNCTIONS CLASSES MODULES FILES inspect'), | |
'STRINGS': ('strings', 'str UNICODE SEQUENCES STRINGMETHODS ' | |
'FORMATTING TYPES'), | |
'STRINGMETHODS': ('string-methods', 'STRINGS FORMATTING'), | |
'FORMATTING': ('formatstrings', 'OPERATORS'), | |
'UNICODE': ('strings', 'encodings unicode SEQUENCES STRINGMETHODS ' | |
'FORMATTING TYPES'), | |
'NUMBERS': ('numbers', 'INTEGER FLOAT COMPLEX TYPES'), | |
'INTEGER': ('integers', 'int range'), | |
'FLOAT': ('floating', 'float math'), | |
'COMPLEX': ('imaginary', 'complex cmath'), | |
'SEQUENCES': ('typesseq', 'STRINGMETHODS FORMATTING range LISTS'), | |
'MAPPINGS': 'DICTIONARIES', | |
'FUNCTIONS': ('typesfunctions', 'def TYPES'), | |
'METHODS': ('typesmethods', 'class def CLASSES TYPES'), | |
'CODEOBJECTS': ('bltin-code-objects', 'compile FUNCTIONS TYPES'), | |
'TYPEOBJECTS': ('bltin-type-objects', 'types TYPES'), | |
'FRAMEOBJECTS': 'TYPES', | |
'TRACEBACKS': 'TYPES', | |
'NONE': ('bltin-null-object', ''), | |
'ELLIPSIS': ('bltin-ellipsis-object', 'SLICINGS'), | |
'SPECIALATTRIBUTES': ('specialattrs', ''), | |
'CLASSES': ('types', 'class SPECIALMETHODS PRIVATENAMES'), | |
'MODULES': ('typesmodules', 'import'), | |
'PACKAGES': 'import', | |
'EXPRESSIONS': ('operator-summary', 'lambda or and not in is BOOLEAN ' | |
'COMPARISON BITWISE SHIFTING BINARY FORMATTING POWER ' | |
'UNARY ATTRIBUTES SUBSCRIPTS SLICINGS CALLS TUPLES ' | |
'LISTS DICTIONARIES'), | |
'OPERATORS': 'EXPRESSIONS', | |
'PRECEDENCE': 'EXPRESSIONS', | |
'OBJECTS': ('objects', 'TYPES'), | |
'SPECIALMETHODS': ('specialnames', 'BASICMETHODS ATTRIBUTEMETHODS ' | |
'CALLABLEMETHODS SEQUENCEMETHODS MAPPINGMETHODS ' | |
'NUMBERMETHODS CLASSES'), | |
'BASICMETHODS': ('customization', 'hash repr str SPECIALMETHODS'), | |
'ATTRIBUTEMETHODS': ('attribute-access', 'ATTRIBUTES SPECIALMETHODS'), | |
'CALLABLEMETHODS': ('callable-types', 'CALLS SPECIALMETHODS'), | |
'SEQUENCEMETHODS': ('sequence-types', 'SEQUENCES SEQUENCEMETHODS ' | |
'SPECIALMETHODS'), | |