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