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