Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 2 | """Generate Python documentation in HTML or text for interactive use. |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 3 | |
R David Murray | 3d050dd | 2014-04-19 12:59:30 -0400 | [diff] [blame] | 4 | At the Python interactive prompt, calling help(thing) on a Python object |
| 5 | documents the object, and calling help() starts up an interactive |
| 6 | help session. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 7 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 8 | Or, at the shell command line outside of Python: |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 9 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 10 | Run "pydoc <name>" to show documentation on something. <name> may be |
| 11 | the name of a function, module, package, or a dotted reference to a |
| 12 | class or function within a module or module in a package. If the |
| 13 | argument contains a path segment delimiter (e.g. slash on Unix, |
| 14 | backslash on Windows) it is treated as the path to a Python source file. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 15 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 16 | Run "pydoc -k <keyword>" to search for a keyword in the synopsis lines |
| 17 | of all available modules. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 18 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 19 | Run "pydoc -p <port>" to start an HTTP server on the given port on the |
| 20 | local machine. Port number 0 can be used to get an arbitrary unused port. |
| 21 | |
| 22 | Run "pydoc -b" to start an HTTP server on an arbitrary unused port and |
| 23 | open a Web browser to interactively browse documentation. The -p option |
| 24 | can be used with the -b option to explicitly specify the server port. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 25 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 26 | Run "pydoc -w <name>" to write out the HTML documentation for a module |
| 27 | to a file named "<name>.html". |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 28 | |
| 29 | Module docs for core modules are assumed to be in |
| 30 | |
Martin Panter | 4f8aaf6 | 2016-06-12 04:24:06 +0000 | [diff] [blame] | 31 | https://docs.python.org/X.Y/library/ |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 32 | |
| 33 | This can be overridden by setting the PYTHONDOCS environment variable |
| 34 | to a different URL or to a local directory containing the Library |
| 35 | Reference Manual pages. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 36 | """ |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 37 | __all__ = ['help'] |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 38 | __author__ = "Ka-Ping Yee <ping@lfw.org>" |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 39 | __date__ = "26 February 2001" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 40 | |
Martin v. Löwis | 6fe8f19 | 2004-11-14 10:21:04 +0000 | [diff] [blame] | 41 | __credits__ = """Guido van Rossum, for an excellent programming language. |
Ka-Ping Yee | 5e2b173 | 2001-02-27 23:35:09 +0000 | [diff] [blame] | 42 | Tommy Burnette, the original creator of manpy. |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 43 | Paul Prescod, for all his work on onlinehelp. |
| 44 | Richard Chamberlain, for the first implementation of textdoc. |
Raymond Hettinger | ed2dbe3 | 2005-01-01 07:51:01 +0000 | [diff] [blame] | 45 | """ |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 46 | |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 47 | # Known bugs that can't be fixed here: |
Brett Cannon | f4ba4ec | 2013-06-15 14:25:04 -0400 | [diff] [blame] | 48 | # - synopsis() cannot be prevented from clobbering existing |
| 49 | # loaded modules. |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 50 | # - 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 Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 53 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 54 | import builtins |
Brett Cannon | d5e6f2e | 2013-06-11 17:09:36 -0400 | [diff] [blame] | 55 | import importlib._bootstrap |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 56 | import importlib._bootstrap_external |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 57 | import importlib.machinery |
Brett Cannon | f4ba4ec | 2013-06-15 14:25:04 -0400 | [diff] [blame] | 58 | import importlib.util |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 59 | import inspect |
Victor Stinner | e6c910e | 2011-06-30 15:55:43 +0200 | [diff] [blame] | 60 | import io |
| 61 | import os |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 62 | import pkgutil |
| 63 | import platform |
| 64 | import re |
Victor Stinner | e6c910e | 2011-06-30 15:55:43 +0200 | [diff] [blame] | 65 | import sys |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 66 | import time |
Victor Stinner | e6c910e | 2011-06-30 15:55:43 +0200 | [diff] [blame] | 67 | import tokenize |
Zachary Ware | eb43214 | 2014-07-10 11:18:00 -0500 | [diff] [blame] | 68 | import urllib.parse |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 69 | import warnings |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 70 | from collections import deque |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 71 | from reprlib import Repr |
Victor Stinner | 7fa767e | 2014-03-20 09:16:38 +0100 | [diff] [blame] | 72 | from traceback import format_exception_only |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 73 | |
| 74 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 75 | # --------------------------------------------------------- common routines |
| 76 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 77 | def pathdirs(): |
| 78 | """Convert sys.path into a list of absolute, existing, unique paths.""" |
| 79 | dirs = [] |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 80 | normdirs = [] |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 81 | for dir in sys.path: |
| 82 | dir = os.path.abspath(dir or '.') |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 83 | normdir = os.path.normcase(dir) |
| 84 | if normdir not in normdirs and os.path.isdir(dir): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 85 | dirs.append(dir) |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 86 | normdirs.append(normdir) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 87 | return dirs |
| 88 | |
| 89 | def getdoc(object): |
| 90 | """Get the doc string or comments for an object.""" |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 91 | result = inspect.getdoc(object) or inspect.getcomments(object) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 92 | return result and re.sub('^ *\n', '', result.rstrip()) or '' |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 93 | |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 94 | def splitdoc(doc): |
| 95 | """Split a doc string into a synopsis line (if any) and the rest.""" |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 96 | lines = doc.strip().split('\n') |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 97 | if len(lines) == 1: |
| 98 | return lines[0], '' |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 99 | elif len(lines) >= 2 and not lines[1].rstrip(): |
| 100 | return lines[0], '\n'.join(lines[2:]) |
| 101 | return '', '\n'.join(lines) |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 102 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 103 | def classname(object, modname): |
| 104 | """Get a class name and qualify it with a module name if necessary.""" |
| 105 | name = object.__name__ |
| 106 | if object.__module__ != modname: |
| 107 | name = object.__module__ + '.' + name |
| 108 | return name |
| 109 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 110 | def isdata(object): |
Georg Brandl | 9443242 | 2005-07-22 21:52:25 +0000 | [diff] [blame] | 111 | """Check if an object is of a type that probably means it's data.""" |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 112 | return not (inspect.ismodule(object) or inspect.isclass(object) or |
| 113 | inspect.isroutine(object) or inspect.isframe(object) or |
| 114 | inspect.istraceback(object) or inspect.iscode(object)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 115 | |
| 116 | def replace(text, *pairs): |
| 117 | """Do a series of global replacements on a string.""" |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 118 | while pairs: |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 119 | text = pairs[1].join(text.split(pairs[0])) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 120 | pairs = pairs[2:] |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 121 | return text |
| 122 | |
| 123 | def cram(text, maxlen): |
| 124 | """Omit part of a string if needed to make it fit in a maximum length.""" |
| 125 | if len(text) > maxlen: |
Raymond Hettinger | fca3bb6 | 2002-10-21 04:44:11 +0000 | [diff] [blame] | 126 | pre = max(0, (maxlen-3)//2) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 127 | post = max(0, maxlen-3-pre) |
| 128 | return text[:pre] + '...' + text[len(text)-post:] |
| 129 | return text |
| 130 | |
Brett Cannon | 84601f1 | 2004-06-19 01:22:48 +0000 | [diff] [blame] | 131 | _re_stripid = re.compile(r' at 0x[0-9a-f]{6,16}(>+)$', re.IGNORECASE) |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 132 | def stripid(text): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 133 | """Remove the hexadecimal id from a Python object representation.""" |
Brett Cannon | c6c1f47 | 2004-06-19 01:02:51 +0000 | [diff] [blame] | 134 | # The behaviour of %p is implementation-dependent in terms of case. |
Ezio Melotti | 412c95a | 2010-02-16 23:31:04 +0000 | [diff] [blame] | 135 | return _re_stripid.sub(r'\1', text) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 136 | |
Brett Cannon | c6c1f47 | 2004-06-19 01:02:51 +0000 | [diff] [blame] | 137 | def _is_some_method(obj): |
R David Murray | ac0cea5 | 2013-03-19 02:47:44 -0400 | [diff] [blame] | 138 | return (inspect.isfunction(obj) or |
| 139 | inspect.ismethod(obj) or |
| 140 | inspect.isbuiltin(obj) or |
| 141 | inspect.ismethoddescriptor(obj)) |
Tim Peters | 536d226 | 2001-09-20 05:13:38 +0000 | [diff] [blame] | 142 | |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 143 | def _is_bound_method(fn): |
| 144 | """ |
| 145 | Returns True if fn is a bound method, regardless of whether |
| 146 | fn was implemented in Python or in C. |
| 147 | """ |
| 148 | if inspect.ismethod(fn): |
| 149 | return True |
| 150 | if inspect.isbuiltin(fn): |
| 151 | self = getattr(fn, '__self__', None) |
| 152 | return not (inspect.ismodule(self) or (self is None)) |
| 153 | return False |
| 154 | |
| 155 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 156 | def allmethods(cl): |
| 157 | methods = {} |
Tim Peters | 536d226 | 2001-09-20 05:13:38 +0000 | [diff] [blame] | 158 | for key, value in inspect.getmembers(cl, _is_some_method): |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 159 | methods[key] = 1 |
| 160 | for base in cl.__bases__: |
| 161 | methods.update(allmethods(base)) # all your base are belong to us |
| 162 | for key in methods.keys(): |
| 163 | methods[key] = getattr(cl, key) |
| 164 | return methods |
| 165 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 166 | def _split_list(s, predicate): |
| 167 | """Split sequence s via predicate, and return pair ([true], [false]). |
| 168 | |
| 169 | The return value is a 2-tuple of lists, |
| 170 | ([x for x in s if predicate(x)], |
| 171 | [x for x in s if not predicate(x)]) |
| 172 | """ |
| 173 | |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 174 | yes = [] |
| 175 | no = [] |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 176 | for x in s: |
| 177 | if predicate(x): |
| 178 | yes.append(x) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 179 | else: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 180 | no.append(x) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 181 | return yes, no |
| 182 | |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 183 | def visiblename(name, all=None, obj=None): |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 184 | """Decide whether to show documentation on a variable.""" |
Brett Cannon | d340b43 | 2012-08-06 17:19:22 -0400 | [diff] [blame] | 185 | # Certain special names are redundant or internal. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 186 | # XXX Remove __initializing__? |
Brett Cannon | d340b43 | 2012-08-06 17:19:22 -0400 | [diff] [blame] | 187 | if name in {'__author__', '__builtins__', '__cached__', '__credits__', |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 188 | '__date__', '__doc__', '__file__', '__spec__', |
Brett Cannon | d340b43 | 2012-08-06 17:19:22 -0400 | [diff] [blame] | 189 | '__loader__', '__module__', '__name__', '__package__', |
| 190 | '__path__', '__qualname__', '__slots__', '__version__'}: |
Raymond Hettinger | 6827294 | 2011-03-18 02:22:15 -0700 | [diff] [blame] | 191 | return 0 |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 192 | # Private names are hidden, but special names are displayed. |
| 193 | if name.startswith('__') and name.endswith('__'): return 1 |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 194 | # Namedtuples have public fields and methods with a single leading underscore |
| 195 | if name.startswith('_') and hasattr(obj, '_fields'): |
| 196 | return True |
Skip Montanaro | a5616d2 | 2004-06-11 04:46:12 +0000 | [diff] [blame] | 197 | if all is not None: |
| 198 | # only document that which the programmer exported in __all__ |
| 199 | return name in all |
| 200 | else: |
| 201 | return not name.startswith('_') |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 202 | |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 203 | def classify_class_attrs(object): |
| 204 | """Wrap inspect.classify_class_attrs, with fixup for data descriptors.""" |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 205 | results = [] |
| 206 | for (name, kind, cls, value) in inspect.classify_class_attrs(object): |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 207 | if inspect.isdatadescriptor(value): |
| 208 | kind = 'data descriptor' |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 209 | results.append((name, kind, cls, value)) |
| 210 | return results |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 211 | |
Raymond Hettinger | 95801bb | 2015-08-18 22:25:16 -0700 | [diff] [blame] | 212 | def sort_attributes(attrs, object): |
| 213 | 'Sort the attrs list in-place by _fields and then alphabetically by name' |
| 214 | # This allows data descriptors to be ordered according |
| 215 | # to a _fields attribute if present. |
| 216 | fields = getattr(object, '_fields', []) |
| 217 | try: |
| 218 | field_order = {name : i-len(fields) for (i, name) in enumerate(fields)} |
| 219 | except TypeError: |
| 220 | field_order = {} |
| 221 | keyfunc = lambda attr: (field_order.get(attr[0], 0), attr[0]) |
| 222 | attrs.sort(key=keyfunc) |
| 223 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 224 | # ----------------------------------------------------- module manipulation |
| 225 | |
| 226 | def ispackage(path): |
| 227 | """Guess whether a path refers to a package directory.""" |
| 228 | if os.path.isdir(path): |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 229 | for ext in ('.py', '.pyc'): |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 230 | if os.path.isfile(os.path.join(path, '__init__' + ext)): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 231 | return True |
| 232 | return False |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 233 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 234 | def source_synopsis(file): |
| 235 | line = file.readline() |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 236 | while line[:1] == '#' or not line.strip(): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 237 | line = file.readline() |
| 238 | if not line: break |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 239 | line = line.strip() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 240 | if line[:4] == 'r"""': line = line[1:] |
| 241 | if line[:3] == '"""': |
| 242 | line = line[3:] |
| 243 | if line[-1:] == '\\': line = line[:-1] |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 244 | while not line.strip(): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 245 | line = file.readline() |
| 246 | if not line: break |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 247 | result = line.split('"""')[0].strip() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 248 | else: result = None |
| 249 | return result |
| 250 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 251 | def synopsis(filename, cache={}): |
| 252 | """Get the one-line summary out of a module file.""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 253 | mtime = os.stat(filename).st_mtime |
Charles-François Natali | 27c4e88 | 2011-07-27 19:40:02 +0200 | [diff] [blame] | 254 | lastupdate, result = cache.get(filename, (None, None)) |
| 255 | if lastupdate is None or lastupdate < mtime: |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 256 | # Look for binary suffixes first, falling back to source. |
| 257 | if filename.endswith(tuple(importlib.machinery.BYTECODE_SUFFIXES)): |
| 258 | loader_cls = importlib.machinery.SourcelessFileLoader |
| 259 | elif filename.endswith(tuple(importlib.machinery.EXTENSION_SUFFIXES)): |
| 260 | loader_cls = importlib.machinery.ExtensionFileLoader |
| 261 | else: |
| 262 | loader_cls = None |
| 263 | # Now handle the choice. |
| 264 | if loader_cls is None: |
| 265 | # Must be a source file. |
| 266 | try: |
| 267 | file = tokenize.open(filename) |
| 268 | except OSError: |
| 269 | # module can't be opened, so skip it |
| 270 | return None |
| 271 | # text modules can be directly examined |
| 272 | with file: |
| 273 | result = source_synopsis(file) |
| 274 | else: |
| 275 | # Must be a binary module, which has to be imported. |
| 276 | loader = loader_cls('__temp__', filename) |
Eric Snow | 3a62d14 | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 277 | # XXX We probably don't need to pass in the loader here. |
| 278 | spec = importlib.util.spec_from_file_location('__temp__', filename, |
| 279 | loader=loader) |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 280 | try: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 281 | module = importlib._bootstrap._load(spec) |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 282 | except: |
| 283 | return None |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 284 | del sys.modules['__temp__'] |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 285 | result = module.__doc__.splitlines()[0] if module.__doc__ else None |
Eric Snow | aed5b22 | 2014-01-04 20:38:11 -0700 | [diff] [blame] | 286 | # Cache the result. |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 287 | cache[filename] = (mtime, result) |
| 288 | return result |
| 289 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 290 | class ErrorDuringImport(Exception): |
| 291 | """Errors that occurred while trying to import something to document it.""" |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 292 | def __init__(self, filename, exc_info): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 293 | self.filename = filename |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 294 | self.exc, self.value, self.tb = exc_info |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 295 | |
| 296 | def __str__(self): |
Guido van Rossum | a01a8b6 | 2007-05-27 09:20:14 +0000 | [diff] [blame] | 297 | exc = self.exc.__name__ |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 298 | return 'problem in %s - %s: %s' % (self.filename, exc, self.value) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 299 | |
| 300 | def importfile(path): |
| 301 | """Import a Python source file or compiled file given its path.""" |
Brett Cannon | f4ba4ec | 2013-06-15 14:25:04 -0400 | [diff] [blame] | 302 | magic = importlib.util.MAGIC_NUMBER |
Victor Stinner | e975af6 | 2011-07-04 02:08:50 +0200 | [diff] [blame] | 303 | with open(path, 'rb') as file: |
Brett Cannon | d5e6f2e | 2013-06-11 17:09:36 -0400 | [diff] [blame] | 304 | is_bytecode = magic == file.read(len(magic)) |
| 305 | filename = os.path.basename(path) |
| 306 | name, ext = os.path.splitext(filename) |
| 307 | if is_bytecode: |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 308 | loader = importlib._bootstrap_external.SourcelessFileLoader(name, path) |
Brett Cannon | d5e6f2e | 2013-06-11 17:09:36 -0400 | [diff] [blame] | 309 | else: |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 310 | loader = importlib._bootstrap_external.SourceFileLoader(name, path) |
Eric Snow | 3a62d14 | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 311 | # XXX We probably don't need to pass in the loader here. |
| 312 | spec = importlib.util.spec_from_file_location(name, path, loader=loader) |
Brett Cannon | d5e6f2e | 2013-06-11 17:09:36 -0400 | [diff] [blame] | 313 | try: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 314 | return importlib._bootstrap._load(spec) |
Brett Cannon | d5e6f2e | 2013-06-11 17:09:36 -0400 | [diff] [blame] | 315 | except: |
| 316 | raise ErrorDuringImport(path, sys.exc_info()) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 317 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 318 | def safeimport(path, forceload=0, cache={}): |
| 319 | """Import a module; handle errors; return None if the module isn't found. |
| 320 | |
| 321 | If the module *is* found but an exception occurs, it's wrapped in an |
| 322 | ErrorDuringImport exception and reraised. Unlike __import__, if a |
| 323 | package path is specified, the module at the end of the path is returned, |
| 324 | not the package at the beginning. If the optional 'forceload' argument |
| 325 | is 1, we reload the module from disk (unless it's a dynamic extension).""" |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 326 | try: |
Ka-Ping Yee | 9a2dcf8 | 2005-11-05 05:04:41 +0000 | [diff] [blame] | 327 | # If forceload is 1 and the module has been previously loaded from |
| 328 | # disk, we always have to reload the module. Checking the file's |
| 329 | # mtime isn't good enough (e.g. the module could contain a class |
| 330 | # that inherits from another module that has changed). |
| 331 | if forceload and path in sys.modules: |
| 332 | if path not in sys.builtin_module_names: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 333 | # Remove the module from sys.modules and re-import to try |
| 334 | # and avoid problems with partially loaded modules. |
| 335 | # Also remove any submodules because they won't appear |
| 336 | # in the newly loaded module's namespace if they're already |
| 337 | # in sys.modules. |
Ka-Ping Yee | 9a2dcf8 | 2005-11-05 05:04:41 +0000 | [diff] [blame] | 338 | subs = [m for m in sys.modules if m.startswith(path + '.')] |
| 339 | for key in [path] + subs: |
| 340 | # Prevent garbage collection. |
| 341 | cache[key] = sys.modules[key] |
| 342 | del sys.modules[key] |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 343 | module = __import__(path) |
| 344 | except: |
| 345 | # Did the error occur before or after the module was found? |
| 346 | (exc, value, tb) = info = sys.exc_info() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 347 | if path in sys.modules: |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 348 | # An error occurred while executing the imported module. |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 349 | raise ErrorDuringImport(sys.modules[path].__file__, info) |
| 350 | elif exc is SyntaxError: |
| 351 | # A SyntaxError occurred before we could execute the module. |
| 352 | raise ErrorDuringImport(value.filename, info) |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 353 | elif issubclass(exc, ImportError) and value.name == path: |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 354 | # No such module in the path. |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 355 | return None |
| 356 | else: |
| 357 | # Some other error occurred during the importing process. |
| 358 | raise ErrorDuringImport(path, sys.exc_info()) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 359 | for part in path.split('.')[1:]: |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 360 | try: module = getattr(module, part) |
| 361 | except AttributeError: return None |
| 362 | return module |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 363 | |
| 364 | # ---------------------------------------------------- formatter base class |
| 365 | |
| 366 | class Doc: |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 367 | |
| 368 | PYTHONDOCS = os.environ.get("PYTHONDOCS", |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 369 | "https://docs.python.org/%d.%d/library" |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 370 | % sys.version_info[:2]) |
| 371 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 372 | def document(self, object, name=None, *args): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 373 | """Generate documentation for an object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 374 | args = (object, name) + args |
Brett Cannon | 28a4f0f | 2003-06-11 23:38:55 +0000 | [diff] [blame] | 375 | # 'try' clause is to attempt to handle the possibility that inspect |
| 376 | # identifies something in a way that pydoc itself has issues handling; |
| 377 | # think 'super' and how it is a descriptor (which raises the exception |
| 378 | # by lacking a __name__ attribute) and an instance. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 379 | if inspect.isgetsetdescriptor(object): return self.docdata(*args) |
| 380 | if inspect.ismemberdescriptor(object): return self.docdata(*args) |
Brett Cannon | 28a4f0f | 2003-06-11 23:38:55 +0000 | [diff] [blame] | 381 | try: |
| 382 | if inspect.ismodule(object): return self.docmodule(*args) |
| 383 | if inspect.isclass(object): return self.docclass(*args) |
| 384 | if inspect.isroutine(object): return self.docroutine(*args) |
| 385 | except AttributeError: |
| 386 | pass |
Johannes Gijsbers | 8de645a | 2004-11-07 19:16:05 +0000 | [diff] [blame] | 387 | if isinstance(object, property): return self.docproperty(*args) |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 388 | return self.docother(*args) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 389 | |
| 390 | def fail(self, object, name=None, *args): |
| 391 | """Raise an exception for unimplemented types.""" |
| 392 | message = "don't know how to document object%s of type %s" % ( |
| 393 | name and ' ' + repr(name), type(object).__name__) |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 394 | raise TypeError(message) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 395 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 396 | docmodule = docclass = docroutine = docother = docproperty = docdata = fail |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 397 | |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 398 | def getdocloc(self, object, |
| 399 | basedir=os.path.join(sys.base_exec_prefix, "lib", |
| 400 | "python%d.%d" % sys.version_info[:2])): |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 401 | """Return the location of module docs or None""" |
| 402 | |
| 403 | try: |
| 404 | file = inspect.getabsfile(object) |
| 405 | except TypeError: |
| 406 | file = '(built-in)' |
| 407 | |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 408 | docloc = os.environ.get("PYTHONDOCS", self.PYTHONDOCS) |
| 409 | |
Martin Panter | 4f8aaf6 | 2016-06-12 04:24:06 +0000 | [diff] [blame] | 410 | basedir = os.path.normcase(basedir) |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 411 | if (isinstance(object, type(os)) and |
| 412 | (object.__name__ in ('errno', 'exceptions', 'gc', 'imp', |
| 413 | 'marshal', 'posix', 'signal', 'sys', |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 414 | '_thread', 'zipimport') or |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 415 | (file.startswith(basedir) and |
Brian Curtin | 49c284c | 2010-03-31 03:19:28 +0000 | [diff] [blame] | 416 | not file.startswith(os.path.join(basedir, 'site-packages')))) and |
Brian Curtin | edef05b | 2010-04-01 04:05:25 +0000 | [diff] [blame] | 417 | object.__name__ not in ('xml.etree', 'test.pydoc_mod')): |
Martin Panter | 4f8aaf6 | 2016-06-12 04:24:06 +0000 | [diff] [blame] | 418 | if docloc.startswith(("http://", "https://")): |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 419 | docloc = "%s/%s" % (docloc.rstrip("/"), object.__name__.lower()) |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 420 | else: |
R David Murray | ead9bfc | 2016-06-03 19:28:35 -0400 | [diff] [blame] | 421 | docloc = os.path.join(docloc, object.__name__.lower() + ".html") |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 422 | else: |
| 423 | docloc = None |
| 424 | return docloc |
| 425 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 426 | # -------------------------------------------- HTML documentation generator |
| 427 | |
| 428 | class HTMLRepr(Repr): |
| 429 | """Class for safely making an HTML representation of a Python object.""" |
| 430 | def __init__(self): |
| 431 | Repr.__init__(self) |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 432 | self.maxlist = self.maxtuple = 20 |
| 433 | self.maxdict = 10 |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 434 | self.maxstring = self.maxother = 100 |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 435 | |
| 436 | def escape(self, text): |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 437 | return replace(text, '&', '&', '<', '<', '>', '>') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 438 | |
| 439 | def repr(self, object): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 440 | return Repr.repr(self, object) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 441 | |
| 442 | def repr1(self, x, level): |
Skip Montanaro | 0fe8fce | 2003-06-27 15:45:41 +0000 | [diff] [blame] | 443 | if hasattr(type(x), '__name__'): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 444 | methodname = 'repr_' + '_'.join(type(x).__name__.split()) |
Skip Montanaro | 0fe8fce | 2003-06-27 15:45:41 +0000 | [diff] [blame] | 445 | if hasattr(self, methodname): |
| 446 | return getattr(self, methodname)(x, level) |
| 447 | return self.escape(cram(stripid(repr(x)), self.maxother)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 448 | |
| 449 | def repr_string(self, x, level): |
Ka-Ping Yee | a2fe103 | 2001-03-02 01:19:14 +0000 | [diff] [blame] | 450 | test = cram(x, self.maxstring) |
| 451 | testrepr = repr(test) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 452 | if '\\' in test and '\\' not in replace(testrepr, r'\\', ''): |
Ka-Ping Yee | a2fe103 | 2001-03-02 01:19:14 +0000 | [diff] [blame] | 453 | # Backslashes are only literal in the string and are never |
| 454 | # needed to make any special characters, so show a raw string. |
| 455 | return 'r' + testrepr[0] + self.escape(test) + testrepr[0] |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 456 | return re.sub(r'((\\[\\abfnrtv\'"]|\\[0-9]..|\\x..|\\u....)+)', |
Raymond Hettinger | 95f285c | 2009-02-24 23:41:47 +0000 | [diff] [blame] | 457 | r'<font color="#c040c0">\1</font>', |
Ka-Ping Yee | a2fe103 | 2001-03-02 01:19:14 +0000 | [diff] [blame] | 458 | self.escape(testrepr)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 459 | |
Skip Montanaro | df70878 | 2002-03-07 22:58:02 +0000 | [diff] [blame] | 460 | repr_str = repr_string |
| 461 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 462 | def repr_instance(self, x, level): |
| 463 | try: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 464 | return self.escape(cram(stripid(repr(x)), self.maxstring)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 465 | except: |
| 466 | return self.escape('<%s instance>' % x.__class__.__name__) |
| 467 | |
| 468 | repr_unicode = repr_string |
| 469 | |
| 470 | class HTMLDoc(Doc): |
| 471 | """Formatter class for HTML documentation.""" |
| 472 | |
| 473 | # ------------------------------------------- HTML formatting utilities |
| 474 | |
| 475 | _repr_instance = HTMLRepr() |
| 476 | repr = _repr_instance.repr |
| 477 | escape = _repr_instance.escape |
| 478 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 479 | def page(self, title, contents): |
| 480 | """Format an HTML page.""" |
Georg Brandl | 388faac | 2009-04-10 08:31:48 +0000 | [diff] [blame] | 481 | return '''\ |
Georg Brandl | e606694 | 2009-04-10 08:28:28 +0000 | [diff] [blame] | 482 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 483 | <html><head><title>Python: %s</title> |
Georg Brandl | 388faac | 2009-04-10 08:31:48 +0000 | [diff] [blame] | 484 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
Raymond Hettinger | 95f285c | 2009-02-24 23:41:47 +0000 | [diff] [blame] | 485 | </head><body bgcolor="#f0f0f8"> |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 486 | %s |
| 487 | </body></html>''' % (title, contents) |
| 488 | |
| 489 | def heading(self, title, fgcol, bgcol, extras=''): |
| 490 | """Format a page heading.""" |
| 491 | return ''' |
Tim Peters | 59ed448 | 2001-10-31 04:20:26 +0000 | [diff] [blame] | 492 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 493 | <tr bgcolor="%s"> |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 494 | <td valign=bottom> <br> |
| 495 | <font color="%s" face="helvetica, arial"> <br>%s</font></td |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 496 | ><td align=right valign=bottom |
Ka-Ping Yee | 987ec90 | 2001-03-23 13:35:45 +0000 | [diff] [blame] | 497 | ><font color="%s" face="helvetica, arial">%s</font></td></tr></table> |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 498 | ''' % (bgcol, fgcol, title, fgcol, extras or ' ') |
| 499 | |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 500 | def section(self, title, fgcol, bgcol, contents, width=6, |
| 501 | prelude='', marginalia=None, gap=' '): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 502 | """Format a section with a heading.""" |
| 503 | if marginalia is None: |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 504 | marginalia = '<tt>' + ' ' * width + '</tt>' |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 505 | result = '''<p> |
Tim Peters | 59ed448 | 2001-10-31 04:20:26 +0000 | [diff] [blame] | 506 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 507 | <tr bgcolor="%s"> |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 508 | <td colspan=3 valign=bottom> <br> |
| 509 | <font color="%s" face="helvetica, arial">%s</font></td></tr> |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 510 | ''' % (bgcol, fgcol, title) |
| 511 | if prelude: |
| 512 | result = result + ''' |
Ka-Ping Yee | 987ec90 | 2001-03-23 13:35:45 +0000 | [diff] [blame] | 513 | <tr bgcolor="%s"><td rowspan=2>%s</td> |
| 514 | <td colspan=2>%s</td></tr> |
| 515 | <tr><td>%s</td>''' % (bgcol, marginalia, prelude, gap) |
| 516 | else: |
| 517 | result = result + ''' |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 518 | <tr><td bgcolor="%s">%s</td><td>%s</td>''' % (bgcol, marginalia, gap) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 519 | |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 520 | return result + '\n<td width="100%%">%s</td></tr></table>' % contents |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 521 | |
| 522 | def bigsection(self, title, *args): |
| 523 | """Format a section with a big heading.""" |
Raymond Hettinger | 95f285c | 2009-02-24 23:41:47 +0000 | [diff] [blame] | 524 | title = '<big><strong>%s</strong></big>' % title |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 525 | return self.section(title, *args) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 526 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 527 | def preformat(self, text): |
| 528 | """Format literal preformatted text.""" |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 529 | text = self.escape(text.expandtabs()) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 530 | return replace(text, '\n\n', '\n \n', '\n\n', '\n \n', |
| 531 | ' ', ' ', '\n', '<br>\n') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 532 | |
| 533 | def multicolumn(self, list, format, cols=4): |
| 534 | """Format a list of items into a multi-column list.""" |
| 535 | result = '' |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 536 | rows = (len(list)+cols-1)//cols |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 537 | for col in range(cols): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 538 | result = result + '<td width="%d%%" valign=top>' % (100//cols) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 539 | for i in range(rows*col, rows*col+rows): |
| 540 | if i < len(list): |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 541 | result = result + format(list[i]) + '<br>\n' |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 542 | result = result + '</td>' |
Tim Peters | 59ed448 | 2001-10-31 04:20:26 +0000 | [diff] [blame] | 543 | return '<table width="100%%" summary="list"><tr>%s</tr></table>' % result |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 544 | |
Raymond Hettinger | 95f285c | 2009-02-24 23:41:47 +0000 | [diff] [blame] | 545 | def grey(self, text): return '<font color="#909090">%s</font>' % text |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 546 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 547 | def namelink(self, name, *dicts): |
| 548 | """Make a link for an identifier, given name-to-URL mappings.""" |
| 549 | for dict in dicts: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 550 | if name in dict: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 551 | return '<a href="%s">%s</a>' % (dict[name], name) |
| 552 | return name |
| 553 | |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 554 | def classlink(self, object, modname): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 555 | """Make a link for a class.""" |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 556 | name, module = object.__name__, sys.modules.get(object.__module__) |
| 557 | if hasattr(module, name) and getattr(module, name) is object: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 558 | return '<a href="%s.html#%s">%s</a>' % ( |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 559 | module.__name__, name, classname(object, modname)) |
| 560 | return classname(object, modname) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 561 | |
| 562 | def modulelink(self, object): |
| 563 | """Make a link for a module.""" |
| 564 | return '<a href="%s.html">%s</a>' % (object.__name__, object.__name__) |
| 565 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 566 | def modpkglink(self, modpkginfo): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 567 | """Make a link for a module or package to display in an index.""" |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 568 | name, path, ispackage, shadowed = modpkginfo |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 569 | if shadowed: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 570 | return self.grey(name) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 571 | if path: |
| 572 | url = '%s.%s.html' % (path, name) |
| 573 | else: |
| 574 | url = '%s.html' % name |
| 575 | if ispackage: |
Raymond Hettinger | 95f285c | 2009-02-24 23:41:47 +0000 | [diff] [blame] | 576 | text = '<strong>%s</strong> (package)' % name |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 577 | else: |
| 578 | text = name |
| 579 | return '<a href="%s">%s</a>' % (url, text) |
| 580 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 581 | def filelink(self, url, path): |
| 582 | """Make a link to source file.""" |
| 583 | return '<a href="file:%s">%s</a>' % (url, path) |
| 584 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 585 | def markup(self, text, escape=None, funcs={}, classes={}, methods={}): |
| 586 | """Mark up some plain text, given a context of symbols to look for. |
| 587 | Each context dictionary maps object names to anchor names.""" |
| 588 | escape = escape or self.escape |
| 589 | results = [] |
| 590 | here = 0 |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 591 | pattern = re.compile(r'\b((http|ftp)://\S+[\w/]|' |
| 592 | r'RFC[- ]?(\d+)|' |
Ka-Ping Yee | f78a81b | 2001-03-27 08:13:42 +0000 | [diff] [blame] | 593 | r'PEP[- ]?(\d+)|' |
Neil Schemenauer | d69711c | 2002-03-24 23:02:07 +0000 | [diff] [blame] | 594 | r'(self\.)?(\w+))') |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 595 | while True: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 596 | match = pattern.search(text, here) |
| 597 | if not match: break |
| 598 | start, end = match.span() |
| 599 | results.append(escape(text[here:start])) |
| 600 | |
Ka-Ping Yee | f78a81b | 2001-03-27 08:13:42 +0000 | [diff] [blame] | 601 | all, scheme, rfc, pep, selfdot, name = match.groups() |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 602 | if scheme: |
Neil Schemenauer | cddc1a0 | 2002-03-24 23:11:21 +0000 | [diff] [blame] | 603 | url = escape(all).replace('"', '"') |
| 604 | results.append('<a href="%s">%s</a>' % (url, url)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 605 | elif rfc: |
Ka-Ping Yee | f78a81b | 2001-03-27 08:13:42 +0000 | [diff] [blame] | 606 | url = 'http://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc) |
| 607 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 608 | elif pep: |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 609 | url = 'http://www.python.org/dev/peps/pep-%04d/' % int(pep) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 610 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
Benjamin Peterson | ed1160b | 2014-06-07 16:44:00 -0700 | [diff] [blame] | 611 | elif selfdot: |
| 612 | # Create a link for methods like 'self.method(...)' |
| 613 | # and use <strong> for attributes like 'self.attr' |
| 614 | if text[end:end+1] == '(': |
| 615 | results.append('self.' + self.namelink(name, methods)) |
| 616 | else: |
| 617 | results.append('self.<strong>%s</strong>' % name) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 618 | elif text[end:end+1] == '(': |
| 619 | results.append(self.namelink(name, methods, funcs, classes)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 620 | else: |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 621 | results.append(self.namelink(name, classes)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 622 | here = end |
| 623 | results.append(escape(text[here:])) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 624 | return ''.join(results) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 625 | |
| 626 | # ---------------------------------------------- type-specific routines |
| 627 | |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 628 | def formattree(self, tree, modname, parent=None): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 629 | """Produce HTML for a class tree as given by inspect.getclasstree().""" |
| 630 | result = '' |
| 631 | for entry in tree: |
| 632 | if type(entry) is type(()): |
| 633 | c, bases = entry |
Raymond Hettinger | 95f285c | 2009-02-24 23:41:47 +0000 | [diff] [blame] | 634 | result = result + '<dt><font face="helvetica, arial">' |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 635 | result = result + self.classlink(c, modname) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 636 | if bases and bases != (parent,): |
| 637 | parents = [] |
| 638 | for base in bases: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 639 | parents.append(self.classlink(base, modname)) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 640 | result = result + '(' + ', '.join(parents) + ')' |
Raymond Hettinger | 95f285c | 2009-02-24 23:41:47 +0000 | [diff] [blame] | 641 | result = result + '\n</font></dt>' |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 642 | elif type(entry) is type([]): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 643 | result = result + '<dd>\n%s</dd>\n' % self.formattree( |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 644 | entry, modname, c) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 645 | return '<dl>\n%s</dl>\n' % result |
| 646 | |
Tim Peters | 8dd7ade | 2001-10-18 19:56:17 +0000 | [diff] [blame] | 647 | def docmodule(self, object, name=None, mod=None, *ignored): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 648 | """Produce HTML documentation for a module object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 649 | name = object.__name__ # ignore the passed-in name |
Skip Montanaro | a5616d2 | 2004-06-11 04:46:12 +0000 | [diff] [blame] | 650 | try: |
| 651 | all = object.__all__ |
| 652 | except AttributeError: |
| 653 | all = None |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 654 | parts = name.split('.') |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 655 | links = [] |
| 656 | for i in range(len(parts)-1): |
| 657 | links.append( |
Raymond Hettinger | 95f285c | 2009-02-24 23:41:47 +0000 | [diff] [blame] | 658 | '<a href="%s.html"><font color="#ffffff">%s</font></a>' % |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 659 | ('.'.join(parts[:i+1]), parts[i])) |
| 660 | linkedname = '.'.join(links + parts[-1:]) |
Raymond Hettinger | 95f285c | 2009-02-24 23:41:47 +0000 | [diff] [blame] | 661 | head = '<big><big><strong>%s</strong></big></big>' % linkedname |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 662 | try: |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 663 | path = inspect.getabsfile(object) |
Zachary Ware | eb43214 | 2014-07-10 11:18:00 -0500 | [diff] [blame] | 664 | url = urllib.parse.quote(path) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 665 | filelink = self.filelink(url, path) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 666 | except TypeError: |
| 667 | filelink = '(built-in)' |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 668 | info = [] |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 669 | if hasattr(object, '__version__'): |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 670 | version = str(object.__version__) |
Ka-Ping Yee | 40c4991 | 2001-02-27 22:46:01 +0000 | [diff] [blame] | 671 | if version[:11] == '$' + 'Revision: ' and version[-1:] == '$': |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 672 | version = version[11:-1].strip() |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 673 | info.append('version %s' % self.escape(version)) |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 674 | if hasattr(object, '__date__'): |
| 675 | info.append(self.escape(str(object.__date__))) |
| 676 | if info: |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 677 | head = head + ' (%s)' % ', '.join(info) |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 678 | docloc = self.getdocloc(object) |
| 679 | if docloc is not None: |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 680 | docloc = '<br><a href="%(docloc)s">Module Reference</a>' % locals() |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 681 | else: |
| 682 | docloc = '' |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 683 | result = self.heading( |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 684 | head, '#ffffff', '#7799ee', |
| 685 | '<a href=".">index</a><br>' + filelink + docloc) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 686 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 687 | modules = inspect.getmembers(object, inspect.ismodule) |
| 688 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 689 | classes, cdict = [], {} |
| 690 | for key, value in inspect.getmembers(object, inspect.isclass): |
Johannes Gijsbers | 4c11f60 | 2004-08-30 14:13:04 +0000 | [diff] [blame] | 691 | # if __all__ exists, believe it. Otherwise use old heuristic. |
| 692 | if (all is not None or |
| 693 | (inspect.getmodule(value) or object) is object): |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 694 | if visiblename(key, all, object): |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 695 | classes.append((key, value)) |
| 696 | cdict[key] = cdict[value] = '#' + key |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 697 | for key, value in classes: |
| 698 | for base in value.__bases__: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 699 | key, modname = base.__name__, base.__module__ |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 700 | module = sys.modules.get(modname) |
| 701 | if modname != name and module and hasattr(module, key): |
| 702 | if getattr(module, key) is base: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 703 | if not key in cdict: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 704 | cdict[key] = cdict[base] = modname + '.html#' + key |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 705 | funcs, fdict = [], {} |
| 706 | for key, value in inspect.getmembers(object, inspect.isroutine): |
Johannes Gijsbers | 4c11f60 | 2004-08-30 14:13:04 +0000 | [diff] [blame] | 707 | # if __all__ exists, believe it. Otherwise use old heuristic. |
| 708 | if (all is not None or |
| 709 | inspect.isbuiltin(value) or inspect.getmodule(value) is object): |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 710 | if visiblename(key, all, object): |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 711 | funcs.append((key, value)) |
| 712 | fdict[key] = '#-' + key |
| 713 | if inspect.isfunction(value): fdict[value] = fdict[key] |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 714 | data = [] |
| 715 | for key, value in inspect.getmembers(object, isdata): |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 716 | if visiblename(key, all, object): |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 717 | data.append((key, value)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 718 | |
| 719 | doc = self.markup(getdoc(object), self.preformat, fdict, cdict) |
| 720 | doc = doc and '<tt>%s</tt>' % doc |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 721 | result = result + '<p>%s</p>\n' % doc |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 722 | |
| 723 | if hasattr(object, '__path__'): |
| 724 | modpkgs = [] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 725 | for importer, modname, ispkg in pkgutil.iter_modules(object.__path__): |
| 726 | modpkgs.append((modname, name, ispkg, 0)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 727 | modpkgs.sort() |
| 728 | contents = self.multicolumn(modpkgs, self.modpkglink) |
| 729 | result = result + self.bigsection( |
| 730 | 'Package Contents', '#ffffff', '#aa55cc', contents) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 731 | elif modules: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 732 | contents = self.multicolumn( |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 733 | modules, lambda t: self.modulelink(t[1])) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 734 | result = result + self.bigsection( |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 735 | 'Modules', '#ffffff', '#aa55cc', contents) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 736 | |
| 737 | if classes: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 738 | classlist = [value for (key, value) in classes] |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 739 | contents = [ |
| 740 | self.formattree(inspect.getclasstree(classlist, 1), name)] |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 741 | for key, value in classes: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 742 | contents.append(self.document(value, key, name, fdict, cdict)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 743 | result = result + self.bigsection( |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 744 | 'Classes', '#ffffff', '#ee77aa', ' '.join(contents)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 745 | if funcs: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 746 | contents = [] |
| 747 | for key, value in funcs: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 748 | contents.append(self.document(value, key, name, fdict, cdict)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 749 | result = result + self.bigsection( |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 750 | 'Functions', '#ffffff', '#eeaa77', ' '.join(contents)) |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 751 | if data: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 752 | contents = [] |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 753 | for key, value in data: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 754 | contents.append(self.document(value, key)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 755 | result = result + self.bigsection( |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 756 | 'Data', '#ffffff', '#55aa55', '<br>\n'.join(contents)) |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 757 | if hasattr(object, '__author__'): |
| 758 | contents = self.markup(str(object.__author__), self.preformat) |
| 759 | result = result + self.bigsection( |
| 760 | 'Author', '#ffffff', '#7799ee', contents) |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 761 | if hasattr(object, '__credits__'): |
| 762 | contents = self.markup(str(object.__credits__), self.preformat) |
| 763 | result = result + self.bigsection( |
| 764 | 'Credits', '#ffffff', '#7799ee', contents) |
| 765 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 766 | return result |
| 767 | |
Tim Peters | 8dd7ade | 2001-10-18 19:56:17 +0000 | [diff] [blame] | 768 | def docclass(self, object, name=None, mod=None, funcs={}, classes={}, |
| 769 | *ignored): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 770 | """Produce HTML documentation for a class object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 771 | realname = object.__name__ |
| 772 | name = name or realname |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 773 | bases = object.__bases__ |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 774 | |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 775 | contents = [] |
| 776 | push = contents.append |
| 777 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 778 | # Cute little class to pump out a horizontal rule between sections. |
| 779 | class HorizontalRule: |
| 780 | def __init__(self): |
| 781 | self.needone = 0 |
| 782 | def maybe(self): |
| 783 | if self.needone: |
| 784 | push('<hr>\n') |
| 785 | self.needone = 1 |
| 786 | hr = HorizontalRule() |
| 787 | |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 788 | # List the mro, if non-trivial. |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 789 | mro = deque(inspect.getmro(object)) |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 790 | if len(mro) > 2: |
| 791 | hr.maybe() |
| 792 | push('<dl><dt>Method resolution order:</dt>\n') |
| 793 | for base in mro: |
| 794 | push('<dd>%s</dd>\n' % self.classlink(base, |
| 795 | object.__module__)) |
| 796 | push('</dl>\n') |
| 797 | |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 798 | def spill(msg, attrs, predicate): |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 799 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 800 | if ok: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 801 | hr.maybe() |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 802 | push(msg) |
| 803 | for name, kind, homecls, value in ok: |
Antoine Pitrou | 86a8a9a | 2011-12-21 09:57:40 +0100 | [diff] [blame] | 804 | try: |
| 805 | value = getattr(object, name) |
| 806 | except Exception: |
| 807 | # Some descriptors may meet a failure in their __get__. |
| 808 | # (bug #1785) |
| 809 | push(self._docdescriptor(name, value, mod)) |
| 810 | else: |
| 811 | push(self.document(value, name, mod, |
| 812 | funcs, classes, mdict, object)) |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 813 | push('\n') |
| 814 | return attrs |
| 815 | |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 816 | def spilldescriptors(msg, attrs, predicate): |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 817 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 818 | if ok: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 819 | hr.maybe() |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 820 | push(msg) |
| 821 | for name, kind, homecls, value in ok: |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 822 | push(self._docdescriptor(name, value, mod)) |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 823 | return attrs |
| 824 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 825 | def spilldata(msg, attrs, predicate): |
| 826 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 827 | if ok: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 828 | hr.maybe() |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 829 | push(msg) |
| 830 | for name, kind, homecls, value in ok: |
| 831 | base = self.docother(getattr(object, name), name, mod) |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame] | 832 | if callable(value) or inspect.isdatadescriptor(value): |
Guido van Rossum | 5e355b2 | 2002-05-21 20:56:15 +0000 | [diff] [blame] | 833 | doc = getattr(value, "__doc__", None) |
| 834 | else: |
| 835 | doc = None |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 836 | if doc is None: |
| 837 | push('<dl><dt>%s</dl>\n' % base) |
| 838 | else: |
| 839 | doc = self.markup(getdoc(value), self.preformat, |
| 840 | funcs, classes, mdict) |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 841 | doc = '<dd><tt>%s</tt>' % doc |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 842 | push('<dl><dt>%s%s</dl>\n' % (base, doc)) |
| 843 | push('\n') |
| 844 | return attrs |
| 845 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 846 | attrs = [(name, kind, cls, value) |
| 847 | for name, kind, cls, value in classify_class_attrs(object) |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 848 | if visiblename(name, obj=object)] |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 849 | |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 850 | mdict = {} |
| 851 | for key, kind, homecls, value in attrs: |
| 852 | mdict[key] = anchor = '#' + name + '-' + key |
Antoine Pitrou | 86a8a9a | 2011-12-21 09:57:40 +0100 | [diff] [blame] | 853 | try: |
| 854 | value = getattr(object, name) |
| 855 | except Exception: |
| 856 | # Some descriptors may meet a failure in their __get__. |
| 857 | # (bug #1785) |
| 858 | pass |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 859 | try: |
| 860 | # The value may not be hashable (e.g., a data attr with |
| 861 | # a dict or list value). |
| 862 | mdict[value] = anchor |
| 863 | except TypeError: |
| 864 | pass |
| 865 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 866 | while attrs: |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 867 | if mro: |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 868 | thisclass = mro.popleft() |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 869 | else: |
| 870 | thisclass = attrs[0][2] |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 871 | attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass) |
| 872 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 873 | if thisclass is builtins.object: |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 874 | attrs = inherited |
| 875 | continue |
| 876 | elif thisclass is object: |
| 877 | tag = 'defined here' |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 878 | else: |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 879 | tag = 'inherited from %s' % self.classlink(thisclass, |
| 880 | object.__module__) |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 881 | tag += ':<br>\n' |
| 882 | |
Raymond Hettinger | 95801bb | 2015-08-18 22:25:16 -0700 | [diff] [blame] | 883 | sort_attributes(attrs, object) |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 884 | |
| 885 | # Pump out the attrs, segregated by kind. |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 886 | attrs = spill('Methods %s' % tag, attrs, |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 887 | lambda t: t[1] == 'method') |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 888 | attrs = spill('Class methods %s' % tag, attrs, |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 889 | lambda t: t[1] == 'class method') |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 890 | attrs = spill('Static methods %s' % tag, attrs, |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 891 | lambda t: t[1] == 'static method') |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 892 | attrs = spilldescriptors('Data descriptors %s' % tag, attrs, |
| 893 | lambda t: t[1] == 'data descriptor') |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 894 | attrs = spilldata('Data and other attributes %s' % tag, attrs, |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 895 | lambda t: t[1] == 'data') |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 896 | assert attrs == [] |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 897 | attrs = inherited |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 898 | |
| 899 | contents = ''.join(contents) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 900 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 901 | if name == realname: |
| 902 | title = '<a name="%s">class <strong>%s</strong></a>' % ( |
| 903 | name, realname) |
| 904 | else: |
| 905 | title = '<strong>%s</strong> = <a name="%s">class %s</a>' % ( |
| 906 | name, name, realname) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 907 | if bases: |
| 908 | parents = [] |
| 909 | for base in bases: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 910 | parents.append(self.classlink(base, object.__module__)) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 911 | title = title + '(%s)' % ', '.join(parents) |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 912 | doc = self.markup(getdoc(object), self.preformat, funcs, classes, mdict) |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 913 | doc = doc and '<tt>%s<br> </tt>' % doc |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 914 | |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 915 | return self.section(title, '#000000', '#ffc8d8', contents, 3, doc) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 916 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 917 | def formatvalue(self, object): |
| 918 | """Format an argument default value as text.""" |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 919 | return self.grey('=' + self.repr(object)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 920 | |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 921 | def docroutine(self, object, name=None, mod=None, |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 922 | funcs={}, classes={}, methods={}, cl=None): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 923 | """Produce HTML documentation for a function or method object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 924 | realname = object.__name__ |
| 925 | name = name or realname |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 926 | anchor = (cl and cl.__name__ or '') + '-' + name |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 927 | note = '' |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 928 | skipdocs = 0 |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 929 | if _is_bound_method(object): |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 930 | imclass = object.__self__.__class__ |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 931 | if cl: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 932 | if imclass is not cl: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 933 | note = ' from ' + self.classlink(imclass, mod) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 934 | else: |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 935 | if object.__self__ is not None: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 936 | note = ' method of %s instance' % self.classlink( |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 937 | object.__self__.__class__, mod) |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 938 | else: |
| 939 | note = ' unbound %s method' % self.classlink(imclass,mod) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 940 | |
| 941 | if name == realname: |
| 942 | title = '<a name="%s"><strong>%s</strong></a>' % (anchor, realname) |
| 943 | else: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 944 | if (cl and realname in cl.__dict__ and |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 945 | cl.__dict__[realname] is object): |
Ka-Ping Yee | e280c06 | 2001-03-23 14:05:53 +0000 | [diff] [blame] | 946 | reallink = '<a href="#%s">%s</a>' % ( |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 947 | cl.__name__ + '-' + realname, realname) |
| 948 | skipdocs = 1 |
| 949 | else: |
| 950 | reallink = realname |
| 951 | title = '<a name="%s"><strong>%s</strong></a> = %s' % ( |
| 952 | anchor, name, reallink) |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 953 | argspec = None |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 954 | if inspect.isroutine(object): |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 955 | try: |
| 956 | signature = inspect.signature(object) |
| 957 | except (ValueError, TypeError): |
| 958 | signature = None |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 959 | if signature: |
| 960 | argspec = str(signature) |
| 961 | if realname == '<lambda>': |
| 962 | title = '<strong>%s</strong> <em>lambda</em> ' % name |
| 963 | # XXX lambda's won't usually have func_annotations['return'] |
| 964 | # since the syntax doesn't support but it is possible. |
| 965 | # So removing parentheses isn't truly safe. |
| 966 | argspec = argspec[1:-1] # remove parentheses |
| 967 | if not argspec: |
Tim Peters | 4bcfa31 | 2001-09-20 06:08:24 +0000 | [diff] [blame] | 968 | argspec = '(...)' |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 969 | |
Serhiy Storchaka | 66dd4aa | 2014-11-17 23:48:02 +0200 | [diff] [blame] | 970 | decl = title + self.escape(argspec) + (note and self.grey( |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 971 | '<font face="helvetica, arial">%s</font>' % note)) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 972 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 973 | if skipdocs: |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 974 | return '<dl><dt>%s</dt></dl>\n' % decl |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 975 | else: |
| 976 | doc = self.markup( |
| 977 | getdoc(object), self.preformat, funcs, classes, methods) |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 978 | doc = doc and '<dd><tt>%s</tt></dd>' % doc |
| 979 | return '<dl><dt>%s</dt>%s</dl>\n' % (decl, doc) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 980 | |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 981 | def _docdescriptor(self, name, value, mod): |
Johannes Gijsbers | 8de645a | 2004-11-07 19:16:05 +0000 | [diff] [blame] | 982 | results = [] |
| 983 | push = results.append |
| 984 | |
| 985 | if name: |
| 986 | push('<dl><dt><strong>%s</strong></dt>\n' % name) |
| 987 | if value.__doc__ is not None: |
Ka-Ping Yee | bba6acc | 2005-02-19 22:58:26 +0000 | [diff] [blame] | 988 | doc = self.markup(getdoc(value), self.preformat) |
Johannes Gijsbers | 8de645a | 2004-11-07 19:16:05 +0000 | [diff] [blame] | 989 | push('<dd><tt>%s</tt></dd>\n' % doc) |
Johannes Gijsbers | 8de645a | 2004-11-07 19:16:05 +0000 | [diff] [blame] | 990 | push('</dl>\n') |
| 991 | |
| 992 | return ''.join(results) |
| 993 | |
| 994 | def docproperty(self, object, name=None, mod=None, cl=None): |
| 995 | """Produce html documentation for a property.""" |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 996 | return self._docdescriptor(name, object, mod) |
Johannes Gijsbers | 8de645a | 2004-11-07 19:16:05 +0000 | [diff] [blame] | 997 | |
Tim Peters | 8dd7ade | 2001-10-18 19:56:17 +0000 | [diff] [blame] | 998 | def docother(self, object, name=None, mod=None, *ignored): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 999 | """Produce HTML documentation for a data object.""" |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1000 | lhs = name and '<strong>%s</strong> = ' % name or '' |
| 1001 | return lhs + self.repr(object) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1002 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1003 | def docdata(self, object, name=None, mod=None, cl=None): |
| 1004 | """Produce html documentation for a data descriptor.""" |
| 1005 | return self._docdescriptor(name, object, mod) |
| 1006 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1007 | def index(self, dir, shadowed=None): |
| 1008 | """Generate an HTML index for a directory of modules.""" |
| 1009 | modpkgs = [] |
| 1010 | if shadowed is None: shadowed = {} |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1011 | for importer, name, ispkg in pkgutil.iter_modules([dir]): |
Victor Stinner | 4d65224 | 2011-04-12 23:41:50 +0200 | [diff] [blame] | 1012 | if any((0xD800 <= ord(ch) <= 0xDFFF) for ch in name): |
| 1013 | # ignore a module if its name contains a surrogate character |
| 1014 | continue |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1015 | modpkgs.append((name, '', ispkg, name in shadowed)) |
| 1016 | shadowed[name] = 1 |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1017 | |
| 1018 | modpkgs.sort() |
| 1019 | contents = self.multicolumn(modpkgs, self.modpkglink) |
| 1020 | return self.bigsection(dir, '#ffffff', '#ee77aa', contents) |
| 1021 | |
| 1022 | # -------------------------------------------- text documentation generator |
| 1023 | |
| 1024 | class TextRepr(Repr): |
| 1025 | """Class for safely making a text representation of a Python object.""" |
| 1026 | def __init__(self): |
| 1027 | Repr.__init__(self) |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1028 | self.maxlist = self.maxtuple = 20 |
| 1029 | self.maxdict = 10 |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1030 | self.maxstring = self.maxother = 100 |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1031 | |
| 1032 | def repr1(self, x, level): |
Skip Montanaro | 0fe8fce | 2003-06-27 15:45:41 +0000 | [diff] [blame] | 1033 | if hasattr(type(x), '__name__'): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1034 | methodname = 'repr_' + '_'.join(type(x).__name__.split()) |
Skip Montanaro | 0fe8fce | 2003-06-27 15:45:41 +0000 | [diff] [blame] | 1035 | if hasattr(self, methodname): |
| 1036 | return getattr(self, methodname)(x, level) |
| 1037 | return cram(stripid(repr(x)), self.maxother) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1038 | |
Ka-Ping Yee | a2fe103 | 2001-03-02 01:19:14 +0000 | [diff] [blame] | 1039 | def repr_string(self, x, level): |
| 1040 | test = cram(x, self.maxstring) |
| 1041 | testrepr = repr(test) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1042 | if '\\' in test and '\\' not in replace(testrepr, r'\\', ''): |
Ka-Ping Yee | a2fe103 | 2001-03-02 01:19:14 +0000 | [diff] [blame] | 1043 | # Backslashes are only literal in the string and are never |
| 1044 | # needed to make any special characters, so show a raw string. |
| 1045 | return 'r' + testrepr[0] + test + testrepr[0] |
| 1046 | return testrepr |
| 1047 | |
Skip Montanaro | df70878 | 2002-03-07 22:58:02 +0000 | [diff] [blame] | 1048 | repr_str = repr_string |
| 1049 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1050 | def repr_instance(self, x, level): |
| 1051 | try: |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 1052 | return cram(stripid(repr(x)), self.maxstring) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1053 | except: |
| 1054 | return '<%s instance>' % x.__class__.__name__ |
| 1055 | |
| 1056 | class TextDoc(Doc): |
| 1057 | """Formatter class for text documentation.""" |
| 1058 | |
| 1059 | # ------------------------------------------- text formatting utilities |
| 1060 | |
| 1061 | _repr_instance = TextRepr() |
| 1062 | repr = _repr_instance.repr |
| 1063 | |
| 1064 | def bold(self, text): |
| 1065 | """Format a string in bold by overstriking.""" |
Georg Brandl | cbd2ab1 | 2010-12-04 10:39:14 +0000 | [diff] [blame] | 1066 | return ''.join(ch + '\b' + ch for ch in text) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1067 | |
| 1068 | def indent(self, text, prefix=' '): |
| 1069 | """Indent text by prepending a given prefix to each line.""" |
| 1070 | if not text: return '' |
Collin Winter | 72e110c | 2007-07-17 00:27:30 +0000 | [diff] [blame] | 1071 | lines = [prefix + line for line in text.split('\n')] |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1072 | if lines: lines[-1] = lines[-1].rstrip() |
| 1073 | return '\n'.join(lines) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1074 | |
| 1075 | def section(self, title, contents): |
| 1076 | """Format a section with a given heading.""" |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1077 | clean_contents = self.indent(contents).rstrip() |
| 1078 | return self.bold(title) + '\n' + clean_contents + '\n\n' |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1079 | |
| 1080 | # ---------------------------------------------- type-specific routines |
| 1081 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1082 | def formattree(self, tree, modname, parent=None, prefix=''): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1083 | """Render in text a class tree as returned by inspect.getclasstree().""" |
| 1084 | result = '' |
| 1085 | for entry in tree: |
| 1086 | if type(entry) is type(()): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1087 | c, bases = entry |
| 1088 | result = result + prefix + classname(c, modname) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1089 | if bases and bases != (parent,): |
Georg Brandl | cbd2ab1 | 2010-12-04 10:39:14 +0000 | [diff] [blame] | 1090 | parents = (classname(c, modname) for c in bases) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1091 | result = result + '(%s)' % ', '.join(parents) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1092 | result = result + '\n' |
| 1093 | elif type(entry) is type([]): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1094 | result = result + self.formattree( |
| 1095 | entry, modname, c, prefix + ' ') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1096 | return result |
| 1097 | |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1098 | def docmodule(self, object, name=None, mod=None): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1099 | """Produce text documentation for a given module object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1100 | name = object.__name__ # ignore the passed-in name |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1101 | synop, desc = splitdoc(getdoc(object)) |
| 1102 | result = self.section('NAME', name + (synop and ' - ' + synop)) |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 1103 | all = getattr(object, '__all__', None) |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 1104 | docloc = self.getdocloc(object) |
| 1105 | if docloc is not None: |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 1106 | result = result + self.section('MODULE REFERENCE', docloc + """ |
| 1107 | |
Éric Araujo | 647ef8c | 2011-09-11 00:43:20 +0200 | [diff] [blame] | 1108 | The following documentation is automatically generated from the Python |
| 1109 | source files. It may be incomplete, incorrect or include features that |
| 1110 | are considered implementation detail and may vary between Python |
| 1111 | implementations. When in doubt, consult the module reference at the |
| 1112 | location listed above. |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 1113 | """) |
Skip Montanaro | 4997a69 | 2003-09-10 16:47:51 +0000 | [diff] [blame] | 1114 | |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1115 | if desc: |
| 1116 | result = result + self.section('DESCRIPTION', desc) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1117 | |
| 1118 | classes = [] |
| 1119 | for key, value in inspect.getmembers(object, inspect.isclass): |
Johannes Gijsbers | 4c11f60 | 2004-08-30 14:13:04 +0000 | [diff] [blame] | 1120 | # if __all__ exists, believe it. Otherwise use old heuristic. |
| 1121 | if (all is not None |
| 1122 | or (inspect.getmodule(value) or object) is object): |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 1123 | if visiblename(key, all, object): |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 1124 | classes.append((key, value)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1125 | funcs = [] |
| 1126 | for key, value in inspect.getmembers(object, inspect.isroutine): |
Johannes Gijsbers | 4c11f60 | 2004-08-30 14:13:04 +0000 | [diff] [blame] | 1127 | # if __all__ exists, believe it. Otherwise use old heuristic. |
| 1128 | if (all is not None or |
| 1129 | inspect.isbuiltin(value) or inspect.getmodule(value) is object): |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 1130 | if visiblename(key, all, object): |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 1131 | funcs.append((key, value)) |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1132 | data = [] |
| 1133 | for key, value in inspect.getmembers(object, isdata): |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 1134 | if visiblename(key, all, object): |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1135 | data.append((key, value)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1136 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1137 | modpkgs = [] |
| 1138 | modpkgs_names = set() |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1139 | if hasattr(object, '__path__'): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1140 | for importer, modname, ispkg in pkgutil.iter_modules(object.__path__): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1141 | modpkgs_names.add(modname) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1142 | if ispkg: |
| 1143 | modpkgs.append(modname + ' (package)') |
| 1144 | else: |
| 1145 | modpkgs.append(modname) |
| 1146 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1147 | modpkgs.sort() |
| 1148 | result = result + self.section( |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1149 | 'PACKAGE CONTENTS', '\n'.join(modpkgs)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1150 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1151 | # Detect submodules as sometimes created by C extensions |
| 1152 | submodules = [] |
| 1153 | for key, value in inspect.getmembers(object, inspect.ismodule): |
| 1154 | if value.__name__.startswith(name + '.') and key not in modpkgs_names: |
| 1155 | submodules.append(key) |
| 1156 | if submodules: |
| 1157 | submodules.sort() |
| 1158 | result = result + self.section( |
Amaury Forgeot d'Arc | 768db92 | 2008-04-24 21:00:04 +0000 | [diff] [blame] | 1159 | 'SUBMODULES', '\n'.join(submodules)) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1160 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1161 | if classes: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1162 | classlist = [value for key, value in classes] |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1163 | contents = [self.formattree( |
| 1164 | inspect.getclasstree(classlist, 1), name)] |
| 1165 | for key, value in classes: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1166 | contents.append(self.document(value, key, name)) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1167 | result = result + self.section('CLASSES', '\n'.join(contents)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1168 | |
| 1169 | if funcs: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1170 | contents = [] |
| 1171 | for key, value in funcs: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1172 | contents.append(self.document(value, key, name)) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1173 | result = result + self.section('FUNCTIONS', '\n'.join(contents)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1174 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1175 | if data: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1176 | contents = [] |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1177 | for key, value in data: |
Georg Brandl | 8b813db | 2005-10-01 16:32:31 +0000 | [diff] [blame] | 1178 | contents.append(self.docother(value, key, name, maxlen=70)) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1179 | result = result + self.section('DATA', '\n'.join(contents)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1180 | |
| 1181 | if hasattr(object, '__version__'): |
| 1182 | version = str(object.__version__) |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 1183 | if version[:11] == '$' + 'Revision: ' and version[-1:] == '$': |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1184 | version = version[11:-1].strip() |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1185 | result = result + self.section('VERSION', version) |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 1186 | if hasattr(object, '__date__'): |
| 1187 | result = result + self.section('DATE', str(object.__date__)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1188 | if hasattr(object, '__author__'): |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 1189 | result = result + self.section('AUTHOR', str(object.__author__)) |
| 1190 | if hasattr(object, '__credits__'): |
| 1191 | result = result + self.section('CREDITS', str(object.__credits__)) |
Alexander Belopolsky | a47bbf5 | 2010-11-18 01:52:54 +0000 | [diff] [blame] | 1192 | try: |
| 1193 | file = inspect.getabsfile(object) |
| 1194 | except TypeError: |
| 1195 | file = '(built-in)' |
| 1196 | result = result + self.section('FILE', file) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1197 | return result |
| 1198 | |
Georg Brandl | 9bd45f99 | 2010-12-03 09:58:38 +0000 | [diff] [blame] | 1199 | def docclass(self, object, name=None, mod=None, *ignored): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1200 | """Produce text documentation for a given class object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1201 | realname = object.__name__ |
| 1202 | name = name or realname |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1203 | bases = object.__bases__ |
| 1204 | |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 1205 | def makename(c, m=object.__module__): |
| 1206 | return classname(c, m) |
| 1207 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1208 | if name == realname: |
| 1209 | title = 'class ' + self.bold(realname) |
| 1210 | else: |
| 1211 | title = self.bold(name) + ' = class ' + realname |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1212 | if bases: |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1213 | parents = map(makename, bases) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1214 | title = title + '(%s)' % ', '.join(parents) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1215 | |
| 1216 | doc = getdoc(object) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1217 | contents = doc and [doc + '\n'] or [] |
| 1218 | push = contents.append |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1219 | |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 1220 | # List the mro, if non-trivial. |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1221 | mro = deque(inspect.getmro(object)) |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 1222 | if len(mro) > 2: |
| 1223 | push("Method resolution order:") |
| 1224 | for base in mro: |
| 1225 | push(' ' + makename(base)) |
| 1226 | push('') |
| 1227 | |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1228 | # Cute little class to pump out a horizontal rule between sections. |
| 1229 | class HorizontalRule: |
| 1230 | def __init__(self): |
| 1231 | self.needone = 0 |
| 1232 | def maybe(self): |
| 1233 | if self.needone: |
| 1234 | push('-' * 70) |
| 1235 | self.needone = 1 |
| 1236 | hr = HorizontalRule() |
| 1237 | |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1238 | def spill(msg, attrs, predicate): |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1239 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1240 | if ok: |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1241 | hr.maybe() |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1242 | push(msg) |
| 1243 | for name, kind, homecls, value in ok: |
Antoine Pitrou | 86a8a9a | 2011-12-21 09:57:40 +0100 | [diff] [blame] | 1244 | try: |
| 1245 | value = getattr(object, name) |
| 1246 | except Exception: |
| 1247 | # Some descriptors may meet a failure in their __get__. |
| 1248 | # (bug #1785) |
| 1249 | push(self._docdescriptor(name, value, mod)) |
| 1250 | else: |
| 1251 | push(self.document(value, |
| 1252 | name, mod, object)) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1253 | return attrs |
| 1254 | |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 1255 | def spilldescriptors(msg, attrs, predicate): |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1256 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1257 | if ok: |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1258 | hr.maybe() |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1259 | push(msg) |
| 1260 | for name, kind, homecls, value in ok: |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 1261 | push(self._docdescriptor(name, value, mod)) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1262 | return attrs |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 1263 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1264 | def spilldata(msg, attrs, predicate): |
| 1265 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1266 | if ok: |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1267 | hr.maybe() |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1268 | push(msg) |
| 1269 | for name, kind, homecls, value in ok: |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame] | 1270 | if callable(value) or inspect.isdatadescriptor(value): |
Ka-Ping Yee | bba6acc | 2005-02-19 22:58:26 +0000 | [diff] [blame] | 1271 | doc = getdoc(value) |
Guido van Rossum | 5e355b2 | 2002-05-21 20:56:15 +0000 | [diff] [blame] | 1272 | else: |
| 1273 | doc = None |
Serhiy Storchaka | 056eb02 | 2014-02-19 23:05:12 +0200 | [diff] [blame] | 1274 | try: |
| 1275 | obj = getattr(object, name) |
| 1276 | except AttributeError: |
| 1277 | obj = homecls.__dict__[name] |
| 1278 | push(self.docother(obj, name, mod, maxlen=70, doc=doc) + |
| 1279 | '\n') |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1280 | return attrs |
| 1281 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1282 | attrs = [(name, kind, cls, value) |
| 1283 | for name, kind, cls, value in classify_class_attrs(object) |
Raymond Hettinger | 1103d05 | 2011-03-25 14:15:24 -0700 | [diff] [blame] | 1284 | if visiblename(name, obj=object)] |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1285 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1286 | while attrs: |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 1287 | if mro: |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1288 | thisclass = mro.popleft() |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 1289 | else: |
| 1290 | thisclass = attrs[0][2] |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1291 | attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass) |
| 1292 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 1293 | if thisclass is builtins.object: |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 1294 | attrs = inherited |
| 1295 | continue |
| 1296 | elif thisclass is object: |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1297 | tag = "defined here" |
| 1298 | else: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1299 | tag = "inherited from %s" % classname(thisclass, |
| 1300 | object.__module__) |
Raymond Hettinger | 95801bb | 2015-08-18 22:25:16 -0700 | [diff] [blame] | 1301 | |
| 1302 | sort_attributes(attrs, object) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1303 | |
| 1304 | # Pump out the attrs, segregated by kind. |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1305 | attrs = spill("Methods %s:\n" % tag, attrs, |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1306 | lambda t: t[1] == 'method') |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1307 | attrs = spill("Class methods %s:\n" % tag, attrs, |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1308 | lambda t: t[1] == 'class method') |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1309 | attrs = spill("Static methods %s:\n" % tag, attrs, |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1310 | lambda t: t[1] == 'static method') |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 1311 | attrs = spilldescriptors("Data descriptors %s:\n" % tag, attrs, |
| 1312 | lambda t: t[1] == 'data descriptor') |
Ka-Ping Yee | d9e213e | 2003-03-28 16:35:51 +0000 | [diff] [blame] | 1313 | attrs = spilldata("Data and other attributes %s:\n" % tag, attrs, |
| 1314 | lambda t: t[1] == 'data') |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 1315 | |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1316 | assert attrs == [] |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 1317 | attrs = inherited |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1318 | |
| 1319 | contents = '\n'.join(contents) |
| 1320 | if not contents: |
| 1321 | return title + '\n' |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1322 | return title + '\n' + self.indent(contents.rstrip(), ' | ') + '\n' |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1323 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1324 | def formatvalue(self, object): |
| 1325 | """Format an argument default value as text.""" |
| 1326 | return '=' + self.repr(object) |
| 1327 | |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1328 | def docroutine(self, object, name=None, mod=None, cl=None): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1329 | """Produce text documentation for a function or method object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1330 | realname = object.__name__ |
| 1331 | name = name or realname |
| 1332 | note = '' |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1333 | skipdocs = 0 |
Larry Hastings | 24a882b | 2014-02-20 23:34:46 -0800 | [diff] [blame] | 1334 | if _is_bound_method(object): |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 1335 | imclass = object.__self__.__class__ |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1336 | if cl: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1337 | if imclass is not cl: |
| 1338 | note = ' from ' + classname(imclass, mod) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1339 | else: |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 1340 | if object.__self__ is not None: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 1341 | note = ' method of %s instance' % classname( |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 1342 | object.__self__.__class__, mod) |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 1343 | else: |
| 1344 | note = ' unbound %s method' % classname(imclass,mod) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1345 | |
| 1346 | if name == realname: |
| 1347 | title = self.bold(realname) |
| 1348 | else: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1349 | if (cl and realname in cl.__dict__ and |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1350 | cl.__dict__[realname] is object): |
| 1351 | skipdocs = 1 |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1352 | title = self.bold(name) + ' = ' + realname |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 1353 | argspec = None |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 1354 | |
| 1355 | if inspect.isroutine(object): |
| 1356 | try: |
| 1357 | signature = inspect.signature(object) |
| 1358 | except (ValueError, TypeError): |
| 1359 | signature = None |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 1360 | if signature: |
| 1361 | argspec = str(signature) |
| 1362 | if realname == '<lambda>': |
| 1363 | title = self.bold(name) + ' lambda ' |
| 1364 | # XXX lambda's won't usually have func_annotations['return'] |
| 1365 | # since the syntax doesn't support but it is possible. |
| 1366 | # So removing parentheses isn't truly safe. |
| 1367 | argspec = argspec[1:-1] # remove parentheses |
| 1368 | if not argspec: |
Tim Peters | 4bcfa31 | 2001-09-20 06:08:24 +0000 | [diff] [blame] | 1369 | argspec = '(...)' |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1370 | decl = title + argspec + note |
| 1371 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1372 | if skipdocs: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1373 | return decl + '\n' |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1374 | else: |
| 1375 | doc = getdoc(object) or '' |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1376 | return decl + '\n' + (doc and self.indent(doc).rstrip() + '\n') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1377 | |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 1378 | def _docdescriptor(self, name, value, mod): |
Johannes Gijsbers | 8de645a | 2004-11-07 19:16:05 +0000 | [diff] [blame] | 1379 | results = [] |
| 1380 | push = results.append |
| 1381 | |
| 1382 | if name: |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 1383 | push(self.bold(name)) |
| 1384 | push('\n') |
Johannes Gijsbers | 8de645a | 2004-11-07 19:16:05 +0000 | [diff] [blame] | 1385 | doc = getdoc(value) or '' |
| 1386 | if doc: |
| 1387 | push(self.indent(doc)) |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 1388 | push('\n') |
| 1389 | return ''.join(results) |
Johannes Gijsbers | 8de645a | 2004-11-07 19:16:05 +0000 | [diff] [blame] | 1390 | |
| 1391 | def docproperty(self, object, name=None, mod=None, cl=None): |
| 1392 | """Produce text documentation for a property.""" |
Johannes Gijsbers | 9ddb300 | 2005-01-08 20:16:43 +0000 | [diff] [blame] | 1393 | return self._docdescriptor(name, object, mod) |
Johannes Gijsbers | 8de645a | 2004-11-07 19:16:05 +0000 | [diff] [blame] | 1394 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1395 | def docdata(self, object, name=None, mod=None, cl=None): |
| 1396 | """Produce text documentation for a data descriptor.""" |
| 1397 | return self._docdescriptor(name, object, mod) |
| 1398 | |
Georg Brandl | 8b813db | 2005-10-01 16:32:31 +0000 | [diff] [blame] | 1399 | def docother(self, object, name=None, mod=None, parent=None, maxlen=None, doc=None): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1400 | """Produce text documentation for a data object.""" |
| 1401 | repr = self.repr(object) |
| 1402 | if maxlen: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1403 | line = (name and name + ' = ' or '') + repr |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1404 | chop = maxlen - len(line) |
| 1405 | if chop < 0: repr = repr[:chop] + '...' |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1406 | line = (name and self.bold(name) + ' = ' or '') + repr |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1407 | if doc is not None: |
| 1408 | line += '\n' + self.indent(str(doc)) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1409 | return line |
| 1410 | |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 1411 | class _PlainTextDoc(TextDoc): |
| 1412 | """Subclass of TextDoc which overrides string styling""" |
| 1413 | def bold(self, text): |
| 1414 | return text |
| 1415 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1416 | # --------------------------------------------------------- user interfaces |
| 1417 | |
| 1418 | def pager(text): |
| 1419 | """The first time this is called, determine what kind of pager to use.""" |
| 1420 | global pager |
| 1421 | pager = getpager() |
| 1422 | pager(text) |
| 1423 | |
| 1424 | def getpager(): |
| 1425 | """Decide what method to use for paging through text.""" |
Benjamin Peterson | 159824e | 2014-06-07 20:14:26 -0700 | [diff] [blame] | 1426 | if not hasattr(sys.stdin, "isatty"): |
| 1427 | return plainpager |
Guido van Rossum | a01a8b6 | 2007-05-27 09:20:14 +0000 | [diff] [blame] | 1428 | if not hasattr(sys.stdout, "isatty"): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1429 | return plainpager |
| 1430 | if not sys.stdin.isatty() or not sys.stdout.isatty(): |
| 1431 | return plainpager |
doko@ubuntu.com | 9657545 | 2016-06-14 08:39:31 +0200 | [diff] [blame] | 1432 | use_pager = os.environ.get('MANPAGER') or os.environ.get('PAGER') |
| 1433 | if use_pager: |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1434 | if sys.platform == 'win32': # pipes completely broken in Windows |
doko@ubuntu.com | c8fd192 | 2016-06-14 09:03:52 +0200 | [diff] [blame] | 1435 | return lambda text: tempfilepager(plain(text), use_pager) |
Raymond Hettinger | dbecd93 | 2005-02-06 06:57:08 +0000 | [diff] [blame] | 1436 | elif os.environ.get('TERM') in ('dumb', 'emacs'): |
doko@ubuntu.com | c8fd192 | 2016-06-14 09:03:52 +0200 | [diff] [blame] | 1437 | return lambda text: pipepager(plain(text), use_pager) |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1438 | else: |
doko@ubuntu.com | c8fd192 | 2016-06-14 09:03:52 +0200 | [diff] [blame] | 1439 | return lambda text: pipepager(text, use_pager) |
Ka-Ping Yee | a487e4e | 2005-11-05 04:49:18 +0000 | [diff] [blame] | 1440 | if os.environ.get('TERM') in ('dumb', 'emacs'): |
| 1441 | return plainpager |
Jesus Cea | 4791a24 | 2012-10-05 03:15:39 +0200 | [diff] [blame] | 1442 | if sys.platform == 'win32': |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1443 | return lambda text: tempfilepager(plain(text), 'more <') |
Skip Montanaro | d404bee | 2002-09-26 21:44:57 +0000 | [diff] [blame] | 1444 | if hasattr(os, 'system') and os.system('(less) 2>/dev/null') == 0: |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1445 | return lambda text: pipepager(text, 'less') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1446 | |
| 1447 | import tempfile |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1448 | (fd, filename) = tempfile.mkstemp() |
| 1449 | os.close(fd) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1450 | try: |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1451 | if hasattr(os, 'system') and os.system('more "%s"' % filename) == 0: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1452 | return lambda text: pipepager(text, 'more') |
| 1453 | else: |
| 1454 | return ttypager |
| 1455 | finally: |
| 1456 | os.unlink(filename) |
| 1457 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1458 | def plain(text): |
| 1459 | """Remove boldface formatting from text.""" |
| 1460 | return re.sub('.\b', '', text) |
| 1461 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1462 | def pipepager(text, cmd): |
| 1463 | """Page through text by feeding it to another program.""" |
Serhiy Storchaka | 5e3d7a4 | 2015-02-20 23:46:06 +0200 | [diff] [blame] | 1464 | import subprocess |
| 1465 | proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1466 | try: |
R David Murray | 1058cda | 2015-03-29 15:15:40 -0400 | [diff] [blame] | 1467 | with io.TextIOWrapper(proc.stdin, errors='backslashreplace') as pipe: |
R David Murray | e7f5e14 | 2015-03-30 10:14:47 -0400 | [diff] [blame] | 1468 | try: |
| 1469 | pipe.write(text) |
| 1470 | except KeyboardInterrupt: |
| 1471 | # We've hereby abandoned whatever text hasn't been written, |
| 1472 | # but the pager is still in control of the terminal. |
| 1473 | pass |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1474 | except OSError: |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1475 | pass # Ignore broken pipes caused by quitting the pager program. |
R David Murray | 1058cda | 2015-03-29 15:15:40 -0400 | [diff] [blame] | 1476 | while True: |
| 1477 | try: |
| 1478 | proc.wait() |
| 1479 | break |
| 1480 | except KeyboardInterrupt: |
| 1481 | # Ignore ctl-c like the pager itself does. Otherwise the pager is |
| 1482 | # left running and the terminal is in raw mode and unusable. |
| 1483 | pass |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1484 | |
| 1485 | def tempfilepager(text, cmd): |
| 1486 | """Page through text by invoking a program on a temporary file.""" |
| 1487 | import tempfile |
Tim Peters | 550e4e5 | 2003-02-07 01:53:46 +0000 | [diff] [blame] | 1488 | filename = tempfile.mktemp() |
Serhiy Storchaka | 5e3d7a4 | 2015-02-20 23:46:06 +0200 | [diff] [blame] | 1489 | with open(filename, 'w', errors='backslashreplace') as file: |
Giampaolo Rodola' | 2f50aaf | 2013-02-12 02:04:27 +0100 | [diff] [blame] | 1490 | file.write(text) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1491 | try: |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1492 | os.system(cmd + ' "' + filename + '"') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1493 | finally: |
| 1494 | os.unlink(filename) |
| 1495 | |
Serhiy Storchaka | 5e3d7a4 | 2015-02-20 23:46:06 +0200 | [diff] [blame] | 1496 | def _escape_stdout(text): |
| 1497 | # Escape non-encodable characters to avoid encoding errors later |
| 1498 | encoding = getattr(sys.stdout, 'encoding', None) or 'utf-8' |
| 1499 | return text.encode(encoding, 'backslashreplace').decode(encoding) |
| 1500 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1501 | def ttypager(text): |
| 1502 | """Page through text on a text terminal.""" |
Serhiy Storchaka | 5e3d7a4 | 2015-02-20 23:46:06 +0200 | [diff] [blame] | 1503 | lines = plain(_escape_stdout(text)).split('\n') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1504 | try: |
| 1505 | import tty |
| 1506 | fd = sys.stdin.fileno() |
| 1507 | old = tty.tcgetattr(fd) |
| 1508 | tty.setcbreak(fd) |
| 1509 | getchar = lambda: sys.stdin.read(1) |
Serhiy Storchaka | ab5e9b9 | 2014-11-28 00:09:29 +0200 | [diff] [blame] | 1510 | except (ImportError, AttributeError, io.UnsupportedOperation): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1511 | tty = None |
| 1512 | getchar = lambda: sys.stdin.readline()[:-1][:1] |
| 1513 | |
| 1514 | try: |
Serhiy Storchaka | ab5e9b9 | 2014-11-28 00:09:29 +0200 | [diff] [blame] | 1515 | try: |
| 1516 | h = int(os.environ.get('LINES', 0)) |
| 1517 | except ValueError: |
| 1518 | h = 0 |
| 1519 | if h <= 1: |
| 1520 | h = 25 |
| 1521 | r = inc = h - 1 |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1522 | sys.stdout.write('\n'.join(lines[:inc]) + '\n') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1523 | while lines[r:]: |
| 1524 | sys.stdout.write('-- more --') |
| 1525 | sys.stdout.flush() |
| 1526 | c = getchar() |
| 1527 | |
Raymond Hettinger | dbecd93 | 2005-02-06 06:57:08 +0000 | [diff] [blame] | 1528 | if c in ('q', 'Q'): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1529 | sys.stdout.write('\r \r') |
| 1530 | break |
Raymond Hettinger | dbecd93 | 2005-02-06 06:57:08 +0000 | [diff] [blame] | 1531 | elif c in ('\r', '\n'): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1532 | sys.stdout.write('\r \r' + lines[r] + '\n') |
| 1533 | r = r + 1 |
| 1534 | continue |
Raymond Hettinger | dbecd93 | 2005-02-06 06:57:08 +0000 | [diff] [blame] | 1535 | if c in ('b', 'B', '\x1b'): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1536 | r = r - inc - inc |
| 1537 | if r < 0: r = 0 |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1538 | sys.stdout.write('\n' + '\n'.join(lines[r:r+inc]) + '\n') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1539 | r = r + inc |
| 1540 | |
| 1541 | finally: |
| 1542 | if tty: |
| 1543 | tty.tcsetattr(fd, tty.TCSAFLUSH, old) |
| 1544 | |
| 1545 | def plainpager(text): |
| 1546 | """Simply print unformatted text. This is the ultimate fallback.""" |
Serhiy Storchaka | 5e3d7a4 | 2015-02-20 23:46:06 +0200 | [diff] [blame] | 1547 | sys.stdout.write(plain(_escape_stdout(text))) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1548 | |
| 1549 | def describe(thing): |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1550 | """Produce a short description of the given thing.""" |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1551 | if inspect.ismodule(thing): |
| 1552 | if thing.__name__ in sys.builtin_module_names: |
| 1553 | return 'built-in module ' + thing.__name__ |
| 1554 | if hasattr(thing, '__path__'): |
| 1555 | return 'package ' + thing.__name__ |
| 1556 | else: |
| 1557 | return 'module ' + thing.__name__ |
| 1558 | if inspect.isbuiltin(thing): |
| 1559 | return 'built-in function ' + thing.__name__ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1560 | if inspect.isgetsetdescriptor(thing): |
| 1561 | return 'getset descriptor %s.%s.%s' % ( |
| 1562 | thing.__objclass__.__module__, thing.__objclass__.__name__, |
| 1563 | thing.__name__) |
| 1564 | if inspect.ismemberdescriptor(thing): |
| 1565 | return 'member descriptor %s.%s.%s' % ( |
| 1566 | thing.__objclass__.__module__, thing.__objclass__.__name__, |
| 1567 | thing.__name__) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1568 | if inspect.isclass(thing): |
| 1569 | return 'class ' + thing.__name__ |
| 1570 | if inspect.isfunction(thing): |
| 1571 | return 'function ' + thing.__name__ |
| 1572 | if inspect.ismethod(thing): |
| 1573 | return 'method ' + thing.__name__ |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1574 | return type(thing).__name__ |
| 1575 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1576 | def locate(path, forceload=0): |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1577 | """Locate an object by name or dotted path, importing as necessary.""" |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1578 | parts = [part for part in path.split('.') if part] |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1579 | module, n = None, 0 |
| 1580 | while n < len(parts): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1581 | nextmodule = safeimport('.'.join(parts[:n+1]), forceload) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1582 | if nextmodule: module, n = nextmodule, n + 1 |
| 1583 | else: break |
| 1584 | if module: |
| 1585 | object = module |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1586 | else: |
Éric Araujo | e64e51b | 2011-07-29 17:03:55 +0200 | [diff] [blame] | 1587 | object = builtins |
| 1588 | for part in parts[n:]: |
| 1589 | try: |
| 1590 | object = getattr(object, part) |
| 1591 | except AttributeError: |
| 1592 | return None |
| 1593 | return object |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1594 | |
| 1595 | # --------------------------------------- interactive interpreter interface |
| 1596 | |
| 1597 | text = TextDoc() |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 1598 | plaintext = _PlainTextDoc() |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1599 | html = HTMLDoc() |
| 1600 | |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 1601 | def resolve(thing, forceload=0): |
| 1602 | """Given an object or a path to an object, get the object and its name.""" |
| 1603 | if isinstance(thing, str): |
| 1604 | object = locate(thing, forceload) |
Serhiy Storchaka | b6076fb | 2015-04-21 21:09:48 +0300 | [diff] [blame] | 1605 | if object is None: |
Serhiy Storchaka | 1c20551 | 2015-03-01 00:42:54 +0200 | [diff] [blame] | 1606 | raise ImportError('''\ |
| 1607 | No Python documentation found for %r. |
| 1608 | Use help() to get the interactive help utility. |
| 1609 | Use help(str) for help on the str class.''' % thing) |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 1610 | return object, thing |
| 1611 | else: |
R David Murray | c43125a | 2012-04-23 13:23:57 -0400 | [diff] [blame] | 1612 | name = getattr(thing, '__name__', None) |
| 1613 | return thing, name if isinstance(name, str) else None |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 1614 | |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 1615 | def render_doc(thing, title='Python Library Documentation: %s', forceload=0, |
| 1616 | renderer=None): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1617 | """Render text documentation, given an object or a path to an object.""" |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 1618 | if renderer is None: |
| 1619 | renderer = text |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1620 | object, name = resolve(thing, forceload) |
| 1621 | desc = describe(object) |
| 1622 | module = inspect.getmodule(object) |
| 1623 | if name and '.' in name: |
| 1624 | desc += ' in ' + name[:name.rfind('.')] |
| 1625 | elif module and module is not object: |
| 1626 | desc += ' in module ' + module.__name__ |
Amaury Forgeot d'Arc | 768db92 | 2008-04-24 21:00:04 +0000 | [diff] [blame] | 1627 | |
| 1628 | if not (inspect.ismodule(object) or |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1629 | inspect.isclass(object) or |
| 1630 | inspect.isroutine(object) or |
| 1631 | inspect.isgetsetdescriptor(object) or |
| 1632 | inspect.ismemberdescriptor(object) or |
| 1633 | isinstance(object, property)): |
| 1634 | # If the passed object is a piece of data or an instance, |
| 1635 | # document its available methods instead of its value. |
| 1636 | object = type(object) |
| 1637 | desc += ' object' |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 1638 | return title % desc + '\n\n' + renderer.document(object, name) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1639 | |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 1640 | def doc(thing, title='Python Library Documentation: %s', forceload=0, |
| 1641 | output=None): |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1642 | """Display text documentation, given an object or a path to an object.""" |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 1643 | try: |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 1644 | if output is None: |
| 1645 | pager(render_doc(thing, title, forceload)) |
| 1646 | else: |
| 1647 | output.write(render_doc(thing, title, forceload, plaintext)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1648 | except (ImportError, ErrorDuringImport) as value: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1649 | print(value) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1650 | |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 1651 | def writedoc(thing, forceload=0): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1652 | """Write HTML documentation to a file in the current directory.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1653 | try: |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 1654 | object, name = resolve(thing, forceload) |
| 1655 | page = html.page(describe(object), html.document(object, name)) |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 1656 | with open(name + '.html', 'w', encoding='utf-8') as file: |
| 1657 | file.write(page) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1658 | print('wrote', name + '.html') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1659 | except (ImportError, ErrorDuringImport) as value: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1660 | print(value) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1661 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1662 | def writedocs(dir, pkgpath='', done=None): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1663 | """Write out HTML documentation for all modules in a directory tree.""" |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1664 | if done is None: done = {} |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1665 | for importer, modname, ispkg in pkgutil.walk_packages([dir], pkgpath): |
| 1666 | writedoc(modname) |
| 1667 | return |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1668 | |
| 1669 | class Helper: |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1670 | |
| 1671 | # These dictionaries map a topic name to either an alias, or a tuple |
| 1672 | # (label, seealso-items). The "label" is the label of the corresponding |
| 1673 | # section in the .rst file under Doc/ and an index into the dictionary |
Georg Brandl | 5617db8 | 2009-04-27 16:28:57 +0000 | [diff] [blame] | 1674 | # in pydoc_data/topics.py. |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1675 | # |
| 1676 | # CAUTION: if you change one of these dictionaries, be sure to adapt the |
Georg Brandl | 61bd1dc | 2014-09-30 22:56:38 +0200 | [diff] [blame] | 1677 | # list of needed labels in Doc/tools/pyspecific.py and |
Georg Brandl | 5617db8 | 2009-04-27 16:28:57 +0000 | [diff] [blame] | 1678 | # regenerate the pydoc_data/topics.py file by running |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1679 | # make pydoc-topics |
| 1680 | # in Doc/ and copying the output file into the Lib/ directory. |
| 1681 | |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1682 | keywords = { |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 1683 | 'False': '', |
| 1684 | 'None': '', |
| 1685 | 'True': '', |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1686 | 'and': 'BOOLEAN', |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1687 | 'as': 'with', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1688 | 'assert': ('assert', ''), |
| 1689 | 'break': ('break', 'while for'), |
| 1690 | 'class': ('class', 'CLASSES SPECIALMETHODS'), |
| 1691 | 'continue': ('continue', 'while for'), |
| 1692 | 'def': ('function', ''), |
| 1693 | 'del': ('del', 'BASICMETHODS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1694 | 'elif': 'if', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1695 | 'else': ('else', 'while for'), |
Georg Brandl | 0d85539 | 2008-08-30 19:53:05 +0000 | [diff] [blame] | 1696 | 'except': 'try', |
| 1697 | 'finally': 'try', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1698 | 'for': ('for', 'break continue while'), |
Georg Brandl | 0d85539 | 2008-08-30 19:53:05 +0000 | [diff] [blame] | 1699 | 'from': 'import', |
Georg Brandl | 74abf6f | 2010-11-20 19:54:36 +0000 | [diff] [blame] | 1700 | 'global': ('global', 'nonlocal NAMESPACES'), |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1701 | 'if': ('if', 'TRUTHVALUE'), |
| 1702 | 'import': ('import', 'MODULES'), |
Georg Brandl | 395ed24 | 2009-09-04 08:07:32 +0000 | [diff] [blame] | 1703 | 'in': ('in', 'SEQUENCEMETHODS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1704 | 'is': 'COMPARISON', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1705 | 'lambda': ('lambda', 'FUNCTIONS'), |
Georg Brandl | 74abf6f | 2010-11-20 19:54:36 +0000 | [diff] [blame] | 1706 | 'nonlocal': ('nonlocal', 'global NAMESPACES'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1707 | 'not': 'BOOLEAN', |
| 1708 | 'or': 'BOOLEAN', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1709 | 'pass': ('pass', ''), |
| 1710 | 'raise': ('raise', 'EXCEPTIONS'), |
| 1711 | 'return': ('return', 'FUNCTIONS'), |
| 1712 | 'try': ('try', 'EXCEPTIONS'), |
| 1713 | 'while': ('while', 'break continue if TRUTHVALUE'), |
| 1714 | 'with': ('with', 'CONTEXTMANAGERS EXCEPTIONS yield'), |
| 1715 | 'yield': ('yield', ''), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1716 | } |
Georg Brandl | db7b6b9 | 2009-01-01 15:53:14 +0000 | [diff] [blame] | 1717 | # Either add symbols to this dictionary or to the symbols dictionary |
| 1718 | # directly: Whichever is easier. They are merged later. |
| 1719 | _symbols_inverse = { |
| 1720 | 'STRINGS' : ("'", "'''", "r'", "b'", '"""', '"', 'r"', 'b"'), |
| 1721 | 'OPERATORS' : ('+', '-', '*', '**', '/', '//', '%', '<<', '>>', '&', |
| 1722 | '|', '^', '~', '<', '>', '<=', '>=', '==', '!=', '<>'), |
| 1723 | 'COMPARISON' : ('<', '>', '<=', '>=', '==', '!=', '<>'), |
| 1724 | 'UNARY' : ('-', '~'), |
| 1725 | 'AUGMENTEDASSIGNMENT' : ('+=', '-=', '*=', '/=', '%=', '&=', '|=', |
| 1726 | '^=', '<<=', '>>=', '**=', '//='), |
| 1727 | 'BITWISE' : ('<<', '>>', '&', '|', '^', '~'), |
| 1728 | 'COMPLEX' : ('j', 'J') |
| 1729 | } |
| 1730 | symbols = { |
| 1731 | '%': 'OPERATORS FORMATTING', |
| 1732 | '**': 'POWER', |
| 1733 | ',': 'TUPLES LISTS FUNCTIONS', |
| 1734 | '.': 'ATTRIBUTES FLOAT MODULES OBJECTS', |
| 1735 | '...': 'ELLIPSIS', |
| 1736 | ':': 'SLICINGS DICTIONARYLITERALS', |
| 1737 | '@': 'def class', |
| 1738 | '\\': 'STRINGS', |
| 1739 | '_': 'PRIVATENAMES', |
| 1740 | '__': 'PRIVATENAMES SPECIALMETHODS', |
| 1741 | '`': 'BACKQUOTES', |
| 1742 | '(': 'TUPLES FUNCTIONS CALLS', |
| 1743 | ')': 'TUPLES FUNCTIONS CALLS', |
| 1744 | '[': 'LISTS SUBSCRIPTS SLICINGS', |
| 1745 | ']': 'LISTS SUBSCRIPTS SLICINGS' |
| 1746 | } |
| 1747 | for topic, symbols_ in _symbols_inverse.items(): |
| 1748 | for symbol in symbols_: |
| 1749 | topics = symbols.get(symbol, topic) |
| 1750 | if topic not in topics: |
| 1751 | topics = topics + ' ' + topic |
| 1752 | symbols[symbol] = topics |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1753 | |
| 1754 | topics = { |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1755 | 'TYPES': ('types', 'STRINGS UNICODE NUMBERS SEQUENCES MAPPINGS ' |
| 1756 | 'FUNCTIONS CLASSES MODULES FILES inspect'), |
| 1757 | 'STRINGS': ('strings', 'str UNICODE SEQUENCES STRINGMETHODS ' |
| 1758 | 'FORMATTING TYPES'), |
| 1759 | 'STRINGMETHODS': ('string-methods', 'STRINGS FORMATTING'), |
| 1760 | 'FORMATTING': ('formatstrings', 'OPERATORS'), |
| 1761 | 'UNICODE': ('strings', 'encodings unicode SEQUENCES STRINGMETHODS ' |
| 1762 | 'FORMATTING TYPES'), |
| 1763 | 'NUMBERS': ('numbers', 'INTEGER FLOAT COMPLEX TYPES'), |
| 1764 | 'INTEGER': ('integers', 'int range'), |
| 1765 | 'FLOAT': ('floating', 'float math'), |
| 1766 | 'COMPLEX': ('imaginary', 'complex cmath'), |
| 1767 | 'SEQUENCES': ('typesseq', 'STRINGMETHODS FORMATTING range LISTS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1768 | 'MAPPINGS': 'DICTIONARIES', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1769 | 'FUNCTIONS': ('typesfunctions', 'def TYPES'), |
| 1770 | 'METHODS': ('typesmethods', 'class def CLASSES TYPES'), |
| 1771 | 'CODEOBJECTS': ('bltin-code-objects', 'compile FUNCTIONS TYPES'), |
| 1772 | 'TYPEOBJECTS': ('bltin-type-objects', 'types TYPES'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1773 | 'FRAMEOBJECTS': 'TYPES', |
| 1774 | 'TRACEBACKS': 'TYPES', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1775 | 'NONE': ('bltin-null-object', ''), |
| 1776 | 'ELLIPSIS': ('bltin-ellipsis-object', 'SLICINGS'), |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1777 | 'SPECIALATTRIBUTES': ('specialattrs', ''), |
| 1778 | 'CLASSES': ('types', 'class SPECIALMETHODS PRIVATENAMES'), |
| 1779 | 'MODULES': ('typesmodules', 'import'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1780 | 'PACKAGES': 'import', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1781 | 'EXPRESSIONS': ('operator-summary', 'lambda or and not in is BOOLEAN ' |
| 1782 | 'COMPARISON BITWISE SHIFTING BINARY FORMATTING POWER ' |
| 1783 | 'UNARY ATTRIBUTES SUBSCRIPTS SLICINGS CALLS TUPLES ' |
| 1784 | 'LISTS DICTIONARIES'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1785 | 'OPERATORS': 'EXPRESSIONS', |
| 1786 | 'PRECEDENCE': 'EXPRESSIONS', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1787 | 'OBJECTS': ('objects', 'TYPES'), |
| 1788 | 'SPECIALMETHODS': ('specialnames', 'BASICMETHODS ATTRIBUTEMETHODS ' |
Georg Brandl | 395ed24 | 2009-09-04 08:07:32 +0000 | [diff] [blame] | 1789 | 'CALLABLEMETHODS SEQUENCEMETHODS MAPPINGMETHODS ' |
| 1790 | 'NUMBERMETHODS CLASSES'), |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 1791 | 'BASICMETHODS': ('customization', 'hash repr str SPECIALMETHODS'), |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1792 | 'ATTRIBUTEMETHODS': ('attribute-access', 'ATTRIBUTES SPECIALMETHODS'), |
| 1793 | 'CALLABLEMETHODS': ('callable-types', 'CALLS SPECIALMETHODS'), |
Georg Brandl | 395ed24 | 2009-09-04 08:07:32 +0000 | [diff] [blame] | 1794 | 'SEQUENCEMETHODS': ('sequence-types', 'SEQUENCES SEQUENCEMETHODS ' |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1795 | 'SPECIALMETHODS'), |
| 1796 | 'MAPPINGMETHODS': ('sequence-types', 'MAPPINGS SPECIALMETHODS'), |
| 1797 | 'NUMBERMETHODS': ('numeric-types', 'NUMBERS AUGMENTEDASSIGNMENT ' |
| 1798 | 'SPECIALMETHODS'), |
| 1799 | 'EXECUTION': ('execmodel', 'NAMESPACES DYNAMICFEATURES EXCEPTIONS'), |
Georg Brandl | 74abf6f | 2010-11-20 19:54:36 +0000 | [diff] [blame] | 1800 | 'NAMESPACES': ('naming', 'global nonlocal ASSIGNMENT DELETION DYNAMICFEATURES'), |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1801 | 'DYNAMICFEATURES': ('dynamic-features', ''), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1802 | 'SCOPING': 'NAMESPACES', |
| 1803 | 'FRAMES': 'NAMESPACES', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1804 | 'EXCEPTIONS': ('exceptions', 'try except finally raise'), |
| 1805 | 'CONVERSIONS': ('conversions', ''), |
| 1806 | 'IDENTIFIERS': ('identifiers', 'keywords SPECIALIDENTIFIERS'), |
| 1807 | 'SPECIALIDENTIFIERS': ('id-classes', ''), |
| 1808 | 'PRIVATENAMES': ('atom-identifiers', ''), |
| 1809 | 'LITERALS': ('atom-literals', 'STRINGS NUMBERS TUPLELITERALS ' |
| 1810 | 'LISTLITERALS DICTIONARYLITERALS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1811 | 'TUPLES': 'SEQUENCES', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1812 | 'TUPLELITERALS': ('exprlists', 'TUPLES LITERALS'), |
| 1813 | 'LISTS': ('typesseq-mutable', 'LISTLITERALS'), |
| 1814 | 'LISTLITERALS': ('lists', 'LISTS LITERALS'), |
| 1815 | 'DICTIONARIES': ('typesmapping', 'DICTIONARYLITERALS'), |
| 1816 | 'DICTIONARYLITERALS': ('dict', 'DICTIONARIES LITERALS'), |
| 1817 | 'ATTRIBUTES': ('attribute-references', 'getattr hasattr setattr ATTRIBUTEMETHODS'), |
Georg Brandl | 395ed24 | 2009-09-04 08:07:32 +0000 | [diff] [blame] | 1818 | 'SUBSCRIPTS': ('subscriptions', 'SEQUENCEMETHODS'), |
| 1819 | 'SLICINGS': ('slicings', 'SEQUENCEMETHODS'), |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1820 | 'CALLS': ('calls', 'EXPRESSIONS'), |
| 1821 | 'POWER': ('power', 'EXPRESSIONS'), |
| 1822 | 'UNARY': ('unary', 'EXPRESSIONS'), |
| 1823 | 'BINARY': ('binary', 'EXPRESSIONS'), |
| 1824 | 'SHIFTING': ('shifting', 'EXPRESSIONS'), |
| 1825 | 'BITWISE': ('bitwise', 'EXPRESSIONS'), |
| 1826 | 'COMPARISON': ('comparisons', 'EXPRESSIONS BASICMETHODS'), |
| 1827 | 'BOOLEAN': ('booleans', 'EXPRESSIONS TRUTHVALUE'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1828 | 'ASSERTION': 'assert', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1829 | 'ASSIGNMENT': ('assignment', 'AUGMENTEDASSIGNMENT'), |
| 1830 | 'AUGMENTEDASSIGNMENT': ('augassign', 'NUMBERMETHODS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1831 | 'DELETION': 'del', |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1832 | 'RETURNING': 'return', |
| 1833 | 'IMPORTING': 'import', |
| 1834 | 'CONDITIONAL': 'if', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1835 | 'LOOPING': ('compound', 'for while break continue'), |
| 1836 | 'TRUTHVALUE': ('truth', 'if while and or not BASICMETHODS'), |
| 1837 | 'DEBUGGING': ('debugger', 'pdb'), |
| 1838 | 'CONTEXTMANAGERS': ('context-managers', 'with'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Georg Brandl | 78aa396 | 2010-07-31 21:51:48 +0000 | [diff] [blame] | 1841 | def __init__(self, input=None, output=None): |
| 1842 | self._input = input |
| 1843 | self._output = output |
| 1844 | |
Georg Brandl | 76ae397 | 2010-08-01 06:32:55 +0000 | [diff] [blame] | 1845 | input = property(lambda self: self._input or sys.stdin) |
| 1846 | output = property(lambda self: self._output or sys.stdout) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1847 | |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1848 | def __repr__(self): |
Ka-Ping Yee | 9bc576b | 2001-04-13 13:57:31 +0000 | [diff] [blame] | 1849 | if inspect.stack()[1][3] == '?': |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1850 | self() |
| 1851 | return '' |
Serhiy Storchaka | 465e60e | 2014-07-25 23:36:00 +0300 | [diff] [blame] | 1852 | return '<%s.%s instance>' % (self.__class__.__module__, |
| 1853 | self.__class__.__qualname__) |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1854 | |
Alexander Belopolsky | 2e733c9 | 2010-07-04 17:00:20 +0000 | [diff] [blame] | 1855 | _GoInteractive = object() |
| 1856 | def __call__(self, request=_GoInteractive): |
| 1857 | if request is not self._GoInteractive: |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1858 | self.help(request) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1859 | else: |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1860 | self.intro() |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1861 | self.interact() |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1862 | self.output.write(''' |
Fred Drake | e61967f | 2001-05-10 18:41:02 +0000 | [diff] [blame] | 1863 | You are now leaving help and returning to the Python interpreter. |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1864 | If you want to ask for help on a particular object directly from the |
| 1865 | interpreter, you can type "help(object)". Executing "help('string')" |
| 1866 | has the same effect as typing a particular string at the help> prompt. |
| 1867 | ''') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1868 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1869 | def interact(self): |
| 1870 | self.output.write('\n') |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 1871 | while True: |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1872 | try: |
Johannes Gijsbers | e7691d3 | 2004-08-17 13:21:53 +0000 | [diff] [blame] | 1873 | request = self.getline('help> ') |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1874 | if not request: break |
Johannes Gijsbers | e7691d3 | 2004-08-17 13:21:53 +0000 | [diff] [blame] | 1875 | except (KeyboardInterrupt, EOFError): |
| 1876 | break |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1877 | request = replace(request, '"', '', "'", '').strip() |
| 1878 | if request.lower() in ('q', 'quit'): break |
Serhiy Storchaka | 1c20551 | 2015-03-01 00:42:54 +0200 | [diff] [blame] | 1879 | if request == 'help': |
| 1880 | self.intro() |
| 1881 | else: |
| 1882 | self.help(request) |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1883 | |
Johannes Gijsbers | e7691d3 | 2004-08-17 13:21:53 +0000 | [diff] [blame] | 1884 | def getline(self, prompt): |
Guido van Rossum | 7c086c0 | 2008-01-02 03:52:38 +0000 | [diff] [blame] | 1885 | """Read one line, using input() when appropriate.""" |
Johannes Gijsbers | e7691d3 | 2004-08-17 13:21:53 +0000 | [diff] [blame] | 1886 | if self.input is sys.stdin: |
Guido van Rossum | 7c086c0 | 2008-01-02 03:52:38 +0000 | [diff] [blame] | 1887 | return input(prompt) |
Johannes Gijsbers | e7691d3 | 2004-08-17 13:21:53 +0000 | [diff] [blame] | 1888 | else: |
| 1889 | self.output.write(prompt) |
| 1890 | self.output.flush() |
| 1891 | return self.input.readline() |
| 1892 | |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1893 | def help(self, request): |
| 1894 | if type(request) is type(''): |
R. David Murray | 1f1b9d3 | 2009-05-27 20:56:59 +0000 | [diff] [blame] | 1895 | request = request.strip() |
Serhiy Storchaka | 1c20551 | 2015-03-01 00:42:54 +0200 | [diff] [blame] | 1896 | if request == 'keywords': self.listkeywords() |
Georg Brandl | db7b6b9 | 2009-01-01 15:53:14 +0000 | [diff] [blame] | 1897 | elif request == 'symbols': self.listsymbols() |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1898 | elif request == 'topics': self.listtopics() |
| 1899 | elif request == 'modules': self.listmodules() |
| 1900 | elif request[:8] == 'modules ': |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1901 | self.listmodules(request.split()[1]) |
Georg Brandl | db7b6b9 | 2009-01-01 15:53:14 +0000 | [diff] [blame] | 1902 | elif request in self.symbols: self.showsymbol(request) |
Ezio Melotti | b185a04 | 2011-04-28 07:42:55 +0300 | [diff] [blame] | 1903 | elif request in ['True', 'False', 'None']: |
| 1904 | # special case these keywords since they are objects too |
| 1905 | doc(eval(request), 'Help on %s:') |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1906 | elif request in self.keywords: self.showtopic(request) |
| 1907 | elif request in self.topics: self.showtopic(request) |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 1908 | elif request: doc(request, 'Help on %s:', output=self._output) |
Serhiy Storchaka | 1c20551 | 2015-03-01 00:42:54 +0200 | [diff] [blame] | 1909 | else: doc(str, 'Help on %s:', output=self._output) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1910 | elif isinstance(request, Helper): self() |
Georg Brandl | d80d5f4 | 2010-12-03 07:47:22 +0000 | [diff] [blame] | 1911 | else: doc(request, 'Help on %s:', output=self._output) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1912 | self.output.write('\n') |
| 1913 | |
| 1914 | def intro(self): |
| 1915 | self.output.write(''' |
Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 1916 | Welcome to Python {0}'s help utility! |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1917 | |
| 1918 | If this is your first time using Python, you should definitely check out |
Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 1919 | the tutorial on the Internet at http://docs.python.org/{0}/tutorial/. |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1920 | |
| 1921 | Enter the name of any module, keyword, or topic to get help on writing |
| 1922 | Python programs and using Python modules. To quit this help utility and |
| 1923 | return to the interpreter, just type "quit". |
| 1924 | |
Terry Jan Reedy | 3420057 | 2013-02-11 02:23:13 -0500 | [diff] [blame] | 1925 | To get a list of available modules, keywords, symbols, or topics, type |
| 1926 | "modules", "keywords", "symbols", or "topics". Each module also comes |
| 1927 | with a one-line summary of what it does; to list the modules whose name |
| 1928 | or summary contain a given string such as "spam", type "modules spam". |
Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 1929 | '''.format('%d.%d' % sys.version_info[:2])) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1930 | |
| 1931 | def list(self, items, columns=4, width=80): |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 1932 | items = list(sorted(items)) |
| 1933 | colw = width // columns |
| 1934 | rows = (len(items) + columns - 1) // columns |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1935 | for row in range(rows): |
| 1936 | for col in range(columns): |
| 1937 | i = col * rows + row |
| 1938 | if i < len(items): |
| 1939 | self.output.write(items[i]) |
| 1940 | if col < columns - 1: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 1941 | self.output.write(' ' + ' ' * (colw - 1 - len(items[i]))) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1942 | self.output.write('\n') |
| 1943 | |
| 1944 | def listkeywords(self): |
| 1945 | self.output.write(''' |
| 1946 | Here is a list of the Python keywords. Enter any keyword to get more help. |
| 1947 | |
| 1948 | ''') |
| 1949 | self.list(self.keywords.keys()) |
| 1950 | |
Georg Brandl | db7b6b9 | 2009-01-01 15:53:14 +0000 | [diff] [blame] | 1951 | def listsymbols(self): |
| 1952 | self.output.write(''' |
| 1953 | Here is a list of the punctuation symbols which Python assigns special meaning |
| 1954 | to. Enter any symbol to get more help. |
| 1955 | |
| 1956 | ''') |
| 1957 | self.list(self.symbols.keys()) |
| 1958 | |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1959 | def listtopics(self): |
| 1960 | self.output.write(''' |
| 1961 | Here is a list of available topics. Enter any topic name to get more help. |
| 1962 | |
| 1963 | ''') |
| 1964 | self.list(self.topics.keys()) |
| 1965 | |
Georg Brandl | db7b6b9 | 2009-01-01 15:53:14 +0000 | [diff] [blame] | 1966 | def showtopic(self, topic, more_xrefs=''): |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1967 | try: |
Georg Brandl | 5617db8 | 2009-04-27 16:28:57 +0000 | [diff] [blame] | 1968 | import pydoc_data.topics |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 1969 | except ImportError: |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1970 | self.output.write(''' |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1971 | Sorry, topic and keyword documentation is not available because the |
Georg Brandl | 5617db8 | 2009-04-27 16:28:57 +0000 | [diff] [blame] | 1972 | module "pydoc_data.topics" could not be found. |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1973 | ''') |
| 1974 | return |
| 1975 | target = self.topics.get(topic, self.keywords.get(topic)) |
| 1976 | if not target: |
| 1977 | self.output.write('no documentation found for %s\n' % repr(topic)) |
| 1978 | return |
| 1979 | if type(target) is type(''): |
Georg Brandl | db7b6b9 | 2009-01-01 15:53:14 +0000 | [diff] [blame] | 1980 | return self.showtopic(target, more_xrefs) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1981 | |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1982 | label, xrefs = target |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1983 | try: |
Georg Brandl | 5617db8 | 2009-04-27 16:28:57 +0000 | [diff] [blame] | 1984 | doc = pydoc_data.topics.topics[label] |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1985 | except KeyError: |
| 1986 | self.output.write('no documentation found for %s\n' % repr(topic)) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1987 | return |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 1988 | pager(doc.strip() + '\n') |
Georg Brandl | db7b6b9 | 2009-01-01 15:53:14 +0000 | [diff] [blame] | 1989 | if more_xrefs: |
| 1990 | xrefs = (xrefs or '') + ' ' + more_xrefs |
Ka-Ping Yee | da79389 | 2001-04-13 11:02:51 +0000 | [diff] [blame] | 1991 | if xrefs: |
Brett Cannon | 1448ecf | 2013-10-04 11:38:59 -0400 | [diff] [blame] | 1992 | import textwrap |
| 1993 | text = 'Related help topics: ' + ', '.join(xrefs.split()) + '\n' |
| 1994 | wrapped_text = textwrap.wrap(text, 72) |
| 1995 | self.output.write('\n%s\n' % ''.join(wrapped_text)) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1996 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 1997 | def _gettopic(self, topic, more_xrefs=''): |
| 1998 | """Return unbuffered tuple of (topic, xrefs). |
| 1999 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2000 | If an error occurs here, the exception is caught and displayed by |
| 2001 | the url handler. |
| 2002 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2003 | This function duplicates the showtopic method but returns its |
| 2004 | result directly so it can be formatted for display in an html page. |
| 2005 | """ |
| 2006 | try: |
| 2007 | import pydoc_data.topics |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 2008 | except ImportError: |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2009 | return(''' |
| 2010 | Sorry, topic and keyword documentation is not available because the |
| 2011 | module "pydoc_data.topics" could not be found. |
| 2012 | ''' , '') |
| 2013 | target = self.topics.get(topic, self.keywords.get(topic)) |
| 2014 | if not target: |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2015 | raise ValueError('could not find topic') |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2016 | if isinstance(target, str): |
| 2017 | return self._gettopic(target, more_xrefs) |
| 2018 | label, xrefs = target |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2019 | doc = pydoc_data.topics.topics[label] |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2020 | if more_xrefs: |
| 2021 | xrefs = (xrefs or '') + ' ' + more_xrefs |
| 2022 | return doc, xrefs |
| 2023 | |
Georg Brandl | db7b6b9 | 2009-01-01 15:53:14 +0000 | [diff] [blame] | 2024 | def showsymbol(self, symbol): |
| 2025 | target = self.symbols[symbol] |
| 2026 | topic, _, xrefs = target.partition(' ') |
| 2027 | self.showtopic(topic, xrefs) |
| 2028 | |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 2029 | def listmodules(self, key=''): |
| 2030 | if key: |
| 2031 | self.output.write(''' |
Terry Jan Reedy | 3420057 | 2013-02-11 02:23:13 -0500 | [diff] [blame] | 2032 | Here is a list of modules whose name or summary contains '{}'. |
| 2033 | If there are any, enter a module name to get more help. |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 2034 | |
Terry Jan Reedy | 3420057 | 2013-02-11 02:23:13 -0500 | [diff] [blame] | 2035 | '''.format(key)) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 2036 | apropos(key) |
| 2037 | else: |
| 2038 | self.output.write(''' |
| 2039 | Please wait a moment while I gather a list of all available modules... |
| 2040 | |
| 2041 | ''') |
| 2042 | modules = {} |
| 2043 | def callback(path, modname, desc, modules=modules): |
| 2044 | if modname and modname[-9:] == '.__init__': |
| 2045 | modname = modname[:-9] + ' (package)' |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 2046 | if modname.find('.') < 0: |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 2047 | modules[modname] = 1 |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2048 | def onerror(modname): |
| 2049 | callback(None, modname, None) |
| 2050 | ModuleScanner().run(callback, onerror=onerror) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 2051 | self.list(modules.keys()) |
| 2052 | self.output.write(''' |
| 2053 | Enter any module name to get more help. Or, type "modules spam" to search |
Terry Jan Reedy | 3420057 | 2013-02-11 02:23:13 -0500 | [diff] [blame] | 2054 | for modules whose name or summary contain the string "spam". |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 2055 | ''') |
| 2056 | |
Georg Brandl | 78aa396 | 2010-07-31 21:51:48 +0000 | [diff] [blame] | 2057 | help = Helper() |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2058 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2059 | class ModuleScanner: |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2060 | """An interruptible scanner that searches module synopses.""" |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2061 | |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2062 | def run(self, callback, key=None, completer=None, onerror=None): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 2063 | if key: key = key.lower() |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 2064 | self.quit = False |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2065 | seen = {} |
| 2066 | |
| 2067 | for modname in sys.builtin_module_names: |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 2068 | if modname != '__main__': |
| 2069 | seen[modname] = 1 |
Ka-Ping Yee | 6624696 | 2001-04-12 11:59:50 +0000 | [diff] [blame] | 2070 | if key is None: |
| 2071 | callback(None, modname, '') |
| 2072 | else: |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 2073 | name = __import__(modname).__doc__ or '' |
| 2074 | desc = name.split('\n')[0] |
| 2075 | name = modname + ' - ' + desc |
| 2076 | if name.lower().find(key) >= 0: |
Ka-Ping Yee | 6624696 | 2001-04-12 11:59:50 +0000 | [diff] [blame] | 2077 | callback(None, modname, desc) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2078 | |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2079 | for importer, modname, ispkg in pkgutil.walk_packages(onerror=onerror): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2080 | if self.quit: |
| 2081 | break |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2082 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2083 | if key is None: |
| 2084 | callback(None, modname, '') |
| 2085 | else: |
Georg Brandl | 126c879 | 2009-04-05 15:05:48 +0000 | [diff] [blame] | 2086 | try: |
Eric Snow | 3a62d14 | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 2087 | spec = pkgutil._get_spec(importer, modname) |
Georg Brandl | 126c879 | 2009-04-05 15:05:48 +0000 | [diff] [blame] | 2088 | except SyntaxError: |
| 2089 | # raised by tests for bad coding cookies or BOM |
| 2090 | continue |
Eric Snow | 3a62d14 | 2014-01-06 20:42:59 -0700 | [diff] [blame] | 2091 | loader = spec.loader |
Georg Brandl | 126c879 | 2009-04-05 15:05:48 +0000 | [diff] [blame] | 2092 | if hasattr(loader, 'get_source'): |
Amaury Forgeot d'Arc | 9196dc6 | 2008-06-19 20:54:32 +0000 | [diff] [blame] | 2093 | try: |
| 2094 | source = loader.get_source(modname) |
Nick Coghlan | 2824cb5 | 2012-07-15 22:12:14 +1000 | [diff] [blame] | 2095 | except Exception: |
Amaury Forgeot d'Arc | 9196dc6 | 2008-06-19 20:54:32 +0000 | [diff] [blame] | 2096 | if onerror: |
| 2097 | onerror(modname) |
| 2098 | continue |
Amaury Forgeot d'Arc | 9196dc6 | 2008-06-19 20:54:32 +0000 | [diff] [blame] | 2099 | desc = source_synopsis(io.StringIO(source)) or '' |
Georg Brandl | 126c879 | 2009-04-05 15:05:48 +0000 | [diff] [blame] | 2100 | if hasattr(loader, 'get_filename'): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2101 | path = loader.get_filename(modname) |
Ka-Ping Yee | 6624696 | 2001-04-12 11:59:50 +0000 | [diff] [blame] | 2102 | else: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2103 | path = None |
| 2104 | else: |
Amaury Forgeot d'Arc | 9196dc6 | 2008-06-19 20:54:32 +0000 | [diff] [blame] | 2105 | try: |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 2106 | module = importlib._bootstrap._load(spec) |
Amaury Forgeot d'Arc | 9196dc6 | 2008-06-19 20:54:32 +0000 | [diff] [blame] | 2107 | except ImportError: |
| 2108 | if onerror: |
| 2109 | onerror(modname) |
| 2110 | continue |
Benjamin Peterson | 54237f9 | 2015-02-16 19:45:01 -0500 | [diff] [blame] | 2111 | desc = module.__doc__.splitlines()[0] if module.__doc__ else '' |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2112 | path = getattr(module,'__file__',None) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 2113 | name = modname + ' - ' + desc |
| 2114 | if name.lower().find(key) >= 0: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2115 | callback(path, modname, desc) |
| 2116 | |
| 2117 | if completer: |
| 2118 | completer() |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2119 | |
| 2120 | def apropos(key): |
| 2121 | """Print all the one-line module summaries that contain a substring.""" |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2122 | def callback(path, modname, desc): |
| 2123 | if modname[-9:] == '.__init__': |
| 2124 | modname = modname[:-9] + ' (package)' |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 2125 | print(modname, desc and '- ' + desc) |
Amaury Forgeot d'Arc | 9196dc6 | 2008-06-19 20:54:32 +0000 | [diff] [blame] | 2126 | def onerror(modname): |
| 2127 | pass |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2128 | with warnings.catch_warnings(): |
| 2129 | warnings.filterwarnings('ignore') # ignore problems during import |
| 2130 | ModuleScanner().run(callback, key, onerror=onerror) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2131 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2132 | # --------------------------------------- enhanced Web browser interface |
| 2133 | |
| 2134 | def _start_server(urlhandler, port): |
| 2135 | """Start an HTTP server thread on a specific port. |
| 2136 | |
| 2137 | Start an HTML/text server thread, so HTML or text documents can be |
| 2138 | browsed dynamically and interactively with a Web browser. Example use: |
| 2139 | |
| 2140 | >>> import time |
| 2141 | >>> import pydoc |
| 2142 | |
| 2143 | Define a URL handler. To determine what the client is asking |
| 2144 | for, check the URL and content_type. |
| 2145 | |
| 2146 | Then get or generate some text or HTML code and return it. |
| 2147 | |
| 2148 | >>> def my_url_handler(url, content_type): |
| 2149 | ... text = 'the URL sent was: (%s, %s)' % (url, content_type) |
| 2150 | ... return text |
| 2151 | |
| 2152 | Start server thread on port 0. |
| 2153 | If you use port 0, the server will pick a random port number. |
| 2154 | You can then use serverthread.port to get the port number. |
| 2155 | |
| 2156 | >>> port = 0 |
| 2157 | >>> serverthread = pydoc._start_server(my_url_handler, port) |
| 2158 | |
| 2159 | Check that the server is really started. If it is, open browser |
| 2160 | and get first page. Use serverthread.url as the starting page. |
| 2161 | |
| 2162 | >>> if serverthread.serving: |
| 2163 | ... import webbrowser |
| 2164 | |
| 2165 | The next two lines are commented out so a browser doesn't open if |
| 2166 | doctest is run on this module. |
| 2167 | |
| 2168 | #... webbrowser.open(serverthread.url) |
| 2169 | #True |
| 2170 | |
| 2171 | Let the server do its thing. We just need to monitor its status. |
| 2172 | Use time.sleep so the loop doesn't hog the CPU. |
| 2173 | |
| 2174 | >>> starttime = time.time() |
| 2175 | >>> timeout = 1 #seconds |
| 2176 | |
| 2177 | This is a short timeout for testing purposes. |
| 2178 | |
| 2179 | >>> while serverthread.serving: |
| 2180 | ... time.sleep(.01) |
| 2181 | ... if serverthread.serving and time.time() - starttime > timeout: |
| 2182 | ... serverthread.stop() |
| 2183 | ... break |
| 2184 | |
| 2185 | Print any errors that may have occurred. |
| 2186 | |
| 2187 | >>> print(serverthread.error) |
| 2188 | None |
| 2189 | """ |
| 2190 | import http.server |
| 2191 | import email.message |
| 2192 | import select |
| 2193 | import threading |
| 2194 | |
| 2195 | class DocHandler(http.server.BaseHTTPRequestHandler): |
| 2196 | |
| 2197 | def do_GET(self): |
| 2198 | """Process a request from an HTML browser. |
| 2199 | |
| 2200 | The URL received is in self.path. |
| 2201 | Get an HTML page from self.urlhandler and send it. |
| 2202 | """ |
| 2203 | if self.path.endswith('.css'): |
| 2204 | content_type = 'text/css' |
| 2205 | else: |
| 2206 | content_type = 'text/html' |
| 2207 | self.send_response(200) |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2208 | self.send_header('Content-Type', '%s; charset=UTF-8' % content_type) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2209 | self.end_headers() |
| 2210 | self.wfile.write(self.urlhandler( |
| 2211 | self.path, content_type).encode('utf-8')) |
| 2212 | |
| 2213 | def log_message(self, *args): |
| 2214 | # Don't log messages. |
| 2215 | pass |
| 2216 | |
| 2217 | class DocServer(http.server.HTTPServer): |
| 2218 | |
| 2219 | def __init__(self, port, callback): |
Senthil Kumaran | 2a42a0b | 2014-09-17 13:17:58 +0800 | [diff] [blame] | 2220 | self.host = 'localhost' |
| 2221 | self.address = (self.host, port) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2222 | self.callback = callback |
| 2223 | self.base.__init__(self, self.address, self.handler) |
| 2224 | self.quit = False |
| 2225 | |
| 2226 | def serve_until_quit(self): |
| 2227 | while not self.quit: |
| 2228 | rd, wr, ex = select.select([self.socket.fileno()], [], [], 1) |
| 2229 | if rd: |
| 2230 | self.handle_request() |
Victor Stinner | a3abd1d | 2011-01-03 16:12:39 +0000 | [diff] [blame] | 2231 | self.server_close() |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2232 | |
| 2233 | def server_activate(self): |
| 2234 | self.base.server_activate(self) |
| 2235 | if self.callback: |
| 2236 | self.callback(self) |
| 2237 | |
| 2238 | class ServerThread(threading.Thread): |
| 2239 | |
| 2240 | def __init__(self, urlhandler, port): |
| 2241 | self.urlhandler = urlhandler |
| 2242 | self.port = int(port) |
| 2243 | threading.Thread.__init__(self) |
| 2244 | self.serving = False |
| 2245 | self.error = None |
| 2246 | |
| 2247 | def run(self): |
| 2248 | """Start the server.""" |
| 2249 | try: |
| 2250 | DocServer.base = http.server.HTTPServer |
| 2251 | DocServer.handler = DocHandler |
| 2252 | DocHandler.MessageClass = email.message.Message |
| 2253 | DocHandler.urlhandler = staticmethod(self.urlhandler) |
| 2254 | docsvr = DocServer(self.port, self.ready) |
| 2255 | self.docserver = docsvr |
| 2256 | docsvr.serve_until_quit() |
| 2257 | except Exception as e: |
| 2258 | self.error = e |
| 2259 | |
| 2260 | def ready(self, server): |
| 2261 | self.serving = True |
| 2262 | self.host = server.host |
| 2263 | self.port = server.server_port |
| 2264 | self.url = 'http://%s:%d/' % (self.host, self.port) |
| 2265 | |
| 2266 | def stop(self): |
| 2267 | """Stop the server and this thread nicely""" |
| 2268 | self.docserver.quit = True |
| 2269 | self.serving = False |
| 2270 | self.url = None |
| 2271 | |
| 2272 | thread = ServerThread(urlhandler, port) |
| 2273 | thread.start() |
| 2274 | # Wait until thread.serving is True to make sure we are |
| 2275 | # really up before returning. |
| 2276 | while not thread.error and not thread.serving: |
| 2277 | time.sleep(.01) |
| 2278 | return thread |
| 2279 | |
| 2280 | |
| 2281 | def _url_handler(url, content_type="text/html"): |
| 2282 | """The pydoc url handler for use with the pydoc server. |
| 2283 | |
| 2284 | If the content_type is 'text/css', the _pydoc.css style |
| 2285 | sheet is read and returned if it exits. |
| 2286 | |
| 2287 | If the content_type is 'text/html', then the result of |
| 2288 | get_html_page(url) is returned. |
| 2289 | """ |
| 2290 | class _HTMLDoc(HTMLDoc): |
| 2291 | |
| 2292 | def page(self, title, contents): |
| 2293 | """Format an HTML page.""" |
| 2294 | css_path = "pydoc_data/_pydoc.css" |
| 2295 | css_link = ( |
| 2296 | '<link rel="stylesheet" type="text/css" href="%s">' % |
| 2297 | css_path) |
| 2298 | return '''\ |
| 2299 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2300 | <html><head><title>Pydoc: %s</title> |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2301 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2302 | %s</head><body bgcolor="#f0f0f8">%s<div style="clear:both;padding-top:.5em;">%s</div> |
| 2303 | </body></html>''' % (title, css_link, html_navbar(), contents) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2304 | |
| 2305 | def filelink(self, url, path): |
| 2306 | return '<a href="getfile?key=%s">%s</a>' % (url, path) |
| 2307 | |
| 2308 | |
| 2309 | html = _HTMLDoc() |
| 2310 | |
| 2311 | def html_navbar(): |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2312 | version = html.escape("%s [%s, %s]" % (platform.python_version(), |
| 2313 | platform.python_build()[0], |
| 2314 | platform.python_compiler())) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2315 | return """ |
| 2316 | <div style='float:left'> |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2317 | Python %s<br>%s |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2318 | </div> |
| 2319 | <div style='float:right'> |
| 2320 | <div style='text-align:center'> |
| 2321 | <a href="index.html">Module Index</a> |
| 2322 | : <a href="topics.html">Topics</a> |
| 2323 | : <a href="keywords.html">Keywords</a> |
| 2324 | </div> |
| 2325 | <div> |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2326 | <form action="get" style='display:inline;'> |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2327 | <input type=text name=key size=15> |
| 2328 | <input type=submit value="Get"> |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2329 | </form> |
| 2330 | <form action="search" style='display:inline;'> |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2331 | <input type=text name=key size=15> |
| 2332 | <input type=submit value="Search"> |
| 2333 | </form> |
| 2334 | </div> |
| 2335 | </div> |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2336 | """ % (version, html.escape(platform.platform(terse=True))) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2337 | |
| 2338 | def html_index(): |
| 2339 | """Module Index page.""" |
| 2340 | |
| 2341 | def bltinlink(name): |
| 2342 | return '<a href="%s.html">%s</a>' % (name, name) |
| 2343 | |
| 2344 | heading = html.heading( |
| 2345 | '<big><big><strong>Index of Modules</strong></big></big>', |
| 2346 | '#ffffff', '#7799ee') |
| 2347 | names = [name for name in sys.builtin_module_names |
| 2348 | if name != '__main__'] |
| 2349 | contents = html.multicolumn(names, bltinlink) |
| 2350 | contents = [heading, '<p>' + html.bigsection( |
| 2351 | 'Built-in Modules', '#ffffff', '#ee77aa', contents)] |
| 2352 | |
| 2353 | seen = {} |
| 2354 | for dir in sys.path: |
| 2355 | contents.append(html.index(dir, seen)) |
| 2356 | |
| 2357 | contents.append( |
| 2358 | '<p align=right><font color="#909090" face="helvetica,' |
| 2359 | 'arial"><strong>pydoc</strong> by Ka-Ping Yee' |
| 2360 | '<ping@lfw.org></font>') |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 2361 | return 'Index of Modules', ''.join(contents) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2362 | |
| 2363 | def html_search(key): |
| 2364 | """Search results page.""" |
| 2365 | # scan for modules |
| 2366 | search_result = [] |
| 2367 | |
| 2368 | def callback(path, modname, desc): |
| 2369 | if modname[-9:] == '.__init__': |
| 2370 | modname = modname[:-9] + ' (package)' |
| 2371 | search_result.append((modname, desc and '- ' + desc)) |
| 2372 | |
| 2373 | with warnings.catch_warnings(): |
| 2374 | warnings.filterwarnings('ignore') # ignore problems during import |
Martin Panter | 9ad0aae | 2015-11-06 00:27:14 +0000 | [diff] [blame] | 2375 | def onerror(modname): |
| 2376 | pass |
| 2377 | ModuleScanner().run(callback, key, onerror=onerror) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2378 | |
| 2379 | # format page |
| 2380 | def bltinlink(name): |
| 2381 | return '<a href="%s.html">%s</a>' % (name, name) |
| 2382 | |
| 2383 | results = [] |
| 2384 | heading = html.heading( |
| 2385 | '<big><big><strong>Search Results</strong></big></big>', |
| 2386 | '#ffffff', '#7799ee') |
| 2387 | for name, desc in search_result: |
| 2388 | results.append(bltinlink(name) + desc) |
| 2389 | contents = heading + html.bigsection( |
| 2390 | 'key = %s' % key, '#ffffff', '#ee77aa', '<br>'.join(results)) |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 2391 | return 'Search Results', contents |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2392 | |
| 2393 | def html_getfile(path): |
| 2394 | """Get and display a source file listing safely.""" |
Zachary Ware | eb43214 | 2014-07-10 11:18:00 -0500 | [diff] [blame] | 2395 | path = urllib.parse.unquote(path) |
Victor Stinner | 91e0877 | 2011-07-05 14:30:41 +0200 | [diff] [blame] | 2396 | with tokenize.open(path) as fp: |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2397 | lines = html.escape(fp.read()) |
| 2398 | body = '<pre>%s</pre>' % lines |
| 2399 | heading = html.heading( |
| 2400 | '<big><big><strong>File Listing</strong></big></big>', |
| 2401 | '#ffffff', '#7799ee') |
| 2402 | contents = heading + html.bigsection( |
| 2403 | 'File: %s' % path, '#ffffff', '#ee77aa', body) |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 2404 | return 'getfile %s' % path, contents |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2405 | |
| 2406 | def html_topics(): |
| 2407 | """Index of topic texts available.""" |
| 2408 | |
| 2409 | def bltinlink(name): |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2410 | return '<a href="topic?key=%s">%s</a>' % (name, name) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2411 | |
| 2412 | heading = html.heading( |
| 2413 | '<big><big><strong>INDEX</strong></big></big>', |
| 2414 | '#ffffff', '#7799ee') |
| 2415 | names = sorted(Helper.topics.keys()) |
| 2416 | |
| 2417 | contents = html.multicolumn(names, bltinlink) |
| 2418 | contents = heading + html.bigsection( |
| 2419 | 'Topics', '#ffffff', '#ee77aa', contents) |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 2420 | return 'Topics', contents |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2421 | |
| 2422 | def html_keywords(): |
| 2423 | """Index of keywords.""" |
| 2424 | heading = html.heading( |
| 2425 | '<big><big><strong>INDEX</strong></big></big>', |
| 2426 | '#ffffff', '#7799ee') |
| 2427 | names = sorted(Helper.keywords.keys()) |
| 2428 | |
| 2429 | def bltinlink(name): |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2430 | return '<a href="topic?key=%s">%s</a>' % (name, name) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2431 | |
| 2432 | contents = html.multicolumn(names, bltinlink) |
| 2433 | contents = heading + html.bigsection( |
| 2434 | 'Keywords', '#ffffff', '#ee77aa', contents) |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 2435 | return 'Keywords', contents |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2436 | |
| 2437 | def html_topicpage(topic): |
| 2438 | """Topic or keyword help page.""" |
| 2439 | buf = io.StringIO() |
| 2440 | htmlhelp = Helper(buf, buf) |
| 2441 | contents, xrefs = htmlhelp._gettopic(topic) |
| 2442 | if topic in htmlhelp.keywords: |
| 2443 | title = 'KEYWORD' |
| 2444 | else: |
| 2445 | title = 'TOPIC' |
| 2446 | heading = html.heading( |
| 2447 | '<big><big><strong>%s</strong></big></big>' % title, |
| 2448 | '#ffffff', '#7799ee') |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2449 | contents = '<pre>%s</pre>' % html.markup(contents) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2450 | contents = html.bigsection(topic , '#ffffff','#ee77aa', contents) |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2451 | if xrefs: |
| 2452 | xrefs = sorted(xrefs.split()) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2453 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2454 | def bltinlink(name): |
| 2455 | return '<a href="topic?key=%s">%s</a>' % (name, name) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2456 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2457 | xrefs = html.multicolumn(xrefs, bltinlink) |
| 2458 | xrefs = html.section('Related help topics: ', |
| 2459 | '#ffffff', '#ee77aa', xrefs) |
Nick Coghlan | ecace28 | 2010-12-03 16:08:46 +0000 | [diff] [blame] | 2460 | return ('%s %s' % (title, topic), |
| 2461 | ''.join((heading, contents, xrefs))) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2462 | |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2463 | def html_getobj(url): |
| 2464 | obj = locate(url, forceload=1) |
| 2465 | if obj is None and url != 'None': |
| 2466 | raise ValueError('could not find object') |
| 2467 | title = describe(obj) |
| 2468 | content = html.document(obj, url) |
| 2469 | return title, content |
| 2470 | |
| 2471 | def html_error(url, exc): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2472 | heading = html.heading( |
| 2473 | '<big><big><strong>Error</strong></big></big>', |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2474 | '#ffffff', '#7799ee') |
| 2475 | contents = '<br>'.join(html.escape(line) for line in |
| 2476 | format_exception_only(type(exc), exc)) |
| 2477 | contents = heading + html.bigsection(url, '#ffffff', '#bb0000', |
| 2478 | contents) |
| 2479 | return "Error - %s" % url, contents |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2480 | |
| 2481 | def get_html_page(url): |
| 2482 | """Generate an HTML page for url.""" |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2483 | complete_url = url |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2484 | if url.endswith('.html'): |
| 2485 | url = url[:-5] |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2486 | try: |
| 2487 | if url in ("", "index"): |
| 2488 | title, content = html_index() |
| 2489 | elif url == "topics": |
| 2490 | title, content = html_topics() |
| 2491 | elif url == "keywords": |
| 2492 | title, content = html_keywords() |
| 2493 | elif '=' in url: |
| 2494 | op, _, url = url.partition('=') |
| 2495 | if op == "search?key": |
| 2496 | title, content = html_search(url) |
| 2497 | elif op == "getfile?key": |
| 2498 | title, content = html_getfile(url) |
| 2499 | elif op == "topic?key": |
| 2500 | # try topics first, then objects. |
| 2501 | try: |
| 2502 | title, content = html_topicpage(url) |
| 2503 | except ValueError: |
| 2504 | title, content = html_getobj(url) |
| 2505 | elif op == "get?key": |
| 2506 | # try objects first, then topics. |
| 2507 | if url in ("", "index"): |
| 2508 | title, content = html_index() |
| 2509 | else: |
| 2510 | try: |
| 2511 | title, content = html_getobj(url) |
| 2512 | except ValueError: |
| 2513 | title, content = html_topicpage(url) |
| 2514 | else: |
| 2515 | raise ValueError('bad pydoc url') |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2516 | else: |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2517 | title, content = html_getobj(url) |
| 2518 | except Exception as exc: |
| 2519 | # Catch any errors and display them in an error page. |
| 2520 | title, content = html_error(complete_url, exc) |
| 2521 | return html.page(title, content) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2522 | |
| 2523 | if url.startswith('/'): |
| 2524 | url = url[1:] |
| 2525 | if content_type == 'text/css': |
| 2526 | path_here = os.path.dirname(os.path.realpath(__file__)) |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2527 | css_path = os.path.join(path_here, url) |
| 2528 | with open(css_path) as fp: |
| 2529 | return ''.join(fp.readlines()) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2530 | elif content_type == 'text/html': |
| 2531 | return get_html_page(url) |
Georg Brandl | d2f3857 | 2011-01-30 08:37:19 +0000 | [diff] [blame] | 2532 | # Errors outside the url handler are caught by the server. |
| 2533 | raise TypeError('unknown content type %r for url %s' % (content_type, url)) |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2534 | |
| 2535 | |
| 2536 | def browse(port=0, *, open_browser=True): |
| 2537 | """Start the enhanced pydoc Web server and open a Web browser. |
| 2538 | |
| 2539 | Use port '0' to start the server on an arbitrary port. |
| 2540 | Set open_browser to False to suppress opening a browser. |
| 2541 | """ |
| 2542 | import webbrowser |
| 2543 | serverthread = _start_server(_url_handler, port) |
| 2544 | if serverthread.error: |
| 2545 | print(serverthread.error) |
| 2546 | return |
| 2547 | if serverthread.serving: |
| 2548 | server_help_msg = 'Server commands: [b]rowser, [q]uit' |
| 2549 | if open_browser: |
| 2550 | webbrowser.open(serverthread.url) |
| 2551 | try: |
| 2552 | print('Server ready at', serverthread.url) |
| 2553 | print(server_help_msg) |
| 2554 | while serverthread.serving: |
| 2555 | cmd = input('server> ') |
| 2556 | cmd = cmd.lower() |
| 2557 | if cmd == 'q': |
| 2558 | break |
| 2559 | elif cmd == 'b': |
| 2560 | webbrowser.open(serverthread.url) |
| 2561 | else: |
| 2562 | print(server_help_msg) |
| 2563 | except (KeyboardInterrupt, EOFError): |
| 2564 | print() |
| 2565 | finally: |
| 2566 | if serverthread.serving: |
| 2567 | serverthread.stop() |
| 2568 | print('Server stopped') |
| 2569 | |
| 2570 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2571 | # -------------------------------------------------- command-line interface |
| 2572 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 2573 | def ispath(x): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 2574 | return isinstance(x, str) and x.find(os.sep) >= 0 |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 2575 | |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 2576 | def cli(): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2577 | """Command-line interface (looks at sys.argv to decide what to do).""" |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2578 | import getopt |
Guido van Rossum | 756aa93 | 2007-04-07 03:04:01 +0000 | [diff] [blame] | 2579 | class BadUsage(Exception): pass |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2580 | |
Nick Coghlan | 106274b | 2009-11-15 23:04:33 +0000 | [diff] [blame] | 2581 | # Scripts don't get the current directory in their path by default |
| 2582 | # unless they are run with the '-m' switch |
| 2583 | if '' not in sys.path: |
| 2584 | scriptdir = os.path.dirname(sys.argv[0]) |
| 2585 | if scriptdir in sys.path: |
| 2586 | sys.path.remove(scriptdir) |
| 2587 | sys.path.insert(0, '.') |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2588 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 2589 | try: |
Victor Stinner | 383c3fc | 2011-05-25 01:35:05 +0200 | [diff] [blame] | 2590 | opts, args = getopt.getopt(sys.argv[1:], 'bk:p:w') |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2591 | writing = False |
| 2592 | start_server = False |
| 2593 | open_browser = False |
| 2594 | port = None |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2595 | for opt, val in opts: |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2596 | if opt == '-b': |
| 2597 | start_server = True |
| 2598 | open_browser = True |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2599 | if opt == '-k': |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2600 | apropos(val) |
| 2601 | return |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2602 | if opt == '-p': |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2603 | start_server = True |
| 2604 | port = val |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2605 | if opt == '-w': |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2606 | writing = True |
| 2607 | |
Benjamin Peterson | b29614e | 2012-10-09 11:16:03 -0400 | [diff] [blame] | 2608 | if start_server: |
| 2609 | if port is None: |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2610 | port = 0 |
| 2611 | browse(port, open_browser=open_browser) |
| 2612 | return |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2613 | |
| 2614 | if not args: raise BadUsage |
| 2615 | for arg in args: |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 2616 | if ispath(arg) and not os.path.exists(arg): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 2617 | print('file %r does not exist' % arg) |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 2618 | break |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2619 | try: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 2620 | if ispath(arg) and os.path.isfile(arg): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2621 | arg = importfile(arg) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 2622 | if writing: |
| 2623 | if ispath(arg) and os.path.isdir(arg): |
| 2624 | writedocs(arg) |
| 2625 | else: |
| 2626 | writedoc(arg) |
| 2627 | else: |
Martin v. Löwis | b8c084e | 2003-06-14 09:03:46 +0000 | [diff] [blame] | 2628 | help.help(arg) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2629 | except ErrorDuringImport as value: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 2630 | print(value) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2631 | |
| 2632 | except (getopt.error, BadUsage): |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2633 | cmd = os.path.splitext(os.path.basename(sys.argv[0]))[0] |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 2634 | print("""pydoc - the Python documentation tool |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2635 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2636 | {cmd} <name> ... |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2637 | Show text documentation on something. <name> may be the name of a |
Martin v. Löwis | b8c084e | 2003-06-14 09:03:46 +0000 | [diff] [blame] | 2638 | Python keyword, topic, function, module, or package, or a dotted |
| 2639 | reference to a class or function within a module or module in a |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2640 | package. If <name> contains a '{sep}', it is used as the path to a |
Martin v. Löwis | b8c084e | 2003-06-14 09:03:46 +0000 | [diff] [blame] | 2641 | Python source file to document. If name is 'keywords', 'topics', |
| 2642 | or 'modules', a listing of these things is displayed. |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2643 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2644 | {cmd} -k <keyword> |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2645 | Search for a keyword in the synopsis lines of all available modules. |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2646 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2647 | {cmd} -p <port> |
| 2648 | Start an HTTP server on the given port on the local machine. Port |
| 2649 | number 0 can be used to get an arbitrary unused port. |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2650 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2651 | {cmd} -b |
| 2652 | Start an HTTP server on an arbitrary unused port and open a Web browser |
| 2653 | to interactively browse documentation. The -p option can be used with |
| 2654 | the -b option to explicitly specify the server port. |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2655 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2656 | {cmd} -w <name> ... |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2657 | Write out the HTML documentation for a module to a file in the current |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2658 | directory. If <name> contains a '{sep}', it is treated as a filename; if |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 2659 | it names a directory, documentation is written for all the contents. |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2660 | """.format(cmd=cmd, sep=os.sep)) |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 2661 | |
Nick Coghlan | 7bb30b7 | 2010-12-03 09:29:11 +0000 | [diff] [blame] | 2662 | if __name__ == '__main__': |
| 2663 | cli() |