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