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