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