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