blob: c92b324afd26cb5b04d3304f58485e87a5d0c58f [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Ka-Ping Yee1d384632001-03-01 00:24:32 +00002"""Generate Python documentation in HTML or text for interactive use.
Ka-Ping Yeedd175342001-02-27 14:43:46 +00003
R David Murray3d050dd2014-04-19 12:59:30 -04004At the Python interactive prompt, calling help(thing) on a Python object
5documents the object, and calling help() starts up an interactive
6help session.
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00007
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00008Or, at the shell command line outside of Python:
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00009
Ka-Ping Yee37f7b382001-03-23 00:12:53 +000010Run "pydoc <name>" to show documentation on something. <name> may be
11the name of a function, module, package, or a dotted reference to a
12class or function within a module or module in a package. If the
13argument contains a path segment delimiter (e.g. slash on Unix,
14backslash on Windows) it is treated as the path to a Python source file.
Ka-Ping Yee66efbc72001-03-01 13:55:20 +000015
Ka-Ping Yee37f7b382001-03-23 00:12:53 +000016Run "pydoc -k <keyword>" to search for a keyword in the synopsis lines
17of all available modules.
Ka-Ping Yee66efbc72001-03-01 13:55:20 +000018
Nick Coghlan7bb30b72010-12-03 09:29:11 +000019Run "pydoc -p <port>" to start an HTTP server on the given port on the
20local machine. Port number 0 can be used to get an arbitrary unused port.
21
22Run "pydoc -b" to start an HTTP server on an arbitrary unused port and
23open a Web browser to interactively browse documentation. The -p option
24can be used with the -b option to explicitly specify the server port.
Ka-Ping Yee66efbc72001-03-01 13:55:20 +000025
Ka-Ping Yee37f7b382001-03-23 00:12:53 +000026Run "pydoc -w <name>" to write out the HTML documentation for a module
27to a file named "<name>.html".
Skip Montanaro4997a692003-09-10 16:47:51 +000028
29Module docs for core modules are assumed to be in
30
Alexander Belopolskya47bbf52010-11-18 01:52:54 +000031 http://docs.python.org/X.Y/library/
Skip Montanaro4997a692003-09-10 16:47:51 +000032
33This can be overridden by setting the PYTHONDOCS environment variable
34to a different URL or to a local directory containing the Library
35Reference Manual pages.
Ka-Ping Yee66efbc72001-03-01 13:55:20 +000036"""
Alexander Belopolskya47bbf52010-11-18 01:52:54 +000037__all__ = ['help']
Ka-Ping Yeedd175342001-02-27 14:43:46 +000038__author__ = "Ka-Ping Yee <ping@lfw.org>"
Ka-Ping Yee6f3f9a42001-02-27 22:42:36 +000039__date__ = "26 February 2001"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040
Martin v. Löwis6fe8f192004-11-14 10:21:04 +000041__credits__ = """Guido van Rossum, for an excellent programming language.
Ka-Ping Yee5e2b1732001-02-27 23:35:09 +000042Tommy Burnette, the original creator of manpy.
Ka-Ping Yee6f3f9a42001-02-27 22:42:36 +000043Paul Prescod, for all his work on onlinehelp.
44Richard Chamberlain, for the first implementation of textdoc.
Raymond Hettingered2dbe32005-01-01 07:51:01 +000045"""
Ka-Ping Yeedd175342001-02-27 14:43:46 +000046
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +000047# Known bugs that can't be fixed here:
Brett Cannonf4ba4ec2013-06-15 14:25:04 -040048# - synopsis() cannot be prevented from clobbering existing
49# loaded modules.
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +000050# - If the __file__ attribute on a module is a relative path and
51# the current directory is changed with os.chdir(), an incorrect
52# path will be displayed.
Ka-Ping Yee66efbc72001-03-01 13:55:20 +000053
Nick Coghlan7bb30b72010-12-03 09:29:11 +000054import builtins
Brett Cannond5e6f2e2013-06-11 17:09:36 -040055import importlib._bootstrap
Brett Cannoncb66eb02012-05-11 12:58:42 -040056import importlib.machinery
Brett Cannonf4ba4ec2013-06-15 14:25:04 -040057import importlib.util
Nick Coghlan7bb30b72010-12-03 09:29:11 +000058import inspect
Victor Stinnere6c910e2011-06-30 15:55:43 +020059import io
60import os
Nick Coghlan7bb30b72010-12-03 09:29:11 +000061import pkgutil
62import platform
63import re
Victor Stinnere6c910e2011-06-30 15:55:43 +020064import sys
Nick Coghlan7bb30b72010-12-03 09:29:11 +000065import time
Victor Stinnere6c910e2011-06-30 15:55:43 +020066import tokenize
Zachary Wareeb432142014-07-10 11:18:00 -050067import urllib.parse
Nick Coghlan7bb30b72010-12-03 09:29:11 +000068import warnings
Alexander Belopolskya47bbf52010-11-18 01:52:54 +000069from collections import deque
Nick Coghlan7bb30b72010-12-03 09:29:11 +000070from reprlib import Repr
Victor Stinner7fa767e2014-03-20 09:16:38 +010071from traceback import format_exception_only
Nick Coghlan7bb30b72010-12-03 09:29:11 +000072
73
Ka-Ping Yeedd175342001-02-27 14:43:46 +000074# --------------------------------------------------------- common routines
75
Ka-Ping Yeedd175342001-02-27 14:43:46 +000076def pathdirs():
77 """Convert sys.path into a list of absolute, existing, unique paths."""
78 dirs = []
Ka-Ping Yee1d384632001-03-01 00:24:32 +000079 normdirs = []
Ka-Ping Yeedd175342001-02-27 14:43:46 +000080 for dir in sys.path:
81 dir = os.path.abspath(dir or '.')
Ka-Ping Yee1d384632001-03-01 00:24:32 +000082 normdir = os.path.normcase(dir)
83 if normdir not in normdirs and os.path.isdir(dir):
Ka-Ping Yeedd175342001-02-27 14:43:46 +000084 dirs.append(dir)
Ka-Ping Yee1d384632001-03-01 00:24:32 +000085 normdirs.append(normdir)
Ka-Ping Yeedd175342001-02-27 14:43:46 +000086 return dirs
87
88def getdoc(object):
89 """Get the doc string or comments for an object."""
Ka-Ping Yee3bda8792001-03-23 13:17:50 +000090 result = inspect.getdoc(object) or inspect.getcomments(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +000091 return result and re.sub('^ *\n', '', result.rstrip()) or ''
Ka-Ping Yeedd175342001-02-27 14:43:46 +000092
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +000093def splitdoc(doc):
94 """Split a doc string into a synopsis line (if any) and the rest."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +000095 lines = doc.strip().split('\n')
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +000096 if len(lines) == 1:
97 return lines[0], ''
Neal Norwitz9d72bb42007-04-17 08:48:32 +000098 elif len(lines) >= 2 and not lines[1].rstrip():
99 return lines[0], '\n'.join(lines[2:])
100 return '', '\n'.join(lines)
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +0000101
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000102def classname(object, modname):
103 """Get a class name and qualify it with a module name if necessary."""
104 name = object.__name__
105 if object.__module__ != modname:
106 name = object.__module__ + '.' + name
107 return name
108
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000109def isdata(object):
Georg Brandl94432422005-07-22 21:52:25 +0000110 """Check if an object is of a type that probably means it's data."""
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000111 return not (inspect.ismodule(object) or inspect.isclass(object) or
112 inspect.isroutine(object) or inspect.isframe(object) or
113 inspect.istraceback(object) or inspect.iscode(object))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000114
115def replace(text, *pairs):
116 """Do a series of global replacements on a string."""
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +0000117 while pairs:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000118 text = pairs[1].join(text.split(pairs[0]))
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +0000119 pairs = pairs[2:]
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000120 return text
121
122def cram(text, maxlen):
123 """Omit part of a string if needed to make it fit in a maximum length."""
124 if len(text) > maxlen:
Raymond Hettingerfca3bb62002-10-21 04:44:11 +0000125 pre = max(0, (maxlen-3)//2)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000126 post = max(0, maxlen-3-pre)
127 return text[:pre] + '...' + text[len(text)-post:]
128 return text
129
Brett Cannon84601f12004-06-19 01:22:48 +0000130_re_stripid = re.compile(r' at 0x[0-9a-f]{6,16}(>+)$', re.IGNORECASE)
Ka-Ping Yee1d384632001-03-01 00:24:32 +0000131def stripid(text):
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000132 """Remove the hexadecimal id from a Python object representation."""
Brett Cannonc6c1f472004-06-19 01:02:51 +0000133 # The behaviour of %p is implementation-dependent in terms of case.
Ezio Melotti412c95a2010-02-16 23:31:04 +0000134 return _re_stripid.sub(r'\1', text)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000135
Brett Cannonc6c1f472004-06-19 01:02:51 +0000136def _is_some_method(obj):
R David Murrayac0cea52013-03-19 02:47:44 -0400137 return (inspect.isfunction(obj) or
138 inspect.ismethod(obj) or
139 inspect.isbuiltin(obj) or
140 inspect.ismethoddescriptor(obj))
Tim Peters536d2262001-09-20 05:13:38 +0000141
Larry Hastings24a882b2014-02-20 23:34:46 -0800142def _is_bound_method(fn):
143 """
144 Returns True if fn is a bound method, regardless of whether
145 fn was implemented in Python or in C.
146 """
147 if inspect.ismethod(fn):
148 return True
149 if inspect.isbuiltin(fn):
150 self = getattr(fn, '__self__', None)
151 return not (inspect.ismodule(self) or (self is None))
152 return False
153
154
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000155def allmethods(cl):
156 methods = {}
Tim Peters536d2262001-09-20 05:13:38 +0000157 for key, value in inspect.getmembers(cl, _is_some_method):
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000158 methods[key] = 1
159 for base in cl.__bases__:
160 methods.update(allmethods(base)) # all your base are belong to us
161 for key in methods.keys():
162 methods[key] = getattr(cl, key)
163 return methods
164
Tim Petersfa26f7c2001-09-24 08:05:11 +0000165def _split_list(s, predicate):
166 """Split sequence s via predicate, and return pair ([true], [false]).
167
168 The return value is a 2-tuple of lists,
169 ([x for x in s if predicate(x)],
170 [x for x in s if not predicate(x)])
171 """
172
Tim Peters28355492001-09-23 21:29:55 +0000173 yes = []
174 no = []
Tim Petersfa26f7c2001-09-24 08:05:11 +0000175 for x in s:
176 if predicate(x):
177 yes.append(x)
Tim Peters28355492001-09-23 21:29:55 +0000178 else:
Tim Petersfa26f7c2001-09-24 08:05:11 +0000179 no.append(x)
Tim Peters28355492001-09-23 21:29:55 +0000180 return yes, no
181
Raymond Hettinger1103d052011-03-25 14:15:24 -0700182def visiblename(name, all=None, obj=None):
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000183 """Decide whether to show documentation on a variable."""
Brett Cannond340b432012-08-06 17:19:22 -0400184 # Certain special names are redundant or internal.
Eric Snowb523f842013-11-22 09:05:39 -0700185 # XXX Remove __initializing__?
Brett Cannond340b432012-08-06 17:19:22 -0400186 if name in {'__author__', '__builtins__', '__cached__', '__credits__',
Eric Snowb523f842013-11-22 09:05:39 -0700187 '__date__', '__doc__', '__file__', '__spec__',
Brett Cannond340b432012-08-06 17:19:22 -0400188 '__loader__', '__module__', '__name__', '__package__',
189 '__path__', '__qualname__', '__slots__', '__version__'}:
Raymond Hettinger68272942011-03-18 02:22:15 -0700190 return 0
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000191 # Private names are hidden, but special names are displayed.
192 if name.startswith('__') and name.endswith('__'): return 1
Raymond Hettinger1103d052011-03-25 14:15:24 -0700193 # Namedtuples have public fields and methods with a single leading underscore
194 if name.startswith('_') and hasattr(obj, '_fields'):
195 return True
Skip Montanaroa5616d22004-06-11 04:46:12 +0000196 if all is not None:
197 # only document that which the programmer exported in __all__
198 return name in all
199 else:
200 return not name.startswith('_')
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000201
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +0000202def classify_class_attrs(object):
203 """Wrap inspect.classify_class_attrs, with fixup for data descriptors."""
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000204 results = []
205 for (name, kind, cls, value) in inspect.classify_class_attrs(object):
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +0000206 if inspect.isdatadescriptor(value):
207 kind = 'data descriptor'
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000208 results.append((name, kind, cls, value))
209 return results
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +0000210
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000211# ----------------------------------------------------- module manipulation
212
213def ispackage(path):
214 """Guess whether a path refers to a package directory."""
215 if os.path.isdir(path):
Raymond Hettingerdbecd932005-02-06 06:57:08 +0000216 for ext in ('.py', '.pyc', '.pyo'):
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000217 if os.path.isfile(os.path.join(path, '__init__' + ext)):
Tim Petersbc0e9102002-04-04 22:55:58 +0000218 return True
219 return False
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000220
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221def source_synopsis(file):
222 line = file.readline()
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000223 while line[:1] == '#' or not line.strip():
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000224 line = file.readline()
225 if not line: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000226 line = line.strip()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000227 if line[:4] == 'r"""': line = line[1:]
228 if line[:3] == '"""':
229 line = line[3:]
230 if line[-1:] == '\\': line = line[:-1]
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000231 while not line.strip():
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232 line = file.readline()
233 if not line: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000234 result = line.split('"""')[0].strip()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000235 else: result = None
236 return result
237
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000238def synopsis(filename, cache={}):
239 """Get the one-line summary out of a module file."""
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000240 mtime = os.stat(filename).st_mtime
Charles-François Natali27c4e882011-07-27 19:40:02 +0200241 lastupdate, result = cache.get(filename, (None, None))
242 if lastupdate is None or lastupdate < mtime:
Eric Snowaed5b222014-01-04 20:38:11 -0700243 # Look for binary suffixes first, falling back to source.
244 if filename.endswith(tuple(importlib.machinery.BYTECODE_SUFFIXES)):
245 loader_cls = importlib.machinery.SourcelessFileLoader
246 elif filename.endswith(tuple(importlib.machinery.EXTENSION_SUFFIXES)):
247 loader_cls = importlib.machinery.ExtensionFileLoader
248 else:
249 loader_cls = None
250 # Now handle the choice.
251 if loader_cls is None:
252 # Must be a source file.
253 try:
254 file = tokenize.open(filename)
255 except OSError:
256 # module can't be opened, so skip it
257 return None
258 # text modules can be directly examined
259 with file:
260 result = source_synopsis(file)
261 else:
262 # Must be a binary module, which has to be imported.
263 loader = loader_cls('__temp__', filename)
Eric Snow3a62d142014-01-06 20:42:59 -0700264 # XXX We probably don't need to pass in the loader here.
265 spec = importlib.util.spec_from_file_location('__temp__', filename,
266 loader=loader)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400267 try:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400268 module = importlib._bootstrap._load(spec)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400269 except:
270 return None
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000271 del sys.modules['__temp__']
Benjamin Peterson54237f92015-02-16 19:45:01 -0500272 result = module.__doc__.splitlines()[0] if module.__doc__ else None
Eric Snowaed5b222014-01-04 20:38:11 -0700273 # Cache the result.
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000274 cache[filename] = (mtime, result)
275 return result
276
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000277class ErrorDuringImport(Exception):
278 """Errors that occurred while trying to import something to document it."""
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000279 def __init__(self, filename, exc_info):
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000280 self.filename = filename
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000281 self.exc, self.value, self.tb = exc_info
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000282
283 def __str__(self):
Guido van Rossuma01a8b62007-05-27 09:20:14 +0000284 exc = self.exc.__name__
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000285 return 'problem in %s - %s: %s' % (self.filename, exc, self.value)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000286
287def importfile(path):
288 """Import a Python source file or compiled file given its path."""
Brett Cannonf4ba4ec2013-06-15 14:25:04 -0400289 magic = importlib.util.MAGIC_NUMBER
Victor Stinnere975af62011-07-04 02:08:50 +0200290 with open(path, 'rb') as file:
Brett Cannond5e6f2e2013-06-11 17:09:36 -0400291 is_bytecode = magic == file.read(len(magic))
292 filename = os.path.basename(path)
293 name, ext = os.path.splitext(filename)
294 if is_bytecode:
295 loader = importlib._bootstrap.SourcelessFileLoader(name, path)
296 else:
297 loader = importlib._bootstrap.SourceFileLoader(name, path)
Eric Snow3a62d142014-01-06 20:42:59 -0700298 # XXX We probably don't need to pass in the loader here.
299 spec = importlib.util.spec_from_file_location(name, path, loader=loader)
Brett Cannond5e6f2e2013-06-11 17:09:36 -0400300 try:
Brett Cannon2a17bde2014-05-30 14:55:29 -0400301 return importlib._bootstrap._load(spec)
Brett Cannond5e6f2e2013-06-11 17:09:36 -0400302 except:
303 raise ErrorDuringImport(path, sys.exc_info())
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000304
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000305def safeimport(path, forceload=0, cache={}):
306 """Import a module; handle errors; return None if the module isn't found.
307
308 If the module *is* found but an exception occurs, it's wrapped in an
309 ErrorDuringImport exception and reraised. Unlike __import__, if a
310 package path is specified, the module at the end of the path is returned,
311 not the package at the beginning. If the optional 'forceload' argument
312 is 1, we reload the module from disk (unless it's a dynamic extension)."""
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000313 try:
Ka-Ping Yee9a2dcf82005-11-05 05:04:41 +0000314 # If forceload is 1 and the module has been previously loaded from
315 # disk, we always have to reload the module. Checking the file's
316 # mtime isn't good enough (e.g. the module could contain a class
317 # that inherits from another module that has changed).
318 if forceload and path in sys.modules:
319 if path not in sys.builtin_module_names:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000320 # Remove the module from sys.modules and re-import to try
321 # and avoid problems with partially loaded modules.
322 # Also remove any submodules because they won't appear
323 # in the newly loaded module's namespace if they're already
324 # in sys.modules.
Ka-Ping Yee9a2dcf82005-11-05 05:04:41 +0000325 subs = [m for m in sys.modules if m.startswith(path + '.')]
326 for key in [path] + subs:
327 # Prevent garbage collection.
328 cache[key] = sys.modules[key]
329 del sys.modules[key]
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000330 module = __import__(path)
331 except:
332 # Did the error occur before or after the module was found?
333 (exc, value, tb) = info = sys.exc_info()
Raymond Hettinger54f02222002-06-01 14:18:47 +0000334 if path in sys.modules:
Fred Drakedb390c12005-10-28 14:39:47 +0000335 # An error occurred while executing the imported module.
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000336 raise ErrorDuringImport(sys.modules[path].__file__, info)
337 elif exc is SyntaxError:
338 # A SyntaxError occurred before we could execute the module.
339 raise ErrorDuringImport(value.filename, info)
Brett Cannon679ecb52013-07-04 17:51:50 -0400340 elif exc is ImportError and value.name == path:
Brett Cannonfd074152012-04-14 14:10:13 -0400341 # No such module in the path.
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000342 return None
343 else:
344 # Some other error occurred during the importing process.
345 raise ErrorDuringImport(path, sys.exc_info())
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000346 for part in path.split('.')[1:]:
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000347 try: module = getattr(module, part)
348 except AttributeError: return None
349 return module
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000350
351# ---------------------------------------------------- formatter base class
352
353class Doc:
Alexander Belopolskya47bbf52010-11-18 01:52:54 +0000354
355 PYTHONDOCS = os.environ.get("PYTHONDOCS",
356 "http://docs.python.org/%d.%d/library"
357 % sys.version_info[:2])
358
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000359 def document(self, object, name=None, *args):
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000360 """Generate documentation for an object."""
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000361 args = (object, name) + args
Brett Cannon28a4f0f2003-06-11 23:38:55 +0000362 # 'try' clause is to attempt to handle the possibility that inspect
363 # identifies something in a way that pydoc itself has issues handling;
364 # think 'super' and how it is a descriptor (which raises the exception
365 # by lacking a __name__ attribute) and an instance.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000366 if inspect.isgetsetdescriptor(object): return self.docdata(*args)
367 if inspect.ismemberdescriptor(object): return self.docdata(*args)
Brett Cannon28a4f0f2003-06-11 23:38:55 +0000368 try:
369 if inspect.ismodule(object): return self.docmodule(*args)
370 if inspect.isclass(object): return self.docclass(*args)
371 if inspect.isroutine(object): return self.docroutine(*args)
372 except AttributeError:
373 pass
Johannes Gijsbers8de645a2004-11-07 19:16:05 +0000374 if isinstance(object, property): return self.docproperty(*args)
Guido van Rossum68468eb2003-02-27 20:14:51 +0000375 return self.docother(*args)
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000376
377 def fail(self, object, name=None, *args):
378 """Raise an exception for unimplemented types."""
379 message = "don't know how to document object%s of type %s" % (
380 name and ' ' + repr(name), type(object).__name__)
Collin Winterce36ad82007-08-30 01:19:48 +0000381 raise TypeError(message)
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000382
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000383 docmodule = docclass = docroutine = docother = docproperty = docdata = fail
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000384
Skip Montanaro4997a692003-09-10 16:47:51 +0000385 def getdocloc(self, object):
386 """Return the location of module docs or None"""
387
388 try:
389 file = inspect.getabsfile(object)
390 except TypeError:
391 file = '(built-in)'
392
Alexander Belopolskya47bbf52010-11-18 01:52:54 +0000393 docloc = os.environ.get("PYTHONDOCS", self.PYTHONDOCS)
394
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100395 basedir = os.path.join(sys.base_exec_prefix, "lib",
Alexander Belopolskya47bbf52010-11-18 01:52:54 +0000396 "python%d.%d" % sys.version_info[:2])
Skip Montanaro4997a692003-09-10 16:47:51 +0000397 if (isinstance(object, type(os)) and
398 (object.__name__ in ('errno', 'exceptions', 'gc', 'imp',
399 'marshal', 'posix', 'signal', 'sys',
Georg Brandl2067bfd2008-05-25 13:05:15 +0000400 '_thread', 'zipimport') or
Skip Montanaro4997a692003-09-10 16:47:51 +0000401 (file.startswith(basedir) and
Brian Curtin49c284c2010-03-31 03:19:28 +0000402 not file.startswith(os.path.join(basedir, 'site-packages')))) and
Brian Curtinedef05b2010-04-01 04:05:25 +0000403 object.__name__ not in ('xml.etree', 'test.pydoc_mod')):
Skip Montanaro4997a692003-09-10 16:47:51 +0000404 if docloc.startswith("http://"):
Georg Brandl86def6c2008-01-21 20:36:10 +0000405 docloc = "%s/%s" % (docloc.rstrip("/"), object.__name__)
Skip Montanaro4997a692003-09-10 16:47:51 +0000406 else:
Georg Brandl86def6c2008-01-21 20:36:10 +0000407 docloc = os.path.join(docloc, object.__name__ + ".html")
Skip Montanaro4997a692003-09-10 16:47:51 +0000408 else:
409 docloc = None
410 return docloc
411
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000412# -------------------------------------------- HTML documentation generator
413
414class HTMLRepr(Repr):
415 """Class for safely making an HTML representation of a Python object."""
416 def __init__(self):
417 Repr.__init__(self)
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000418 self.maxlist = self.maxtuple = 20
419 self.maxdict = 10
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000420 self.maxstring = self.maxother = 100
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000421
422 def escape(self, text):
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +0000423 return replace(text, '&', '&amp;', '<', '&lt;', '>', '&gt;')
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000424
425 def repr(self, object):
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000426 return Repr.repr(self, object)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000427
428 def repr1(self, x, level):
Skip Montanaro0fe8fce2003-06-27 15:45:41 +0000429 if hasattr(type(x), '__name__'):
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000430 methodname = 'repr_' + '_'.join(type(x).__name__.split())
Skip Montanaro0fe8fce2003-06-27 15:45:41 +0000431 if hasattr(self, methodname):
432 return getattr(self, methodname)(x, level)
433 return self.escape(cram(stripid(repr(x)), self.maxother))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000434
435 def repr_string(self, x, level):
Ka-Ping Yeea2fe1032001-03-02 01:19:14 +0000436 test = cram(x, self.maxstring)
437 testrepr = repr(test)
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +0000438 if '\\' in test and '\\' not in replace(testrepr, r'\\', ''):
Ka-Ping Yeea2fe1032001-03-02 01:19:14 +0000439 # Backslashes are only literal in the string and are never
440 # needed to make any special characters, so show a raw string.
441 return 'r' + testrepr[0] + self.escape(test) + testrepr[0]
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +0000442 return re.sub(r'((\\[\\abfnrtv\'"]|\\[0-9]..|\\x..|\\u....)+)',
Raymond Hettinger95f285c2009-02-24 23:41:47 +0000443 r'<font color="#c040c0">\1</font>',
Ka-Ping Yeea2fe1032001-03-02 01:19:14 +0000444 self.escape(testrepr))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000445
Skip Montanarodf708782002-03-07 22:58:02 +0000446 repr_str = repr_string
447
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000448 def repr_instance(self, x, level):
449 try:
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000450 return self.escape(cram(stripid(repr(x)), self.maxstring))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000451 except:
452 return self.escape('<%s instance>' % x.__class__.__name__)
453
454 repr_unicode = repr_string
455
456class HTMLDoc(Doc):
457 """Formatter class for HTML documentation."""
458
459 # ------------------------------------------- HTML formatting utilities
460
461 _repr_instance = HTMLRepr()
462 repr = _repr_instance.repr
463 escape = _repr_instance.escape
464
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000465 def page(self, title, contents):
466 """Format an HTML page."""
Georg Brandl388faac2009-04-10 08:31:48 +0000467 return '''\
Georg Brandle6066942009-04-10 08:28:28 +0000468<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000469<html><head><title>Python: %s</title>
Georg Brandl388faac2009-04-10 08:31:48 +0000470<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Raymond Hettinger95f285c2009-02-24 23:41:47 +0000471</head><body bgcolor="#f0f0f8">
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000472%s
473</body></html>''' % (title, contents)
474
475 def heading(self, title, fgcol, bgcol, extras=''):
476 """Format a page heading."""
477 return '''
Tim Peters59ed4482001-10-31 04:20:26 +0000478<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000479<tr bgcolor="%s">
Tim Peters2306d242001-09-25 03:18:32 +0000480<td valign=bottom>&nbsp;<br>
481<font color="%s" face="helvetica, arial">&nbsp;<br>%s</font></td
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000482><td align=right valign=bottom
Ka-Ping Yee987ec902001-03-23 13:35:45 +0000483><font color="%s" face="helvetica, arial">%s</font></td></tr></table>
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000484 ''' % (bgcol, fgcol, title, fgcol, extras or '&nbsp;')
485
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000486 def section(self, title, fgcol, bgcol, contents, width=6,
487 prelude='', marginalia=None, gap='&nbsp;'):
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000488 """Format a section with a heading."""
489 if marginalia is None:
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +0000490 marginalia = '<tt>' + '&nbsp;' * width + '</tt>'
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000491 result = '''<p>
Tim Peters59ed4482001-10-31 04:20:26 +0000492<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000493<tr bgcolor="%s">
Tim Peters2306d242001-09-25 03:18:32 +0000494<td colspan=3 valign=bottom>&nbsp;<br>
495<font color="%s" face="helvetica, arial">%s</font></td></tr>
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000496 ''' % (bgcol, fgcol, title)
497 if prelude:
498 result = result + '''
Ka-Ping Yee987ec902001-03-23 13:35:45 +0000499<tr bgcolor="%s"><td rowspan=2>%s</td>
500<td colspan=2>%s</td></tr>
501<tr><td>%s</td>''' % (bgcol, marginalia, prelude, gap)
502 else:
503 result = result + '''
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000504<tr><td bgcolor="%s">%s</td><td>%s</td>''' % (bgcol, marginalia, gap)
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000505
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +0000506 return result + '\n<td width="100%%">%s</td></tr></table>' % contents
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000507
508 def bigsection(self, title, *args):
509 """Format a section with a big heading."""
Raymond Hettinger95f285c2009-02-24 23:41:47 +0000510 title = '<big><strong>%s</strong></big>' % title
Guido van Rossum68468eb2003-02-27 20:14:51 +0000511 return self.section(title, *args)
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000512
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000513 def preformat(self, text):
514 """Format literal preformatted text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000515 text = self.escape(text.expandtabs())
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +0000516 return replace(text, '\n\n', '\n \n', '\n\n', '\n \n',
517 ' ', '&nbsp;', '\n', '<br>\n')
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000518
519 def multicolumn(self, list, format, cols=4):
520 """Format a list of items into a multi-column list."""
521 result = ''
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000522 rows = (len(list)+cols-1)//cols
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000523 for col in range(cols):
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000524 result = result + '<td width="%d%%" valign=top>' % (100//cols)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000525 for i in range(rows*col, rows*col+rows):
526 if i < len(list):
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000527 result = result + format(list[i]) + '<br>\n'
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000528 result = result + '</td>'
Tim Peters59ed4482001-10-31 04:20:26 +0000529 return '<table width="100%%" summary="list"><tr>%s</tr></table>' % result
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000530
Raymond Hettinger95f285c2009-02-24 23:41:47 +0000531 def grey(self, text): return '<font color="#909090">%s</font>' % text
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000532
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000533 def namelink(self, name, *dicts):
534 """Make a link for an identifier, given name-to-URL mappings."""
535 for dict in dicts:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000536 if name in dict:
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000537 return '<a href="%s">%s</a>' % (dict[name], name)
538 return name
539
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000540 def classlink(self, object, modname):
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000541 """Make a link for a class."""
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000542 name, module = object.__name__, sys.modules.get(object.__module__)
543 if hasattr(module, name) and getattr(module, name) is object:
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000544 return '<a href="%s.html#%s">%s</a>' % (
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000545 module.__name__, name, classname(object, modname))
546 return classname(object, modname)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000547
548 def modulelink(self, object):
549 """Make a link for a module."""
550 return '<a href="%s.html">%s</a>' % (object.__name__, object.__name__)
551
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000552 def modpkglink(self, modpkginfo):
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000553 """Make a link for a module or package to display in an index."""
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000554 name, path, ispackage, shadowed = modpkginfo
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000555 if shadowed:
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000556 return self.grey(name)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000557 if path:
558 url = '%s.%s.html' % (path, name)
559 else:
560 url = '%s.html' % name
561 if ispackage:
Raymond Hettinger95f285c2009-02-24 23:41:47 +0000562 text = '<strong>%s</strong>&nbsp;(package)' % name
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000563 else:
564 text = name
565 return '<a href="%s">%s</a>' % (url, text)
566
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000567 def filelink(self, url, path):
568 """Make a link to source file."""
569 return '<a href="file:%s">%s</a>' % (url, path)
570
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000571 def markup(self, text, escape=None, funcs={}, classes={}, methods={}):
572 """Mark up some plain text, given a context of symbols to look for.
573 Each context dictionary maps object names to anchor names."""
574 escape = escape or self.escape
575 results = []
576 here = 0
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000577 pattern = re.compile(r'\b((http|ftp)://\S+[\w/]|'
578 r'RFC[- ]?(\d+)|'
Ka-Ping Yeef78a81b2001-03-27 08:13:42 +0000579 r'PEP[- ]?(\d+)|'
Neil Schemenauerd69711c2002-03-24 23:02:07 +0000580 r'(self\.)?(\w+))')
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000581 while True:
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000582 match = pattern.search(text, here)
583 if not match: break
584 start, end = match.span()
585 results.append(escape(text[here:start]))
586
Ka-Ping Yeef78a81b2001-03-27 08:13:42 +0000587 all, scheme, rfc, pep, selfdot, name = match.groups()
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000588 if scheme:
Neil Schemenauercddc1a02002-03-24 23:11:21 +0000589 url = escape(all).replace('"', '&quot;')
590 results.append('<a href="%s">%s</a>' % (url, url))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000591 elif rfc:
Ka-Ping Yeef78a81b2001-03-27 08:13:42 +0000592 url = 'http://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc)
593 results.append('<a href="%s">%s</a>' % (url, escape(all)))
594 elif pep:
Christian Heimes2202f872008-02-06 14:31:34 +0000595 url = 'http://www.python.org/dev/peps/pep-%04d/' % int(pep)
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000596 results.append('<a href="%s">%s</a>' % (url, escape(all)))
Benjamin Petersoned1160b2014-06-07 16:44:00 -0700597 elif selfdot:
598 # Create a link for methods like 'self.method(...)'
599 # and use <strong> for attributes like 'self.attr'
600 if text[end:end+1] == '(':
601 results.append('self.' + self.namelink(name, methods))
602 else:
603 results.append('self.<strong>%s</strong>' % name)
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000604 elif text[end:end+1] == '(':
605 results.append(self.namelink(name, methods, funcs, classes))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000606 else:
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000607 results.append(self.namelink(name, classes))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000608 here = end
609 results.append(escape(text[here:]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000610 return ''.join(results)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000611
612 # ---------------------------------------------- type-specific routines
613
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000614 def formattree(self, tree, modname, parent=None):
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000615 """Produce HTML for a class tree as given by inspect.getclasstree()."""
616 result = ''
617 for entry in tree:
618 if type(entry) is type(()):
619 c, bases = entry
Raymond Hettinger95f285c2009-02-24 23:41:47 +0000620 result = result + '<dt><font face="helvetica, arial">'
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000621 result = result + self.classlink(c, modname)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000622 if bases and bases != (parent,):
623 parents = []
624 for base in bases:
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000625 parents.append(self.classlink(base, modname))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000626 result = result + '(' + ', '.join(parents) + ')'
Raymond Hettinger95f285c2009-02-24 23:41:47 +0000627 result = result + '\n</font></dt>'
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000628 elif type(entry) is type([]):
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000629 result = result + '<dd>\n%s</dd>\n' % self.formattree(
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000630 entry, modname, c)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000631 return '<dl>\n%s</dl>\n' % result
632
Tim Peters8dd7ade2001-10-18 19:56:17 +0000633 def docmodule(self, object, name=None, mod=None, *ignored):
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000634 """Produce HTML documentation for a module object."""
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000635 name = object.__name__ # ignore the passed-in name
Skip Montanaroa5616d22004-06-11 04:46:12 +0000636 try:
637 all = object.__all__
638 except AttributeError:
639 all = None
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000640 parts = name.split('.')
Ka-Ping Yee66efbc72001-03-01 13:55:20 +0000641 links = []
642 for i in range(len(parts)-1):
643 links.append(
Raymond Hettinger95f285c2009-02-24 23:41:47 +0000644 '<a href="%s.html"><font color="#ffffff">%s</font></a>' %
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000645 ('.'.join(parts[:i+1]), parts[i]))
646 linkedname = '.'.join(links + parts[-1:])
Raymond Hettinger95f285c2009-02-24 23:41:47 +0000647 head = '<big><big><strong>%s</strong></big></big>' % linkedname
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000648 try:
Ka-Ping Yee239432a2001-03-02 02:45:08 +0000649 path = inspect.getabsfile(object)
Zachary Wareeb432142014-07-10 11:18:00 -0500650 url = urllib.parse.quote(path)
Nick Coghlan7bb30b72010-12-03 09:29:11 +0000651 filelink = self.filelink(url, path)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000652 except TypeError:
653 filelink = '(built-in)'
Ka-Ping Yee6f3f9a42001-02-27 22:42:36 +0000654 info = []
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000655 if hasattr(object, '__version__'):
Ka-Ping Yee6f3f9a42001-02-27 22:42:36 +0000656 version = str(object.__version__)
Ka-Ping Yee40c49912001-02-27 22:46:01 +0000657 if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000658 version = version[11:-1].strip()
Ka-Ping Yee1d384632001-03-01 00:24:32 +0000659 info.append('version %s' % self.escape(version))
Ka-Ping Yee6f3f9a42001-02-27 22:42:36 +0000660 if hasattr(object, '__date__'):
661 info.append(self.escape(str(object.__date__)))
662 if info:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000663 head = head + ' (%s)' % ', '.join(info)
Skip Montanaro4997a692003-09-10 16:47:51 +0000664 docloc = self.getdocloc(object)
665 if docloc is not None:
Alexander Belopolskya47bbf52010-11-18 01:52:54 +0000666 docloc = '<br><a href="%(docloc)s">Module Reference</a>' % locals()
Skip Montanaro4997a692003-09-10 16:47:51 +0000667 else:
668 docloc = ''
Ka-Ping Yee66efbc72001-03-01 13:55:20 +0000669 result = self.heading(
Skip Montanaro4997a692003-09-10 16:47:51 +0000670 head, '#ffffff', '#7799ee',
671 '<a href=".">index</a><br>' + filelink + docloc)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000672
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000673 modules = inspect.getmembers(object, inspect.ismodule)
674
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000675 classes, cdict = [], {}
676 for key, value in inspect.getmembers(object, inspect.isclass):
Johannes Gijsbers4c11f602004-08-30 14:13:04 +0000677 # if __all__ exists, believe it. Otherwise use old heuristic.
678 if (all is not None or
679 (inspect.getmodule(value) or object) is object):
Raymond Hettinger1103d052011-03-25 14:15:24 -0700680 if visiblename(key, all, object):
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000681 classes.append((key, value))
682 cdict[key] = cdict[value] = '#' + key
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000683 for key, value in classes:
684 for base in value.__bases__:
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000685 key, modname = base.__name__, base.__module__
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000686 module = sys.modules.get(modname)
687 if modname != name and module and hasattr(module, key):
688 if getattr(module, key) is base:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000689 if not key in cdict:
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000690 cdict[key] = cdict[base] = modname + '.html#' + key
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000691 funcs, fdict = [], {}
692 for key, value in inspect.getmembers(object, inspect.isroutine):
Johannes Gijsbers4c11f602004-08-30 14:13:04 +0000693 # if __all__ exists, believe it. Otherwise use old heuristic.
694 if (all is not None or
695 inspect.isbuiltin(value) or inspect.getmodule(value) is object):
Raymond Hettinger1103d052011-03-25 14:15:24 -0700696 if visiblename(key, all, object):
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000697 funcs.append((key, value))
698 fdict[key] = '#-' + key
699 if inspect.isfunction(value): fdict[value] = fdict[key]
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000700 data = []
701 for key, value in inspect.getmembers(object, isdata):
Raymond Hettinger1103d052011-03-25 14:15:24 -0700702 if visiblename(key, all, object):
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000703 data.append((key, value))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000704
705 doc = self.markup(getdoc(object), self.preformat, fdict, cdict)
706 doc = doc and '<tt>%s</tt>' % doc
Tim Peters2306d242001-09-25 03:18:32 +0000707 result = result + '<p>%s</p>\n' % doc
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000708
709 if hasattr(object, '__path__'):
710 modpkgs = []
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 for importer, modname, ispkg in pkgutil.iter_modules(object.__path__):
712 modpkgs.append((modname, name, ispkg, 0))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000713 modpkgs.sort()
714 contents = self.multicolumn(modpkgs, self.modpkglink)
715 result = result + self.bigsection(
716 'Package Contents', '#ffffff', '#aa55cc', contents)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000717 elif modules:
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000718 contents = self.multicolumn(
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000719 modules, lambda t: self.modulelink(t[1]))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000720 result = result + self.bigsection(
Christian Heimes7131fd92008-02-19 14:21:46 +0000721 'Modules', '#ffffff', '#aa55cc', contents)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000722
723 if classes:
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000724 classlist = [value for (key, value) in classes]
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000725 contents = [
726 self.formattree(inspect.getclasstree(classlist, 1), name)]
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000727 for key, value in classes:
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +0000728 contents.append(self.document(value, key, name, fdict, cdict))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000729 result = result + self.bigsection(
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000730 'Classes', '#ffffff', '#ee77aa', ' '.join(contents))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000731 if funcs:
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000732 contents = []
733 for key, value in funcs:
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +0000734 contents.append(self.document(value, key, name, fdict, cdict))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000735 result = result + self.bigsection(
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000736 'Functions', '#ffffff', '#eeaa77', ' '.join(contents))
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000737 if data:
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000738 contents = []
Ka-Ping Yeedec96e92001-04-13 09:55:49 +0000739 for key, value in data:
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000740 contents.append(self.document(value, key))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000741 result = result + self.bigsection(
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000742 'Data', '#ffffff', '#55aa55', '<br>\n'.join(contents))
Ka-Ping Yee6f3f9a42001-02-27 22:42:36 +0000743 if hasattr(object, '__author__'):
744 contents = self.markup(str(object.__author__), self.preformat)
745 result = result + self.bigsection(
746 'Author', '#ffffff', '#7799ee', contents)
Ka-Ping Yee6f3f9a42001-02-27 22:42:36 +0000747 if hasattr(object, '__credits__'):
748 contents = self.markup(str(object.__credits__), self.preformat)
749 result = result + self.bigsection(
750 'Credits', '#ffffff', '#7799ee', contents)
751
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000752 return result
753
Tim Peters8dd7ade2001-10-18 19:56:17 +0000754 def docclass(self, object, name=None, mod=None, funcs={}, classes={},
755 *ignored):
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000756 """Produce HTML documentation for a class object."""
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000757 realname = object.__name__
758 name = name or realname
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000759 bases = object.__bases__
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000760
Tim Petersb47879b2001-09-24 04:47:19 +0000761 contents = []
762 push = contents.append
763
Tim Petersfa26f7c2001-09-24 08:05:11 +0000764 # Cute little class to pump out a horizontal rule between sections.
765 class HorizontalRule:
766 def __init__(self):
767 self.needone = 0
768 def maybe(self):
769 if self.needone:
770 push('<hr>\n')
771 self.needone = 1
772 hr = HorizontalRule()
773
Tim Petersc86f6ca2001-09-26 21:31:51 +0000774 # List the mro, if non-trivial.
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000775 mro = deque(inspect.getmro(object))
Tim Petersc86f6ca2001-09-26 21:31:51 +0000776 if len(mro) > 2:
777 hr.maybe()
778 push('<dl><dt>Method resolution order:</dt>\n')
779 for base in mro:
780 push('<dd>%s</dd>\n' % self.classlink(base,
781 object.__module__))
782 push('</dl>\n')
783
Tim Petersb47879b2001-09-24 04:47:19 +0000784 def spill(msg, attrs, predicate):
Tim Petersfa26f7c2001-09-24 08:05:11 +0000785 ok, attrs = _split_list(attrs, predicate)
Tim Petersb47879b2001-09-24 04:47:19 +0000786 if ok:
Tim Petersfa26f7c2001-09-24 08:05:11 +0000787 hr.maybe()
Tim Petersb47879b2001-09-24 04:47:19 +0000788 push(msg)
789 for name, kind, homecls, value in ok:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100790 try:
791 value = getattr(object, name)
792 except Exception:
793 # Some descriptors may meet a failure in their __get__.
794 # (bug #1785)
795 push(self._docdescriptor(name, value, mod))
796 else:
797 push(self.document(value, name, mod,
798 funcs, classes, mdict, object))
Tim Petersb47879b2001-09-24 04:47:19 +0000799 push('\n')
800 return attrs
801
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +0000802 def spilldescriptors(msg, attrs, predicate):
Tim Petersfa26f7c2001-09-24 08:05:11 +0000803 ok, attrs = _split_list(attrs, predicate)
Tim Petersb47879b2001-09-24 04:47:19 +0000804 if ok:
Tim Petersfa26f7c2001-09-24 08:05:11 +0000805 hr.maybe()
Tim Petersb47879b2001-09-24 04:47:19 +0000806 push(msg)
807 for name, kind, homecls, value in ok:
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +0000808 push(self._docdescriptor(name, value, mod))
Tim Petersb47879b2001-09-24 04:47:19 +0000809 return attrs
810
Tim Petersfa26f7c2001-09-24 08:05:11 +0000811 def spilldata(msg, attrs, predicate):
812 ok, attrs = _split_list(attrs, predicate)
Tim Petersb47879b2001-09-24 04:47:19 +0000813 if ok:
Tim Petersfa26f7c2001-09-24 08:05:11 +0000814 hr.maybe()
Tim Petersb47879b2001-09-24 04:47:19 +0000815 push(msg)
816 for name, kind, homecls, value in ok:
817 base = self.docother(getattr(object, name), name, mod)
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200818 if callable(value) or inspect.isdatadescriptor(value):
Guido van Rossum5e355b22002-05-21 20:56:15 +0000819 doc = getattr(value, "__doc__", None)
820 else:
821 doc = None
Tim Petersb47879b2001-09-24 04:47:19 +0000822 if doc is None:
823 push('<dl><dt>%s</dl>\n' % base)
824 else:
825 doc = self.markup(getdoc(value), self.preformat,
826 funcs, classes, mdict)
Tim Peters2306d242001-09-25 03:18:32 +0000827 doc = '<dd><tt>%s</tt>' % doc
Tim Petersb47879b2001-09-24 04:47:19 +0000828 push('<dl><dt>%s%s</dl>\n' % (base, doc))
829 push('\n')
830 return attrs
831
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000832 attrs = [(name, kind, cls, value)
833 for name, kind, cls, value in classify_class_attrs(object)
Raymond Hettinger1103d052011-03-25 14:15:24 -0700834 if visiblename(name, obj=object)]
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000835
Tim Petersb47879b2001-09-24 04:47:19 +0000836 mdict = {}
837 for key, kind, homecls, value in attrs:
838 mdict[key] = anchor = '#' + name + '-' + key
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100839 try:
840 value = getattr(object, name)
841 except Exception:
842 # Some descriptors may meet a failure in their __get__.
843 # (bug #1785)
844 pass
Tim Petersb47879b2001-09-24 04:47:19 +0000845 try:
846 # The value may not be hashable (e.g., a data attr with
847 # a dict or list value).
848 mdict[value] = anchor
849 except TypeError:
850 pass
851
Tim Petersfa26f7c2001-09-24 08:05:11 +0000852 while attrs:
Tim Peters351e3622001-09-27 03:29:51 +0000853 if mro:
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000854 thisclass = mro.popleft()
Tim Peters351e3622001-09-27 03:29:51 +0000855 else:
856 thisclass = attrs[0][2]
Tim Petersfa26f7c2001-09-24 08:05:11 +0000857 attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
858
Georg Brandl1a3284e2007-12-02 09:40:06 +0000859 if thisclass is builtins.object:
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000860 attrs = inherited
861 continue
862 elif thisclass is object:
863 tag = 'defined here'
Tim Petersb47879b2001-09-24 04:47:19 +0000864 else:
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000865 tag = 'inherited from %s' % self.classlink(thisclass,
866 object.__module__)
Tim Petersb47879b2001-09-24 04:47:19 +0000867 tag += ':<br>\n'
868
869 # Sort attrs by name.
Raymond Hettingerd4cb56d2008-01-30 02:55:10 +0000870 attrs.sort(key=lambda t: t[0])
Tim Petersb47879b2001-09-24 04:47:19 +0000871
872 # Pump out the attrs, segregated by kind.
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000873 attrs = spill('Methods %s' % tag, attrs,
Tim Petersb47879b2001-09-24 04:47:19 +0000874 lambda t: t[1] == 'method')
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000875 attrs = spill('Class methods %s' % tag, attrs,
Tim Petersb47879b2001-09-24 04:47:19 +0000876 lambda t: t[1] == 'class method')
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000877 attrs = spill('Static methods %s' % tag, attrs,
Tim Petersb47879b2001-09-24 04:47:19 +0000878 lambda t: t[1] == 'static method')
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +0000879 attrs = spilldescriptors('Data descriptors %s' % tag, attrs,
880 lambda t: t[1] == 'data descriptor')
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000881 attrs = spilldata('Data and other attributes %s' % tag, attrs,
Tim Petersfa26f7c2001-09-24 08:05:11 +0000882 lambda t: t[1] == 'data')
Tim Petersb47879b2001-09-24 04:47:19 +0000883 assert attrs == []
Tim Peters351e3622001-09-27 03:29:51 +0000884 attrs = inherited
Tim Petersb47879b2001-09-24 04:47:19 +0000885
886 contents = ''.join(contents)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000887
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000888 if name == realname:
889 title = '<a name="%s">class <strong>%s</strong></a>' % (
890 name, realname)
891 else:
892 title = '<strong>%s</strong> = <a name="%s">class %s</a>' % (
893 name, name, realname)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000894 if bases:
895 parents = []
896 for base in bases:
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000897 parents.append(self.classlink(base, object.__module__))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000898 title = title + '(%s)' % ', '.join(parents)
Tim Peters2306d242001-09-25 03:18:32 +0000899 doc = self.markup(getdoc(object), self.preformat, funcs, classes, mdict)
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000900 doc = doc and '<tt>%s<br>&nbsp;</tt>' % doc
Tim Petersc86f6ca2001-09-26 21:31:51 +0000901
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +0000902 return self.section(title, '#000000', '#ffc8d8', contents, 3, doc)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000903
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000904 def formatvalue(self, object):
905 """Format an argument default value as text."""
Tim Peters2306d242001-09-25 03:18:32 +0000906 return self.grey('=' + self.repr(object))
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000907
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +0000908 def docroutine(self, object, name=None, mod=None,
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000909 funcs={}, classes={}, methods={}, cl=None):
Ka-Ping Yee66efbc72001-03-01 13:55:20 +0000910 """Produce HTML documentation for a function or method object."""
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000911 realname = object.__name__
912 name = name or realname
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000913 anchor = (cl and cl.__name__ or '') + '-' + name
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000914 note = ''
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000915 skipdocs = 0
Larry Hastings24a882b2014-02-20 23:34:46 -0800916 if _is_bound_method(object):
Christian Heimesff737952007-11-27 10:40:20 +0000917 imclass = object.__self__.__class__
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000918 if cl:
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +0000919 if imclass is not cl:
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000920 note = ' from ' + self.classlink(imclass, mod)
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000921 else:
Christian Heimesff737952007-11-27 10:40:20 +0000922 if object.__self__ is not None:
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000923 note = ' method of %s instance' % self.classlink(
Christian Heimesff737952007-11-27 10:40:20 +0000924 object.__self__.__class__, mod)
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +0000925 else:
926 note = ' unbound %s method' % self.classlink(imclass,mod)
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000927
928 if name == realname:
929 title = '<a name="%s"><strong>%s</strong></a>' % (anchor, realname)
930 else:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000931 if (cl and realname in cl.__dict__ and
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000932 cl.__dict__[realname] is object):
Ka-Ping Yeee280c062001-03-23 14:05:53 +0000933 reallink = '<a href="#%s">%s</a>' % (
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000934 cl.__name__ + '-' + realname, realname)
935 skipdocs = 1
936 else:
937 reallink = realname
938 title = '<a name="%s"><strong>%s</strong></a> = %s' % (
939 anchor, name, reallink)
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800940 argspec = None
Larry Hastings24a882b2014-02-20 23:34:46 -0800941 if inspect.isroutine(object):
Larry Hastings5c661892014-01-24 06:17:25 -0800942 try:
943 signature = inspect.signature(object)
944 except (ValueError, TypeError):
945 signature = None
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800946 if signature:
947 argspec = str(signature)
948 if realname == '<lambda>':
949 title = '<strong>%s</strong> <em>lambda</em> ' % name
950 # XXX lambda's won't usually have func_annotations['return']
951 # since the syntax doesn't support but it is possible.
952 # So removing parentheses isn't truly safe.
953 argspec = argspec[1:-1] # remove parentheses
954 if not argspec:
Tim Peters4bcfa312001-09-20 06:08:24 +0000955 argspec = '(...)'
Ka-Ping Yee66efbc72001-03-01 13:55:20 +0000956
Serhiy Storchaka66dd4aa2014-11-17 23:48:02 +0200957 decl = title + self.escape(argspec) + (note and self.grey(
Tim Peters2306d242001-09-25 03:18:32 +0000958 '<font face="helvetica, arial">%s</font>' % note))
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000959
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000960 if skipdocs:
Tim Peters2306d242001-09-25 03:18:32 +0000961 return '<dl><dt>%s</dt></dl>\n' % decl
Ka-Ping Yee3bda8792001-03-23 13:17:50 +0000962 else:
963 doc = self.markup(
964 getdoc(object), self.preformat, funcs, classes, methods)
Tim Peters2306d242001-09-25 03:18:32 +0000965 doc = doc and '<dd><tt>%s</tt></dd>' % doc
966 return '<dl><dt>%s</dt>%s</dl>\n' % (decl, doc)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000967
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +0000968 def _docdescriptor(self, name, value, mod):
Johannes Gijsbers8de645a2004-11-07 19:16:05 +0000969 results = []
970 push = results.append
971
972 if name:
973 push('<dl><dt><strong>%s</strong></dt>\n' % name)
974 if value.__doc__ is not None:
Ka-Ping Yeebba6acc2005-02-19 22:58:26 +0000975 doc = self.markup(getdoc(value), self.preformat)
Johannes Gijsbers8de645a2004-11-07 19:16:05 +0000976 push('<dd><tt>%s</tt></dd>\n' % doc)
Johannes Gijsbers8de645a2004-11-07 19:16:05 +0000977 push('</dl>\n')
978
979 return ''.join(results)
980
981 def docproperty(self, object, name=None, mod=None, cl=None):
982 """Produce html documentation for a property."""
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +0000983 return self._docdescriptor(name, object, mod)
Johannes Gijsbers8de645a2004-11-07 19:16:05 +0000984
Tim Peters8dd7ade2001-10-18 19:56:17 +0000985 def docother(self, object, name=None, mod=None, *ignored):
Ka-Ping Yee37f7b382001-03-23 00:12:53 +0000986 """Produce HTML documentation for a data object."""
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +0000987 lhs = name and '<strong>%s</strong> = ' % name or ''
988 return lhs + self.repr(object)
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000989
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000990 def docdata(self, object, name=None, mod=None, cl=None):
991 """Produce html documentation for a data descriptor."""
992 return self._docdescriptor(name, object, mod)
993
Ka-Ping Yeedd175342001-02-27 14:43:46 +0000994 def index(self, dir, shadowed=None):
995 """Generate an HTML index for a directory of modules."""
996 modpkgs = []
997 if shadowed is None: shadowed = {}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000998 for importer, name, ispkg in pkgutil.iter_modules([dir]):
Victor Stinner4d652242011-04-12 23:41:50 +0200999 if any((0xD800 <= ord(ch) <= 0xDFFF) for ch in name):
1000 # ignore a module if its name contains a surrogate character
1001 continue
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001002 modpkgs.append((name, '', ispkg, name in shadowed))
1003 shadowed[name] = 1
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001004
1005 modpkgs.sort()
1006 contents = self.multicolumn(modpkgs, self.modpkglink)
1007 return self.bigsection(dir, '#ffffff', '#ee77aa', contents)
1008
1009# -------------------------------------------- text documentation generator
1010
1011class TextRepr(Repr):
1012 """Class for safely making a text representation of a Python object."""
1013 def __init__(self):
1014 Repr.__init__(self)
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001015 self.maxlist = self.maxtuple = 20
1016 self.maxdict = 10
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001017 self.maxstring = self.maxother = 100
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001018
1019 def repr1(self, x, level):
Skip Montanaro0fe8fce2003-06-27 15:45:41 +00001020 if hasattr(type(x), '__name__'):
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001021 methodname = 'repr_' + '_'.join(type(x).__name__.split())
Skip Montanaro0fe8fce2003-06-27 15:45:41 +00001022 if hasattr(self, methodname):
1023 return getattr(self, methodname)(x, level)
1024 return cram(stripid(repr(x)), self.maxother)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001025
Ka-Ping Yeea2fe1032001-03-02 01:19:14 +00001026 def repr_string(self, x, level):
1027 test = cram(x, self.maxstring)
1028 testrepr = repr(test)
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001029 if '\\' in test and '\\' not in replace(testrepr, r'\\', ''):
Ka-Ping Yeea2fe1032001-03-02 01:19:14 +00001030 # Backslashes are only literal in the string and are never
1031 # needed to make any special characters, so show a raw string.
1032 return 'r' + testrepr[0] + test + testrepr[0]
1033 return testrepr
1034
Skip Montanarodf708782002-03-07 22:58:02 +00001035 repr_str = repr_string
1036
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001037 def repr_instance(self, x, level):
1038 try:
Ka-Ping Yee1d384632001-03-01 00:24:32 +00001039 return cram(stripid(repr(x)), self.maxstring)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001040 except:
1041 return '<%s instance>' % x.__class__.__name__
1042
1043class TextDoc(Doc):
1044 """Formatter class for text documentation."""
1045
1046 # ------------------------------------------- text formatting utilities
1047
1048 _repr_instance = TextRepr()
1049 repr = _repr_instance.repr
1050
1051 def bold(self, text):
1052 """Format a string in bold by overstriking."""
Georg Brandlcbd2ab12010-12-04 10:39:14 +00001053 return ''.join(ch + '\b' + ch for ch in text)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001054
1055 def indent(self, text, prefix=' '):
1056 """Indent text by prepending a given prefix to each line."""
1057 if not text: return ''
Collin Winter72e110c2007-07-17 00:27:30 +00001058 lines = [prefix + line for line in text.split('\n')]
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001059 if lines: lines[-1] = lines[-1].rstrip()
1060 return '\n'.join(lines)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001061
1062 def section(self, title, contents):
1063 """Format a section with a given heading."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001064 clean_contents = self.indent(contents).rstrip()
1065 return self.bold(title) + '\n' + clean_contents + '\n\n'
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001066
1067 # ---------------------------------------------- type-specific routines
1068
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001069 def formattree(self, tree, modname, parent=None, prefix=''):
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001070 """Render in text a class tree as returned by inspect.getclasstree()."""
1071 result = ''
1072 for entry in tree:
1073 if type(entry) is type(()):
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001074 c, bases = entry
1075 result = result + prefix + classname(c, modname)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001076 if bases and bases != (parent,):
Georg Brandlcbd2ab12010-12-04 10:39:14 +00001077 parents = (classname(c, modname) for c in bases)
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001078 result = result + '(%s)' % ', '.join(parents)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001079 result = result + '\n'
1080 elif type(entry) is type([]):
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001081 result = result + self.formattree(
1082 entry, modname, c, prefix + ' ')
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001083 return result
1084
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001085 def docmodule(self, object, name=None, mod=None):
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001086 """Produce text documentation for a given module object."""
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001087 name = object.__name__ # ignore the passed-in name
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +00001088 synop, desc = splitdoc(getdoc(object))
1089 result = self.section('NAME', name + (synop and ' - ' + synop))
Alexander Belopolskya47bbf52010-11-18 01:52:54 +00001090 all = getattr(object, '__all__', None)
Skip Montanaro4997a692003-09-10 16:47:51 +00001091 docloc = self.getdocloc(object)
1092 if docloc is not None:
Alexander Belopolskya47bbf52010-11-18 01:52:54 +00001093 result = result + self.section('MODULE REFERENCE', docloc + """
1094
Éric Araujo647ef8c2011-09-11 00:43:20 +02001095The following documentation is automatically generated from the Python
1096source files. It may be incomplete, incorrect or include features that
1097are considered implementation detail and may vary between Python
1098implementations. When in doubt, consult the module reference at the
1099location listed above.
Alexander Belopolskya47bbf52010-11-18 01:52:54 +00001100""")
Skip Montanaro4997a692003-09-10 16:47:51 +00001101
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +00001102 if desc:
1103 result = result + self.section('DESCRIPTION', desc)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001104
1105 classes = []
1106 for key, value in inspect.getmembers(object, inspect.isclass):
Johannes Gijsbers4c11f602004-08-30 14:13:04 +00001107 # if __all__ exists, believe it. Otherwise use old heuristic.
1108 if (all is not None
1109 or (inspect.getmodule(value) or object) is object):
Raymond Hettinger1103d052011-03-25 14:15:24 -07001110 if visiblename(key, all, object):
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +00001111 classes.append((key, value))
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001112 funcs = []
1113 for key, value in inspect.getmembers(object, inspect.isroutine):
Johannes Gijsbers4c11f602004-08-30 14:13:04 +00001114 # if __all__ exists, believe it. Otherwise use old heuristic.
1115 if (all is not None or
1116 inspect.isbuiltin(value) or inspect.getmodule(value) is object):
Raymond Hettinger1103d052011-03-25 14:15:24 -07001117 if visiblename(key, all, object):
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +00001118 funcs.append((key, value))
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001119 data = []
1120 for key, value in inspect.getmembers(object, isdata):
Raymond Hettinger1103d052011-03-25 14:15:24 -07001121 if visiblename(key, all, object):
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001122 data.append((key, value))
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001123
Christian Heimes1af737c2008-01-23 08:24:23 +00001124 modpkgs = []
1125 modpkgs_names = set()
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001126 if hasattr(object, '__path__'):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001127 for importer, modname, ispkg in pkgutil.iter_modules(object.__path__):
Christian Heimes1af737c2008-01-23 08:24:23 +00001128 modpkgs_names.add(modname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001129 if ispkg:
1130 modpkgs.append(modname + ' (package)')
1131 else:
1132 modpkgs.append(modname)
1133
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001134 modpkgs.sort()
1135 result = result + self.section(
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001136 'PACKAGE CONTENTS', '\n'.join(modpkgs))
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001137
Christian Heimes1af737c2008-01-23 08:24:23 +00001138 # Detect submodules as sometimes created by C extensions
1139 submodules = []
1140 for key, value in inspect.getmembers(object, inspect.ismodule):
1141 if value.__name__.startswith(name + '.') and key not in modpkgs_names:
1142 submodules.append(key)
1143 if submodules:
1144 submodules.sort()
1145 result = result + self.section(
Amaury Forgeot d'Arc768db922008-04-24 21:00:04 +00001146 'SUBMODULES', '\n'.join(submodules))
Christian Heimes1af737c2008-01-23 08:24:23 +00001147
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001148 if classes:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001149 classlist = [value for key, value in classes]
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001150 contents = [self.formattree(
1151 inspect.getclasstree(classlist, 1), name)]
1152 for key, value in classes:
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001153 contents.append(self.document(value, key, name))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001154 result = result + self.section('CLASSES', '\n'.join(contents))
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001155
1156 if funcs:
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001157 contents = []
1158 for key, value in funcs:
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001159 contents.append(self.document(value, key, name))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001160 result = result + self.section('FUNCTIONS', '\n'.join(contents))
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001161
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001162 if data:
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001163 contents = []
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001164 for key, value in data:
Georg Brandl8b813db2005-10-01 16:32:31 +00001165 contents.append(self.docother(value, key, name, maxlen=70))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001166 result = result + self.section('DATA', '\n'.join(contents))
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001167
1168 if hasattr(object, '__version__'):
1169 version = str(object.__version__)
Ka-Ping Yee1d384632001-03-01 00:24:32 +00001170 if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001171 version = version[11:-1].strip()
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001172 result = result + self.section('VERSION', version)
Ka-Ping Yee6f3f9a42001-02-27 22:42:36 +00001173 if hasattr(object, '__date__'):
1174 result = result + self.section('DATE', str(object.__date__))
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001175 if hasattr(object, '__author__'):
Ka-Ping Yee6f3f9a42001-02-27 22:42:36 +00001176 result = result + self.section('AUTHOR', str(object.__author__))
1177 if hasattr(object, '__credits__'):
1178 result = result + self.section('CREDITS', str(object.__credits__))
Alexander Belopolskya47bbf52010-11-18 01:52:54 +00001179 try:
1180 file = inspect.getabsfile(object)
1181 except TypeError:
1182 file = '(built-in)'
1183 result = result + self.section('FILE', file)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001184 return result
1185
Georg Brandl9bd45f992010-12-03 09:58:38 +00001186 def docclass(self, object, name=None, mod=None, *ignored):
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001187 """Produce text documentation for a given class object."""
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001188 realname = object.__name__
1189 name = name or realname
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001190 bases = object.__bases__
1191
Tim Petersc86f6ca2001-09-26 21:31:51 +00001192 def makename(c, m=object.__module__):
1193 return classname(c, m)
1194
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001195 if name == realname:
1196 title = 'class ' + self.bold(realname)
1197 else:
1198 title = self.bold(name) + ' = class ' + realname
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001199 if bases:
Ka-Ping Yee3bda8792001-03-23 13:17:50 +00001200 parents = map(makename, bases)
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001201 title = title + '(%s)' % ', '.join(parents)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001202
1203 doc = getdoc(object)
Tim Peters28355492001-09-23 21:29:55 +00001204 contents = doc and [doc + '\n'] or []
1205 push = contents.append
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001206
Tim Petersc86f6ca2001-09-26 21:31:51 +00001207 # List the mro, if non-trivial.
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001208 mro = deque(inspect.getmro(object))
Tim Petersc86f6ca2001-09-26 21:31:51 +00001209 if len(mro) > 2:
1210 push("Method resolution order:")
1211 for base in mro:
1212 push(' ' + makename(base))
1213 push('')
1214
Tim Petersf4aad8e2001-09-24 22:40:47 +00001215 # Cute little class to pump out a horizontal rule between sections.
1216 class HorizontalRule:
1217 def __init__(self):
1218 self.needone = 0
1219 def maybe(self):
1220 if self.needone:
1221 push('-' * 70)
1222 self.needone = 1
1223 hr = HorizontalRule()
1224
Tim Peters28355492001-09-23 21:29:55 +00001225 def spill(msg, attrs, predicate):
Tim Petersfa26f7c2001-09-24 08:05:11 +00001226 ok, attrs = _split_list(attrs, predicate)
Tim Peters28355492001-09-23 21:29:55 +00001227 if ok:
Tim Petersf4aad8e2001-09-24 22:40:47 +00001228 hr.maybe()
Tim Peters28355492001-09-23 21:29:55 +00001229 push(msg)
1230 for name, kind, homecls, value in ok:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +01001231 try:
1232 value = getattr(object, name)
1233 except Exception:
1234 # Some descriptors may meet a failure in their __get__.
1235 # (bug #1785)
1236 push(self._docdescriptor(name, value, mod))
1237 else:
1238 push(self.document(value,
1239 name, mod, object))
Tim Peters28355492001-09-23 21:29:55 +00001240 return attrs
1241
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +00001242 def spilldescriptors(msg, attrs, predicate):
Tim Petersfa26f7c2001-09-24 08:05:11 +00001243 ok, attrs = _split_list(attrs, predicate)
Tim Peters28355492001-09-23 21:29:55 +00001244 if ok:
Tim Petersf4aad8e2001-09-24 22:40:47 +00001245 hr.maybe()
Tim Peters28355492001-09-23 21:29:55 +00001246 push(msg)
1247 for name, kind, homecls, value in ok:
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +00001248 push(self._docdescriptor(name, value, mod))
Tim Peters28355492001-09-23 21:29:55 +00001249 return attrs
Tim Petersb47879b2001-09-24 04:47:19 +00001250
Tim Petersfa26f7c2001-09-24 08:05:11 +00001251 def spilldata(msg, attrs, predicate):
1252 ok, attrs = _split_list(attrs, predicate)
Tim Peters28355492001-09-23 21:29:55 +00001253 if ok:
Tim Petersf4aad8e2001-09-24 22:40:47 +00001254 hr.maybe()
Tim Peters28355492001-09-23 21:29:55 +00001255 push(msg)
1256 for name, kind, homecls, value in ok:
Florent Xicluna5d1155c2011-10-28 14:45:05 +02001257 if callable(value) or inspect.isdatadescriptor(value):
Ka-Ping Yeebba6acc2005-02-19 22:58:26 +00001258 doc = getdoc(value)
Guido van Rossum5e355b22002-05-21 20:56:15 +00001259 else:
1260 doc = None
Serhiy Storchaka056eb022014-02-19 23:05:12 +02001261 try:
1262 obj = getattr(object, name)
1263 except AttributeError:
1264 obj = homecls.__dict__[name]
1265 push(self.docother(obj, name, mod, maxlen=70, doc=doc) +
1266 '\n')
Tim Peters28355492001-09-23 21:29:55 +00001267 return attrs
1268
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001269 attrs = [(name, kind, cls, value)
1270 for name, kind, cls, value in classify_class_attrs(object)
Raymond Hettinger1103d052011-03-25 14:15:24 -07001271 if visiblename(name, obj=object)]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001272
Tim Petersfa26f7c2001-09-24 08:05:11 +00001273 while attrs:
Tim Peters351e3622001-09-27 03:29:51 +00001274 if mro:
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001275 thisclass = mro.popleft()
Tim Peters351e3622001-09-27 03:29:51 +00001276 else:
1277 thisclass = attrs[0][2]
Tim Petersfa26f7c2001-09-24 08:05:11 +00001278 attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
1279
Georg Brandl1a3284e2007-12-02 09:40:06 +00001280 if thisclass is builtins.object:
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +00001281 attrs = inherited
1282 continue
1283 elif thisclass is object:
Tim Peters28355492001-09-23 21:29:55 +00001284 tag = "defined here"
1285 else:
Tim Petersfa26f7c2001-09-24 08:05:11 +00001286 tag = "inherited from %s" % classname(thisclass,
1287 object.__module__)
Tim Peters28355492001-09-23 21:29:55 +00001288 # Sort attrs by name.
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +00001289 attrs.sort()
Tim Peters28355492001-09-23 21:29:55 +00001290
1291 # Pump out the attrs, segregated by kind.
Tim Petersf4aad8e2001-09-24 22:40:47 +00001292 attrs = spill("Methods %s:\n" % tag, attrs,
Tim Peters28355492001-09-23 21:29:55 +00001293 lambda t: t[1] == 'method')
Tim Petersf4aad8e2001-09-24 22:40:47 +00001294 attrs = spill("Class methods %s:\n" % tag, attrs,
Tim Peters28355492001-09-23 21:29:55 +00001295 lambda t: t[1] == 'class method')
Tim Petersf4aad8e2001-09-24 22:40:47 +00001296 attrs = spill("Static methods %s:\n" % tag, attrs,
Tim Peters28355492001-09-23 21:29:55 +00001297 lambda t: t[1] == 'static method')
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +00001298 attrs = spilldescriptors("Data descriptors %s:\n" % tag, attrs,
1299 lambda t: t[1] == 'data descriptor')
Ka-Ping Yeed9e213e2003-03-28 16:35:51 +00001300 attrs = spilldata("Data and other attributes %s:\n" % tag, attrs,
1301 lambda t: t[1] == 'data')
Ethan Furmanb0c84cd2013-10-20 22:37:39 -07001302
Tim Peters28355492001-09-23 21:29:55 +00001303 assert attrs == []
Tim Peters351e3622001-09-27 03:29:51 +00001304 attrs = inherited
Tim Peters28355492001-09-23 21:29:55 +00001305
1306 contents = '\n'.join(contents)
1307 if not contents:
1308 return title + '\n'
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001309 return title + '\n' + self.indent(contents.rstrip(), ' | ') + '\n'
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001310
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001311 def formatvalue(self, object):
1312 """Format an argument default value as text."""
1313 return '=' + self.repr(object)
1314
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001315 def docroutine(self, object, name=None, mod=None, cl=None):
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00001316 """Produce text documentation for a function or method object."""
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001317 realname = object.__name__
1318 name = name or realname
1319 note = ''
Ka-Ping Yee3bda8792001-03-23 13:17:50 +00001320 skipdocs = 0
Larry Hastings24a882b2014-02-20 23:34:46 -08001321 if _is_bound_method(object):
Christian Heimesff737952007-11-27 10:40:20 +00001322 imclass = object.__self__.__class__
Ka-Ping Yee3bda8792001-03-23 13:17:50 +00001323 if cl:
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001324 if imclass is not cl:
1325 note = ' from ' + classname(imclass, mod)
Ka-Ping Yee3bda8792001-03-23 13:17:50 +00001326 else:
Christian Heimesff737952007-11-27 10:40:20 +00001327 if object.__self__ is not None:
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +00001328 note = ' method of %s instance' % classname(
Christian Heimesff737952007-11-27 10:40:20 +00001329 object.__self__.__class__, mod)
Ka-Ping Yeeb7a48302001-04-12 20:39:14 +00001330 else:
1331 note = ' unbound %s method' % classname(imclass,mod)
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001332
1333 if name == realname:
1334 title = self.bold(realname)
1335 else:
Raymond Hettinger54f02222002-06-01 14:18:47 +00001336 if (cl and realname in cl.__dict__ and
Ka-Ping Yee3bda8792001-03-23 13:17:50 +00001337 cl.__dict__[realname] is object):
1338 skipdocs = 1
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001339 title = self.bold(name) + ' = ' + realname
Larry Hastings44e2eaa2013-11-23 15:37:55 -08001340 argspec = None
Larry Hastings5c661892014-01-24 06:17:25 -08001341
1342 if inspect.isroutine(object):
1343 try:
1344 signature = inspect.signature(object)
1345 except (ValueError, TypeError):
1346 signature = None
Larry Hastings44e2eaa2013-11-23 15:37:55 -08001347 if signature:
1348 argspec = str(signature)
1349 if realname == '<lambda>':
1350 title = self.bold(name) + ' lambda '
1351 # XXX lambda's won't usually have func_annotations['return']
1352 # since the syntax doesn't support but it is possible.
1353 # So removing parentheses isn't truly safe.
1354 argspec = argspec[1:-1] # remove parentheses
1355 if not argspec:
Tim Peters4bcfa312001-09-20 06:08:24 +00001356 argspec = '(...)'
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001357 decl = title + argspec + note
1358
Ka-Ping Yee3bda8792001-03-23 13:17:50 +00001359 if skipdocs:
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001360 return decl + '\n'
Ka-Ping Yee3bda8792001-03-23 13:17:50 +00001361 else:
1362 doc = getdoc(object) or ''
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001363 return decl + '\n' + (doc and self.indent(doc).rstrip() + '\n')
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001364
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +00001365 def _docdescriptor(self, name, value, mod):
Johannes Gijsbers8de645a2004-11-07 19:16:05 +00001366 results = []
1367 push = results.append
1368
1369 if name:
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +00001370 push(self.bold(name))
1371 push('\n')
Johannes Gijsbers8de645a2004-11-07 19:16:05 +00001372 doc = getdoc(value) or ''
1373 if doc:
1374 push(self.indent(doc))
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +00001375 push('\n')
1376 return ''.join(results)
Johannes Gijsbers8de645a2004-11-07 19:16:05 +00001377
1378 def docproperty(self, object, name=None, mod=None, cl=None):
1379 """Produce text documentation for a property."""
Johannes Gijsbers9ddb3002005-01-08 20:16:43 +00001380 return self._docdescriptor(name, object, mod)
Johannes Gijsbers8de645a2004-11-07 19:16:05 +00001381
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001382 def docdata(self, object, name=None, mod=None, cl=None):
1383 """Produce text documentation for a data descriptor."""
1384 return self._docdescriptor(name, object, mod)
1385
Georg Brandl8b813db2005-10-01 16:32:31 +00001386 def docother(self, object, name=None, mod=None, parent=None, maxlen=None, doc=None):
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001387 """Produce text documentation for a data object."""
1388 repr = self.repr(object)
1389 if maxlen:
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001390 line = (name and name + ' = ' or '') + repr
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001391 chop = maxlen - len(line)
1392 if chop < 0: repr = repr[:chop] + '...'
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001393 line = (name and self.bold(name) + ' = ' or '') + repr
Tim Peters28355492001-09-23 21:29:55 +00001394 if doc is not None:
1395 line += '\n' + self.indent(str(doc))
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001396 return line
1397
Georg Brandld80d5f42010-12-03 07:47:22 +00001398class _PlainTextDoc(TextDoc):
1399 """Subclass of TextDoc which overrides string styling"""
1400 def bold(self, text):
1401 return text
1402
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001403# --------------------------------------------------------- user interfaces
1404
1405def pager(text):
1406 """The first time this is called, determine what kind of pager to use."""
1407 global pager
1408 pager = getpager()
1409 pager(text)
1410
1411def getpager():
1412 """Decide what method to use for paging through text."""
Benjamin Peterson159824e2014-06-07 20:14:26 -07001413 if not hasattr(sys.stdin, "isatty"):
1414 return plainpager
Guido van Rossuma01a8b62007-05-27 09:20:14 +00001415 if not hasattr(sys.stdout, "isatty"):
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001416 return plainpager
1417 if not sys.stdin.isatty() or not sys.stdout.isatty():
1418 return plainpager
Raymond Hettinger54f02222002-06-01 14:18:47 +00001419 if 'PAGER' in os.environ:
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +00001420 if sys.platform == 'win32': # pipes completely broken in Windows
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001421 return lambda text: tempfilepager(plain(text), os.environ['PAGER'])
Raymond Hettingerdbecd932005-02-06 06:57:08 +00001422 elif os.environ.get('TERM') in ('dumb', 'emacs'):
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001423 return lambda text: pipepager(plain(text), os.environ['PAGER'])
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +00001424 else:
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001425 return lambda text: pipepager(text, os.environ['PAGER'])
Ka-Ping Yeea487e4e2005-11-05 04:49:18 +00001426 if os.environ.get('TERM') in ('dumb', 'emacs'):
1427 return plainpager
Jesus Cea4791a242012-10-05 03:15:39 +02001428 if sys.platform == 'win32':
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001429 return lambda text: tempfilepager(plain(text), 'more <')
Skip Montanarod404bee2002-09-26 21:44:57 +00001430 if hasattr(os, 'system') and os.system('(less) 2>/dev/null') == 0:
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001431 return lambda text: pipepager(text, 'less')
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001432
1433 import tempfile
Guido van Rossum3b0a3292002-08-09 16:38:32 +00001434 (fd, filename) = tempfile.mkstemp()
1435 os.close(fd)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001436 try:
Georg Brandl3dbca812008-07-23 16:10:53 +00001437 if hasattr(os, 'system') and os.system('more "%s"' % filename) == 0:
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001438 return lambda text: pipepager(text, 'more')
1439 else:
1440 return ttypager
1441 finally:
1442 os.unlink(filename)
1443
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001444def plain(text):
1445 """Remove boldface formatting from text."""
1446 return re.sub('.\b', '', text)
1447
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001448def pipepager(text, cmd):
1449 """Page through text by feeding it to another program."""
Serhiy Storchaka5e3d7a42015-02-20 23:46:06 +02001450 import subprocess
1451 proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001452 try:
Serhiy Storchaka5e3d7a42015-02-20 23:46:06 +02001453 with proc:
1454 with io.TextIOWrapper(proc.stdin, errors='backslashreplace') as pipe:
1455 pipe.write(text)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001456 except OSError:
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001457 pass # Ignore broken pipes caused by quitting the pager program.
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001458
1459def tempfilepager(text, cmd):
1460 """Page through text by invoking a program on a temporary file."""
1461 import tempfile
Tim Peters550e4e52003-02-07 01:53:46 +00001462 filename = tempfile.mktemp()
Serhiy Storchaka5e3d7a42015-02-20 23:46:06 +02001463 with open(filename, 'w', errors='backslashreplace') as file:
Giampaolo Rodola'2f50aaf2013-02-12 02:04:27 +01001464 file.write(text)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001465 try:
Georg Brandl3dbca812008-07-23 16:10:53 +00001466 os.system(cmd + ' "' + filename + '"')
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001467 finally:
1468 os.unlink(filename)
1469
Serhiy Storchaka5e3d7a42015-02-20 23:46:06 +02001470def _escape_stdout(text):
1471 # Escape non-encodable characters to avoid encoding errors later
1472 encoding = getattr(sys.stdout, 'encoding', None) or 'utf-8'
1473 return text.encode(encoding, 'backslashreplace').decode(encoding)
1474
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001475def ttypager(text):
1476 """Page through text on a text terminal."""
Serhiy Storchaka5e3d7a42015-02-20 23:46:06 +02001477 lines = plain(_escape_stdout(text)).split('\n')
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001478 try:
1479 import tty
1480 fd = sys.stdin.fileno()
1481 old = tty.tcgetattr(fd)
1482 tty.setcbreak(fd)
1483 getchar = lambda: sys.stdin.read(1)
Serhiy Storchakaab5e9b92014-11-28 00:09:29 +02001484 except (ImportError, AttributeError, io.UnsupportedOperation):
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001485 tty = None
1486 getchar = lambda: sys.stdin.readline()[:-1][:1]
1487
1488 try:
Serhiy Storchakaab5e9b92014-11-28 00:09:29 +02001489 try:
1490 h = int(os.environ.get('LINES', 0))
1491 except ValueError:
1492 h = 0
1493 if h <= 1:
1494 h = 25
1495 r = inc = h - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001496 sys.stdout.write('\n'.join(lines[:inc]) + '\n')
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001497 while lines[r:]:
1498 sys.stdout.write('-- more --')
1499 sys.stdout.flush()
1500 c = getchar()
1501
Raymond Hettingerdbecd932005-02-06 06:57:08 +00001502 if c in ('q', 'Q'):
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001503 sys.stdout.write('\r \r')
1504 break
Raymond Hettingerdbecd932005-02-06 06:57:08 +00001505 elif c in ('\r', '\n'):
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001506 sys.stdout.write('\r \r' + lines[r] + '\n')
1507 r = r + 1
1508 continue
Raymond Hettingerdbecd932005-02-06 06:57:08 +00001509 if c in ('b', 'B', '\x1b'):
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001510 r = r - inc - inc
1511 if r < 0: r = 0
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001512 sys.stdout.write('\n' + '\n'.join(lines[r:r+inc]) + '\n')
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001513 r = r + inc
1514
1515 finally:
1516 if tty:
1517 tty.tcsetattr(fd, tty.TCSAFLUSH, old)
1518
1519def plainpager(text):
1520 """Simply print unformatted text. This is the ultimate fallback."""
Serhiy Storchaka5e3d7a42015-02-20 23:46:06 +02001521 sys.stdout.write(plain(_escape_stdout(text)))
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001522
1523def describe(thing):
Ka-Ping Yee3bda8792001-03-23 13:17:50 +00001524 """Produce a short description of the given thing."""
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001525 if inspect.ismodule(thing):
1526 if thing.__name__ in sys.builtin_module_names:
1527 return 'built-in module ' + thing.__name__
1528 if hasattr(thing, '__path__'):
1529 return 'package ' + thing.__name__
1530 else:
1531 return 'module ' + thing.__name__
1532 if inspect.isbuiltin(thing):
1533 return 'built-in function ' + thing.__name__
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001534 if inspect.isgetsetdescriptor(thing):
1535 return 'getset descriptor %s.%s.%s' % (
1536 thing.__objclass__.__module__, thing.__objclass__.__name__,
1537 thing.__name__)
1538 if inspect.ismemberdescriptor(thing):
1539 return 'member descriptor %s.%s.%s' % (
1540 thing.__objclass__.__module__, thing.__objclass__.__name__,
1541 thing.__name__)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001542 if inspect.isclass(thing):
1543 return 'class ' + thing.__name__
1544 if inspect.isfunction(thing):
1545 return 'function ' + thing.__name__
1546 if inspect.ismethod(thing):
1547 return 'method ' + thing.__name__
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001548 return type(thing).__name__
1549
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001550def locate(path, forceload=0):
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001551 """Locate an object by name or dotted path, importing as necessary."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001552 parts = [part for part in path.split('.') if part]
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001553 module, n = None, 0
1554 while n < len(parts):
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001555 nextmodule = safeimport('.'.join(parts[:n+1]), forceload)
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001556 if nextmodule: module, n = nextmodule, n + 1
1557 else: break
1558 if module:
1559 object = module
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001560 else:
Éric Araujoe64e51b2011-07-29 17:03:55 +02001561 object = builtins
1562 for part in parts[n:]:
1563 try:
1564 object = getattr(object, part)
1565 except AttributeError:
1566 return None
1567 return object
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001568
1569# --------------------------------------- interactive interpreter interface
1570
1571text = TextDoc()
Georg Brandld80d5f42010-12-03 07:47:22 +00001572plaintext = _PlainTextDoc()
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001573html = HTMLDoc()
1574
Ka-Ping Yee45daeb02002-08-11 15:11:33 +00001575def resolve(thing, forceload=0):
1576 """Given an object or a path to an object, get the object and its name."""
1577 if isinstance(thing, str):
1578 object = locate(thing, forceload)
1579 if not object:
Serhiy Storchaka1c205512015-03-01 00:42:54 +02001580 raise ImportError('''\
1581No Python documentation found for %r.
1582Use help() to get the interactive help utility.
1583Use help(str) for help on the str class.''' % thing)
Ka-Ping Yee45daeb02002-08-11 15:11:33 +00001584 return object, thing
1585 else:
R David Murrayc43125a2012-04-23 13:23:57 -04001586 name = getattr(thing, '__name__', None)
1587 return thing, name if isinstance(name, str) else None
Ka-Ping Yee45daeb02002-08-11 15:11:33 +00001588
Georg Brandld80d5f42010-12-03 07:47:22 +00001589def render_doc(thing, title='Python Library Documentation: %s', forceload=0,
1590 renderer=None):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001591 """Render text documentation, given an object or a path to an object."""
Georg Brandld80d5f42010-12-03 07:47:22 +00001592 if renderer is None:
1593 renderer = text
Guido van Rossumd8faa362007-04-27 19:54:29 +00001594 object, name = resolve(thing, forceload)
1595 desc = describe(object)
1596 module = inspect.getmodule(object)
1597 if name and '.' in name:
1598 desc += ' in ' + name[:name.rfind('.')]
1599 elif module and module is not object:
1600 desc += ' in module ' + module.__name__
Amaury Forgeot d'Arc768db922008-04-24 21:00:04 +00001601
1602 if not (inspect.ismodule(object) or
Guido van Rossumd8faa362007-04-27 19:54:29 +00001603 inspect.isclass(object) or
1604 inspect.isroutine(object) or
1605 inspect.isgetsetdescriptor(object) or
1606 inspect.ismemberdescriptor(object) or
1607 isinstance(object, property)):
1608 # If the passed object is a piece of data or an instance,
1609 # document its available methods instead of its value.
1610 object = type(object)
1611 desc += ' object'
Georg Brandld80d5f42010-12-03 07:47:22 +00001612 return title % desc + '\n\n' + renderer.document(object, name)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001613
Georg Brandld80d5f42010-12-03 07:47:22 +00001614def doc(thing, title='Python Library Documentation: %s', forceload=0,
1615 output=None):
Ka-Ping Yee9aa0d902001-04-12 10:50:23 +00001616 """Display text documentation, given an object or a path to an object."""
Ka-Ping Yee45daeb02002-08-11 15:11:33 +00001617 try:
Georg Brandld80d5f42010-12-03 07:47:22 +00001618 if output is None:
1619 pager(render_doc(thing, title, forceload))
1620 else:
1621 output.write(render_doc(thing, title, forceload, plaintext))
Guido van Rossumb940e112007-01-10 16:19:56 +00001622 except (ImportError, ErrorDuringImport) as value:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001623 print(value)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001624
Ka-Ping Yee45daeb02002-08-11 15:11:33 +00001625def writedoc(thing, forceload=0):
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001626 """Write HTML documentation to a file in the current directory."""
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001627 try:
Ka-Ping Yee45daeb02002-08-11 15:11:33 +00001628 object, name = resolve(thing, forceload)
1629 page = html.page(describe(object), html.document(object, name))
Georg Brandl388faac2009-04-10 08:31:48 +00001630 file = open(name + '.html', 'w', encoding='utf-8')
Ka-Ping Yee45daeb02002-08-11 15:11:33 +00001631 file.write(page)
1632 file.close()
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001633 print('wrote', name + '.html')
Guido van Rossumb940e112007-01-10 16:19:56 +00001634 except (ImportError, ErrorDuringImport) as value:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001635 print(value)
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001636
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001637def writedocs(dir, pkgpath='', done=None):
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00001638 """Write out HTML documentation for all modules in a directory tree."""
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001639 if done is None: done = {}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001640 for importer, modname, ispkg in pkgutil.walk_packages([dir], pkgpath):
1641 writedoc(modname)
1642 return
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001643
1644class Helper:
Georg Brandl6b38daa2008-06-01 21:05:17 +00001645
1646 # These dictionaries map a topic name to either an alias, or a tuple
1647 # (label, seealso-items). The "label" is the label of the corresponding
1648 # section in the .rst file under Doc/ and an index into the dictionary
Georg Brandl5617db82009-04-27 16:28:57 +00001649 # in pydoc_data/topics.py.
Georg Brandl6b38daa2008-06-01 21:05:17 +00001650 #
1651 # CAUTION: if you change one of these dictionaries, be sure to adapt the
Georg Brandl61bd1dc2014-09-30 22:56:38 +02001652 # list of needed labels in Doc/tools/pyspecific.py and
Georg Brandl5617db82009-04-27 16:28:57 +00001653 # regenerate the pydoc_data/topics.py file by running
Georg Brandl6b38daa2008-06-01 21:05:17 +00001654 # make pydoc-topics
1655 # in Doc/ and copying the output file into the Lib/ directory.
1656
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001657 keywords = {
Ezio Melottib185a042011-04-28 07:42:55 +03001658 'False': '',
1659 'None': '',
1660 'True': '',
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001661 'and': 'BOOLEAN',
Guido van Rossumd8faa362007-04-27 19:54:29 +00001662 'as': 'with',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001663 'assert': ('assert', ''),
1664 'break': ('break', 'while for'),
1665 'class': ('class', 'CLASSES SPECIALMETHODS'),
1666 'continue': ('continue', 'while for'),
1667 'def': ('function', ''),
1668 'del': ('del', 'BASICMETHODS'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001669 'elif': 'if',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001670 'else': ('else', 'while for'),
Georg Brandl0d855392008-08-30 19:53:05 +00001671 'except': 'try',
1672 'finally': 'try',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001673 'for': ('for', 'break continue while'),
Georg Brandl0d855392008-08-30 19:53:05 +00001674 'from': 'import',
Georg Brandl74abf6f2010-11-20 19:54:36 +00001675 'global': ('global', 'nonlocal NAMESPACES'),
Georg Brandl6b38daa2008-06-01 21:05:17 +00001676 'if': ('if', 'TRUTHVALUE'),
1677 'import': ('import', 'MODULES'),
Georg Brandl395ed242009-09-04 08:07:32 +00001678 'in': ('in', 'SEQUENCEMETHODS'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001679 'is': 'COMPARISON',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001680 'lambda': ('lambda', 'FUNCTIONS'),
Georg Brandl74abf6f2010-11-20 19:54:36 +00001681 'nonlocal': ('nonlocal', 'global NAMESPACES'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001682 'not': 'BOOLEAN',
1683 'or': 'BOOLEAN',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001684 'pass': ('pass', ''),
1685 'raise': ('raise', 'EXCEPTIONS'),
1686 'return': ('return', 'FUNCTIONS'),
1687 'try': ('try', 'EXCEPTIONS'),
1688 'while': ('while', 'break continue if TRUTHVALUE'),
1689 'with': ('with', 'CONTEXTMANAGERS EXCEPTIONS yield'),
1690 'yield': ('yield', ''),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001691 }
Georg Brandldb7b6b92009-01-01 15:53:14 +00001692 # Either add symbols to this dictionary or to the symbols dictionary
1693 # directly: Whichever is easier. They are merged later.
1694 _symbols_inverse = {
1695 'STRINGS' : ("'", "'''", "r'", "b'", '"""', '"', 'r"', 'b"'),
1696 'OPERATORS' : ('+', '-', '*', '**', '/', '//', '%', '<<', '>>', '&',
1697 '|', '^', '~', '<', '>', '<=', '>=', '==', '!=', '<>'),
1698 'COMPARISON' : ('<', '>', '<=', '>=', '==', '!=', '<>'),
1699 'UNARY' : ('-', '~'),
1700 'AUGMENTEDASSIGNMENT' : ('+=', '-=', '*=', '/=', '%=', '&=', '|=',
1701 '^=', '<<=', '>>=', '**=', '//='),
1702 'BITWISE' : ('<<', '>>', '&', '|', '^', '~'),
1703 'COMPLEX' : ('j', 'J')
1704 }
1705 symbols = {
1706 '%': 'OPERATORS FORMATTING',
1707 '**': 'POWER',
1708 ',': 'TUPLES LISTS FUNCTIONS',
1709 '.': 'ATTRIBUTES FLOAT MODULES OBJECTS',
1710 '...': 'ELLIPSIS',
1711 ':': 'SLICINGS DICTIONARYLITERALS',
1712 '@': 'def class',
1713 '\\': 'STRINGS',
1714 '_': 'PRIVATENAMES',
1715 '__': 'PRIVATENAMES SPECIALMETHODS',
1716 '`': 'BACKQUOTES',
1717 '(': 'TUPLES FUNCTIONS CALLS',
1718 ')': 'TUPLES FUNCTIONS CALLS',
1719 '[': 'LISTS SUBSCRIPTS SLICINGS',
1720 ']': 'LISTS SUBSCRIPTS SLICINGS'
1721 }
1722 for topic, symbols_ in _symbols_inverse.items():
1723 for symbol in symbols_:
1724 topics = symbols.get(symbol, topic)
1725 if topic not in topics:
1726 topics = topics + ' ' + topic
1727 symbols[symbol] = topics
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001728
1729 topics = {
Georg Brandl6b38daa2008-06-01 21:05:17 +00001730 'TYPES': ('types', 'STRINGS UNICODE NUMBERS SEQUENCES MAPPINGS '
1731 'FUNCTIONS CLASSES MODULES FILES inspect'),
1732 'STRINGS': ('strings', 'str UNICODE SEQUENCES STRINGMETHODS '
1733 'FORMATTING TYPES'),
1734 'STRINGMETHODS': ('string-methods', 'STRINGS FORMATTING'),
1735 'FORMATTING': ('formatstrings', 'OPERATORS'),
1736 'UNICODE': ('strings', 'encodings unicode SEQUENCES STRINGMETHODS '
1737 'FORMATTING TYPES'),
1738 'NUMBERS': ('numbers', 'INTEGER FLOAT COMPLEX TYPES'),
1739 'INTEGER': ('integers', 'int range'),
1740 'FLOAT': ('floating', 'float math'),
1741 'COMPLEX': ('imaginary', 'complex cmath'),
1742 'SEQUENCES': ('typesseq', 'STRINGMETHODS FORMATTING range LISTS'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001743 'MAPPINGS': 'DICTIONARIES',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001744 'FUNCTIONS': ('typesfunctions', 'def TYPES'),
1745 'METHODS': ('typesmethods', 'class def CLASSES TYPES'),
1746 'CODEOBJECTS': ('bltin-code-objects', 'compile FUNCTIONS TYPES'),
1747 'TYPEOBJECTS': ('bltin-type-objects', 'types TYPES'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001748 'FRAMEOBJECTS': 'TYPES',
1749 'TRACEBACKS': 'TYPES',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001750 'NONE': ('bltin-null-object', ''),
1751 'ELLIPSIS': ('bltin-ellipsis-object', 'SLICINGS'),
Georg Brandl6b38daa2008-06-01 21:05:17 +00001752 'SPECIALATTRIBUTES': ('specialattrs', ''),
1753 'CLASSES': ('types', 'class SPECIALMETHODS PRIVATENAMES'),
1754 'MODULES': ('typesmodules', 'import'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001755 'PACKAGES': 'import',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001756 'EXPRESSIONS': ('operator-summary', 'lambda or and not in is BOOLEAN '
1757 'COMPARISON BITWISE SHIFTING BINARY FORMATTING POWER '
1758 'UNARY ATTRIBUTES SUBSCRIPTS SLICINGS CALLS TUPLES '
1759 'LISTS DICTIONARIES'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001760 'OPERATORS': 'EXPRESSIONS',
1761 'PRECEDENCE': 'EXPRESSIONS',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001762 'OBJECTS': ('objects', 'TYPES'),
1763 'SPECIALMETHODS': ('specialnames', 'BASICMETHODS ATTRIBUTEMETHODS '
Georg Brandl395ed242009-09-04 08:07:32 +00001764 'CALLABLEMETHODS SEQUENCEMETHODS MAPPINGMETHODS '
1765 'NUMBERMETHODS CLASSES'),
Mark Dickinsona56c4672009-01-27 18:17:45 +00001766 'BASICMETHODS': ('customization', 'hash repr str SPECIALMETHODS'),
Georg Brandl6b38daa2008-06-01 21:05:17 +00001767 'ATTRIBUTEMETHODS': ('attribute-access', 'ATTRIBUTES SPECIALMETHODS'),
1768 'CALLABLEMETHODS': ('callable-types', 'CALLS SPECIALMETHODS'),
Georg Brandl395ed242009-09-04 08:07:32 +00001769 'SEQUENCEMETHODS': ('sequence-types', 'SEQUENCES SEQUENCEMETHODS '
Georg Brandl6b38daa2008-06-01 21:05:17 +00001770 'SPECIALMETHODS'),
1771 'MAPPINGMETHODS': ('sequence-types', 'MAPPINGS SPECIALMETHODS'),
1772 'NUMBERMETHODS': ('numeric-types', 'NUMBERS AUGMENTEDASSIGNMENT '
1773 'SPECIALMETHODS'),
1774 'EXECUTION': ('execmodel', 'NAMESPACES DYNAMICFEATURES EXCEPTIONS'),
Georg Brandl74abf6f2010-11-20 19:54:36 +00001775 'NAMESPACES': ('naming', 'global nonlocal ASSIGNMENT DELETION DYNAMICFEATURES'),
Georg Brandl6b38daa2008-06-01 21:05:17 +00001776 'DYNAMICFEATURES': ('dynamic-features', ''),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001777 'SCOPING': 'NAMESPACES',
1778 'FRAMES': 'NAMESPACES',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001779 'EXCEPTIONS': ('exceptions', 'try except finally raise'),
1780 'CONVERSIONS': ('conversions', ''),
1781 'IDENTIFIERS': ('identifiers', 'keywords SPECIALIDENTIFIERS'),
1782 'SPECIALIDENTIFIERS': ('id-classes', ''),
1783 'PRIVATENAMES': ('atom-identifiers', ''),
1784 'LITERALS': ('atom-literals', 'STRINGS NUMBERS TUPLELITERALS '
1785 'LISTLITERALS DICTIONARYLITERALS'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001786 'TUPLES': 'SEQUENCES',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001787 'TUPLELITERALS': ('exprlists', 'TUPLES LITERALS'),
1788 'LISTS': ('typesseq-mutable', 'LISTLITERALS'),
1789 'LISTLITERALS': ('lists', 'LISTS LITERALS'),
1790 'DICTIONARIES': ('typesmapping', 'DICTIONARYLITERALS'),
1791 'DICTIONARYLITERALS': ('dict', 'DICTIONARIES LITERALS'),
1792 'ATTRIBUTES': ('attribute-references', 'getattr hasattr setattr ATTRIBUTEMETHODS'),
Georg Brandl395ed242009-09-04 08:07:32 +00001793 'SUBSCRIPTS': ('subscriptions', 'SEQUENCEMETHODS'),
1794 'SLICINGS': ('slicings', 'SEQUENCEMETHODS'),
Georg Brandl6b38daa2008-06-01 21:05:17 +00001795 'CALLS': ('calls', 'EXPRESSIONS'),
1796 'POWER': ('power', 'EXPRESSIONS'),
1797 'UNARY': ('unary', 'EXPRESSIONS'),
1798 'BINARY': ('binary', 'EXPRESSIONS'),
1799 'SHIFTING': ('shifting', 'EXPRESSIONS'),
1800 'BITWISE': ('bitwise', 'EXPRESSIONS'),
1801 'COMPARISON': ('comparisons', 'EXPRESSIONS BASICMETHODS'),
1802 'BOOLEAN': ('booleans', 'EXPRESSIONS TRUTHVALUE'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001803 'ASSERTION': 'assert',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001804 'ASSIGNMENT': ('assignment', 'AUGMENTEDASSIGNMENT'),
1805 'AUGMENTEDASSIGNMENT': ('augassign', 'NUMBERMETHODS'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001806 'DELETION': 'del',
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001807 'RETURNING': 'return',
1808 'IMPORTING': 'import',
1809 'CONDITIONAL': 'if',
Georg Brandl6b38daa2008-06-01 21:05:17 +00001810 'LOOPING': ('compound', 'for while break continue'),
1811 'TRUTHVALUE': ('truth', 'if while and or not BASICMETHODS'),
1812 'DEBUGGING': ('debugger', 'pdb'),
1813 'CONTEXTMANAGERS': ('context-managers', 'with'),
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001814 }
1815
Georg Brandl78aa3962010-07-31 21:51:48 +00001816 def __init__(self, input=None, output=None):
1817 self._input = input
1818 self._output = output
1819
Georg Brandl76ae3972010-08-01 06:32:55 +00001820 input = property(lambda self: self._input or sys.stdin)
1821 output = property(lambda self: self._output or sys.stdout)
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001822
Ka-Ping Yee79c009d2001-04-13 10:53:25 +00001823 def __repr__(self):
Ka-Ping Yee9bc576b2001-04-13 13:57:31 +00001824 if inspect.stack()[1][3] == '?':
Ka-Ping Yee79c009d2001-04-13 10:53:25 +00001825 self()
1826 return ''
Serhiy Storchaka465e60e2014-07-25 23:36:00 +03001827 return '<%s.%s instance>' % (self.__class__.__module__,
1828 self.__class__.__qualname__)
Ka-Ping Yee79c009d2001-04-13 10:53:25 +00001829
Alexander Belopolsky2e733c92010-07-04 17:00:20 +00001830 _GoInteractive = object()
1831 def __call__(self, request=_GoInteractive):
1832 if request is not self._GoInteractive:
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001833 self.help(request)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001834 else:
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001835 self.intro()
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001836 self.interact()
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001837 self.output.write('''
Fred Drakee61967f2001-05-10 18:41:02 +00001838You are now leaving help and returning to the Python interpreter.
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001839If you want to ask for help on a particular object directly from the
1840interpreter, you can type "help(object)". Executing "help('string')"
1841has the same effect as typing a particular string at the help> prompt.
1842''')
Ka-Ping Yeedd175342001-02-27 14:43:46 +00001843
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001844 def interact(self):
1845 self.output.write('\n')
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001846 while True:
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001847 try:
Johannes Gijsberse7691d32004-08-17 13:21:53 +00001848 request = self.getline('help> ')
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001849 if not request: break
Johannes Gijsberse7691d32004-08-17 13:21:53 +00001850 except (KeyboardInterrupt, EOFError):
1851 break
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001852 request = replace(request, '"', '', "'", '').strip()
1853 if request.lower() in ('q', 'quit'): break
Serhiy Storchaka1c205512015-03-01 00:42:54 +02001854 if request == 'help':
1855 self.intro()
1856 else:
1857 self.help(request)
Ka-Ping Yeedec96e92001-04-13 09:55:49 +00001858
Johannes Gijsberse7691d32004-08-17 13:21:53 +00001859 def getline(self, prompt):
Guido van Rossum7c086c02008-01-02 03:52:38 +00001860 """Read one line, using input() when appropriate."""
Johannes Gijsberse7691d32004-08-17 13:21:53 +00001861 if self.input is sys.stdin:
Guido van Rossum7c086c02008-01-02 03:52:38 +00001862 return input(prompt)
Johannes Gijsberse7691d32004-08-17 13:21:53 +00001863 else:
1864 self.output.write(prompt)
1865 self.output.flush()
1866 return self.input.readline()
1867
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001868 def help(self, request):
1869 if type(request) is type(''):
R. David Murray1f1b9d32009-05-27 20:56:59 +00001870 request = request.strip()
Serhiy Storchaka1c205512015-03-01 00:42:54 +02001871 if request == 'keywords': self.listkeywords()
Georg Brandldb7b6b92009-01-01 15:53:14 +00001872 elif request == 'symbols': self.listsymbols()
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001873 elif request == 'topics': self.listtopics()
1874 elif request == 'modules': self.listmodules()
1875 elif request[:8] == 'modules ':
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001876 self.listmodules(request.split()[1])
Georg Brandldb7b6b92009-01-01 15:53:14 +00001877 elif request in self.symbols: self.showsymbol(request)
Ezio Melottib185a042011-04-28 07:42:55 +03001878 elif request in ['True', 'False', 'None']:
1879 # special case these keywords since they are objects too
1880 doc(eval(request), 'Help on %s:')
Raymond Hettinger54f02222002-06-01 14:18:47 +00001881 elif request in self.keywords: self.showtopic(request)
1882 elif request in self.topics: self.showtopic(request)
Georg Brandld80d5f42010-12-03 07:47:22 +00001883 elif request: doc(request, 'Help on %s:', output=self._output)
Serhiy Storchaka1c205512015-03-01 00:42:54 +02001884 else: doc(str, 'Help on %s:', output=self._output)
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001885 elif isinstance(request, Helper): self()
Georg Brandld80d5f42010-12-03 07:47:22 +00001886 else: doc(request, 'Help on %s:', output=self._output)
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001887 self.output.write('\n')
1888
1889 def intro(self):
1890 self.output.write('''
R David Murray3d050dd2014-04-19 12:59:30 -04001891Welcome to Python %s's help utility!
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001892
1893If this is your first time using Python, you should definitely check out
R David Murrayde0f6292012-03-31 12:06:35 -04001894the tutorial on the Internet at http://docs.python.org/%s/tutorial/.
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001895
1896Enter the name of any module, keyword, or topic to get help on writing
1897Python programs and using Python modules. To quit this help utility and
1898return to the interpreter, just type "quit".
1899
Terry Jan Reedy34200572013-02-11 02:23:13 -05001900To get a list of available modules, keywords, symbols, or topics, type
1901"modules", "keywords", "symbols", or "topics". Each module also comes
1902with a one-line summary of what it does; to list the modules whose name
1903or summary contain a given string such as "spam", type "modules spam".
R David Murrayde0f6292012-03-31 12:06:35 -04001904''' % tuple([sys.version[:3]]*2))
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001905
1906 def list(self, items, columns=4, width=80):
Guido van Rossum486364b2007-06-30 05:01:58 +00001907 items = list(sorted(items))
1908 colw = width // columns
1909 rows = (len(items) + columns - 1) // columns
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001910 for row in range(rows):
1911 for col in range(columns):
1912 i = col * rows + row
1913 if i < len(items):
1914 self.output.write(items[i])
1915 if col < columns - 1:
Guido van Rossum486364b2007-06-30 05:01:58 +00001916 self.output.write(' ' + ' ' * (colw - 1 - len(items[i])))
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001917 self.output.write('\n')
1918
1919 def listkeywords(self):
1920 self.output.write('''
1921Here is a list of the Python keywords. Enter any keyword to get more help.
1922
1923''')
1924 self.list(self.keywords.keys())
1925
Georg Brandldb7b6b92009-01-01 15:53:14 +00001926 def listsymbols(self):
1927 self.output.write('''
1928Here is a list of the punctuation symbols which Python assigns special meaning
1929to. Enter any symbol to get more help.
1930
1931''')
1932 self.list(self.symbols.keys())
1933
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001934 def listtopics(self):
1935 self.output.write('''
1936Here is a list of available topics. Enter any topic name to get more help.
1937
1938''')
1939 self.list(self.topics.keys())
1940
Georg Brandldb7b6b92009-01-01 15:53:14 +00001941 def showtopic(self, topic, more_xrefs=''):
Georg Brandl6b38daa2008-06-01 21:05:17 +00001942 try:
Georg Brandl5617db82009-04-27 16:28:57 +00001943 import pydoc_data.topics
Brett Cannoncd171c82013-07-04 17:43:24 -04001944 except ImportError:
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001945 self.output.write('''
Georg Brandl6b38daa2008-06-01 21:05:17 +00001946Sorry, topic and keyword documentation is not available because the
Georg Brandl5617db82009-04-27 16:28:57 +00001947module "pydoc_data.topics" could not be found.
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001948''')
1949 return
1950 target = self.topics.get(topic, self.keywords.get(topic))
1951 if not target:
1952 self.output.write('no documentation found for %s\n' % repr(topic))
1953 return
1954 if type(target) is type(''):
Georg Brandldb7b6b92009-01-01 15:53:14 +00001955 return self.showtopic(target, more_xrefs)
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001956
Georg Brandl6b38daa2008-06-01 21:05:17 +00001957 label, xrefs = target
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001958 try:
Georg Brandl5617db82009-04-27 16:28:57 +00001959 doc = pydoc_data.topics.topics[label]
Georg Brandl6b38daa2008-06-01 21:05:17 +00001960 except KeyError:
1961 self.output.write('no documentation found for %s\n' % repr(topic))
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001962 return
Georg Brandl6b38daa2008-06-01 21:05:17 +00001963 pager(doc.strip() + '\n')
Georg Brandldb7b6b92009-01-01 15:53:14 +00001964 if more_xrefs:
1965 xrefs = (xrefs or '') + ' ' + more_xrefs
Ka-Ping Yeeda793892001-04-13 11:02:51 +00001966 if xrefs:
Brett Cannon1448ecf2013-10-04 11:38:59 -04001967 import textwrap
1968 text = 'Related help topics: ' + ', '.join(xrefs.split()) + '\n'
1969 wrapped_text = textwrap.wrap(text, 72)
1970 self.output.write('\n%s\n' % ''.join(wrapped_text))
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00001971
Nick Coghlan7bb30b72010-12-03 09:29:11 +00001972 def _gettopic(self, topic, more_xrefs=''):
1973 """Return unbuffered tuple of (topic, xrefs).
1974
Georg Brandld2f38572011-01-30 08:37:19 +00001975 If an error occurs here, the exception is caught and displayed by
1976 the url handler.
1977
Nick Coghlan7bb30b72010-12-03 09:29:11 +00001978 This function duplicates the showtopic method but returns its
1979 result directly so it can be formatted for display in an html page.
1980 """
1981 try:
1982 import pydoc_data.topics
Brett Cannoncd171c82013-07-04 17:43:24 -04001983 except ImportError:
Nick Coghlan7bb30b72010-12-03 09:29:11 +00001984 return('''
1985Sorry, topic and keyword documentation is not available because the
1986module "pydoc_data.topics" could not be found.
1987''' , '')
1988 target = self.topics.get(topic, self.keywords.get(topic))
1989 if not target:
Georg Brandld2f38572011-01-30 08:37:19 +00001990 raise ValueError('could not find topic')
Nick Coghlan7bb30b72010-12-03 09:29:11 +00001991 if isinstance(target, str):
1992 return self._gettopic(target, more_xrefs)
1993 label, xrefs = target
Georg Brandld2f38572011-01-30 08:37:19 +00001994 doc = pydoc_data.topics.topics[label]
Nick Coghlan7bb30b72010-12-03 09:29:11 +00001995 if more_xrefs:
1996 xrefs = (xrefs or '') + ' ' + more_xrefs
1997 return doc, xrefs
1998
Georg Brandldb7b6b92009-01-01 15:53:14 +00001999 def showsymbol(self, symbol):
2000 target = self.symbols[symbol]
2001 topic, _, xrefs = target.partition(' ')
2002 self.showtopic(topic, xrefs)
2003
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00002004 def listmodules(self, key=''):
2005 if key:
2006 self.output.write('''
Terry Jan Reedy34200572013-02-11 02:23:13 -05002007Here is a list of modules whose name or summary contains '{}'.
2008If there are any, enter a module name to get more help.
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00002009
Terry Jan Reedy34200572013-02-11 02:23:13 -05002010'''.format(key))
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00002011 apropos(key)
2012 else:
2013 self.output.write('''
2014Please wait a moment while I gather a list of all available modules...
2015
2016''')
2017 modules = {}
2018 def callback(path, modname, desc, modules=modules):
2019 if modname and modname[-9:] == '.__init__':
2020 modname = modname[:-9] + ' (package)'
Neal Norwitz9d72bb42007-04-17 08:48:32 +00002021 if modname.find('.') < 0:
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00002022 modules[modname] = 1
Christian Heimesd32ed6f2008-01-14 18:49:24 +00002023 def onerror(modname):
2024 callback(None, modname, None)
2025 ModuleScanner().run(callback, onerror=onerror)
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00002026 self.list(modules.keys())
2027 self.output.write('''
2028Enter any module name to get more help. Or, type "modules spam" to search
Terry Jan Reedy34200572013-02-11 02:23:13 -05002029for modules whose name or summary contain the string "spam".
Ka-Ping Yee35cf0a32001-04-12 19:53:52 +00002030''')
2031
Georg Brandl78aa3962010-07-31 21:51:48 +00002032help = Helper()
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002033
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002034class ModuleScanner:
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002035 """An interruptible scanner that searches module synopses."""
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002036
Christian Heimesd32ed6f2008-01-14 18:49:24 +00002037 def run(self, callback, key=None, completer=None, onerror=None):
Neal Norwitz9d72bb42007-04-17 08:48:32 +00002038 if key: key = key.lower()
Guido van Rossum8ca162f2002-04-07 06:36:23 +00002039 self.quit = False
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002040 seen = {}
2041
2042 for modname in sys.builtin_module_names:
Ka-Ping Yee239432a2001-03-02 02:45:08 +00002043 if modname != '__main__':
2044 seen[modname] = 1
Ka-Ping Yee66246962001-04-12 11:59:50 +00002045 if key is None:
2046 callback(None, modname, '')
2047 else:
Neal Norwitz9d72bb42007-04-17 08:48:32 +00002048 name = __import__(modname).__doc__ or ''
2049 desc = name.split('\n')[0]
2050 name = modname + ' - ' + desc
2051 if name.lower().find(key) >= 0:
Ka-Ping Yee66246962001-04-12 11:59:50 +00002052 callback(None, modname, desc)
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002053
Christian Heimesd32ed6f2008-01-14 18:49:24 +00002054 for importer, modname, ispkg in pkgutil.walk_packages(onerror=onerror):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002055 if self.quit:
2056 break
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002057
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002058 if key is None:
2059 callback(None, modname, '')
2060 else:
Georg Brandl126c8792009-04-05 15:05:48 +00002061 try:
Eric Snow3a62d142014-01-06 20:42:59 -07002062 spec = pkgutil._get_spec(importer, modname)
Georg Brandl126c8792009-04-05 15:05:48 +00002063 except SyntaxError:
2064 # raised by tests for bad coding cookies or BOM
2065 continue
Eric Snow3a62d142014-01-06 20:42:59 -07002066 loader = spec.loader
Georg Brandl126c8792009-04-05 15:05:48 +00002067 if hasattr(loader, 'get_source'):
Amaury Forgeot d'Arc9196dc62008-06-19 20:54:32 +00002068 try:
2069 source = loader.get_source(modname)
Nick Coghlan2824cb52012-07-15 22:12:14 +10002070 except Exception:
Amaury Forgeot d'Arc9196dc62008-06-19 20:54:32 +00002071 if onerror:
2072 onerror(modname)
2073 continue
Amaury Forgeot d'Arc9196dc62008-06-19 20:54:32 +00002074 desc = source_synopsis(io.StringIO(source)) or ''
Georg Brandl126c8792009-04-05 15:05:48 +00002075 if hasattr(loader, 'get_filename'):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002076 path = loader.get_filename(modname)
Ka-Ping Yee66246962001-04-12 11:59:50 +00002077 else:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002078 path = None
2079 else:
Amaury Forgeot d'Arc9196dc62008-06-19 20:54:32 +00002080 try:
Brett Cannon2a17bde2014-05-30 14:55:29 -04002081 module = importlib._bootstrap._load(spec)
Amaury Forgeot d'Arc9196dc62008-06-19 20:54:32 +00002082 except ImportError:
2083 if onerror:
2084 onerror(modname)
2085 continue
Benjamin Peterson54237f92015-02-16 19:45:01 -05002086 desc = module.__doc__.splitlines()[0] if module.__doc__ else ''
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002087 path = getattr(module,'__file__',None)
Neal Norwitz9d72bb42007-04-17 08:48:32 +00002088 name = modname + ' - ' + desc
2089 if name.lower().find(key) >= 0:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002090 callback(path, modname, desc)
2091
2092 if completer:
2093 completer()
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002094
2095def apropos(key):
2096 """Print all the one-line module summaries that contain a substring."""
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002097 def callback(path, modname, desc):
2098 if modname[-9:] == '.__init__':
2099 modname = modname[:-9] + ' (package)'
Guido van Rossumbe19ed72007-02-09 05:37:30 +00002100 print(modname, desc and '- ' + desc)
Amaury Forgeot d'Arc9196dc62008-06-19 20:54:32 +00002101 def onerror(modname):
2102 pass
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002103 with warnings.catch_warnings():
2104 warnings.filterwarnings('ignore') # ignore problems during import
2105 ModuleScanner().run(callback, key, onerror=onerror)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002106
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002107# --------------------------------------- enhanced Web browser interface
2108
2109def _start_server(urlhandler, port):
2110 """Start an HTTP server thread on a specific port.
2111
2112 Start an HTML/text server thread, so HTML or text documents can be
2113 browsed dynamically and interactively with a Web browser. Example use:
2114
2115 >>> import time
2116 >>> import pydoc
2117
2118 Define a URL handler. To determine what the client is asking
2119 for, check the URL and content_type.
2120
2121 Then get or generate some text or HTML code and return it.
2122
2123 >>> def my_url_handler(url, content_type):
2124 ... text = 'the URL sent was: (%s, %s)' % (url, content_type)
2125 ... return text
2126
2127 Start server thread on port 0.
2128 If you use port 0, the server will pick a random port number.
2129 You can then use serverthread.port to get the port number.
2130
2131 >>> port = 0
2132 >>> serverthread = pydoc._start_server(my_url_handler, port)
2133
2134 Check that the server is really started. If it is, open browser
2135 and get first page. Use serverthread.url as the starting page.
2136
2137 >>> if serverthread.serving:
2138 ... import webbrowser
2139
2140 The next two lines are commented out so a browser doesn't open if
2141 doctest is run on this module.
2142
2143 #... webbrowser.open(serverthread.url)
2144 #True
2145
2146 Let the server do its thing. We just need to monitor its status.
2147 Use time.sleep so the loop doesn't hog the CPU.
2148
2149 >>> starttime = time.time()
2150 >>> timeout = 1 #seconds
2151
2152 This is a short timeout for testing purposes.
2153
2154 >>> while serverthread.serving:
2155 ... time.sleep(.01)
2156 ... if serverthread.serving and time.time() - starttime > timeout:
2157 ... serverthread.stop()
2158 ... break
2159
2160 Print any errors that may have occurred.
2161
2162 >>> print(serverthread.error)
2163 None
2164 """
2165 import http.server
2166 import email.message
2167 import select
2168 import threading
2169
2170 class DocHandler(http.server.BaseHTTPRequestHandler):
2171
2172 def do_GET(self):
2173 """Process a request from an HTML browser.
2174
2175 The URL received is in self.path.
2176 Get an HTML page from self.urlhandler and send it.
2177 """
2178 if self.path.endswith('.css'):
2179 content_type = 'text/css'
2180 else:
2181 content_type = 'text/html'
2182 self.send_response(200)
Georg Brandld2f38572011-01-30 08:37:19 +00002183 self.send_header('Content-Type', '%s; charset=UTF-8' % content_type)
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002184 self.end_headers()
2185 self.wfile.write(self.urlhandler(
2186 self.path, content_type).encode('utf-8'))
2187
2188 def log_message(self, *args):
2189 # Don't log messages.
2190 pass
2191
2192 class DocServer(http.server.HTTPServer):
2193
2194 def __init__(self, port, callback):
Senthil Kumaran2a42a0b2014-09-17 13:17:58 +08002195 self.host = 'localhost'
2196 self.address = (self.host, port)
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002197 self.callback = callback
2198 self.base.__init__(self, self.address, self.handler)
2199 self.quit = False
2200
2201 def serve_until_quit(self):
2202 while not self.quit:
2203 rd, wr, ex = select.select([self.socket.fileno()], [], [], 1)
2204 if rd:
2205 self.handle_request()
Victor Stinnera3abd1d2011-01-03 16:12:39 +00002206 self.server_close()
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002207
2208 def server_activate(self):
2209 self.base.server_activate(self)
2210 if self.callback:
2211 self.callback(self)
2212
2213 class ServerThread(threading.Thread):
2214
2215 def __init__(self, urlhandler, port):
2216 self.urlhandler = urlhandler
2217 self.port = int(port)
2218 threading.Thread.__init__(self)
2219 self.serving = False
2220 self.error = None
2221
2222 def run(self):
2223 """Start the server."""
2224 try:
2225 DocServer.base = http.server.HTTPServer
2226 DocServer.handler = DocHandler
2227 DocHandler.MessageClass = email.message.Message
2228 DocHandler.urlhandler = staticmethod(self.urlhandler)
2229 docsvr = DocServer(self.port, self.ready)
2230 self.docserver = docsvr
2231 docsvr.serve_until_quit()
2232 except Exception as e:
2233 self.error = e
2234
2235 def ready(self, server):
2236 self.serving = True
2237 self.host = server.host
2238 self.port = server.server_port
2239 self.url = 'http://%s:%d/' % (self.host, self.port)
2240
2241 def stop(self):
2242 """Stop the server and this thread nicely"""
2243 self.docserver.quit = True
2244 self.serving = False
2245 self.url = None
2246
2247 thread = ServerThread(urlhandler, port)
2248 thread.start()
2249 # Wait until thread.serving is True to make sure we are
2250 # really up before returning.
2251 while not thread.error and not thread.serving:
2252 time.sleep(.01)
2253 return thread
2254
2255
2256def _url_handler(url, content_type="text/html"):
2257 """The pydoc url handler for use with the pydoc server.
2258
2259 If the content_type is 'text/css', the _pydoc.css style
2260 sheet is read and returned if it exits.
2261
2262 If the content_type is 'text/html', then the result of
2263 get_html_page(url) is returned.
2264 """
2265 class _HTMLDoc(HTMLDoc):
2266
2267 def page(self, title, contents):
2268 """Format an HTML page."""
2269 css_path = "pydoc_data/_pydoc.css"
2270 css_link = (
2271 '<link rel="stylesheet" type="text/css" href="%s">' %
2272 css_path)
2273 return '''\
2274<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Georg Brandld2f38572011-01-30 08:37:19 +00002275<html><head><title>Pydoc: %s</title>
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002276<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Georg Brandld2f38572011-01-30 08:37:19 +00002277%s</head><body bgcolor="#f0f0f8">%s<div style="clear:both;padding-top:.5em;">%s</div>
2278</body></html>''' % (title, css_link, html_navbar(), contents)
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002279
2280 def filelink(self, url, path):
2281 return '<a href="getfile?key=%s">%s</a>' % (url, path)
2282
2283
2284 html = _HTMLDoc()
2285
2286 def html_navbar():
Georg Brandld2f38572011-01-30 08:37:19 +00002287 version = html.escape("%s [%s, %s]" % (platform.python_version(),
2288 platform.python_build()[0],
2289 platform.python_compiler()))
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002290 return """
2291 <div style='float:left'>
Georg Brandld2f38572011-01-30 08:37:19 +00002292 Python %s<br>%s
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002293 </div>
2294 <div style='float:right'>
2295 <div style='text-align:center'>
2296 <a href="index.html">Module Index</a>
2297 : <a href="topics.html">Topics</a>
2298 : <a href="keywords.html">Keywords</a>
2299 </div>
2300 <div>
Georg Brandld2f38572011-01-30 08:37:19 +00002301 <form action="get" style='display:inline;'>
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002302 <input type=text name=key size=15>
2303 <input type=submit value="Get">
Georg Brandld2f38572011-01-30 08:37:19 +00002304 </form>&nbsp;
2305 <form action="search" style='display:inline;'>
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002306 <input type=text name=key size=15>
2307 <input type=submit value="Search">
2308 </form>
2309 </div>
2310 </div>
Georg Brandld2f38572011-01-30 08:37:19 +00002311 """ % (version, html.escape(platform.platform(terse=True)))
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002312
2313 def html_index():
2314 """Module Index page."""
2315
2316 def bltinlink(name):
2317 return '<a href="%s.html">%s</a>' % (name, name)
2318
2319 heading = html.heading(
2320 '<big><big><strong>Index of Modules</strong></big></big>',
2321 '#ffffff', '#7799ee')
2322 names = [name for name in sys.builtin_module_names
2323 if name != '__main__']
2324 contents = html.multicolumn(names, bltinlink)
2325 contents = [heading, '<p>' + html.bigsection(
2326 'Built-in Modules', '#ffffff', '#ee77aa', contents)]
2327
2328 seen = {}
2329 for dir in sys.path:
2330 contents.append(html.index(dir, seen))
2331
2332 contents.append(
2333 '<p align=right><font color="#909090" face="helvetica,'
2334 'arial"><strong>pydoc</strong> by Ka-Ping Yee'
2335 '&lt;ping@lfw.org&gt;</font>')
Nick Coghlanecace282010-12-03 16:08:46 +00002336 return 'Index of Modules', ''.join(contents)
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002337
2338 def html_search(key):
2339 """Search results page."""
2340 # scan for modules
2341 search_result = []
2342
2343 def callback(path, modname, desc):
2344 if modname[-9:] == '.__init__':
2345 modname = modname[:-9] + ' (package)'
2346 search_result.append((modname, desc and '- ' + desc))
2347
2348 with warnings.catch_warnings():
2349 warnings.filterwarnings('ignore') # ignore problems during import
2350 ModuleScanner().run(callback, key)
2351
2352 # format page
2353 def bltinlink(name):
2354 return '<a href="%s.html">%s</a>' % (name, name)
2355
2356 results = []
2357 heading = html.heading(
2358 '<big><big><strong>Search Results</strong></big></big>',
2359 '#ffffff', '#7799ee')
2360 for name, desc in search_result:
2361 results.append(bltinlink(name) + desc)
2362 contents = heading + html.bigsection(
2363 'key = %s' % key, '#ffffff', '#ee77aa', '<br>'.join(results))
Nick Coghlanecace282010-12-03 16:08:46 +00002364 return 'Search Results', contents
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002365
2366 def html_getfile(path):
2367 """Get and display a source file listing safely."""
Zachary Wareeb432142014-07-10 11:18:00 -05002368 path = urllib.parse.unquote(path)
Victor Stinner91e08772011-07-05 14:30:41 +02002369 with tokenize.open(path) as fp:
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002370 lines = html.escape(fp.read())
2371 body = '<pre>%s</pre>' % lines
2372 heading = html.heading(
2373 '<big><big><strong>File Listing</strong></big></big>',
2374 '#ffffff', '#7799ee')
2375 contents = heading + html.bigsection(
2376 'File: %s' % path, '#ffffff', '#ee77aa', body)
Nick Coghlanecace282010-12-03 16:08:46 +00002377 return 'getfile %s' % path, contents
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002378
2379 def html_topics():
2380 """Index of topic texts available."""
2381
2382 def bltinlink(name):
Georg Brandld2f38572011-01-30 08:37:19 +00002383 return '<a href="topic?key=%s">%s</a>' % (name, name)
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002384
2385 heading = html.heading(
2386 '<big><big><strong>INDEX</strong></big></big>',
2387 '#ffffff', '#7799ee')
2388 names = sorted(Helper.topics.keys())
2389
2390 contents = html.multicolumn(names, bltinlink)
2391 contents = heading + html.bigsection(
2392 'Topics', '#ffffff', '#ee77aa', contents)
Nick Coghlanecace282010-12-03 16:08:46 +00002393 return 'Topics', contents
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002394
2395 def html_keywords():
2396 """Index of keywords."""
2397 heading = html.heading(
2398 '<big><big><strong>INDEX</strong></big></big>',
2399 '#ffffff', '#7799ee')
2400 names = sorted(Helper.keywords.keys())
2401
2402 def bltinlink(name):
Georg Brandld2f38572011-01-30 08:37:19 +00002403 return '<a href="topic?key=%s">%s</a>' % (name, name)
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002404
2405 contents = html.multicolumn(names, bltinlink)
2406 contents = heading + html.bigsection(
2407 'Keywords', '#ffffff', '#ee77aa', contents)
Nick Coghlanecace282010-12-03 16:08:46 +00002408 return 'Keywords', contents
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002409
2410 def html_topicpage(topic):
2411 """Topic or keyword help page."""
2412 buf = io.StringIO()
2413 htmlhelp = Helper(buf, buf)
2414 contents, xrefs = htmlhelp._gettopic(topic)
2415 if topic in htmlhelp.keywords:
2416 title = 'KEYWORD'
2417 else:
2418 title = 'TOPIC'
2419 heading = html.heading(
2420 '<big><big><strong>%s</strong></big></big>' % title,
2421 '#ffffff', '#7799ee')
Georg Brandld2f38572011-01-30 08:37:19 +00002422 contents = '<pre>%s</pre>' % html.markup(contents)
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002423 contents = html.bigsection(topic , '#ffffff','#ee77aa', contents)
Georg Brandld2f38572011-01-30 08:37:19 +00002424 if xrefs:
2425 xrefs = sorted(xrefs.split())
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002426
Georg Brandld2f38572011-01-30 08:37:19 +00002427 def bltinlink(name):
2428 return '<a href="topic?key=%s">%s</a>' % (name, name)
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002429
Georg Brandld2f38572011-01-30 08:37:19 +00002430 xrefs = html.multicolumn(xrefs, bltinlink)
2431 xrefs = html.section('Related help topics: ',
2432 '#ffffff', '#ee77aa', xrefs)
Nick Coghlanecace282010-12-03 16:08:46 +00002433 return ('%s %s' % (title, topic),
2434 ''.join((heading, contents, xrefs)))
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002435
Georg Brandld2f38572011-01-30 08:37:19 +00002436 def html_getobj(url):
2437 obj = locate(url, forceload=1)
2438 if obj is None and url != 'None':
2439 raise ValueError('could not find object')
2440 title = describe(obj)
2441 content = html.document(obj, url)
2442 return title, content
2443
2444 def html_error(url, exc):
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002445 heading = html.heading(
2446 '<big><big><strong>Error</strong></big></big>',
Georg Brandld2f38572011-01-30 08:37:19 +00002447 '#ffffff', '#7799ee')
2448 contents = '<br>'.join(html.escape(line) for line in
2449 format_exception_only(type(exc), exc))
2450 contents = heading + html.bigsection(url, '#ffffff', '#bb0000',
2451 contents)
2452 return "Error - %s" % url, contents
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002453
2454 def get_html_page(url):
2455 """Generate an HTML page for url."""
Georg Brandld2f38572011-01-30 08:37:19 +00002456 complete_url = url
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002457 if url.endswith('.html'):
2458 url = url[:-5]
Georg Brandld2f38572011-01-30 08:37:19 +00002459 try:
2460 if url in ("", "index"):
2461 title, content = html_index()
2462 elif url == "topics":
2463 title, content = html_topics()
2464 elif url == "keywords":
2465 title, content = html_keywords()
2466 elif '=' in url:
2467 op, _, url = url.partition('=')
2468 if op == "search?key":
2469 title, content = html_search(url)
2470 elif op == "getfile?key":
2471 title, content = html_getfile(url)
2472 elif op == "topic?key":
2473 # try topics first, then objects.
2474 try:
2475 title, content = html_topicpage(url)
2476 except ValueError:
2477 title, content = html_getobj(url)
2478 elif op == "get?key":
2479 # try objects first, then topics.
2480 if url in ("", "index"):
2481 title, content = html_index()
2482 else:
2483 try:
2484 title, content = html_getobj(url)
2485 except ValueError:
2486 title, content = html_topicpage(url)
2487 else:
2488 raise ValueError('bad pydoc url')
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002489 else:
Georg Brandld2f38572011-01-30 08:37:19 +00002490 title, content = html_getobj(url)
2491 except Exception as exc:
2492 # Catch any errors and display them in an error page.
2493 title, content = html_error(complete_url, exc)
2494 return html.page(title, content)
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002495
2496 if url.startswith('/'):
2497 url = url[1:]
2498 if content_type == 'text/css':
2499 path_here = os.path.dirname(os.path.realpath(__file__))
Georg Brandld2f38572011-01-30 08:37:19 +00002500 css_path = os.path.join(path_here, url)
2501 with open(css_path) as fp:
2502 return ''.join(fp.readlines())
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002503 elif content_type == 'text/html':
2504 return get_html_page(url)
Georg Brandld2f38572011-01-30 08:37:19 +00002505 # Errors outside the url handler are caught by the server.
2506 raise TypeError('unknown content type %r for url %s' % (content_type, url))
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002507
2508
2509def browse(port=0, *, open_browser=True):
2510 """Start the enhanced pydoc Web server and open a Web browser.
2511
2512 Use port '0' to start the server on an arbitrary port.
2513 Set open_browser to False to suppress opening a browser.
2514 """
2515 import webbrowser
2516 serverthread = _start_server(_url_handler, port)
2517 if serverthread.error:
2518 print(serverthread.error)
2519 return
2520 if serverthread.serving:
2521 server_help_msg = 'Server commands: [b]rowser, [q]uit'
2522 if open_browser:
2523 webbrowser.open(serverthread.url)
2524 try:
2525 print('Server ready at', serverthread.url)
2526 print(server_help_msg)
2527 while serverthread.serving:
2528 cmd = input('server> ')
2529 cmd = cmd.lower()
2530 if cmd == 'q':
2531 break
2532 elif cmd == 'b':
2533 webbrowser.open(serverthread.url)
2534 else:
2535 print(server_help_msg)
2536 except (KeyboardInterrupt, EOFError):
2537 print()
2538 finally:
2539 if serverthread.serving:
2540 serverthread.stop()
2541 print('Server stopped')
2542
2543
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002544# -------------------------------------------------- command-line interface
2545
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00002546def ispath(x):
Neal Norwitz9d72bb42007-04-17 08:48:32 +00002547 return isinstance(x, str) and x.find(os.sep) >= 0
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00002548
Ka-Ping Yee1d384632001-03-01 00:24:32 +00002549def cli():
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002550 """Command-line interface (looks at sys.argv to decide what to do)."""
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002551 import getopt
Guido van Rossum756aa932007-04-07 03:04:01 +00002552 class BadUsage(Exception): pass
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002553
Nick Coghlan106274b2009-11-15 23:04:33 +00002554 # Scripts don't get the current directory in their path by default
2555 # unless they are run with the '-m' switch
2556 if '' not in sys.path:
2557 scriptdir = os.path.dirname(sys.argv[0])
2558 if scriptdir in sys.path:
2559 sys.path.remove(scriptdir)
2560 sys.path.insert(0, '.')
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002561
Ka-Ping Yee3bda8792001-03-23 13:17:50 +00002562 try:
Victor Stinner383c3fc2011-05-25 01:35:05 +02002563 opts, args = getopt.getopt(sys.argv[1:], 'bk:p:w')
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002564 writing = False
2565 start_server = False
2566 open_browser = False
2567 port = None
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002568 for opt, val in opts:
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002569 if opt == '-b':
2570 start_server = True
2571 open_browser = True
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002572 if opt == '-k':
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002573 apropos(val)
2574 return
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002575 if opt == '-p':
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002576 start_server = True
2577 port = val
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002578 if opt == '-w':
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002579 writing = True
2580
Benjamin Petersonb29614e2012-10-09 11:16:03 -04002581 if start_server:
2582 if port is None:
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002583 port = 0
2584 browse(port, open_browser=open_browser)
2585 return
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002586
2587 if not args: raise BadUsage
2588 for arg in args:
Ka-Ping Yee45daeb02002-08-11 15:11:33 +00002589 if ispath(arg) and not os.path.exists(arg):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00002590 print('file %r does not exist' % arg)
Ka-Ping Yee45daeb02002-08-11 15:11:33 +00002591 break
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002592 try:
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00002593 if ispath(arg) and os.path.isfile(arg):
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002594 arg = importfile(arg)
Ka-Ping Yee37f7b382001-03-23 00:12:53 +00002595 if writing:
2596 if ispath(arg) and os.path.isdir(arg):
2597 writedocs(arg)
2598 else:
2599 writedoc(arg)
2600 else:
Martin v. Löwisb8c084e2003-06-14 09:03:46 +00002601 help.help(arg)
Guido van Rossumb940e112007-01-10 16:19:56 +00002602 except ErrorDuringImport as value:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00002603 print(value)
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002604
2605 except (getopt.error, BadUsage):
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002606 cmd = os.path.splitext(os.path.basename(sys.argv[0]))[0]
Guido van Rossumbe19ed72007-02-09 05:37:30 +00002607 print("""pydoc - the Python documentation tool
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002608
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002609{cmd} <name> ...
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002610 Show text documentation on something. <name> may be the name of a
Martin v. Löwisb8c084e2003-06-14 09:03:46 +00002611 Python keyword, topic, function, module, or package, or a dotted
2612 reference to a class or function within a module or module in a
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002613 package. If <name> contains a '{sep}', it is used as the path to a
Martin v. Löwisb8c084e2003-06-14 09:03:46 +00002614 Python source file to document. If name is 'keywords', 'topics',
2615 or 'modules', a listing of these things is displayed.
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002616
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002617{cmd} -k <keyword>
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002618 Search for a keyword in the synopsis lines of all available modules.
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002619
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002620{cmd} -p <port>
2621 Start an HTTP server on the given port on the local machine. Port
2622 number 0 can be used to get an arbitrary unused port.
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002623
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002624{cmd} -b
2625 Start an HTTP server on an arbitrary unused port and open a Web browser
2626 to interactively browse documentation. The -p option can be used with
2627 the -b option to explicitly specify the server port.
Ka-Ping Yeedd175342001-02-27 14:43:46 +00002628
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002629{cmd} -w <name> ...
Ka-Ping Yee66efbc72001-03-01 13:55:20 +00002630 Write out the HTML documentation for a module to a file in the current
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002631 directory. If <name> contains a '{sep}', it is treated as a filename; if
Ka-Ping Yee5a804ed2001-04-10 11:46:02 +00002632 it names a directory, documentation is written for all the contents.
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002633""".format(cmd=cmd, sep=os.sep))
Ka-Ping Yee1d384632001-03-01 00:24:32 +00002634
Nick Coghlan7bb30b72010-12-03 09:29:11 +00002635if __name__ == '__main__':
2636 cli()