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