Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Guido van Rossum | fce538c | 2002-08-06 17:29:38 +0000 | [diff] [blame] | 2 | # -*- coding: Latin-1 -*- |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 3 | """Generate Python documentation in HTML or text for interactive use. |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 4 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 5 | 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] | 6 | help. Calling help(thing) on a Python object documents the object. |
| 7 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 8 | Or, at the shell command line outside of Python: |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 9 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 10 | Run "pydoc <name>" to show documentation on something. <name> may be |
| 11 | the name of a function, module, package, or a dotted reference to a |
| 12 | class or function within a module or module in a package. If the |
| 13 | argument contains a path segment delimiter (e.g. slash on Unix, |
| 14 | backslash on Windows) it is treated as the path to a Python source file. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 15 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 16 | Run "pydoc -k <keyword>" to search for a keyword in the synopsis lines |
| 17 | of all available modules. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 18 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 19 | Run "pydoc -p <port>" to start an HTTP server on a given port on the |
| 20 | local machine to generate documentation web pages. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 21 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 22 | For platforms without a command line, "pydoc -g" starts the HTTP server |
| 23 | and also pops up a little window for controlling it. |
| 24 | |
| 25 | Run "pydoc -w <name>" to write out the HTML documentation for a module |
| 26 | to a file named "<name>.html". |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 27 | """ |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 28 | |
| 29 | __author__ = "Ka-Ping Yee <ping@lfw.org>" |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 30 | __date__ = "26 February 2001" |
Ka-Ping Yee | 09d7d9a | 2001-02-27 22:43:48 +0000 | [diff] [blame] | 31 | __version__ = "$Revision$" |
Ka-Ping Yee | 5e2b173 | 2001-02-27 23:35:09 +0000 | [diff] [blame] | 32 | __credits__ = """Guido van Rossum, for an excellent programming language. |
| 33 | Tommy Burnette, the original creator of manpy. |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 34 | Paul Prescod, for all his work on onlinehelp. |
| 35 | Richard Chamberlain, for the first implementation of textdoc. |
| 36 | |
Ka-Ping Yee | 5e2b173 | 2001-02-27 23:35:09 +0000 | [diff] [blame] | 37 | Mynd you, møøse bites Kan be pretty nasti...""" |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 38 | |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 39 | # Known bugs that can't be fixed here: |
| 40 | # - imp.load_module() cannot be prevented from clobbering existing |
| 41 | # loaded modules, so calling synopsis() on a binary module file |
| 42 | # changes the contents of any existing module with the same name. |
| 43 | # - If the __file__ attribute on a module is a relative path and |
| 44 | # the current directory is changed with os.chdir(), an incorrect |
| 45 | # path will be displayed. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 46 | |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 47 | import sys, imp, os, re, types, inspect |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 48 | from repr import Repr |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 49 | from string import expandtabs, find, join, lower, split, strip, rfind, rstrip |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 50 | |
| 51 | # --------------------------------------------------------- common routines |
| 52 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 53 | def pathdirs(): |
| 54 | """Convert sys.path into a list of absolute, existing, unique paths.""" |
| 55 | dirs = [] |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 56 | normdirs = [] |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 57 | for dir in sys.path: |
| 58 | dir = os.path.abspath(dir or '.') |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 59 | normdir = os.path.normcase(dir) |
| 60 | if normdir not in normdirs and os.path.isdir(dir): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 61 | dirs.append(dir) |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 62 | normdirs.append(normdir) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 63 | return dirs |
| 64 | |
| 65 | def getdoc(object): |
| 66 | """Get the doc string or comments for an object.""" |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 67 | result = inspect.getdoc(object) or inspect.getcomments(object) |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 68 | return result and re.sub('^ *\n', '', rstrip(result)) or '' |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 69 | |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 70 | def splitdoc(doc): |
| 71 | """Split a doc string into a synopsis line (if any) and the rest.""" |
| 72 | lines = split(strip(doc), '\n') |
| 73 | if len(lines) == 1: |
| 74 | return lines[0], '' |
| 75 | elif len(lines) >= 2 and not rstrip(lines[1]): |
| 76 | return lines[0], join(lines[2:], '\n') |
| 77 | return '', join(lines, '\n') |
| 78 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 79 | def classname(object, modname): |
| 80 | """Get a class name and qualify it with a module name if necessary.""" |
| 81 | name = object.__name__ |
| 82 | if object.__module__ != modname: |
| 83 | name = object.__module__ + '.' + name |
| 84 | return name |
| 85 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 86 | def isdata(object): |
| 87 | """Check if an object is of a type that probably means it's data.""" |
| 88 | return not (inspect.ismodule(object) or inspect.isclass(object) or |
| 89 | inspect.isroutine(object) or inspect.isframe(object) or |
| 90 | inspect.istraceback(object) or inspect.iscode(object)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 91 | |
| 92 | def replace(text, *pairs): |
| 93 | """Do a series of global replacements on a string.""" |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 94 | while pairs: |
| 95 | text = join(split(text, pairs[0]), pairs[1]) |
| 96 | pairs = pairs[2:] |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 97 | return text |
| 98 | |
| 99 | def cram(text, maxlen): |
| 100 | """Omit part of a string if needed to make it fit in a maximum length.""" |
| 101 | if len(text) > maxlen: |
Raymond Hettinger | fca3bb6 | 2002-10-21 04:44:11 +0000 | [diff] [blame] | 102 | pre = max(0, (maxlen-3)//2) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 103 | post = max(0, maxlen-3-pre) |
| 104 | return text[:pre] + '...' + text[len(text)-post:] |
| 105 | return text |
| 106 | |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 107 | def stripid(text): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 108 | """Remove the hexadecimal id from a Python object representation.""" |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 109 | # The behaviour of %p is implementation-dependent; we check two cases. |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 110 | for pattern in [' at 0x[0-9a-f]{6,}(>+)$', ' at [0-9A-F]{8,}(>+)$']: |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 111 | if re.search(pattern, repr(Exception)): |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 112 | return re.sub(pattern, '\\1', text) |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 113 | return text |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 114 | |
Tim Peters | 536d226 | 2001-09-20 05:13:38 +0000 | [diff] [blame] | 115 | def _is_some_method(object): |
| 116 | return inspect.ismethod(object) or inspect.ismethoddescriptor(object) |
| 117 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 118 | def allmethods(cl): |
| 119 | methods = {} |
Tim Peters | 536d226 | 2001-09-20 05:13:38 +0000 | [diff] [blame] | 120 | for key, value in inspect.getmembers(cl, _is_some_method): |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 121 | methods[key] = 1 |
| 122 | for base in cl.__bases__: |
| 123 | methods.update(allmethods(base)) # all your base are belong to us |
| 124 | for key in methods.keys(): |
| 125 | methods[key] = getattr(cl, key) |
| 126 | return methods |
| 127 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 128 | def _split_list(s, predicate): |
| 129 | """Split sequence s via predicate, and return pair ([true], [false]). |
| 130 | |
| 131 | The return value is a 2-tuple of lists, |
| 132 | ([x for x in s if predicate(x)], |
| 133 | [x for x in s if not predicate(x)]) |
| 134 | """ |
| 135 | |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 136 | yes = [] |
| 137 | no = [] |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 138 | for x in s: |
| 139 | if predicate(x): |
| 140 | yes.append(x) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 141 | else: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 142 | no.append(x) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 143 | return yes, no |
| 144 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 145 | # ----------------------------------------------------- module manipulation |
| 146 | |
| 147 | def ispackage(path): |
| 148 | """Guess whether a path refers to a package directory.""" |
| 149 | if os.path.isdir(path): |
| 150 | for ext in ['.py', '.pyc', '.pyo']: |
| 151 | if os.path.isfile(os.path.join(path, '__init__' + ext)): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 152 | return True |
| 153 | return False |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 154 | |
| 155 | def synopsis(filename, cache={}): |
| 156 | """Get the one-line summary out of a module file.""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 157 | mtime = os.stat(filename).st_mtime |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 158 | lastupdate, result = cache.get(filename, (0, None)) |
| 159 | if lastupdate < mtime: |
| 160 | info = inspect.getmoduleinfo(filename) |
| 161 | file = open(filename) |
| 162 | if info and 'b' in info[2]: # binary modules have to be imported |
| 163 | try: module = imp.load_module('__temp__', file, filename, info[1:]) |
| 164 | except: return None |
| 165 | result = split(module.__doc__ or '', '\n')[0] |
| 166 | del sys.modules['__temp__'] |
| 167 | else: # text modules can be directly examined |
| 168 | line = file.readline() |
| 169 | while line[:1] == '#' or not strip(line): |
| 170 | line = file.readline() |
| 171 | if not line: break |
| 172 | line = strip(line) |
| 173 | if line[:4] == 'r"""': line = line[1:] |
| 174 | if line[:3] == '"""': |
| 175 | line = line[3:] |
| 176 | if line[-1:] == '\\': line = line[:-1] |
| 177 | while not strip(line): |
| 178 | line = file.readline() |
| 179 | if not line: break |
| 180 | result = strip(split(line, '"""')[0]) |
| 181 | else: result = None |
| 182 | file.close() |
| 183 | cache[filename] = (mtime, result) |
| 184 | return result |
| 185 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 186 | class ErrorDuringImport(Exception): |
| 187 | """Errors that occurred while trying to import something to document it.""" |
| 188 | def __init__(self, filename, (exc, value, tb)): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 189 | self.filename = filename |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 190 | self.exc = exc |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 191 | self.value = value |
| 192 | self.tb = tb |
| 193 | |
| 194 | def __str__(self): |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 195 | exc = self.exc |
| 196 | if type(exc) is types.ClassType: |
| 197 | exc = exc.__name__ |
| 198 | return 'problem in %s - %s: %s' % (self.filename, exc, self.value) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 199 | |
| 200 | def importfile(path): |
| 201 | """Import a Python source file or compiled file given its path.""" |
| 202 | magic = imp.get_magic() |
| 203 | file = open(path, 'r') |
| 204 | if file.read(len(magic)) == magic: |
| 205 | kind = imp.PY_COMPILED |
| 206 | else: |
| 207 | kind = imp.PY_SOURCE |
| 208 | file.close() |
| 209 | filename = os.path.basename(path) |
| 210 | name, ext = os.path.splitext(filename) |
| 211 | file = open(path, 'r') |
| 212 | try: |
| 213 | module = imp.load_module(name, file, path, (ext, 'r', kind)) |
| 214 | except: |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 215 | raise ErrorDuringImport(path, sys.exc_info()) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 216 | file.close() |
| 217 | return module |
| 218 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 219 | def safeimport(path, forceload=0, cache={}): |
| 220 | """Import a module; handle errors; return None if the module isn't found. |
| 221 | |
| 222 | If the module *is* found but an exception occurs, it's wrapped in an |
| 223 | ErrorDuringImport exception and reraised. Unlike __import__, if a |
| 224 | package path is specified, the module at the end of the path is returned, |
| 225 | not the package at the beginning. If the optional 'forceload' argument |
| 226 | is 1, we reload the module from disk (unless it's a dynamic extension).""" |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 227 | if forceload and path in sys.modules: |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 228 | # This is the only way to be sure. Checking the mtime of the file |
| 229 | # isn't good enough (e.g. what if the module contains a class that |
| 230 | # inherits from another module that has changed?). |
| 231 | if path not in sys.builtin_module_names: |
| 232 | # Python never loads a dynamic extension a second time from the |
| 233 | # same path, even if the file is changed or missing. Deleting |
| 234 | # the entry in sys.modules doesn't help for dynamic extensions, |
| 235 | # so we're not even going to try to keep them up to date. |
| 236 | info = inspect.getmoduleinfo(sys.modules[path].__file__) |
| 237 | if info[3] != imp.C_EXTENSION: |
| 238 | cache[path] = sys.modules[path] # prevent module from clearing |
| 239 | del sys.modules[path] |
| 240 | try: |
| 241 | module = __import__(path) |
| 242 | except: |
| 243 | # Did the error occur before or after the module was found? |
| 244 | (exc, value, tb) = info = sys.exc_info() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 245 | if path in sys.modules: |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 246 | # An error occured while executing the imported module. |
| 247 | raise ErrorDuringImport(sys.modules[path].__file__, info) |
| 248 | elif exc is SyntaxError: |
| 249 | # A SyntaxError occurred before we could execute the module. |
| 250 | raise ErrorDuringImport(value.filename, info) |
| 251 | elif exc is ImportError and \ |
| 252 | split(lower(str(value)))[:2] == ['no', 'module']: |
| 253 | # The module was not found. |
| 254 | return None |
| 255 | else: |
| 256 | # Some other error occurred during the importing process. |
| 257 | raise ErrorDuringImport(path, sys.exc_info()) |
| 258 | for part in split(path, '.')[1:]: |
| 259 | try: module = getattr(module, part) |
| 260 | except AttributeError: return None |
| 261 | return module |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 262 | |
| 263 | # ---------------------------------------------------- formatter base class |
| 264 | |
| 265 | class Doc: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 266 | def document(self, object, name=None, *args): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 267 | """Generate documentation for an object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 268 | args = (object, name) + args |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 269 | if inspect.ismodule(object): return apply(self.docmodule, args) |
| 270 | if inspect.isclass(object): return apply(self.docclass, args) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 271 | if inspect.isroutine(object): return apply(self.docroutine, args) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 272 | return apply(self.docother, args) |
| 273 | |
| 274 | def fail(self, object, name=None, *args): |
| 275 | """Raise an exception for unimplemented types.""" |
| 276 | message = "don't know how to document object%s of type %s" % ( |
| 277 | name and ' ' + repr(name), type(object).__name__) |
| 278 | raise TypeError, message |
| 279 | |
| 280 | docmodule = docclass = docroutine = docother = fail |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 281 | |
| 282 | # -------------------------------------------- HTML documentation generator |
| 283 | |
| 284 | class HTMLRepr(Repr): |
| 285 | """Class for safely making an HTML representation of a Python object.""" |
| 286 | def __init__(self): |
| 287 | Repr.__init__(self) |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 288 | self.maxlist = self.maxtuple = 20 |
| 289 | self.maxdict = 10 |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 290 | self.maxstring = self.maxother = 100 |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 291 | |
| 292 | def escape(self, text): |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 293 | return replace(text, '&', '&', '<', '<', '>', '>') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 294 | |
| 295 | def repr(self, object): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 296 | return Repr.repr(self, object) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 297 | |
| 298 | def repr1(self, x, level): |
| 299 | methodname = 'repr_' + join(split(type(x).__name__), '_') |
| 300 | if hasattr(self, methodname): |
| 301 | return getattr(self, methodname)(x, level) |
| 302 | else: |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 303 | return self.escape(cram(stripid(repr(x)), self.maxother)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 304 | |
| 305 | def repr_string(self, x, level): |
Ka-Ping Yee | a2fe103 | 2001-03-02 01:19:14 +0000 | [diff] [blame] | 306 | test = cram(x, self.maxstring) |
| 307 | testrepr = repr(test) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 308 | if '\\' in test and '\\' not in replace(testrepr, r'\\', ''): |
Ka-Ping Yee | a2fe103 | 2001-03-02 01:19:14 +0000 | [diff] [blame] | 309 | # Backslashes are only literal in the string and are never |
| 310 | # needed to make any special characters, so show a raw string. |
| 311 | return 'r' + testrepr[0] + self.escape(test) + testrepr[0] |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 312 | return re.sub(r'((\\[\\abfnrtv\'"]|\\[0-9]..|\\x..|\\u....)+)', |
Ka-Ping Yee | a2fe103 | 2001-03-02 01:19:14 +0000 | [diff] [blame] | 313 | r'<font color="#c040c0">\1</font>', |
| 314 | self.escape(testrepr)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 315 | |
Skip Montanaro | df70878 | 2002-03-07 22:58:02 +0000 | [diff] [blame] | 316 | repr_str = repr_string |
| 317 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 318 | def repr_instance(self, x, level): |
| 319 | try: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 320 | return self.escape(cram(stripid(repr(x)), self.maxstring)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 321 | except: |
| 322 | return self.escape('<%s instance>' % x.__class__.__name__) |
| 323 | |
| 324 | repr_unicode = repr_string |
| 325 | |
| 326 | class HTMLDoc(Doc): |
| 327 | """Formatter class for HTML documentation.""" |
| 328 | |
| 329 | # ------------------------------------------- HTML formatting utilities |
| 330 | |
| 331 | _repr_instance = HTMLRepr() |
| 332 | repr = _repr_instance.repr |
| 333 | escape = _repr_instance.escape |
| 334 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 335 | def page(self, title, contents): |
| 336 | """Format an HTML page.""" |
| 337 | return ''' |
Tim Peters | 59ed448 | 2001-10-31 04:20:26 +0000 | [diff] [blame] | 338 | <!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 339 | <html><head><title>Python: %s</title> |
Ka-Ping Yee | e280c06 | 2001-03-23 14:05:53 +0000 | [diff] [blame] | 340 | <style type="text/css"><!-- |
Ka-Ping Yee | d03f8fe | 2001-04-13 15:04:32 +0000 | [diff] [blame] | 341 | TT { font-family: lucidatypewriter, lucida console, courier } |
Ka-Ping Yee | e280c06 | 2001-03-23 14:05:53 +0000 | [diff] [blame] | 342 | --></style></head><body bgcolor="#f0f0f8"> |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 343 | %s |
| 344 | </body></html>''' % (title, contents) |
| 345 | |
| 346 | def heading(self, title, fgcol, bgcol, extras=''): |
| 347 | """Format a page heading.""" |
| 348 | return ''' |
Tim Peters | 59ed448 | 2001-10-31 04:20:26 +0000 | [diff] [blame] | 349 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 350 | <tr bgcolor="%s"> |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 351 | <td valign=bottom> <br> |
| 352 | <font color="%s" face="helvetica, arial"> <br>%s</font></td |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 353 | ><td align=right valign=bottom |
Ka-Ping Yee | 987ec90 | 2001-03-23 13:35:45 +0000 | [diff] [blame] | 354 | ><font color="%s" face="helvetica, arial">%s</font></td></tr></table> |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 355 | ''' % (bgcol, fgcol, title, fgcol, extras or ' ') |
| 356 | |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 357 | def section(self, title, fgcol, bgcol, contents, width=10, |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 358 | prelude='', marginalia=None, gap=' '): |
| 359 | """Format a section with a heading.""" |
| 360 | if marginalia is None: |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 361 | marginalia = '<tt>' + ' ' * width + '</tt>' |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 362 | result = ''' |
Tim Peters | 59ed448 | 2001-10-31 04:20:26 +0000 | [diff] [blame] | 363 | <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 364 | <tr bgcolor="%s"> |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 365 | <td colspan=3 valign=bottom> <br> |
| 366 | <font color="%s" face="helvetica, arial">%s</font></td></tr> |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 367 | ''' % (bgcol, fgcol, title) |
| 368 | if prelude: |
| 369 | result = result + ''' |
Ka-Ping Yee | 987ec90 | 2001-03-23 13:35:45 +0000 | [diff] [blame] | 370 | <tr bgcolor="%s"><td rowspan=2>%s</td> |
| 371 | <td colspan=2>%s</td></tr> |
| 372 | <tr><td>%s</td>''' % (bgcol, marginalia, prelude, gap) |
| 373 | else: |
| 374 | result = result + ''' |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 375 | <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] | 376 | |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 377 | return result + '\n<td width="100%%">%s</td></tr></table>' % contents |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 378 | |
| 379 | def bigsection(self, title, *args): |
| 380 | """Format a section with a big heading.""" |
| 381 | title = '<big><strong>%s</strong></big>' % title |
| 382 | return apply(self.section, (title,) + args) |
| 383 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 384 | def preformat(self, text): |
| 385 | """Format literal preformatted text.""" |
| 386 | text = self.escape(expandtabs(text)) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 387 | return replace(text, '\n\n', '\n \n', '\n\n', '\n \n', |
| 388 | ' ', ' ', '\n', '<br>\n') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 389 | |
| 390 | def multicolumn(self, list, format, cols=4): |
| 391 | """Format a list of items into a multi-column list.""" |
| 392 | result = '' |
| 393 | rows = (len(list)+cols-1)/cols |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 394 | for col in range(cols): |
| 395 | result = result + '<td width="%d%%" valign=top>' % (100/cols) |
| 396 | for i in range(rows*col, rows*col+rows): |
| 397 | if i < len(list): |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 398 | result = result + format(list[i]) + '<br>\n' |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 399 | result = result + '</td>' |
Tim Peters | 59ed448 | 2001-10-31 04:20:26 +0000 | [diff] [blame] | 400 | return '<table width="100%%" summary="list"><tr>%s</tr></table>' % result |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 401 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 402 | def grey(self, text): return '<font color="#909090">%s</font>' % text |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 403 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 404 | def namelink(self, name, *dicts): |
| 405 | """Make a link for an identifier, given name-to-URL mappings.""" |
| 406 | for dict in dicts: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 407 | if name in dict: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 408 | return '<a href="%s">%s</a>' % (dict[name], name) |
| 409 | return name |
| 410 | |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 411 | def classlink(self, object, modname): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 412 | """Make a link for a class.""" |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 413 | name, module = object.__name__, sys.modules.get(object.__module__) |
| 414 | if hasattr(module, name) and getattr(module, name) is object: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 415 | return '<a href="%s.html#%s">%s</a>' % ( |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 416 | module.__name__, name, classname(object, modname)) |
| 417 | return classname(object, modname) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 418 | |
| 419 | def modulelink(self, object): |
| 420 | """Make a link for a module.""" |
| 421 | return '<a href="%s.html">%s</a>' % (object.__name__, object.__name__) |
| 422 | |
| 423 | def modpkglink(self, (name, path, ispackage, shadowed)): |
| 424 | """Make a link for a module or package to display in an index.""" |
| 425 | if shadowed: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 426 | return self.grey(name) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 427 | if path: |
| 428 | url = '%s.%s.html' % (path, name) |
| 429 | else: |
| 430 | url = '%s.html' % name |
| 431 | if ispackage: |
| 432 | text = '<strong>%s</strong> (package)' % name |
| 433 | else: |
| 434 | text = name |
| 435 | return '<a href="%s">%s</a>' % (url, text) |
| 436 | |
| 437 | def markup(self, text, escape=None, funcs={}, classes={}, methods={}): |
| 438 | """Mark up some plain text, given a context of symbols to look for. |
| 439 | Each context dictionary maps object names to anchor names.""" |
| 440 | escape = escape or self.escape |
| 441 | results = [] |
| 442 | here = 0 |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 443 | pattern = re.compile(r'\b((http|ftp)://\S+[\w/]|' |
| 444 | r'RFC[- ]?(\d+)|' |
Ka-Ping Yee | f78a81b | 2001-03-27 08:13:42 +0000 | [diff] [blame] | 445 | r'PEP[- ]?(\d+)|' |
Neil Schemenauer | d69711c | 2002-03-24 23:02:07 +0000 | [diff] [blame] | 446 | r'(self\.)?(\w+))') |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 447 | while True: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 448 | match = pattern.search(text, here) |
| 449 | if not match: break |
| 450 | start, end = match.span() |
| 451 | results.append(escape(text[here:start])) |
| 452 | |
Ka-Ping Yee | f78a81b | 2001-03-27 08:13:42 +0000 | [diff] [blame] | 453 | all, scheme, rfc, pep, selfdot, name = match.groups() |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 454 | if scheme: |
Neil Schemenauer | cddc1a0 | 2002-03-24 23:11:21 +0000 | [diff] [blame] | 455 | url = escape(all).replace('"', '"') |
| 456 | results.append('<a href="%s">%s</a>' % (url, url)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 457 | elif rfc: |
Ka-Ping Yee | f78a81b | 2001-03-27 08:13:42 +0000 | [diff] [blame] | 458 | url = 'http://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc) |
| 459 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 460 | elif pep: |
| 461 | url = 'http://www.python.org/peps/pep-%04d.html' % int(pep) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 462 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 463 | elif text[end:end+1] == '(': |
| 464 | results.append(self.namelink(name, methods, funcs, classes)) |
| 465 | elif selfdot: |
| 466 | results.append('self.<strong>%s</strong>' % name) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 467 | else: |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 468 | results.append(self.namelink(name, classes)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 469 | here = end |
| 470 | results.append(escape(text[here:])) |
| 471 | return join(results, '') |
| 472 | |
| 473 | # ---------------------------------------------- type-specific routines |
| 474 | |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 475 | def formattree(self, tree, modname, parent=None): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 476 | """Produce HTML for a class tree as given by inspect.getclasstree().""" |
| 477 | result = '' |
| 478 | for entry in tree: |
| 479 | if type(entry) is type(()): |
| 480 | c, bases = entry |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 481 | result = result + '<dt><font face="helvetica, arial">' |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 482 | result = result + self.classlink(c, modname) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 483 | if bases and bases != (parent,): |
| 484 | parents = [] |
| 485 | for base in bases: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 486 | parents.append(self.classlink(base, modname)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 487 | result = result + '(' + join(parents, ', ') + ')' |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 488 | result = result + '\n</font></dt>' |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 489 | elif type(entry) is type([]): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 490 | result = result + '<dd>\n%s</dd>\n' % self.formattree( |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 491 | entry, modname, c) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 492 | return '<dl>\n%s</dl>\n' % result |
| 493 | |
Tim Peters | 8dd7ade | 2001-10-18 19:56:17 +0000 | [diff] [blame] | 494 | def docmodule(self, object, name=None, mod=None, *ignored): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 495 | """Produce HTML documentation for a module object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 496 | name = object.__name__ # ignore the passed-in name |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 497 | parts = split(name, '.') |
| 498 | links = [] |
| 499 | for i in range(len(parts)-1): |
| 500 | links.append( |
| 501 | '<a href="%s.html"><font color="#ffffff">%s</font></a>' % |
| 502 | (join(parts[:i+1], '.'), parts[i])) |
| 503 | linkedname = join(links + parts[-1:], '.') |
| 504 | head = '<big><big><strong>%s</strong></big></big>' % linkedname |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 505 | try: |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 506 | path = inspect.getabsfile(object) |
Ka-Ping Yee | 6191a23 | 2001-04-13 15:00:27 +0000 | [diff] [blame] | 507 | url = path |
| 508 | if sys.platform == 'win32': |
| 509 | import nturl2path |
| 510 | url = nturl2path.pathname2url(path) |
| 511 | filelink = '<a href="file:%s">%s</a>' % (url, path) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 512 | except TypeError: |
| 513 | filelink = '(built-in)' |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 514 | info = [] |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 515 | if hasattr(object, '__version__'): |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 516 | version = str(object.__version__) |
Ka-Ping Yee | 40c4991 | 2001-02-27 22:46:01 +0000 | [diff] [blame] | 517 | if version[:11] == '$' + 'Revision: ' and version[-1:] == '$': |
| 518 | version = strip(version[11:-1]) |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 519 | info.append('version %s' % self.escape(version)) |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 520 | if hasattr(object, '__date__'): |
| 521 | info.append(self.escape(str(object.__date__))) |
| 522 | if info: |
| 523 | head = head + ' (%s)' % join(info, ', ') |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 524 | result = self.heading( |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 525 | head, '#ffffff', '#7799ee', '<a href=".">index</a><br>' + filelink) |
| 526 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 527 | modules = inspect.getmembers(object, inspect.ismodule) |
| 528 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 529 | classes, cdict = [], {} |
| 530 | for key, value in inspect.getmembers(object, inspect.isclass): |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 531 | if (inspect.getmodule(value) or object) is object: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 532 | classes.append((key, value)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 533 | cdict[key] = cdict[value] = '#' + key |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 534 | for key, value in classes: |
| 535 | for base in value.__bases__: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 536 | key, modname = base.__name__, base.__module__ |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 537 | module = sys.modules.get(modname) |
| 538 | if modname != name and module and hasattr(module, key): |
| 539 | if getattr(module, key) is base: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 540 | if not key in cdict: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 541 | cdict[key] = cdict[base] = modname + '.html#' + key |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 542 | funcs, fdict = [], {} |
| 543 | for key, value in inspect.getmembers(object, inspect.isroutine): |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 544 | if inspect.isbuiltin(value) or inspect.getmodule(value) is object: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 545 | funcs.append((key, value)) |
| 546 | fdict[key] = '#-' + key |
| 547 | if inspect.isfunction(value): fdict[value] = fdict[key] |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 548 | data = [] |
| 549 | for key, value in inspect.getmembers(object, isdata): |
| 550 | if key not in ['__builtins__', '__doc__']: |
| 551 | data.append((key, value)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 552 | |
| 553 | doc = self.markup(getdoc(object), self.preformat, fdict, cdict) |
| 554 | doc = doc and '<tt>%s</tt>' % doc |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 555 | result = result + '<p>%s</p>\n' % doc |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 556 | |
| 557 | if hasattr(object, '__path__'): |
| 558 | modpkgs = [] |
| 559 | modnames = [] |
| 560 | for file in os.listdir(object.__path__[0]): |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 561 | path = os.path.join(object.__path__[0], file) |
| 562 | modname = inspect.getmodulename(file) |
| 563 | if modname and modname not in modnames: |
| 564 | modpkgs.append((modname, name, 0, 0)) |
| 565 | modnames.append(modname) |
| 566 | elif ispackage(path): |
| 567 | modpkgs.append((file, name, 1, 0)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 568 | modpkgs.sort() |
| 569 | contents = self.multicolumn(modpkgs, self.modpkglink) |
| 570 | result = result + self.bigsection( |
| 571 | 'Package Contents', '#ffffff', '#aa55cc', contents) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 572 | elif modules: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 573 | contents = self.multicolumn( |
| 574 | modules, lambda (key, value), s=self: s.modulelink(value)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 575 | result = result + self.bigsection( |
| 576 | 'Modules', '#fffff', '#aa55cc', contents) |
| 577 | |
| 578 | if classes: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 579 | classlist = map(lambda (key, value): value, classes) |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 580 | contents = [ |
| 581 | self.formattree(inspect.getclasstree(classlist, 1), name)] |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 582 | for key, value in classes: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 583 | contents.append(self.document(value, key, name, fdict, cdict)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 584 | result = result + self.bigsection( |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 585 | 'Classes', '#ffffff', '#ee77aa', join(contents)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 586 | if funcs: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 587 | contents = [] |
| 588 | for key, value in funcs: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 589 | contents.append(self.document(value, key, name, fdict, cdict)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 590 | result = result + self.bigsection( |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 591 | 'Functions', '#ffffff', '#eeaa77', join(contents)) |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 592 | if data: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 593 | contents = [] |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 594 | for key, value in data: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 595 | contents.append(self.document(value, key)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 596 | result = result + self.bigsection( |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 597 | 'Data', '#ffffff', '#55aa55', join(contents, '<br>\n')) |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 598 | if hasattr(object, '__author__'): |
| 599 | contents = self.markup(str(object.__author__), self.preformat) |
| 600 | result = result + self.bigsection( |
| 601 | 'Author', '#ffffff', '#7799ee', contents) |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 602 | if hasattr(object, '__credits__'): |
| 603 | contents = self.markup(str(object.__credits__), self.preformat) |
| 604 | result = result + self.bigsection( |
| 605 | 'Credits', '#ffffff', '#7799ee', contents) |
| 606 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 607 | return result |
| 608 | |
Tim Peters | 8dd7ade | 2001-10-18 19:56:17 +0000 | [diff] [blame] | 609 | def docclass(self, object, name=None, mod=None, funcs={}, classes={}, |
| 610 | *ignored): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 611 | """Produce HTML documentation for a class object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 612 | realname = object.__name__ |
| 613 | name = name or realname |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 614 | bases = object.__bases__ |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 615 | |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 616 | contents = [] |
| 617 | push = contents.append |
| 618 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 619 | # Cute little class to pump out a horizontal rule between sections. |
| 620 | class HorizontalRule: |
| 621 | def __init__(self): |
| 622 | self.needone = 0 |
| 623 | def maybe(self): |
| 624 | if self.needone: |
| 625 | push('<hr>\n') |
| 626 | self.needone = 1 |
| 627 | hr = HorizontalRule() |
| 628 | |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 629 | # List the mro, if non-trivial. |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 630 | mro = list(inspect.getmro(object)) |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 631 | if len(mro) > 2: |
| 632 | hr.maybe() |
| 633 | push('<dl><dt>Method resolution order:</dt>\n') |
| 634 | for base in mro: |
| 635 | push('<dd>%s</dd>\n' % self.classlink(base, |
| 636 | object.__module__)) |
| 637 | push('</dl>\n') |
| 638 | |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 639 | def spill(msg, attrs, predicate): |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 640 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 641 | if ok: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 642 | hr.maybe() |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 643 | push(msg) |
| 644 | for name, kind, homecls, value in ok: |
| 645 | push(self.document(getattr(object, name), name, mod, |
| 646 | funcs, classes, mdict, object)) |
| 647 | push('\n') |
| 648 | return attrs |
| 649 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 650 | def spillproperties(msg, attrs, predicate): |
| 651 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 652 | if ok: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 653 | hr.maybe() |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 654 | push(msg) |
| 655 | for name, kind, homecls, value in ok: |
Tim Peters | 3e767d1 | 2001-09-25 00:01:06 +0000 | [diff] [blame] | 656 | push('<dl><dt><strong>%s</strong></dt>\n' % name) |
| 657 | if value.__doc__ is not None: |
| 658 | doc = self.markup(value.__doc__, self.preformat, |
| 659 | funcs, classes, mdict) |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 660 | push('<dd><tt>%s</tt></dd>\n' % doc) |
Tim Peters | f33532c | 2001-09-25 06:30:51 +0000 | [diff] [blame] | 661 | for attr, tag in [("fget", " getter"), |
| 662 | ("fset", " setter"), |
Tim Peters | 3e767d1 | 2001-09-25 00:01:06 +0000 | [diff] [blame] | 663 | ("fdel", " deleter")]: |
| 664 | func = getattr(value, attr) |
| 665 | if func is not None: |
| 666 | base = self.document(func, name + tag, mod, |
| 667 | funcs, classes, mdict, object) |
| 668 | push('<dd>%s</dd>\n' % base) |
| 669 | push('</dl>\n') |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 670 | return attrs |
| 671 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 672 | def spilldata(msg, attrs, predicate): |
| 673 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 674 | if ok: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 675 | hr.maybe() |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 676 | push(msg) |
| 677 | for name, kind, homecls, value in ok: |
| 678 | base = self.docother(getattr(object, name), name, mod) |
Guido van Rossum | 5e355b2 | 2002-05-21 20:56:15 +0000 | [diff] [blame] | 679 | if callable(value): |
| 680 | doc = getattr(value, "__doc__", None) |
| 681 | else: |
| 682 | doc = None |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 683 | if doc is None: |
| 684 | push('<dl><dt>%s</dl>\n' % base) |
| 685 | else: |
| 686 | doc = self.markup(getdoc(value), self.preformat, |
| 687 | funcs, classes, mdict) |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 688 | doc = '<dd><tt>%s</tt>' % doc |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 689 | push('<dl><dt>%s%s</dl>\n' % (base, doc)) |
| 690 | push('\n') |
| 691 | return attrs |
| 692 | |
| 693 | attrs = inspect.classify_class_attrs(object) |
| 694 | mdict = {} |
| 695 | for key, kind, homecls, value in attrs: |
| 696 | mdict[key] = anchor = '#' + name + '-' + key |
| 697 | value = getattr(object, key) |
| 698 | try: |
| 699 | # The value may not be hashable (e.g., a data attr with |
| 700 | # a dict or list value). |
| 701 | mdict[value] = anchor |
| 702 | except TypeError: |
| 703 | pass |
| 704 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 705 | while attrs: |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 706 | if mro: |
| 707 | thisclass = mro.pop(0) |
| 708 | else: |
| 709 | thisclass = attrs[0][2] |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 710 | attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass) |
| 711 | |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 712 | if thisclass is object: |
| 713 | tag = "defined here" |
| 714 | else: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 715 | tag = "inherited from %s" % self.classlink(thisclass, |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 716 | object.__module__) |
| 717 | tag += ':<br>\n' |
| 718 | |
| 719 | # Sort attrs by name. |
| 720 | attrs.sort(lambda t1, t2: cmp(t1[0], t2[0])) |
| 721 | |
| 722 | # Pump out the attrs, segregated by kind. |
| 723 | attrs = spill("Methods %s" % tag, attrs, |
| 724 | lambda t: t[1] == 'method') |
| 725 | attrs = spill("Class methods %s" % tag, attrs, |
| 726 | lambda t: t[1] == 'class method') |
| 727 | attrs = spill("Static methods %s" % tag, attrs, |
| 728 | lambda t: t[1] == 'static method') |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 729 | attrs = spillproperties("Properties %s" % tag, attrs, |
| 730 | lambda t: t[1] == 'property') |
Tim Peters | f33532c | 2001-09-25 06:30:51 +0000 | [diff] [blame] | 731 | attrs = spilldata("Data and non-method functions %s" % tag, attrs, |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 732 | lambda t: t[1] == 'data') |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 733 | assert attrs == [] |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 734 | attrs = inherited |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 735 | |
| 736 | contents = ''.join(contents) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 737 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 738 | if name == realname: |
| 739 | title = '<a name="%s">class <strong>%s</strong></a>' % ( |
| 740 | name, realname) |
| 741 | else: |
| 742 | title = '<strong>%s</strong> = <a name="%s">class %s</a>' % ( |
| 743 | name, name, realname) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 744 | if bases: |
| 745 | parents = [] |
| 746 | for base in bases: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 747 | parents.append(self.classlink(base, object.__module__)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 748 | title = title + '(%s)' % join(parents, ', ') |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 749 | doc = self.markup(getdoc(object), self.preformat, funcs, classes, mdict) |
| 750 | doc = doc and '<tt>%s<br> </tt>' % doc or ' ' |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 751 | |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 752 | return self.section(title, '#000000', '#ffc8d8', contents, 5, doc) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 753 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 754 | def formatvalue(self, object): |
| 755 | """Format an argument default value as text.""" |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 756 | return self.grey('=' + self.repr(object)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 757 | |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 758 | def docroutine(self, object, name=None, mod=None, |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 759 | funcs={}, classes={}, methods={}, cl=None): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 760 | """Produce HTML documentation for a function or method object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 761 | realname = object.__name__ |
| 762 | name = name or realname |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 763 | anchor = (cl and cl.__name__ or '') + '-' + name |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 764 | note = '' |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 765 | skipdocs = 0 |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 766 | if inspect.ismethod(object): |
Ka-Ping Yee | 6dcfa38 | 2001-04-12 20:27:31 +0000 | [diff] [blame] | 767 | imclass = object.im_class |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 768 | if cl: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 769 | if imclass is not cl: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 770 | note = ' from ' + self.classlink(imclass, mod) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 771 | else: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 772 | if object.im_self: |
| 773 | note = ' method of %s instance' % self.classlink( |
| 774 | object.im_self.__class__, mod) |
| 775 | else: |
| 776 | note = ' unbound %s method' % self.classlink(imclass,mod) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 777 | object = object.im_func |
| 778 | |
| 779 | if name == realname: |
| 780 | title = '<a name="%s"><strong>%s</strong></a>' % (anchor, realname) |
| 781 | else: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 782 | if (cl and realname in cl.__dict__ and |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 783 | cl.__dict__[realname] is object): |
Ka-Ping Yee | e280c06 | 2001-03-23 14:05:53 +0000 | [diff] [blame] | 784 | reallink = '<a href="#%s">%s</a>' % ( |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 785 | cl.__name__ + '-' + realname, realname) |
| 786 | skipdocs = 1 |
| 787 | else: |
| 788 | reallink = realname |
| 789 | title = '<a name="%s"><strong>%s</strong></a> = %s' % ( |
| 790 | anchor, name, reallink) |
Tim Peters | 4bcfa31 | 2001-09-20 06:08:24 +0000 | [diff] [blame] | 791 | if inspect.isfunction(object): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 792 | args, varargs, varkw, defaults = inspect.getargspec(object) |
| 793 | argspec = inspect.formatargspec( |
| 794 | args, varargs, varkw, defaults, formatvalue=self.formatvalue) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 795 | if realname == '<lambda>': |
Tim Peters | 59ed448 | 2001-10-31 04:20:26 +0000 | [diff] [blame] | 796 | title = '<strong>%s</strong> <em>lambda</em> ' % name |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 797 | argspec = argspec[1:-1] # remove parentheses |
Tim Peters | 4bcfa31 | 2001-09-20 06:08:24 +0000 | [diff] [blame] | 798 | else: |
| 799 | argspec = '(...)' |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 800 | |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 801 | decl = title + argspec + (note and self.grey( |
| 802 | '<font face="helvetica, arial">%s</font>' % note)) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 803 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 804 | if skipdocs: |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 805 | return '<dl><dt>%s</dt></dl>\n' % decl |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 806 | else: |
| 807 | doc = self.markup( |
| 808 | getdoc(object), self.preformat, funcs, classes, methods) |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 809 | doc = doc and '<dd><tt>%s</tt></dd>' % doc |
| 810 | return '<dl><dt>%s</dt>%s</dl>\n' % (decl, doc) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 811 | |
Tim Peters | 8dd7ade | 2001-10-18 19:56:17 +0000 | [diff] [blame] | 812 | def docother(self, object, name=None, mod=None, *ignored): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 813 | """Produce HTML documentation for a data object.""" |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 814 | lhs = name and '<strong>%s</strong> = ' % name or '' |
| 815 | return lhs + self.repr(object) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 816 | |
| 817 | def index(self, dir, shadowed=None): |
| 818 | """Generate an HTML index for a directory of modules.""" |
| 819 | modpkgs = [] |
| 820 | if shadowed is None: shadowed = {} |
| 821 | seen = {} |
| 822 | files = os.listdir(dir) |
| 823 | |
| 824 | def found(name, ispackage, |
| 825 | modpkgs=modpkgs, shadowed=shadowed, seen=seen): |
Raymond Hettinger | 4f759d8 | 2002-11-02 02:02:46 +0000 | [diff] [blame] | 826 | if name not in seen: |
| 827 | modpkgs.append((name, '', ispackage, name in shadowed)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 828 | seen[name] = 1 |
| 829 | shadowed[name] = 1 |
| 830 | |
| 831 | # Package spam/__init__.py takes precedence over module spam.py. |
| 832 | for file in files: |
| 833 | path = os.path.join(dir, file) |
| 834 | if ispackage(path): found(file, 1) |
| 835 | for file in files: |
| 836 | path = os.path.join(dir, file) |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 837 | if os.path.isfile(path): |
| 838 | modname = inspect.getmodulename(file) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 839 | if modname: found(modname, 0) |
| 840 | |
| 841 | modpkgs.sort() |
| 842 | contents = self.multicolumn(modpkgs, self.modpkglink) |
| 843 | return self.bigsection(dir, '#ffffff', '#ee77aa', contents) |
| 844 | |
| 845 | # -------------------------------------------- text documentation generator |
| 846 | |
| 847 | class TextRepr(Repr): |
| 848 | """Class for safely making a text representation of a Python object.""" |
| 849 | def __init__(self): |
| 850 | Repr.__init__(self) |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 851 | self.maxlist = self.maxtuple = 20 |
| 852 | self.maxdict = 10 |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 853 | self.maxstring = self.maxother = 100 |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 854 | |
| 855 | def repr1(self, x, level): |
| 856 | methodname = 'repr_' + join(split(type(x).__name__), '_') |
| 857 | if hasattr(self, methodname): |
| 858 | return getattr(self, methodname)(x, level) |
| 859 | else: |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 860 | return cram(stripid(repr(x)), self.maxother) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 861 | |
Ka-Ping Yee | a2fe103 | 2001-03-02 01:19:14 +0000 | [diff] [blame] | 862 | def repr_string(self, x, level): |
| 863 | test = cram(x, self.maxstring) |
| 864 | testrepr = repr(test) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 865 | if '\\' in test and '\\' not in replace(testrepr, r'\\', ''): |
Ka-Ping Yee | a2fe103 | 2001-03-02 01:19:14 +0000 | [diff] [blame] | 866 | # Backslashes are only literal in the string and are never |
| 867 | # needed to make any special characters, so show a raw string. |
| 868 | return 'r' + testrepr[0] + test + testrepr[0] |
| 869 | return testrepr |
| 870 | |
Skip Montanaro | df70878 | 2002-03-07 22:58:02 +0000 | [diff] [blame] | 871 | repr_str = repr_string |
| 872 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 873 | def repr_instance(self, x, level): |
| 874 | try: |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 875 | return cram(stripid(repr(x)), self.maxstring) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 876 | except: |
| 877 | return '<%s instance>' % x.__class__.__name__ |
| 878 | |
| 879 | class TextDoc(Doc): |
| 880 | """Formatter class for text documentation.""" |
| 881 | |
| 882 | # ------------------------------------------- text formatting utilities |
| 883 | |
| 884 | _repr_instance = TextRepr() |
| 885 | repr = _repr_instance.repr |
| 886 | |
| 887 | def bold(self, text): |
| 888 | """Format a string in bold by overstriking.""" |
| 889 | return join(map(lambda ch: ch + '\b' + ch, text), '') |
| 890 | |
| 891 | def indent(self, text, prefix=' '): |
| 892 | """Indent text by prepending a given prefix to each line.""" |
| 893 | if not text: return '' |
| 894 | lines = split(text, '\n') |
| 895 | lines = map(lambda line, prefix=prefix: prefix + line, lines) |
| 896 | if lines: lines[-1] = rstrip(lines[-1]) |
| 897 | return join(lines, '\n') |
| 898 | |
| 899 | def section(self, title, contents): |
| 900 | """Format a section with a given heading.""" |
| 901 | return self.bold(title) + '\n' + rstrip(self.indent(contents)) + '\n\n' |
| 902 | |
| 903 | # ---------------------------------------------- type-specific routines |
| 904 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 905 | def formattree(self, tree, modname, parent=None, prefix=''): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 906 | """Render in text a class tree as returned by inspect.getclasstree().""" |
| 907 | result = '' |
| 908 | for entry in tree: |
| 909 | if type(entry) is type(()): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 910 | c, bases = entry |
| 911 | result = result + prefix + classname(c, modname) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 912 | if bases and bases != (parent,): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 913 | parents = map(lambda c, m=modname: classname(c, m), bases) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 914 | result = result + '(%s)' % join(parents, ', ') |
| 915 | result = result + '\n' |
| 916 | elif type(entry) is type([]): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 917 | result = result + self.formattree( |
| 918 | entry, modname, c, prefix + ' ') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 919 | return result |
| 920 | |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 921 | def docmodule(self, object, name=None, mod=None): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 922 | """Produce text documentation for a given module object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 923 | name = object.__name__ # ignore the passed-in name |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 924 | synop, desc = splitdoc(getdoc(object)) |
| 925 | result = self.section('NAME', name + (synop and ' - ' + synop)) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 926 | |
| 927 | try: |
| 928 | file = inspect.getabsfile(object) |
| 929 | except TypeError: |
| 930 | file = '(built-in)' |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 931 | result = result + self.section('FILE', file) |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 932 | if desc: |
| 933 | result = result + self.section('DESCRIPTION', desc) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 934 | |
| 935 | classes = [] |
| 936 | for key, value in inspect.getmembers(object, inspect.isclass): |
| 937 | if (inspect.getmodule(value) or object) is object: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 938 | classes.append((key, value)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 939 | funcs = [] |
| 940 | for key, value in inspect.getmembers(object, inspect.isroutine): |
| 941 | if inspect.isbuiltin(value) or inspect.getmodule(value) is object: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 942 | funcs.append((key, value)) |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 943 | data = [] |
| 944 | for key, value in inspect.getmembers(object, isdata): |
| 945 | if key not in ['__builtins__', '__doc__']: |
| 946 | data.append((key, value)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 947 | |
| 948 | if hasattr(object, '__path__'): |
| 949 | modpkgs = [] |
| 950 | for file in os.listdir(object.__path__[0]): |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 951 | path = os.path.join(object.__path__[0], file) |
| 952 | modname = inspect.getmodulename(file) |
| 953 | if modname and modname not in modpkgs: |
| 954 | modpkgs.append(modname) |
| 955 | elif ispackage(path): |
| 956 | modpkgs.append(file + ' (package)') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 957 | modpkgs.sort() |
| 958 | result = result + self.section( |
| 959 | 'PACKAGE CONTENTS', join(modpkgs, '\n')) |
| 960 | |
| 961 | if classes: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 962 | classlist = map(lambda (key, value): value, classes) |
| 963 | contents = [self.formattree( |
| 964 | inspect.getclasstree(classlist, 1), name)] |
| 965 | for key, value in classes: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 966 | contents.append(self.document(value, key, name)) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 967 | result = result + self.section('CLASSES', join(contents, '\n')) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 968 | |
| 969 | if funcs: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 970 | contents = [] |
| 971 | for key, value in funcs: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 972 | contents.append(self.document(value, key, name)) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 973 | result = result + self.section('FUNCTIONS', join(contents, '\n')) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 974 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 975 | if data: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 976 | contents = [] |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 977 | for key, value in data: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 978 | contents.append(self.docother(value, key, name, 70)) |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 979 | result = result + self.section('DATA', join(contents, '\n')) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 980 | |
| 981 | if hasattr(object, '__version__'): |
| 982 | version = str(object.__version__) |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 983 | if version[:11] == '$' + 'Revision: ' and version[-1:] == '$': |
| 984 | version = strip(version[11:-1]) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 985 | result = result + self.section('VERSION', version) |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 986 | if hasattr(object, '__date__'): |
| 987 | result = result + self.section('DATE', str(object.__date__)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 988 | if hasattr(object, '__author__'): |
Ka-Ping Yee | 6f3f9a4 | 2001-02-27 22:42:36 +0000 | [diff] [blame] | 989 | result = result + self.section('AUTHOR', str(object.__author__)) |
| 990 | if hasattr(object, '__credits__'): |
| 991 | result = result + self.section('CREDITS', str(object.__credits__)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 992 | return result |
| 993 | |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 994 | def docclass(self, object, name=None, mod=None): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 995 | """Produce text documentation for a given class object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 996 | realname = object.__name__ |
| 997 | name = name or realname |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 998 | bases = object.__bases__ |
| 999 | |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 1000 | def makename(c, m=object.__module__): |
| 1001 | return classname(c, m) |
| 1002 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1003 | if name == realname: |
| 1004 | title = 'class ' + self.bold(realname) |
| 1005 | else: |
| 1006 | title = self.bold(name) + ' = class ' + realname |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1007 | if bases: |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1008 | parents = map(makename, bases) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1009 | title = title + '(%s)' % join(parents, ', ') |
| 1010 | |
| 1011 | doc = getdoc(object) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1012 | contents = doc and [doc + '\n'] or [] |
| 1013 | push = contents.append |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1014 | |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 1015 | # List the mro, if non-trivial. |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 1016 | mro = list(inspect.getmro(object)) |
Tim Peters | c86f6ca | 2001-09-26 21:31:51 +0000 | [diff] [blame] | 1017 | if len(mro) > 2: |
| 1018 | push("Method resolution order:") |
| 1019 | for base in mro: |
| 1020 | push(' ' + makename(base)) |
| 1021 | push('') |
| 1022 | |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1023 | # Cute little class to pump out a horizontal rule between sections. |
| 1024 | class HorizontalRule: |
| 1025 | def __init__(self): |
| 1026 | self.needone = 0 |
| 1027 | def maybe(self): |
| 1028 | if self.needone: |
| 1029 | push('-' * 70) |
| 1030 | self.needone = 1 |
| 1031 | hr = HorizontalRule() |
| 1032 | |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1033 | def spill(msg, attrs, predicate): |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1034 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1035 | if ok: |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1036 | hr.maybe() |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1037 | push(msg) |
| 1038 | for name, kind, homecls, value in ok: |
| 1039 | push(self.document(getattr(object, name), |
| 1040 | name, mod, object)) |
| 1041 | return attrs |
| 1042 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1043 | def spillproperties(msg, attrs, predicate): |
| 1044 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1045 | if ok: |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1046 | hr.maybe() |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1047 | push(msg) |
| 1048 | for name, kind, homecls, value in ok: |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1049 | push(name) |
| 1050 | need_blank_after_doc = 0 |
| 1051 | doc = getdoc(value) or '' |
| 1052 | if doc: |
| 1053 | push(self.indent(doc)) |
| 1054 | need_blank_after_doc = 1 |
Tim Peters | f33532c | 2001-09-25 06:30:51 +0000 | [diff] [blame] | 1055 | for attr, tag in [("fget", " getter"), |
| 1056 | ("fset", " setter"), |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1057 | ("fdel", " deleter")]: |
| 1058 | func = getattr(value, attr) |
| 1059 | if func is not None: |
| 1060 | if need_blank_after_doc: |
| 1061 | push('') |
| 1062 | need_blank_after_doc = 0 |
| 1063 | base = self.docother(func, name + tag, mod, 70) |
| 1064 | push(self.indent(base)) |
| 1065 | push('') |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1066 | return attrs |
Tim Peters | b47879b | 2001-09-24 04:47:19 +0000 | [diff] [blame] | 1067 | |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1068 | def spilldata(msg, attrs, predicate): |
| 1069 | ok, attrs = _split_list(attrs, predicate) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1070 | if ok: |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1071 | hr.maybe() |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1072 | push(msg) |
| 1073 | for name, kind, homecls, value in ok: |
Guido van Rossum | 5e355b2 | 2002-05-21 20:56:15 +0000 | [diff] [blame] | 1074 | if callable(value): |
| 1075 | doc = getattr(value, "__doc__", None) |
| 1076 | else: |
| 1077 | doc = None |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1078 | push(self.docother(getattr(object, name), |
| 1079 | name, mod, 70, doc) + '\n') |
| 1080 | return attrs |
| 1081 | |
| 1082 | attrs = inspect.classify_class_attrs(object) |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1083 | while attrs: |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 1084 | if mro: |
| 1085 | thisclass = mro.pop(0) |
| 1086 | else: |
| 1087 | thisclass = attrs[0][2] |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1088 | attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass) |
| 1089 | |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1090 | if thisclass is object: |
| 1091 | tag = "defined here" |
| 1092 | else: |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1093 | tag = "inherited from %s" % classname(thisclass, |
| 1094 | object.__module__) |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1095 | |
| 1096 | # Sort attrs by name. |
| 1097 | attrs.sort(lambda t1, t2: cmp(t1[0], t2[0])) |
| 1098 | |
| 1099 | # Pump out the attrs, segregated by kind. |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1100 | attrs = spill("Methods %s:\n" % tag, attrs, |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1101 | lambda t: t[1] == 'method') |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1102 | attrs = spill("Class methods %s:\n" % tag, attrs, |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1103 | lambda t: t[1] == 'class method') |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1104 | attrs = spill("Static methods %s:\n" % tag, attrs, |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1105 | lambda t: t[1] == 'static method') |
Tim Peters | f4aad8e | 2001-09-24 22:40:47 +0000 | [diff] [blame] | 1106 | attrs = spillproperties("Properties %s:\n" % tag, attrs, |
Tim Peters | fa26f7c | 2001-09-24 08:05:11 +0000 | [diff] [blame] | 1107 | lambda t: t[1] == 'property') |
Tim Peters | f33532c | 2001-09-25 06:30:51 +0000 | [diff] [blame] | 1108 | attrs = spilldata("Data and non-method functions %s:\n" % tag, |
| 1109 | attrs, lambda t: t[1] == 'data') |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1110 | assert attrs == [] |
Tim Peters | 351e362 | 2001-09-27 03:29:51 +0000 | [diff] [blame] | 1111 | attrs = inherited |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1112 | |
| 1113 | contents = '\n'.join(contents) |
| 1114 | if not contents: |
| 1115 | return title + '\n' |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1116 | return title + '\n' + self.indent(rstrip(contents), ' | ') + '\n' |
| 1117 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1118 | def formatvalue(self, object): |
| 1119 | """Format an argument default value as text.""" |
| 1120 | return '=' + self.repr(object) |
| 1121 | |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1122 | def docroutine(self, object, name=None, mod=None, cl=None): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1123 | """Produce text documentation for a function or method object.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1124 | realname = object.__name__ |
| 1125 | name = name or realname |
| 1126 | note = '' |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1127 | skipdocs = 0 |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1128 | if inspect.ismethod(object): |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1129 | imclass = object.im_class |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1130 | if cl: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1131 | if imclass is not cl: |
| 1132 | note = ' from ' + classname(imclass, mod) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1133 | else: |
Ka-Ping Yee | b7a4830 | 2001-04-12 20:39:14 +0000 | [diff] [blame] | 1134 | if object.im_self: |
| 1135 | note = ' method of %s instance' % classname( |
| 1136 | object.im_self.__class__, mod) |
| 1137 | else: |
| 1138 | note = ' unbound %s method' % classname(imclass,mod) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1139 | object = object.im_func |
| 1140 | |
| 1141 | if name == realname: |
| 1142 | title = self.bold(realname) |
| 1143 | else: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1144 | if (cl and realname in cl.__dict__ and |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1145 | cl.__dict__[realname] is object): |
| 1146 | skipdocs = 1 |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1147 | title = self.bold(name) + ' = ' + realname |
Tim Peters | 4bcfa31 | 2001-09-20 06:08:24 +0000 | [diff] [blame] | 1148 | if inspect.isfunction(object): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1149 | args, varargs, varkw, defaults = inspect.getargspec(object) |
| 1150 | argspec = inspect.formatargspec( |
| 1151 | args, varargs, varkw, defaults, formatvalue=self.formatvalue) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1152 | if realname == '<lambda>': |
| 1153 | title = 'lambda' |
| 1154 | argspec = argspec[1:-1] # remove parentheses |
Tim Peters | 4bcfa31 | 2001-09-20 06:08:24 +0000 | [diff] [blame] | 1155 | else: |
| 1156 | argspec = '(...)' |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1157 | decl = title + argspec + note |
| 1158 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1159 | if skipdocs: |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1160 | return decl + '\n' |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1161 | else: |
| 1162 | doc = getdoc(object) or '' |
| 1163 | return decl + '\n' + (doc and rstrip(self.indent(doc)) + '\n') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1164 | |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1165 | def docother(self, object, name=None, mod=None, maxlen=None, doc=None): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1166 | """Produce text documentation for a data object.""" |
| 1167 | repr = self.repr(object) |
| 1168 | if maxlen: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1169 | line = (name and name + ' = ' or '') + repr |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1170 | chop = maxlen - len(line) |
| 1171 | if chop < 0: repr = repr[:chop] + '...' |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1172 | line = (name and self.bold(name) + ' = ' or '') + repr |
Tim Peters | 2835549 | 2001-09-23 21:29:55 +0000 | [diff] [blame] | 1173 | if doc is not None: |
| 1174 | line += '\n' + self.indent(str(doc)) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1175 | return line |
| 1176 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1177 | # --------------------------------------------------------- user interfaces |
| 1178 | |
| 1179 | def pager(text): |
| 1180 | """The first time this is called, determine what kind of pager to use.""" |
| 1181 | global pager |
| 1182 | pager = getpager() |
| 1183 | pager(text) |
| 1184 | |
| 1185 | def getpager(): |
| 1186 | """Decide what method to use for paging through text.""" |
| 1187 | if type(sys.stdout) is not types.FileType: |
| 1188 | return plainpager |
| 1189 | if not sys.stdin.isatty() or not sys.stdout.isatty(): |
| 1190 | return plainpager |
Fred Drake | 0a66fcb | 2001-07-23 19:44:30 +0000 | [diff] [blame] | 1191 | if os.environ.get('TERM') in ['dumb', 'emacs']: |
Fred Drake | 5e9eb98 | 2001-07-23 19:48:10 +0000 | [diff] [blame] | 1192 | return plainpager |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1193 | if 'PAGER' in os.environ: |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1194 | if sys.platform == 'win32': # pipes completely broken in Windows |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1195 | return lambda text: tempfilepager(plain(text), os.environ['PAGER']) |
| 1196 | elif os.environ.get('TERM') in ['dumb', 'emacs']: |
| 1197 | return lambda text: pipepager(plain(text), os.environ['PAGER']) |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1198 | else: |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1199 | return lambda text: pipepager(text, os.environ['PAGER']) |
Andrew MacIntyre | 54e0eab | 2002-03-03 03:12:30 +0000 | [diff] [blame] | 1200 | if sys.platform == 'win32' or sys.platform.startswith('os2'): |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1201 | return lambda text: tempfilepager(plain(text), 'more <') |
Skip Montanaro | d404bee | 2002-09-26 21:44:57 +0000 | [diff] [blame] | 1202 | 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] | 1203 | return lambda text: pipepager(text, 'less') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1204 | |
| 1205 | import tempfile |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1206 | (fd, filename) = tempfile.mkstemp() |
| 1207 | os.close(fd) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1208 | try: |
| 1209 | if hasattr(os, 'system') and os.system('more %s' % filename) == 0: |
| 1210 | return lambda text: pipepager(text, 'more') |
| 1211 | else: |
| 1212 | return ttypager |
| 1213 | finally: |
| 1214 | os.unlink(filename) |
| 1215 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1216 | def plain(text): |
| 1217 | """Remove boldface formatting from text.""" |
| 1218 | return re.sub('.\b', '', text) |
| 1219 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1220 | def pipepager(text, cmd): |
| 1221 | """Page through text by feeding it to another program.""" |
| 1222 | pipe = os.popen(cmd, 'w') |
| 1223 | try: |
| 1224 | pipe.write(text) |
| 1225 | pipe.close() |
| 1226 | except IOError: |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1227 | pass # Ignore broken pipes caused by quitting the pager program. |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1228 | |
| 1229 | def tempfilepager(text, cmd): |
| 1230 | """Page through text by invoking a program on a temporary file.""" |
| 1231 | import tempfile |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1232 | (fd, filename) = tempfile.mkstemp() |
| 1233 | file = os.fdopen(fd, 'w') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1234 | file.write(text) |
| 1235 | file.close() |
| 1236 | try: |
Ka-Ping Yee | c92cdf7 | 2001-03-02 05:54:35 +0000 | [diff] [blame] | 1237 | os.system(cmd + ' ' + filename) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1238 | finally: |
| 1239 | os.unlink(filename) |
| 1240 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1241 | def ttypager(text): |
| 1242 | """Page through text on a text terminal.""" |
| 1243 | lines = split(plain(text), '\n') |
| 1244 | try: |
| 1245 | import tty |
| 1246 | fd = sys.stdin.fileno() |
| 1247 | old = tty.tcgetattr(fd) |
| 1248 | tty.setcbreak(fd) |
| 1249 | getchar = lambda: sys.stdin.read(1) |
Ka-Ping Yee | 457aab2 | 2001-02-27 23:36:29 +0000 | [diff] [blame] | 1250 | except (ImportError, AttributeError): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1251 | tty = None |
| 1252 | getchar = lambda: sys.stdin.readline()[:-1][:1] |
| 1253 | |
| 1254 | try: |
| 1255 | r = inc = os.environ.get('LINES', 25) - 1 |
| 1256 | sys.stdout.write(join(lines[:inc], '\n') + '\n') |
| 1257 | while lines[r:]: |
| 1258 | sys.stdout.write('-- more --') |
| 1259 | sys.stdout.flush() |
| 1260 | c = getchar() |
| 1261 | |
| 1262 | if c in ['q', 'Q']: |
| 1263 | sys.stdout.write('\r \r') |
| 1264 | break |
| 1265 | elif c in ['\r', '\n']: |
| 1266 | sys.stdout.write('\r \r' + lines[r] + '\n') |
| 1267 | r = r + 1 |
| 1268 | continue |
| 1269 | if c in ['b', 'B', '\x1b']: |
| 1270 | r = r - inc - inc |
| 1271 | if r < 0: r = 0 |
| 1272 | sys.stdout.write('\n' + join(lines[r:r+inc], '\n') + '\n') |
| 1273 | r = r + inc |
| 1274 | |
| 1275 | finally: |
| 1276 | if tty: |
| 1277 | tty.tcsetattr(fd, tty.TCSAFLUSH, old) |
| 1278 | |
| 1279 | def plainpager(text): |
| 1280 | """Simply print unformatted text. This is the ultimate fallback.""" |
| 1281 | sys.stdout.write(plain(text)) |
| 1282 | |
| 1283 | def describe(thing): |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1284 | """Produce a short description of the given thing.""" |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1285 | if inspect.ismodule(thing): |
| 1286 | if thing.__name__ in sys.builtin_module_names: |
| 1287 | return 'built-in module ' + thing.__name__ |
| 1288 | if hasattr(thing, '__path__'): |
| 1289 | return 'package ' + thing.__name__ |
| 1290 | else: |
| 1291 | return 'module ' + thing.__name__ |
| 1292 | if inspect.isbuiltin(thing): |
| 1293 | return 'built-in function ' + thing.__name__ |
| 1294 | if inspect.isclass(thing): |
| 1295 | return 'class ' + thing.__name__ |
| 1296 | if inspect.isfunction(thing): |
| 1297 | return 'function ' + thing.__name__ |
| 1298 | if inspect.ismethod(thing): |
| 1299 | return 'method ' + thing.__name__ |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1300 | if type(thing) is types.InstanceType: |
| 1301 | return 'instance of ' + thing.__class__.__name__ |
| 1302 | return type(thing).__name__ |
| 1303 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1304 | def locate(path, forceload=0): |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1305 | """Locate an object by name or dotted path, importing as necessary.""" |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1306 | parts = split(path, '.') |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1307 | module, n = None, 0 |
| 1308 | while n < len(parts): |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1309 | nextmodule = safeimport(join(parts[:n+1], '.'), forceload) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1310 | if nextmodule: module, n = nextmodule, n + 1 |
| 1311 | else: break |
| 1312 | if module: |
| 1313 | object = module |
| 1314 | for part in parts[n:]: |
| 1315 | try: object = getattr(object, part) |
| 1316 | except AttributeError: return None |
| 1317 | return object |
| 1318 | else: |
| 1319 | import __builtin__ |
| 1320 | if hasattr(__builtin__, path): |
| 1321 | return getattr(__builtin__, path) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1322 | |
| 1323 | # --------------------------------------- interactive interpreter interface |
| 1324 | |
| 1325 | text = TextDoc() |
| 1326 | html = HTMLDoc() |
| 1327 | |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 1328 | def resolve(thing, forceload=0): |
| 1329 | """Given an object or a path to an object, get the object and its name.""" |
| 1330 | if isinstance(thing, str): |
| 1331 | object = locate(thing, forceload) |
| 1332 | if not object: |
| 1333 | raise ImportError, 'no Python documentation found for %r' % thing |
| 1334 | return object, thing |
| 1335 | else: |
| 1336 | return thing, getattr(thing, '__name__', None) |
| 1337 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1338 | def doc(thing, title='Python Library Documentation: %s', forceload=0): |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1339 | """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] | 1340 | try: |
| 1341 | object, name = resolve(thing, forceload) |
| 1342 | desc = describe(object) |
| 1343 | module = inspect.getmodule(object) |
| 1344 | if name and '.' in name: |
| 1345 | desc += ' in ' + name[:name.rfind('.')] |
| 1346 | elif module and module is not object: |
| 1347 | desc += ' in module ' + module.__name__ |
| 1348 | pager(title % desc + '\n\n' + text.document(object, name)) |
| 1349 | except (ImportError, ErrorDuringImport), value: |
| 1350 | print value |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1351 | |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 1352 | def writedoc(thing, forceload=0): |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1353 | """Write HTML documentation to a file in the current directory.""" |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1354 | try: |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 1355 | object, name = resolve(thing, forceload) |
| 1356 | page = html.page(describe(object), html.document(object, name)) |
| 1357 | file = open(name + '.html', 'w') |
| 1358 | file.write(page) |
| 1359 | file.close() |
| 1360 | print 'wrote', name + '.html' |
| 1361 | except (ImportError, ErrorDuringImport), value: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1362 | print value |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1363 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1364 | def writedocs(dir, pkgpath='', done=None): |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1365 | """Write out HTML documentation for all modules in a directory tree.""" |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1366 | if done is None: done = {} |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1367 | for file in os.listdir(dir): |
| 1368 | path = os.path.join(dir, file) |
| 1369 | if ispackage(path): |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1370 | writedocs(path, pkgpath + file + '.', done) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1371 | elif os.path.isfile(path): |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1372 | modname = inspect.getmodulename(path) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1373 | if modname: |
| 1374 | modname = pkgpath + modname |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1375 | if not modname in done: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1376 | done[modname] = 1 |
| 1377 | writedoc(modname) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1378 | |
| 1379 | class Helper: |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1380 | keywords = { |
| 1381 | 'and': 'BOOLEAN', |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1382 | 'assert': ('ref/assert', ''), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1383 | 'break': ('ref/break', 'while for'), |
| 1384 | 'class': ('ref/class', 'CLASSES SPECIALMETHODS'), |
| 1385 | 'continue': ('ref/continue', 'while for'), |
| 1386 | 'def': ('ref/function', ''), |
| 1387 | 'del': ('ref/del', 'BASICMETHODS'), |
| 1388 | 'elif': 'if', |
| 1389 | 'else': ('ref/if', 'while for'), |
| 1390 | 'except': 'try', |
| 1391 | 'exec': ('ref/exec', ''), |
| 1392 | 'finally': 'try', |
| 1393 | 'for': ('ref/for', 'break continue while'), |
| 1394 | 'from': 'import', |
| 1395 | 'global': ('ref/global', 'NAMESPACES'), |
| 1396 | 'if': ('ref/if', 'TRUTHVALUE'), |
| 1397 | 'import': ('ref/import', 'MODULES'), |
| 1398 | 'in': ('ref/comparisons', 'SEQUENCEMETHODS2'), |
| 1399 | 'is': 'COMPARISON', |
| 1400 | 'lambda': ('ref/lambda', 'FUNCTIONS'), |
| 1401 | 'not': 'BOOLEAN', |
| 1402 | 'or': 'BOOLEAN', |
| 1403 | 'pass': 'PASS', |
| 1404 | 'print': ('ref/print', ''), |
| 1405 | 'raise': ('ref/raise', 'EXCEPTIONS'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1406 | 'return': ('ref/return', 'FUNCTIONS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1407 | 'try': ('ref/try', 'EXCEPTIONS'), |
| 1408 | 'while': ('ref/while', 'break continue if TRUTHVALUE'), |
Tim Peters | fb05c4e | 2002-10-30 05:21:00 +0000 | [diff] [blame] | 1409 | 'yield': ('ref/yield', ''), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | topics = { |
| 1413 | 'TYPES': ('ref/types', 'STRINGS UNICODE NUMBERS SEQUENCES MAPPINGS FUNCTIONS CLASSES MODULES FILES inspect'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1414 | 'STRINGS': ('ref/strings', 'str UNICODE SEQUENCES STRINGMETHODS FORMATTING TYPES'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1415 | 'STRINGMETHODS': ('lib/string-methods', 'STRINGS FORMATTING'), |
| 1416 | 'FORMATTING': ('lib/typesseq-strings', 'OPERATORS'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1417 | 'UNICODE': ('ref/unicode', 'encodings unicode TYPES STRING'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1418 | 'NUMBERS': ('ref/numbers', 'INTEGER FLOAT COMPLEX TYPES'), |
| 1419 | 'INTEGER': ('ref/integers', 'int range'), |
| 1420 | 'FLOAT': ('ref/floating', 'float math'), |
| 1421 | 'COMPLEX': ('ref/imaginary', 'complex cmath'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1422 | 'SEQUENCES': ('lib/typesseq', 'STRINGMETHODS FORMATTING xrange LISTS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1423 | 'MAPPINGS': 'DICTIONARIES', |
| 1424 | 'FUNCTIONS': ('lib/typesfunctions', 'def TYPES'), |
| 1425 | 'METHODS': ('lib/typesmethods', 'class def CLASSES TYPES'), |
| 1426 | 'CODEOBJECTS': ('lib/bltin-code-objects', 'compile FUNCTIONS TYPES'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1427 | 'TYPEOBJECTS': ('lib/bltin-type-objects', 'types TYPES'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1428 | 'FRAMEOBJECTS': 'TYPES', |
| 1429 | 'TRACEBACKS': 'TYPES', |
| 1430 | 'NONE': ('lib/bltin-null-object', ''), |
| 1431 | 'ELLIPSIS': ('lib/bltin-ellipsis-object', 'SLICINGS'), |
| 1432 | 'FILES': ('lib/bltin-file-objects', ''), |
| 1433 | 'SPECIALATTRIBUTES': ('lib/specialattrs', ''), |
| 1434 | 'CLASSES': ('ref/types', 'class SPECIALMETHODS PRIVATENAMES'), |
| 1435 | 'MODULES': ('lib/typesmodules', 'import'), |
| 1436 | 'PACKAGES': 'import', |
| 1437 | 'EXPRESSIONS': ('ref/summary', 'lambda or and not in is BOOLEAN COMPARISON BITWISE SHIFTING BINARY FORMATTING POWER UNARY ATTRIBUTES SUBSCRIPTS SLICINGS CALLS TUPLES LISTS DICTIONARIES BACKQUOTES'), |
| 1438 | 'OPERATORS': 'EXPRESSIONS', |
| 1439 | 'PRECEDENCE': 'EXPRESSIONS', |
| 1440 | 'OBJECTS': ('ref/objects', 'TYPES'), |
| 1441 | 'SPECIALMETHODS': ('ref/specialnames', 'BASICMETHODS ATTRIBUTEMETHODS CALLABLEMETHODS SEQUENCEMETHODS1 MAPPINGMETHODS SEQUENCEMETHODS2 NUMBERMETHODS CLASSES'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1442 | 'BASICMETHODS': ('ref/customization', 'cmp hash repr str SPECIALMETHODS'), |
| 1443 | 'ATTRIBUTEMETHODS': ('ref/attribute-access', 'ATTRIBUTES SPECIALMETHODS'), |
| 1444 | 'CALLABLEMETHODS': ('ref/callable-types', 'CALLS SPECIALMETHODS'), |
| 1445 | 'SEQUENCEMETHODS1': ('ref/sequence-types', 'SEQUENCES SEQUENCEMETHODS2 SPECIALMETHODS'), |
| 1446 | 'SEQUENCEMETHODS2': ('ref/sequence-methods', 'SEQUENCES SEQUENCEMETHODS1 SPECIALMETHODS'), |
| 1447 | 'MAPPINGMETHODS': ('ref/sequence-types', 'MAPPINGS SPECIALMETHODS'), |
| 1448 | 'NUMBERMETHODS': ('ref/numeric-types', 'NUMBERS AUGMENTEDASSIGNMENT SPECIALMETHODS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1449 | 'EXECUTION': ('ref/execframes', ''), |
| 1450 | 'NAMESPACES': ('ref/execframes', 'global ASSIGNMENT DELETION'), |
| 1451 | 'SCOPING': 'NAMESPACES', |
| 1452 | 'FRAMES': 'NAMESPACES', |
| 1453 | 'EXCEPTIONS': ('ref/exceptions', 'try except finally raise'), |
| 1454 | 'COERCIONS': 'CONVERSIONS', |
| 1455 | 'CONVERSIONS': ('ref/conversions', ''), |
| 1456 | 'IDENTIFIERS': ('ref/identifiers', 'keywords SPECIALIDENTIFIERS'), |
| 1457 | 'SPECIALIDENTIFIERS': ('ref/id-classes', ''), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1458 | 'PRIVATENAMES': ('ref/atom-identifiers', ''), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1459 | 'LITERALS': ('ref/atom-literals', 'STRINGS BACKQUOTES NUMBERS TUPLELITERALS LISTLITERALS DICTIONARYLITERALS'), |
| 1460 | 'TUPLES': 'SEQUENCES', |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1461 | 'TUPLELITERALS': ('ref/exprlists', 'TUPLES LITERALS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1462 | 'LISTS': ('lib/typesseq-mutable', 'LISTLITERALS'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1463 | 'LISTLITERALS': ('ref/lists', 'LISTS LITERALS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1464 | 'DICTIONARIES': ('lib/typesmapping', 'DICTIONARYLITERALS'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1465 | 'DICTIONARYLITERALS': ('ref/dict', 'DICTIONARIES LITERALS'), |
| 1466 | 'BACKQUOTES': ('ref/string-conversions', 'repr str STRINGS LITERALS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1467 | 'ATTRIBUTES': ('ref/attribute-references', 'getattr hasattr setattr ATTRIBUTEMETHODS'), |
| 1468 | 'SUBSCRIPTS': ('ref/subscriptions', 'SEQUENCEMETHODS1'), |
| 1469 | 'SLICINGS': ('ref/slicings', 'SEQUENCEMETHODS2'), |
| 1470 | 'CALLS': ('ref/calls', 'EXPRESSIONS'), |
| 1471 | 'POWER': ('ref/power', 'EXPRESSIONS'), |
| 1472 | 'UNARY': ('ref/unary', 'EXPRESSIONS'), |
| 1473 | 'BINARY': ('ref/binary', 'EXPRESSIONS'), |
| 1474 | 'SHIFTING': ('ref/shifting', 'EXPRESSIONS'), |
| 1475 | 'BITWISE': ('ref/bitwise', 'EXPRESSIONS'), |
| 1476 | 'COMPARISON': ('ref/comparisons', 'EXPRESSIONS BASICMETHODS'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1477 | 'BOOLEAN': ('ref/lambda', 'EXPRESSIONS TRUTHVALUE'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1478 | 'ASSERTION': 'assert', |
| 1479 | 'ASSIGNMENT': ('ref/assignment', 'AUGMENTEDASSIGNMENT'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1480 | 'AUGMENTEDASSIGNMENT': ('ref/augassign', 'NUMBERMETHODS'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1481 | 'DELETION': 'del', |
| 1482 | 'PRINTING': 'print', |
| 1483 | 'RETURNING': 'return', |
| 1484 | 'IMPORTING': 'import', |
| 1485 | 'CONDITIONAL': 'if', |
| 1486 | 'LOOPING': ('ref/compound', 'for while break continue'), |
| 1487 | 'TRUTHVALUE': ('lib/truth', 'if while and or not BASICMETHODS'), |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1488 | 'DEBUGGING': ('lib/module-pdb', 'pdb'), |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | def __init__(self, input, output): |
| 1492 | self.input = input |
| 1493 | self.output = output |
| 1494 | self.docdir = None |
| 1495 | execdir = os.path.dirname(sys.executable) |
| 1496 | homedir = os.environ.get('PYTHONHOME') |
| 1497 | for dir in [os.environ.get('PYTHONDOCS'), |
| 1498 | homedir and os.path.join(homedir, 'doc'), |
| 1499 | os.path.join(execdir, 'doc'), |
| 1500 | '/usr/doc/python-docs-' + split(sys.version)[0], |
| 1501 | '/usr/doc/python-' + split(sys.version)[0], |
| 1502 | '/usr/doc/python-docs-' + sys.version[:3], |
Jack Jansen | b2628b0 | 2002-08-23 08:40:42 +0000 | [diff] [blame] | 1503 | '/usr/doc/python-' + sys.version[:3], |
| 1504 | os.path.join(sys.prefix, 'Resources/English.lproj/Documentation')]: |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1505 | if dir and os.path.isdir(os.path.join(dir, 'lib')): |
| 1506 | self.docdir = dir |
| 1507 | |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1508 | def __repr__(self): |
Ka-Ping Yee | 9bc576b | 2001-04-13 13:57:31 +0000 | [diff] [blame] | 1509 | if inspect.stack()[1][3] == '?': |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1510 | self() |
| 1511 | return '' |
Ka-Ping Yee | 9bc576b | 2001-04-13 13:57:31 +0000 | [diff] [blame] | 1512 | return '<pydoc.Helper instance>' |
Ka-Ping Yee | 79c009d | 2001-04-13 10:53:25 +0000 | [diff] [blame] | 1513 | |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1514 | def __call__(self, request=None): |
| 1515 | if request is not None: |
| 1516 | self.help(request) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1517 | else: |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1518 | self.intro() |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1519 | self.interact() |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1520 | self.output.write(''' |
Fred Drake | e61967f | 2001-05-10 18:41:02 +0000 | [diff] [blame] | 1521 | You are now leaving help and returning to the Python interpreter. |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1522 | If you want to ask for help on a particular object directly from the |
| 1523 | interpreter, you can type "help(object)". Executing "help('string')" |
| 1524 | has the same effect as typing a particular string at the help> prompt. |
| 1525 | ''') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1526 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1527 | def interact(self): |
| 1528 | self.output.write('\n') |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 1529 | while True: |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1530 | self.output.write('help> ') |
| 1531 | self.output.flush() |
| 1532 | try: |
| 1533 | request = self.input.readline() |
| 1534 | if not request: break |
| 1535 | except KeyboardInterrupt: break |
| 1536 | request = strip(replace(request, '"', '', "'", '')) |
| 1537 | if lower(request) in ['q', 'quit']: break |
| 1538 | self.help(request) |
| 1539 | |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1540 | def help(self, request): |
| 1541 | if type(request) is type(''): |
| 1542 | if request == 'help': self.intro() |
| 1543 | elif request == 'keywords': self.listkeywords() |
| 1544 | elif request == 'topics': self.listtopics() |
| 1545 | elif request == 'modules': self.listmodules() |
| 1546 | elif request[:8] == 'modules ': |
| 1547 | self.listmodules(split(request)[1]) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1548 | elif request in self.keywords: self.showtopic(request) |
| 1549 | elif request in self.topics: self.showtopic(request) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1550 | elif request: doc(request, 'Help on %s:') |
| 1551 | elif isinstance(request, Helper): self() |
| 1552 | else: doc(request, 'Help on %s:') |
| 1553 | self.output.write('\n') |
| 1554 | |
| 1555 | def intro(self): |
| 1556 | self.output.write(''' |
| 1557 | Welcome to Python %s! This is the online help utility. |
| 1558 | |
| 1559 | If this is your first time using Python, you should definitely check out |
| 1560 | the tutorial on the Internet at http://www.python.org/doc/tut/. |
| 1561 | |
| 1562 | Enter the name of any module, keyword, or topic to get help on writing |
| 1563 | Python programs and using Python modules. To quit this help utility and |
| 1564 | return to the interpreter, just type "quit". |
| 1565 | |
| 1566 | To get a list of available modules, keywords, or topics, type "modules", |
| 1567 | "keywords", or "topics". Each module also comes with a one-line summary |
| 1568 | of what it does; to list the modules whose summaries contain a given word |
| 1569 | such as "spam", type "modules spam". |
| 1570 | ''' % sys.version[:3]) |
| 1571 | |
| 1572 | def list(self, items, columns=4, width=80): |
| 1573 | items = items[:] |
| 1574 | items.sort() |
| 1575 | colw = width / columns |
| 1576 | rows = (len(items) + columns - 1) / columns |
| 1577 | for row in range(rows): |
| 1578 | for col in range(columns): |
| 1579 | i = col * rows + row |
| 1580 | if i < len(items): |
| 1581 | self.output.write(items[i]) |
| 1582 | if col < columns - 1: |
| 1583 | self.output.write(' ' + ' ' * (colw-1 - len(items[i]))) |
| 1584 | self.output.write('\n') |
| 1585 | |
| 1586 | def listkeywords(self): |
| 1587 | self.output.write(''' |
| 1588 | Here is a list of the Python keywords. Enter any keyword to get more help. |
| 1589 | |
| 1590 | ''') |
| 1591 | self.list(self.keywords.keys()) |
| 1592 | |
| 1593 | def listtopics(self): |
| 1594 | self.output.write(''' |
| 1595 | Here is a list of available topics. Enter any topic name to get more help. |
| 1596 | |
| 1597 | ''') |
| 1598 | self.list(self.topics.keys()) |
| 1599 | |
| 1600 | def showtopic(self, topic): |
| 1601 | if not self.docdir: |
| 1602 | self.output.write(''' |
| 1603 | Sorry, topic and keyword documentation is not available because the Python |
| 1604 | HTML documentation files could not be found. If you have installed them, |
| 1605 | please set the environment variable PYTHONDOCS to indicate their location. |
| 1606 | ''') |
| 1607 | return |
| 1608 | target = self.topics.get(topic, self.keywords.get(topic)) |
| 1609 | if not target: |
| 1610 | self.output.write('no documentation found for %s\n' % repr(topic)) |
| 1611 | return |
| 1612 | if type(target) is type(''): |
| 1613 | return self.showtopic(target) |
| 1614 | |
| 1615 | filename, xrefs = target |
| 1616 | filename = self.docdir + '/' + filename + '.html' |
| 1617 | try: |
| 1618 | file = open(filename) |
| 1619 | except: |
| 1620 | self.output.write('could not read docs from %s\n' % filename) |
| 1621 | return |
| 1622 | |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1623 | divpat = re.compile('<div[^>]*navigat.*?</div.*?>', re.I | re.S) |
| 1624 | addrpat = re.compile('<address.*?>.*?</address.*?>', re.I | re.S) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1625 | document = re.sub(addrpat, '', re.sub(divpat, '', file.read())) |
| 1626 | file.close() |
| 1627 | |
| 1628 | import htmllib, formatter, StringIO |
| 1629 | buffer = StringIO.StringIO() |
| 1630 | parser = htmllib.HTMLParser( |
| 1631 | formatter.AbstractFormatter(formatter.DumbWriter(buffer))) |
| 1632 | parser.start_table = parser.do_p |
| 1633 | parser.end_table = lambda parser=parser: parser.do_p({}) |
| 1634 | parser.start_tr = parser.do_br |
| 1635 | parser.start_td = parser.start_th = lambda a, b=buffer: b.write('\t') |
| 1636 | parser.feed(document) |
| 1637 | buffer = replace(buffer.getvalue(), '\xa0', ' ', '\n', '\n ') |
| 1638 | pager(' ' + strip(buffer) + '\n') |
Ka-Ping Yee | da79389 | 2001-04-13 11:02:51 +0000 | [diff] [blame] | 1639 | if xrefs: |
| 1640 | buffer = StringIO.StringIO() |
| 1641 | formatter.DumbWriter(buffer).send_flowing_data( |
| 1642 | 'Related help topics: ' + join(split(xrefs), ', ') + '\n') |
| 1643 | self.output.write('\n%s\n' % buffer.getvalue()) |
Ka-Ping Yee | 35cf0a3 | 2001-04-12 19:53:52 +0000 | [diff] [blame] | 1644 | |
| 1645 | def listmodules(self, key=''): |
| 1646 | if key: |
| 1647 | self.output.write(''' |
| 1648 | Here is a list of matching modules. Enter any module name to get more help. |
| 1649 | |
| 1650 | ''') |
| 1651 | apropos(key) |
| 1652 | else: |
| 1653 | self.output.write(''' |
| 1654 | Please wait a moment while I gather a list of all available modules... |
| 1655 | |
| 1656 | ''') |
| 1657 | modules = {} |
| 1658 | def callback(path, modname, desc, modules=modules): |
| 1659 | if modname and modname[-9:] == '.__init__': |
| 1660 | modname = modname[:-9] + ' (package)' |
| 1661 | if find(modname, '.') < 0: |
| 1662 | modules[modname] = 1 |
| 1663 | ModuleScanner().run(callback) |
| 1664 | self.list(modules.keys()) |
| 1665 | self.output.write(''' |
| 1666 | Enter any module name to get more help. Or, type "modules spam" to search |
| 1667 | for modules whose descriptions contain the word "spam". |
| 1668 | ''') |
| 1669 | |
| 1670 | help = Helper(sys.stdin, sys.stdout) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1671 | |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1672 | class Scanner: |
| 1673 | """A generic tree iterator.""" |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1674 | def __init__(self, roots, children, descendp): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1675 | self.roots = roots[:] |
| 1676 | self.state = [] |
| 1677 | self.children = children |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1678 | self.descendp = descendp |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1679 | |
| 1680 | def next(self): |
| 1681 | if not self.state: |
| 1682 | if not self.roots: |
| 1683 | return None |
| 1684 | root = self.roots.pop(0) |
| 1685 | self.state = [(root, self.children(root))] |
| 1686 | node, children = self.state[-1] |
| 1687 | if not children: |
| 1688 | self.state.pop() |
| 1689 | return self.next() |
| 1690 | child = children.pop(0) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1691 | if self.descendp(child): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1692 | self.state.append((child, self.children(child))) |
| 1693 | return child |
| 1694 | |
| 1695 | class ModuleScanner(Scanner): |
| 1696 | """An interruptible scanner that searches module synopses.""" |
| 1697 | def __init__(self): |
| 1698 | roots = map(lambda dir: (dir, ''), pathdirs()) |
Ka-Ping Yee | eca15c1 | 2001-04-13 13:53:07 +0000 | [diff] [blame] | 1699 | Scanner.__init__(self, roots, self.submodules, self.isnewpackage) |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 1700 | self.inodes = map(lambda (dir, pkg): os.stat(dir).st_ino, roots) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1701 | |
| 1702 | def submodules(self, (dir, package)): |
| 1703 | children = [] |
| 1704 | for file in os.listdir(dir): |
| 1705 | path = os.path.join(dir, file) |
Tim Peters | 30edd23 | 2001-03-16 08:29:48 +0000 | [diff] [blame] | 1706 | if ispackage(path): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1707 | children.append((path, package + (package and '.') + file)) |
| 1708 | else: |
| 1709 | children.append((path, package)) |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1710 | children.sort() # so that spam.py comes before spam.pyc or spam.pyo |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1711 | return children |
| 1712 | |
Ka-Ping Yee | eca15c1 | 2001-04-13 13:53:07 +0000 | [diff] [blame] | 1713 | def isnewpackage(self, (dir, package)): |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 1714 | inode = os.path.exists(dir) and os.stat(dir).st_ino |
Ka-Ping Yee | eca15c1 | 2001-04-13 13:53:07 +0000 | [diff] [blame] | 1715 | if not (os.path.islink(dir) and inode in self.inodes): |
Ka-Ping Yee | 6191a23 | 2001-04-13 15:00:27 +0000 | [diff] [blame] | 1716 | self.inodes.append(inode) # detect circular symbolic links |
Ka-Ping Yee | eca15c1 | 2001-04-13 13:53:07 +0000 | [diff] [blame] | 1717 | return ispackage(dir) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 1718 | return False |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1719 | |
Ka-Ping Yee | 6624696 | 2001-04-12 11:59:50 +0000 | [diff] [blame] | 1720 | def run(self, callback, key=None, completer=None): |
| 1721 | if key: key = lower(key) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 1722 | self.quit = False |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1723 | seen = {} |
| 1724 | |
| 1725 | for modname in sys.builtin_module_names: |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 1726 | if modname != '__main__': |
| 1727 | seen[modname] = 1 |
Ka-Ping Yee | 6624696 | 2001-04-12 11:59:50 +0000 | [diff] [blame] | 1728 | if key is None: |
| 1729 | callback(None, modname, '') |
| 1730 | else: |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1731 | desc = split(__import__(modname).__doc__ or '', '\n')[0] |
Ka-Ping Yee | 6624696 | 2001-04-12 11:59:50 +0000 | [diff] [blame] | 1732 | if find(lower(modname + ' - ' + desc), key) >= 0: |
| 1733 | callback(None, modname, desc) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1734 | |
| 1735 | while not self.quit: |
| 1736 | node = self.next() |
| 1737 | if not node: break |
| 1738 | path, package = node |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1739 | modname = inspect.getmodulename(path) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1740 | if os.path.isfile(path) and modname: |
| 1741 | modname = package + (package and '.') + modname |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1742 | if not modname in seen: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1743 | seen[modname] = 1 # if we see spam.py, skip spam.pyc |
Ka-Ping Yee | 6624696 | 2001-04-12 11:59:50 +0000 | [diff] [blame] | 1744 | if key is None: |
| 1745 | callback(path, modname, '') |
| 1746 | else: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1747 | desc = synopsis(path) or '' |
| 1748 | if find(lower(modname + ' - ' + desc), key) >= 0: |
| 1749 | callback(path, modname, desc) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1750 | if completer: completer() |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1751 | |
| 1752 | def apropos(key): |
| 1753 | """Print all the one-line module summaries that contain a substring.""" |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1754 | def callback(path, modname, desc): |
| 1755 | if modname[-9:] == '.__init__': |
| 1756 | modname = modname[:-9] + ' (package)' |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1757 | print modname, desc and '- ' + desc |
| 1758 | try: import warnings |
| 1759 | except ImportError: pass |
| 1760 | else: warnings.filterwarnings('ignore') # ignore problems during import |
Ka-Ping Yee | 6624696 | 2001-04-12 11:59:50 +0000 | [diff] [blame] | 1761 | ModuleScanner().run(callback, key) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1762 | |
| 1763 | # --------------------------------------------------- web browser interface |
| 1764 | |
Ka-Ping Yee | 6624696 | 2001-04-12 11:59:50 +0000 | [diff] [blame] | 1765 | def serve(port, callback=None, completer=None): |
Ka-Ping Yee | fd54069 | 2001-04-12 12:54:36 +0000 | [diff] [blame] | 1766 | import BaseHTTPServer, mimetools, select |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1767 | |
| 1768 | # Patch up mimetools.Message so it doesn't break if rfc822 is reloaded. |
| 1769 | class Message(mimetools.Message): |
| 1770 | def __init__(self, fp, seekable=1): |
| 1771 | Message = self.__class__ |
| 1772 | Message.__bases__[0].__bases__[0].__init__(self, fp, seekable) |
| 1773 | self.encodingheader = self.getheader('content-transfer-encoding') |
| 1774 | self.typeheader = self.getheader('content-type') |
| 1775 | self.parsetype() |
| 1776 | self.parseplist() |
| 1777 | |
| 1778 | class DocHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 1779 | def send_document(self, title, contents): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1780 | try: |
| 1781 | self.send_response(200) |
| 1782 | self.send_header('Content-Type', 'text/html') |
| 1783 | self.end_headers() |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1784 | self.wfile.write(html.page(title, contents)) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1785 | except IOError: pass |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1786 | |
| 1787 | def do_GET(self): |
| 1788 | path = self.path |
| 1789 | if path[-5:] == '.html': path = path[:-5] |
| 1790 | if path[:1] == '/': path = path[1:] |
| 1791 | if path and path != '.': |
| 1792 | try: |
Ka-Ping Yee | dec96e9 | 2001-04-13 09:55:49 +0000 | [diff] [blame] | 1793 | obj = locate(path, forceload=1) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1794 | except ErrorDuringImport, value: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1795 | self.send_document(path, html.escape(str(value))) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1796 | return |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1797 | if obj: |
| 1798 | self.send_document(describe(obj), html.document(obj, path)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1799 | else: |
| 1800 | self.send_document(path, |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1801 | 'no Python documentation found for %s' % repr(path)) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1802 | else: |
| 1803 | heading = html.heading( |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1804 | '<big><big><strong>Python: Index of Modules</strong></big></big>', |
| 1805 | '#ffffff', '#7799ee') |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 1806 | def bltinlink(name): |
| 1807 | return '<a href="%s.html">%s</a>' % (name, name) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 1808 | names = filter(lambda x: x != '__main__', |
| 1809 | sys.builtin_module_names) |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 1810 | contents = html.multicolumn(names, bltinlink) |
| 1811 | indices = ['<p>' + html.bigsection( |
| 1812 | 'Built-in Modules', '#ffffff', '#ee77aa', contents)] |
| 1813 | |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1814 | seen = {} |
| 1815 | for dir in pathdirs(): |
| 1816 | indices.append(html.index(dir, seen)) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1817 | contents = heading + join(indices) + '''<p align=right> |
Tim Peters | 2306d24 | 2001-09-25 03:18:32 +0000 | [diff] [blame] | 1818 | <font color="#909090" face="helvetica, arial"><strong> |
| 1819 | pydoc</strong> by Ka-Ping Yee <ping@lfw.org></font>''' |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1820 | self.send_document('Index of Modules', contents) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1821 | |
| 1822 | def log_message(self, *args): pass |
| 1823 | |
Ka-Ping Yee | fd54069 | 2001-04-12 12:54:36 +0000 | [diff] [blame] | 1824 | class DocServer(BaseHTTPServer.HTTPServer): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1825 | def __init__(self, port, callback): |
Ka-Ping Yee | db8ed15 | 2001-03-02 05:58:17 +0000 | [diff] [blame] | 1826 | host = (sys.platform == 'mac') and '127.0.0.1' or 'localhost' |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 1827 | self.address = ('', port) |
Ka-Ping Yee | db8ed15 | 2001-03-02 05:58:17 +0000 | [diff] [blame] | 1828 | self.url = 'http://%s:%d/' % (host, port) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1829 | self.callback = callback |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1830 | self.base.__init__(self, self.address, self.handler) |
| 1831 | |
| 1832 | def serve_until_quit(self): |
| 1833 | import select |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 1834 | self.quit = False |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1835 | while not self.quit: |
| 1836 | rd, wr, ex = select.select([self.socket.fileno()], [], [], 1) |
| 1837 | if rd: self.handle_request() |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1838 | |
| 1839 | def server_activate(self): |
| 1840 | self.base.server_activate(self) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1841 | if self.callback: self.callback(self) |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 1842 | |
| 1843 | DocServer.base = BaseHTTPServer.HTTPServer |
| 1844 | DocServer.handler = DocHandler |
| 1845 | DocHandler.MessageClass = Message |
| 1846 | try: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1847 | try: |
| 1848 | DocServer(port, callback).serve_until_quit() |
| 1849 | except (KeyboardInterrupt, select.error): |
| 1850 | pass |
| 1851 | finally: |
Ka-Ping Yee | 6624696 | 2001-04-12 11:59:50 +0000 | [diff] [blame] | 1852 | if completer: completer() |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1853 | |
| 1854 | # ----------------------------------------------------- graphical interface |
| 1855 | |
| 1856 | def gui(): |
| 1857 | """Graphical interface (starts web server and pops up a control window).""" |
| 1858 | class GUI: |
| 1859 | def __init__(self, window, port=7464): |
| 1860 | self.window = window |
| 1861 | self.server = None |
| 1862 | self.scanner = None |
| 1863 | |
| 1864 | import Tkinter |
| 1865 | self.server_frm = Tkinter.Frame(window) |
| 1866 | self.title_lbl = Tkinter.Label(self.server_frm, |
| 1867 | text='Starting server...\n ') |
| 1868 | self.open_btn = Tkinter.Button(self.server_frm, |
| 1869 | text='open browser', command=self.open, state='disabled') |
| 1870 | self.quit_btn = Tkinter.Button(self.server_frm, |
| 1871 | text='quit serving', command=self.quit, state='disabled') |
| 1872 | |
| 1873 | self.search_frm = Tkinter.Frame(window) |
| 1874 | self.search_lbl = Tkinter.Label(self.search_frm, text='Search for') |
| 1875 | self.search_ent = Tkinter.Entry(self.search_frm) |
| 1876 | self.search_ent.bind('<Return>', self.search) |
| 1877 | self.stop_btn = Tkinter.Button(self.search_frm, |
| 1878 | text='stop', pady=0, command=self.stop, state='disabled') |
| 1879 | if sys.platform == 'win32': |
Ka-Ping Yee | c92cdf7 | 2001-03-02 05:54:35 +0000 | [diff] [blame] | 1880 | # Trying to hide and show this button crashes under Windows. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1881 | self.stop_btn.pack(side='right') |
| 1882 | |
| 1883 | self.window.title('pydoc') |
| 1884 | self.window.protocol('WM_DELETE_WINDOW', self.quit) |
| 1885 | self.title_lbl.pack(side='top', fill='x') |
| 1886 | self.open_btn.pack(side='left', fill='x', expand=1) |
| 1887 | self.quit_btn.pack(side='right', fill='x', expand=1) |
| 1888 | self.server_frm.pack(side='top', fill='x') |
| 1889 | |
| 1890 | self.search_lbl.pack(side='left') |
| 1891 | self.search_ent.pack(side='right', fill='x', expand=1) |
| 1892 | self.search_frm.pack(side='top', fill='x') |
| 1893 | self.search_ent.focus_set() |
| 1894 | |
Ka-Ping Yee | c92cdf7 | 2001-03-02 05:54:35 +0000 | [diff] [blame] | 1895 | font = ('helvetica', sys.platform == 'win32' and 8 or 10) |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 1896 | self.result_lst = Tkinter.Listbox(window, font=font, height=6) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1897 | self.result_lst.bind('<Button-1>', self.select) |
| 1898 | self.result_lst.bind('<Double-Button-1>', self.goto) |
| 1899 | self.result_scr = Tkinter.Scrollbar(window, |
| 1900 | orient='vertical', command=self.result_lst.yview) |
| 1901 | self.result_lst.config(yscrollcommand=self.result_scr.set) |
| 1902 | |
| 1903 | self.result_frm = Tkinter.Frame(window) |
| 1904 | self.goto_btn = Tkinter.Button(self.result_frm, |
| 1905 | text='go to selected', command=self.goto) |
| 1906 | self.hide_btn = Tkinter.Button(self.result_frm, |
| 1907 | text='hide results', command=self.hide) |
| 1908 | self.goto_btn.pack(side='left', fill='x', expand=1) |
| 1909 | self.hide_btn.pack(side='right', fill='x', expand=1) |
| 1910 | |
| 1911 | self.window.update() |
| 1912 | self.minwidth = self.window.winfo_width() |
| 1913 | self.minheight = self.window.winfo_height() |
| 1914 | self.bigminheight = (self.server_frm.winfo_reqheight() + |
| 1915 | self.search_frm.winfo_reqheight() + |
| 1916 | self.result_lst.winfo_reqheight() + |
| 1917 | self.result_frm.winfo_reqheight()) |
| 1918 | self.bigwidth, self.bigheight = self.minwidth, self.bigminheight |
| 1919 | self.expanded = 0 |
| 1920 | self.window.wm_geometry('%dx%d' % (self.minwidth, self.minheight)) |
| 1921 | self.window.wm_minsize(self.minwidth, self.minheight) |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 1922 | self.window.tk.willdispatch() |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1923 | |
| 1924 | import threading |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 1925 | threading.Thread( |
| 1926 | target=serve, args=(port, self.ready, self.quit)).start() |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1927 | |
| 1928 | def ready(self, server): |
| 1929 | self.server = server |
| 1930 | self.title_lbl.config( |
| 1931 | text='Python documentation server at\n' + server.url) |
| 1932 | self.open_btn.config(state='normal') |
| 1933 | self.quit_btn.config(state='normal') |
| 1934 | |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 1935 | def open(self, event=None, url=None): |
| 1936 | url = url or self.server.url |
| 1937 | try: |
| 1938 | import webbrowser |
| 1939 | webbrowser.open(url) |
| 1940 | except ImportError: # pre-webbrowser.py compatibility |
Ka-Ping Yee | c92cdf7 | 2001-03-02 05:54:35 +0000 | [diff] [blame] | 1941 | if sys.platform == 'win32': |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 1942 | os.system('start "%s"' % url) |
| 1943 | elif sys.platform == 'mac': |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1944 | try: import ic |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 1945 | except ImportError: pass |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 1946 | else: ic.launchurl(url) |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 1947 | else: |
| 1948 | rc = os.system('netscape -remote "openURL(%s)" &' % url) |
| 1949 | if rc: os.system('netscape "%s" &' % url) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1950 | |
| 1951 | def quit(self, event=None): |
| 1952 | if self.server: |
| 1953 | self.server.quit = 1 |
| 1954 | self.window.quit() |
| 1955 | |
| 1956 | def search(self, event=None): |
| 1957 | key = self.search_ent.get() |
| 1958 | self.stop_btn.pack(side='right') |
| 1959 | self.stop_btn.config(state='normal') |
| 1960 | self.search_lbl.config(text='Searching for "%s"...' % key) |
| 1961 | self.search_ent.forget() |
| 1962 | self.search_lbl.pack(side='left') |
| 1963 | self.result_lst.delete(0, 'end') |
| 1964 | self.goto_btn.config(state='disabled') |
| 1965 | self.expand() |
| 1966 | |
| 1967 | import threading |
| 1968 | if self.scanner: |
| 1969 | self.scanner.quit = 1 |
| 1970 | self.scanner = ModuleScanner() |
| 1971 | threading.Thread(target=self.scanner.run, |
Ka-Ping Yee | 6dcfa38 | 2001-04-12 20:27:31 +0000 | [diff] [blame] | 1972 | args=(self.update, key, self.done)).start() |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1973 | |
| 1974 | def update(self, path, modname, desc): |
| 1975 | if modname[-9:] == '.__init__': |
| 1976 | modname = modname[:-9] + ' (package)' |
| 1977 | self.result_lst.insert('end', |
| 1978 | modname + ' - ' + (desc or '(no description)')) |
| 1979 | |
| 1980 | def stop(self, event=None): |
| 1981 | if self.scanner: |
| 1982 | self.scanner.quit = 1 |
| 1983 | self.scanner = None |
| 1984 | |
| 1985 | def done(self): |
| 1986 | self.scanner = None |
| 1987 | self.search_lbl.config(text='Search for') |
| 1988 | self.search_lbl.pack(side='left') |
| 1989 | self.search_ent.pack(side='right', fill='x', expand=1) |
| 1990 | if sys.platform != 'win32': self.stop_btn.forget() |
| 1991 | self.stop_btn.config(state='disabled') |
| 1992 | |
| 1993 | def select(self, event=None): |
| 1994 | self.goto_btn.config(state='normal') |
| 1995 | |
| 1996 | def goto(self, event=None): |
| 1997 | selection = self.result_lst.curselection() |
| 1998 | if selection: |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 1999 | modname = split(self.result_lst.get(selection[0]))[0] |
Ka-Ping Yee | 239432a | 2001-03-02 02:45:08 +0000 | [diff] [blame] | 2000 | self.open(url=self.server.url + modname + '.html') |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2001 | |
| 2002 | def collapse(self): |
| 2003 | if not self.expanded: return |
| 2004 | self.result_frm.forget() |
| 2005 | self.result_scr.forget() |
| 2006 | self.result_lst.forget() |
| 2007 | self.bigwidth = self.window.winfo_width() |
| 2008 | self.bigheight = self.window.winfo_height() |
| 2009 | self.window.wm_geometry('%dx%d' % (self.minwidth, self.minheight)) |
| 2010 | self.window.wm_minsize(self.minwidth, self.minheight) |
| 2011 | self.expanded = 0 |
| 2012 | |
| 2013 | def expand(self): |
| 2014 | if self.expanded: return |
| 2015 | self.result_frm.pack(side='bottom', fill='x') |
| 2016 | self.result_scr.pack(side='right', fill='y') |
| 2017 | self.result_lst.pack(side='top', fill='both', expand=1) |
| 2018 | self.window.wm_geometry('%dx%d' % (self.bigwidth, self.bigheight)) |
| 2019 | self.window.wm_minsize(self.minwidth, self.bigminheight) |
| 2020 | self.expanded = 1 |
| 2021 | |
| 2022 | def hide(self, event=None): |
| 2023 | self.stop() |
| 2024 | self.collapse() |
| 2025 | |
| 2026 | import Tkinter |
| 2027 | try: |
| 2028 | gui = GUI(Tkinter.Tk()) |
| 2029 | Tkinter.mainloop() |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2030 | except KeyboardInterrupt: |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2031 | pass |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2032 | |
| 2033 | # -------------------------------------------------- command-line interface |
| 2034 | |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 2035 | def ispath(x): |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 2036 | return isinstance(x, str) and find(x, os.sep) >= 0 |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 2037 | |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 2038 | def cli(): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2039 | """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] | 2040 | import getopt |
| 2041 | class BadUsage: pass |
| 2042 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 2043 | # Scripts don't get the current directory in their path by default. |
Ka-Ping Yee | f78a81b | 2001-03-27 08:13:42 +0000 | [diff] [blame] | 2044 | scriptdir = os.path.dirname(sys.argv[0]) |
| 2045 | if scriptdir in sys.path: |
| 2046 | sys.path.remove(scriptdir) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 2047 | sys.path.insert(0, '.') |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2048 | |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 2049 | try: |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2050 | opts, args = getopt.getopt(sys.argv[1:], 'gk:p:w') |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2051 | writing = 0 |
| 2052 | |
| 2053 | for opt, val in opts: |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2054 | if opt == '-g': |
| 2055 | gui() |
| 2056 | return |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2057 | if opt == '-k': |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2058 | apropos(val) |
| 2059 | return |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2060 | if opt == '-p': |
| 2061 | try: |
| 2062 | port = int(val) |
| 2063 | except ValueError: |
| 2064 | raise BadUsage |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2065 | def ready(server): |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 2066 | print 'pydoc server ready at %s' % server.url |
| 2067 | def stopped(): |
| 2068 | print 'pydoc server stopped' |
| 2069 | serve(port, ready, stopped) |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2070 | return |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2071 | if opt == '-w': |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2072 | writing = 1 |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2073 | |
| 2074 | if not args: raise BadUsage |
| 2075 | for arg in args: |
Ka-Ping Yee | 45daeb0 | 2002-08-11 15:11:33 +0000 | [diff] [blame] | 2076 | if ispath(arg) and not os.path.exists(arg): |
| 2077 | print 'file %r does not exist' % arg |
| 2078 | break |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2079 | try: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 2080 | if ispath(arg) and os.path.isfile(arg): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2081 | arg = importfile(arg) |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 2082 | if writing: |
| 2083 | if ispath(arg) and os.path.isdir(arg): |
| 2084 | writedocs(arg) |
| 2085 | else: |
| 2086 | writedoc(arg) |
| 2087 | else: |
Ka-Ping Yee | 9aa0d90 | 2001-04-12 10:50:23 +0000 | [diff] [blame] | 2088 | doc(arg) |
Ka-Ping Yee | 3bda879 | 2001-03-23 13:17:50 +0000 | [diff] [blame] | 2089 | except ErrorDuringImport, value: |
Ka-Ping Yee | 37f7b38 | 2001-03-23 00:12:53 +0000 | [diff] [blame] | 2090 | print value |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2091 | |
| 2092 | except (getopt.error, BadUsage): |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2093 | cmd = sys.argv[0] |
| 2094 | print """pydoc - the Python documentation tool |
| 2095 | |
| 2096 | %s <name> ... |
| 2097 | Show text documentation on something. <name> may be the name of a |
| 2098 | function, module, or package, or a dotted reference to a class or |
| 2099 | function within a module or module in a package. If <name> contains |
| 2100 | a '%s', it is used as the path to a Python source file to document. |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2101 | |
| 2102 | %s -k <keyword> |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2103 | 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] | 2104 | |
| 2105 | %s -p <port> |
| 2106 | Start an HTTP server on the given port on the local machine. |
| 2107 | |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2108 | %s -g |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 2109 | Pop up a graphical interface for finding and serving documentation. |
Ka-Ping Yee | dd17534 | 2001-02-27 14:43:46 +0000 | [diff] [blame] | 2110 | |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2111 | %s -w <name> ... |
| 2112 | Write out the HTML documentation for a module to a file in the current |
Ka-Ping Yee | 5a804ed | 2001-04-10 11:46:02 +0000 | [diff] [blame] | 2113 | directory. If <name> contains a '%s', it is treated as a filename; if |
| 2114 | it names a directory, documentation is written for all the contents. |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2115 | """ % (cmd, os.sep, cmd, cmd, cmd, cmd, os.sep) |
Ka-Ping Yee | 1d38463 | 2001-03-01 00:24:32 +0000 | [diff] [blame] | 2116 | |
Ka-Ping Yee | 66efbc7 | 2001-03-01 13:55:20 +0000 | [diff] [blame] | 2117 | if __name__ == '__main__': cli() |