Showing content from http://www.lfw.org/python/textdoc.py below:
"""Generate text documentation from live Python objects.""" __version__ = 'Ka-Ping Yee , 10 Jan 2001' import sys, os, string, types, inspect from string import join, split, strip, rstrip # ---------------------------------------------------- formatting utilities def serialize(stuff): """Combine a list containing strings and nested lists into a single string. This lets us manipulate lists until the last moment, since rearranging lists is faster than rearranging strings.""" if type(stuff) is type(''): return stuff results = [] for item in stuff: if type(item) is type(''): results.append(item) else: results.append(serialize(item)) return join(results, '') def bold(text): result = '' for char in text: result = result + char + '\b' + char return result def section(title, contents): return bold(title) + '\n' + rstrip(indent(contents)) + '\n\n' def indent(text, spaces=4): """Indent text a given number of spaces.""" if type(text) is type([]): text = serialize(text) lines = split(text, '\n') lines = map(lambda line, prefix=' ' * spaces: prefix + line, lines) if lines: lines[-1] = rstrip(lines[-1]) return join(lines, '\n') def classname(object, modname): name = object.__name__ if object.__module__ != modname: name = object.__module__ + '.' + name return name def getdoc(object): result = inspect.getdoc(object) if not result: try: result = inspect.getcomments(object) except: pass return result and rstrip(result) + '\n' or '' # -------------------------------------------------- type-specific routines def document_tree(tree, modname, parent=None, prefix=''): """Render in text a class tree as returned by inspect.getclasstree().""" results = [] for entry in tree: if type(entry) is type(()): c, bases = entry results.append(prefix + classname(c, modname)) if bases and bases != (parent,): parents = map(lambda c, m=modname: classname(c, m), bases) results.append('(%s)' % join(parents, ', ')) results.append('\n') elif type(entry) is type([]): results.append(document_tree(entry, modname, c, prefix + ' ')) return results def isconstant(object): """Check if an object is of a type that probably means it's a constant.""" return type(object) in [ types.FloatType, types.IntType, types.ListType, types.LongType, types.StringType, types.TupleType, types.TypeType, hasattr(types, 'UnicodeType') and types.UnicodeType or 0] def document_module(object): """Produce text documentation for a given module object.""" results = [] name = object.__name__ lines = split(strip(getdoc(object)), '\n') if len(lines) == 1: if lines[0]: name = name + ' - ' + lines[0] lines = [] elif len(lines) >= 2 and not rstrip(lines[1]): if lines[0]: name = name + ' - ' + lines[0] lines = lines[2:] results.append(section('NAME', name)) try: file = inspect.getsourcefile(object) except TypeError: file = None results.append(section('FILE', file or '(built-in)')) if lines: results.append(section('DESCRIPTION', join(lines, '\n'))) classes = [] for key, value in inspect.getmembers(object, inspect.isclass): if (inspect.getmodule(value) or object) is object: classes.append(value) functions = [] for key, value in inspect.getmembers(object, inspect.isroutine): if inspect.isbuiltin(value) or inspect.getmodule(value) is object: functions.append(value) constants = [] for key, value in inspect.getmembers(object, isconstant): if key[:1] != '_': constants.append((key, value)) if hasattr(object, '__path__'): modpkgs = [] for file in os.listdir(object.__path__[0]): if file[:1] != '_': path = os.path.join(object.__path__[0], file) if file[-3:] == '.py' and file[:-3] not in modpkgs: modpkgs.append(file[:-3]) elif file[-4:] == '.pyc' and file[:-4] not in modpkgs: modpkgs.append(file[:-4]) elif os.path.isdir(path): init = os.path.join(path, '__init__.py') initc = os.path.join(path, '__init__.pyc') if os.path.isfile(init) or os.path.isfile(initc): modpkgs.append(file + ' (package)') modpkgs.sort() results.append(section('PACKAGE CONTENTS', string.join(modpkgs, '\n'))) if classes: contents = document_tree( inspect.getclasstree(classes, 1), object.__name__) contents.append('\n') for item in classes: contents.append([document_class(item), '\n']) results.append(section('CLASSES', contents)) if functions: contents = [] for item in functions: contents.append([document_function(item), '\n']) results.append(section('FUNCTIONS', contents)) if constants: contents = [] for key, value in constants: line = key + ' = ' + repr(value) chop = 70 - len(line) line = bold(key) + ' = ' + repr(value) if chop < 0: line = line[:chop] + '...' contents.append(line + '\n') results.append(section('CONSTANTS', contents)) if hasattr(object, '__version__'): version = object.__version__ if hasattr(object, '__date__'): version = version + ', ' + object.__date__ results.append(section('VERSION', version)) if hasattr(object, '__author__'): author = object.__author__ if hasattr(object, '__email__'): author = author + ' <' + object.__email__ + '>' results.append(section('AUTHOR', author)) return results def document_class(object): """Produce text documentation for a given class object.""" name = object.__name__ bases = object.__bases__ doc = getdoc(object) if doc: results = [doc, '\n'] else: results = [doc] methods = map(lambda (key, value): value, inspect.getmembers(object, inspect.ismethod)) for item in methods: results.append([document_method(item), '\n']) title = 'class ' + bold(name) if bases: parents = map(lambda c, m=object.__module__: classname(c, m), bases) title = title + '(%s)' % join(parents, ', ') return title + '\n' + rstrip(indent(results)) + '\n' def document_method(object): return document_function(object.im_func) def document_function(object): """Produce text documentation for a given function object.""" try: args, varargs, varkw, defaults = inspect.getargspec(object) argspec = inspect.formatargspec(args, varargs, varkw, defaults) except TypeError: argspec = '(...)' if object.__name__ == ' ': decl = [' ', argspec[1:-1]] else: decl = bold(object.__name__) + argspec doc = getdoc(object) if doc: return decl + '\n' + rstrip(indent(doc)) + '\n' else: return decl + '\n' def document_builtin(object): return [bold(object.__name__) + '(...)\n', rstrip(indent(object.__doc__)) + '\n'] # --------------------------------------------------- main dispatch routine def document(object): """Generate documentation for a given object.""" if inspect.ismodule(object): results = document_module(object) elif inspect.isclass(object): results = document_class(object) elif inspect.ismethod(object): results = document_method(object) elif inspect.isfunction(object): results = document_function(object) elif inspect.isbuiltin(object): results = document_builtin(object) else: raise TypeError, 'don\'t know how to document this kind of object' return serialize(results)
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4