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