PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/python/lib/Lib/pydoc.py

http://github.com/JetBrains/intellij-community
Python | 2266 lines | 2207 code | 39 blank | 20 comment | 122 complexity | 64054c3074046237f97ddeb8759f39d0 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. #!/usr/bin/env python
  2. # -*- coding: Latin-1 -*-
  3. """Generate Python documentation in HTML or text for interactive use.
  4. In the Python interpreter, do "from pydoc import help" to provide online
  5. help. Calling help(thing) on a Python object documents the object.
  6. Or, at the shell command line outside of Python:
  7. Run "pydoc <name>" to show documentation on something. <name> may be
  8. the name of a function, module, package, or a dotted reference to a
  9. class or function within a module or module in a package. If the
  10. argument contains a path segment delimiter (e.g. slash on Unix,
  11. backslash on Windows) it is treated as the path to a Python source file.
  12. Run "pydoc -k <keyword>" to search for a keyword in the synopsis lines
  13. of all available modules.
  14. Run "pydoc -p <port>" to start an HTTP server on a given port on the
  15. local machine to generate documentation web pages.
  16. For platforms without a command line, "pydoc -g" starts the HTTP server
  17. and also pops up a little window for controlling it.
  18. Run "pydoc -w <name>" to write out the HTML documentation for a module
  19. to a file named "<name>.html".
  20. Module docs for core modules are assumed to be in
  21. http://www.python.org/doc/current/lib/
  22. This can be overridden by setting the PYTHONDOCS environment variable
  23. to a different URL or to a local directory containing the Library
  24. Reference Manual pages.
  25. """
  26. __author__ = "Ka-Ping Yee <ping@lfw.org>"
  27. __date__ = "26 February 2001"
  28. __version__ = "$Revision: 54366 $"
  29. __credits__ = """Guido van Rossum, for an excellent programming language.
  30. Tommy Burnette, the original creator of manpy.
  31. Paul Prescod, for all his work on onlinehelp.
  32. Richard Chamberlain, for the first implementation of textdoc.
  33. """
  34. # Known bugs that can't be fixed here:
  35. # - imp.load_module() cannot be prevented from clobbering existing
  36. # loaded modules, so calling synopsis() on a binary module file
  37. # changes the contents of any existing module with the same name.
  38. # - If the __file__ attribute on a module is a relative path and
  39. # the current directory is changed with os.chdir(), an incorrect
  40. # path will be displayed.
  41. import sys, imp, os, re, types, inspect, __builtin__, pkgutil
  42. from repr import Repr
  43. from string import expandtabs, find, join, lower, split, strip, rfind, rstrip
  44. try:
  45. from collections import deque
  46. except ImportError:
  47. # Python 2.3 compatibility
  48. class deque(list):
  49. def popleft(self):
  50. return self.pop(0)
  51. # --------------------------------------------------------- common routines
  52. def pathdirs():
  53. """Convert sys.path into a list of absolute, existing, unique paths."""
  54. dirs = []
  55. normdirs = []
  56. for dir in sys.path:
  57. dir = os.path.abspath(dir or '.')
  58. normdir = os.path.normcase(dir)
  59. if normdir not in normdirs and os.path.isdir(dir):
  60. dirs.append(dir)
  61. normdirs.append(normdir)
  62. return dirs
  63. def getdoc(object):
  64. """Get the doc string or comments for an object."""
  65. result = inspect.getdoc(object) or inspect.getcomments(object)
  66. return result and re.sub('^ *\n', '', rstrip(result)) or ''
  67. def splitdoc(doc):
  68. """Split a doc string into a synopsis line (if any) and the rest."""
  69. lines = split(strip(doc), '\n')
  70. if len(lines) == 1:
  71. return lines[0], ''
  72. elif len(lines) >= 2 and not rstrip(lines[1]):
  73. return lines[0], join(lines[2:], '\n')
  74. return '', join(lines, '\n')
  75. def classname(object, modname):
  76. """Get a class name and qualify it with a module name if necessary."""
  77. name = object.__name__
  78. if object.__module__ != modname:
  79. name = object.__module__ + '.' + name
  80. return name
  81. def isdata(object):
  82. """Check if an object is of a type that probably means it's data."""
  83. return not (inspect.ismodule(object) or inspect.isclass(object) or
  84. inspect.isroutine(object) or inspect.isframe(object) or
  85. inspect.istraceback(object) or inspect.iscode(object))
  86. def replace(text, *pairs):
  87. """Do a series of global replacements on a string."""
  88. while pairs:
  89. text = join(split(text, pairs[0]), pairs[1])
  90. pairs = pairs[2:]
  91. return text
  92. def cram(text, maxlen):
  93. """Omit part of a string if needed to make it fit in a maximum length."""
  94. if len(text) > maxlen:
  95. pre = max(0, (maxlen-3)//2)
  96. post = max(0, maxlen-3-pre)
  97. return text[:pre] + '...' + text[len(text)-post:]
  98. return text
  99. _re_stripid = re.compile(r' at 0x[0-9a-f]{6,16}(>+)$', re.IGNORECASE)
  100. def stripid(text):
  101. """Remove the hexadecimal id from a Python object representation."""
  102. # The behaviour of %p is implementation-dependent in terms of case.
  103. if _re_stripid.search(repr(Exception)):
  104. return _re_stripid.sub(r'\1', text)
  105. return text
  106. def _is_some_method(obj):
  107. return inspect.ismethod(obj) or inspect.ismethoddescriptor(obj)
  108. def allmethods(cl):
  109. methods = {}
  110. for key, value in inspect.getmembers(cl, _is_some_method):
  111. methods[key] = 1
  112. for base in cl.__bases__:
  113. methods.update(allmethods(base)) # all your base are belong to us
  114. for key in methods.keys():
  115. methods[key] = getattr(cl, key)
  116. return methods
  117. def _split_list(s, predicate):
  118. """Split sequence s via predicate, and return pair ([true], [false]).
  119. The return value is a 2-tuple of lists,
  120. ([x for x in s if predicate(x)],
  121. [x for x in s if not predicate(x)])
  122. """
  123. yes = []
  124. no = []
  125. for x in s:
  126. if predicate(x):
  127. yes.append(x)
  128. else:
  129. no.append(x)
  130. return yes, no
  131. def visiblename(name, all=None):
  132. """Decide whether to show documentation on a variable."""
  133. # Certain special names are redundant.
  134. if name in ('__builtins__', '__doc__', '__file__', '__path__',
  135. '__module__', '__name__', '__slots__'): return 0
  136. # Private names are hidden, but special names are displayed.
  137. if name.startswith('__') and name.endswith('__'): return 1
  138. if all is not None:
  139. # only document that which the programmer exported in __all__
  140. return name in all
  141. else:
  142. return not name.startswith('_')
  143. def classify_class_attrs(object):
  144. """Wrap inspect.classify_class_attrs, with fixup for data descriptors."""
  145. def fixup((name, kind, cls, value)):
  146. if inspect.isdatadescriptor(value):
  147. kind = 'data descriptor'
  148. return name, kind, cls, value
  149. return map(fixup, inspect.classify_class_attrs(object))
  150. # ----------------------------------------------------- module manipulation
  151. def ispackage(path):
  152. """Guess whether a path refers to a package directory."""
  153. if os.path.isdir(path):
  154. for ext in ('.py', '.pyc', '.pyo', '$py.class'):
  155. if os.path.isfile(os.path.join(path, '__init__' + ext)):
  156. return True
  157. return False
  158. def source_synopsis(file):
  159. line = file.readline()
  160. while line[:1] == '#' or not strip(line):
  161. line = file.readline()
  162. if not line: break
  163. line = strip(line)
  164. if line[:4] == 'r"""': line = line[1:]
  165. if line[:3] == '"""':
  166. line = line[3:]
  167. if line[-1:] == '\\': line = line[:-1]
  168. while not strip(line):
  169. line = file.readline()
  170. if not line: break
  171. result = strip(split(line, '"""')[0])
  172. else: result = None
  173. return result
  174. def synopsis(filename, cache={}):
  175. """Get the one-line summary out of a module file."""
  176. mtime = os.stat(filename).st_mtime
  177. lastupdate, result = cache.get(filename, (0, None))
  178. if lastupdate < mtime:
  179. info = inspect.getmoduleinfo(filename)
  180. try:
  181. file = open(filename)
  182. except IOError:
  183. # module can't be opened, so skip it
  184. return None
  185. if info and 'b' in info[2]: # binary modules have to be imported
  186. try: module = imp.load_module('__temp__', file, filename, info[1:])
  187. except: return None
  188. result = (module.__doc__ or '').splitlines()[0]
  189. del sys.modules['__temp__']
  190. else: # text modules can be directly examined
  191. result = source_synopsis(file)
  192. file.close()
  193. cache[filename] = (mtime, result)
  194. return result
  195. class ErrorDuringImport(Exception):
  196. """Errors that occurred while trying to import something to document it."""
  197. def __init__(self, filename, (exc, value, tb)):
  198. self.filename = filename
  199. self.exc = exc
  200. self.value = value
  201. self.tb = tb
  202. def __str__(self):
  203. exc = self.exc
  204. if type(exc) is types.ClassType:
  205. exc = exc.__name__
  206. return 'problem in %s - %s: %s' % (self.filename, exc, self.value)
  207. def importfile(path):
  208. """Import a Python source file or compiled file given its path."""
  209. magic = imp.get_magic()
  210. file = open(path, 'r')
  211. if file.read(len(magic)) == magic:
  212. kind = imp.PY_COMPILED
  213. else:
  214. kind = imp.PY_SOURCE
  215. file.close()
  216. filename = os.path.basename(path)
  217. name, ext = os.path.splitext(filename)
  218. file = open(path, 'r')
  219. try:
  220. module = imp.load_module(name, file, path, (ext, 'r', kind))
  221. except:
  222. raise ErrorDuringImport(path, sys.exc_info())
  223. file.close()
  224. return module
  225. def safeimport(path, forceload=0, cache={}):
  226. """Import a module; handle errors; return None if the module isn't found.
  227. If the module *is* found but an exception occurs, it's wrapped in an
  228. ErrorDuringImport exception and reraised. Unlike __import__, if a
  229. package path is specified, the module at the end of the path is returned,
  230. not the package at the beginning. If the optional 'forceload' argument
  231. is 1, we reload the module from disk (unless it's a dynamic extension)."""
  232. try:
  233. # If forceload is 1 and the module has been previously loaded from
  234. # disk, we always have to reload the module. Checking the file's
  235. # mtime isn't good enough (e.g. the module could contain a class
  236. # that inherits from another module that has changed).
  237. if forceload and path in sys.modules:
  238. if path not in sys.builtin_module_names:
  239. # Avoid simply calling reload() because it leaves names in
  240. # the currently loaded module lying around if they're not
  241. # defined in the new source file. Instead, remove the
  242. # module from sys.modules and re-import. Also remove any
  243. # submodules because they won't appear in the newly loaded
  244. # module's namespace if they're already in sys.modules.
  245. subs = [m for m in sys.modules if m.startswith(path + '.')]
  246. for key in [path] + subs:
  247. # Prevent garbage collection.
  248. cache[key] = sys.modules[key]
  249. del sys.modules[key]
  250. module = __import__(path)
  251. except:
  252. # Did the error occur before or after the module was found?
  253. (exc, value, tb) = info = sys.exc_info()
  254. if path in sys.modules:
  255. # An error occurred while executing the imported module.
  256. raise ErrorDuringImport(sys.modules[path].__file__, info)
  257. elif exc is SyntaxError:
  258. # A SyntaxError occurred before we could execute the module.
  259. raise ErrorDuringImport(value.filename, info)
  260. elif exc is ImportError and \
  261. split(lower(str(value)))[:2] == ['no', 'module']:
  262. # The module was not found.
  263. return None
  264. else:
  265. # Some other error occurred during the importing process.
  266. raise ErrorDuringImport(path, sys.exc_info())
  267. for part in split(path, '.')[1:]:
  268. try: module = getattr(module, part)
  269. except AttributeError: return None
  270. return module
  271. # ---------------------------------------------------- formatter base class
  272. class Doc:
  273. def document(self, object, name=None, *args):
  274. """Generate documentation for an object."""
  275. args = (object, name) + args
  276. # 'try' clause is to attempt to handle the possibility that inspect
  277. # identifies something in a way that pydoc itself has issues handling;
  278. # think 'super' and how it is a descriptor (which raises the exception
  279. # by lacking a __name__ attribute) and an instance.
  280. if inspect.isgetsetdescriptor(object): return self.docdata(*args)
  281. if inspect.ismemberdescriptor(object): return self.docdata(*args)
  282. try:
  283. if inspect.ismodule(object): return self.docmodule(*args)
  284. if inspect.isclass(object): return self.docclass(*args)
  285. if inspect.isroutine(object): return self.docroutine(*args)
  286. except AttributeError:
  287. pass
  288. if isinstance(object, property): return self.docproperty(*args)
  289. return self.docother(*args)
  290. def fail(self, object, name=None, *args):
  291. """Raise an exception for unimplemented types."""
  292. message = "don't know how to document object%s of type %s" % (
  293. name and ' ' + repr(name), type(object).__name__)
  294. raise TypeError, message
  295. docmodule = docclass = docroutine = docother = docproperty = docdata = fail
  296. def getdocloc(self, object):
  297. """Return the location of module docs or None"""
  298. try:
  299. file = inspect.getabsfile(object)
  300. except TypeError:
  301. file = '(built-in)'
  302. docloc = os.environ.get("PYTHONDOCS",
  303. "http://www.python.org/doc/current/lib")
  304. basedir = os.path.join(sys.exec_prefix, "lib",
  305. "python"+sys.version[0:3])
  306. if (isinstance(object, type(os)) and
  307. (object.__name__ in ('errno', 'exceptions', 'gc', 'imp',
  308. 'marshal', 'posix', 'signal', 'sys',
  309. 'thread', 'zipimport') or
  310. (file.startswith(basedir) and
  311. not file.startswith(os.path.join(basedir, 'site-packages'))))):
  312. htmlfile = "module-%s.html" % object.__name__
  313. if docloc.startswith("http://"):
  314. docloc = "%s/%s" % (docloc.rstrip("/"), htmlfile)
  315. else:
  316. docloc = os.path.join(docloc, htmlfile)
  317. else:
  318. docloc = None
  319. return docloc
  320. # -------------------------------------------- HTML documentation generator
  321. class HTMLRepr(Repr):
  322. """Class for safely making an HTML representation of a Python object."""
  323. def __init__(self):
  324. Repr.__init__(self)
  325. self.maxlist = self.maxtuple = 20
  326. self.maxdict = 10
  327. self.maxstring = self.maxother = 100
  328. def escape(self, text):
  329. return replace(text, '&', '&amp;', '<', '&lt;', '>', '&gt;')
  330. def repr(self, object):
  331. return Repr.repr(self, object)
  332. def repr1(self, x, level):
  333. if hasattr(type(x), '__name__'):
  334. methodname = 'repr_' + join(split(type(x).__name__), '_')
  335. if hasattr(self, methodname):
  336. return getattr(self, methodname)(x, level)
  337. return self.escape(cram(stripid(repr(x)), self.maxother))
  338. def repr_string(self, x, level):
  339. test = cram(x, self.maxstring)
  340. testrepr = repr(test)
  341. if '\\' in test and '\\' not in replace(testrepr, r'\\', ''):
  342. # Backslashes are only literal in the string and are never
  343. # needed to make any special characters, so show a raw string.
  344. return 'r' + testrepr[0] + self.escape(test) + testrepr[0]
  345. return re.sub(r'((\\[\\abfnrtv\'"]|\\[0-9]..|\\x..|\\u....)+)',
  346. r'<font color="#c040c0">\1</font>',
  347. self.escape(testrepr))
  348. repr_str = repr_string
  349. def repr_instance(self, x, level):
  350. try:
  351. return self.escape(cram(stripid(repr(x)), self.maxstring))
  352. except:
  353. return self.escape('<%s instance>' % x.__class__.__name__)
  354. repr_unicode = repr_string
  355. class HTMLDoc(Doc):
  356. """Formatter class for HTML documentation."""
  357. # ------------------------------------------- HTML formatting utilities
  358. _repr_instance = HTMLRepr()
  359. repr = _repr_instance.repr
  360. escape = _repr_instance.escape
  361. def page(self, title, contents):
  362. """Format an HTML page."""
  363. return '''
  364. <!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  365. <html><head><title>Python: %s</title>
  366. </head><body bgcolor="#f0f0f8">
  367. %s
  368. </body></html>''' % (title, contents)
  369. def heading(self, title, fgcol, bgcol, extras=''):
  370. """Format a page heading."""
  371. return '''
  372. <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
  373. <tr bgcolor="%s">
  374. <td valign=bottom>&nbsp;<br>
  375. <font color="%s" face="helvetica, arial">&nbsp;<br>%s</font></td
  376. ><td align=right valign=bottom
  377. ><font color="%s" face="helvetica, arial">%s</font></td></tr></table>
  378. ''' % (bgcol, fgcol, title, fgcol, extras or '&nbsp;')
  379. def section(self, title, fgcol, bgcol, contents, width=6,
  380. prelude='', marginalia=None, gap='&nbsp;'):
  381. """Format a section with a heading."""
  382. if marginalia is None:
  383. marginalia = '<tt>' + '&nbsp;' * width + '</tt>'
  384. result = '''<p>
  385. <table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
  386. <tr bgcolor="%s">
  387. <td colspan=3 valign=bottom>&nbsp;<br>
  388. <font color="%s" face="helvetica, arial">%s</font></td></tr>
  389. ''' % (bgcol, fgcol, title)
  390. if prelude:
  391. result = result + '''
  392. <tr bgcolor="%s"><td rowspan=2>%s</td>
  393. <td colspan=2>%s</td></tr>
  394. <tr><td>%s</td>''' % (bgcol, marginalia, prelude, gap)
  395. else:
  396. result = result + '''
  397. <tr><td bgcolor="%s">%s</td><td>%s</td>''' % (bgcol, marginalia, gap)
  398. return result + '\n<td width="100%%">%s</td></tr></table>' % contents
  399. def bigsection(self, title, *args):
  400. """Format a section with a big heading."""
  401. title = '<big><strong>%s</strong></big>' % title
  402. return self.section(title, *args)
  403. def preformat(self, text):
  404. """Format literal preformatted text."""
  405. text = self.escape(expandtabs(text))
  406. return replace(text, '\n\n', '\n \n', '\n\n', '\n \n',
  407. ' ', '&nbsp;', '\n', '<br>\n')
  408. def multicolumn(self, list, format, cols=4):
  409. """Format a list of items into a multi-column list."""
  410. result = ''
  411. rows = (len(list)+cols-1)/cols
  412. for col in range(cols):
  413. result = result + '<td width="%d%%" valign=top>' % (100/cols)
  414. for i in range(rows*col, rows*col+rows):
  415. if i < len(list):
  416. result = result + format(list[i]) + '<br>\n'
  417. result = result + '</td>'
  418. return '<table width="100%%" summary="list"><tr>%s</tr></table>' % result
  419. def grey(self, text): return '<font color="#909090">%s</font>' % text
  420. def namelink(self, name, *dicts):
  421. """Make a link for an identifier, given name-to-URL mappings."""
  422. for dict in dicts:
  423. if name in dict:
  424. return '<a href="%s">%s</a>' % (dict[name], name)
  425. return name
  426. def classlink(self, object, modname):
  427. """Make a link for a class."""
  428. name, module = object.__name__, sys.modules.get(object.__module__)
  429. if hasattr(module, name) and getattr(module, name) is object:
  430. return '<a href="%s.html#%s">%s</a>' % (
  431. module.__name__, name, classname(object, modname))
  432. return classname(object, modname)
  433. def modulelink(self, object):
  434. """Make a link for a module."""
  435. return '<a href="%s.html">%s</a>' % (object.__name__, object.__name__)
  436. def modpkglink(self, (name, path, ispackage, shadowed)):
  437. """Make a link for a module or package to display in an index."""
  438. if shadowed:
  439. return self.grey(name)
  440. if path:
  441. url = '%s.%s.html' % (path, name)
  442. else:
  443. url = '%s.html' % name
  444. if ispackage:
  445. text = '<strong>%s</strong>&nbsp;(package)' % name
  446. else:
  447. text = name
  448. return '<a href="%s">%s</a>' % (url, text)
  449. def markup(self, text, escape=None, funcs={}, classes={}, methods={}):
  450. """Mark up some plain text, given a context of symbols to look for.
  451. Each context dictionary maps object names to anchor names."""
  452. escape = escape or self.escape
  453. results = []
  454. here = 0
  455. pattern = re.compile(r'\b((http|ftp)://\S+[\w/]|'
  456. r'RFC[- ]?(\d+)|'
  457. r'PEP[- ]?(\d+)|'
  458. r'(self\.)?(\w+))')
  459. while True:
  460. match = pattern.search(text, here)
  461. if not match: break
  462. start, end = match.span()
  463. results.append(escape(text[here:start]))
  464. all, scheme, rfc, pep, selfdot, name = match.groups()
  465. if scheme:
  466. url = escape(all).replace('"', '&quot;')
  467. results.append('<a href="%s">%s</a>' % (url, url))
  468. elif rfc:
  469. url = 'http://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc)
  470. results.append('<a href="%s">%s</a>' % (url, escape(all)))
  471. elif pep:
  472. url = 'http://www.python.org/peps/pep-%04d.html' % int(pep)
  473. results.append('<a href="%s">%s</a>' % (url, escape(all)))
  474. elif text[end:end+1] == '(':
  475. results.append(self.namelink(name, methods, funcs, classes))
  476. elif selfdot:
  477. results.append('self.<strong>%s</strong>' % name)
  478. else:
  479. results.append(self.namelink(name, classes))
  480. here = end
  481. results.append(escape(text[here:]))
  482. return join(results, '')
  483. # ---------------------------------------------- type-specific routines
  484. def formattree(self, tree, modname, parent=None):
  485. """Produce HTML for a class tree as given by inspect.getclasstree()."""
  486. result = ''
  487. for entry in tree:
  488. if type(entry) is type(()):
  489. c, bases = entry
  490. result = result + '<dt><font face="helvetica, arial">'
  491. result = result + self.classlink(c, modname)
  492. if bases and bases != (parent,):
  493. parents = []
  494. for base in bases:
  495. parents.append(self.classlink(base, modname))
  496. result = result + '(' + join(parents, ', ') + ')'
  497. result = result + '\n</font></dt>'
  498. elif type(entry) is type([]):
  499. result = result + '<dd>\n%s</dd>\n' % self.formattree(
  500. entry, modname, c)
  501. return '<dl>\n%s</dl>\n' % result
  502. def docmodule(self, object, name=None, mod=None, *ignored):
  503. """Produce HTML documentation for a module object."""
  504. name = object.__name__ # ignore the passed-in name
  505. try:
  506. all = object.__all__
  507. except AttributeError:
  508. all = None
  509. parts = split(name, '.')
  510. links = []
  511. for i in range(len(parts)-1):
  512. links.append(
  513. '<a href="%s.html"><font color="#ffffff">%s</font></a>' %
  514. (join(parts[:i+1], '.'), parts[i]))
  515. linkedname = join(links + parts[-1:], '.')
  516. head = '<big><big><strong>%s</strong></big></big>' % linkedname
  517. try:
  518. path = inspect.getabsfile(object)
  519. url = path
  520. if sys.platform == 'win32':
  521. import nturl2path
  522. url = nturl2path.pathname2url(path)
  523. filelink = '<a href="file:%s">%s</a>' % (url, path)
  524. except TypeError:
  525. filelink = '(built-in)'
  526. info = []
  527. if hasattr(object, '__version__'):
  528. version = str(object.__version__)
  529. if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
  530. version = strip(version[11:-1])
  531. info.append('version %s' % self.escape(version))
  532. if hasattr(object, '__date__'):
  533. info.append(self.escape(str(object.__date__)))
  534. if info:
  535. head = head + ' (%s)' % join(info, ', ')
  536. docloc = self.getdocloc(object)
  537. if docloc is not None:
  538. docloc = '<br><a href="%(docloc)s">Module Docs</a>' % locals()
  539. else:
  540. docloc = ''
  541. result = self.heading(
  542. head, '#ffffff', '#7799ee',
  543. '<a href=".">index</a><br>' + filelink + docloc)
  544. modules = inspect.getmembers(object, inspect.ismodule)
  545. classes, cdict = [], {}
  546. for key, value in inspect.getmembers(object, inspect.isclass):
  547. # if __all__ exists, believe it. Otherwise use old heuristic.
  548. if (all is not None or
  549. (inspect.getmodule(value) or object) is object):
  550. if visiblename(key, all):
  551. classes.append((key, value))
  552. cdict[key] = cdict[value] = '#' + key
  553. for key, value in classes:
  554. for base in value.__bases__:
  555. key, modname = base.__name__, base.__module__
  556. module = sys.modules.get(modname)
  557. if modname != name and module and hasattr(module, key):
  558. if getattr(module, key) is base:
  559. if not key in cdict:
  560. cdict[key] = cdict[base] = modname + '.html#' + key
  561. funcs, fdict = [], {}
  562. for key, value in inspect.getmembers(object, inspect.isroutine):
  563. # if __all__ exists, believe it. Otherwise use old heuristic.
  564. if (all is not None or
  565. inspect.isbuiltin(value) or inspect.getmodule(value) is object):
  566. if visiblename(key, all):
  567. funcs.append((key, value))
  568. fdict[key] = '#-' + key
  569. if inspect.isfunction(value): fdict[value] = fdict[key]
  570. data = []
  571. for key, value in inspect.getmembers(object, isdata):
  572. if visiblename(key, all):
  573. data.append((key, value))
  574. doc = self.markup(getdoc(object), self.preformat, fdict, cdict)
  575. doc = doc and '<tt>%s</tt>' % doc
  576. result = result + '<p>%s</p>\n' % doc
  577. if hasattr(object, '__path__'):
  578. modpkgs = []
  579. for importer, modname, ispkg in pkgutil.iter_modules(object.__path__):
  580. modpkgs.append((modname, name, ispkg, 0))
  581. modpkgs.sort()
  582. contents = self.multicolumn(modpkgs, self.modpkglink)
  583. result = result + self.bigsection(
  584. 'Package Contents', '#ffffff', '#aa55cc', contents)
  585. elif modules:
  586. contents = self.multicolumn(
  587. modules, lambda (key, value), s=self: s.modulelink(value))
  588. result = result + self.bigsection(
  589. 'Modules', '#fffff', '#aa55cc', contents)
  590. if classes:
  591. classlist = map(lambda (key, value): value, classes)
  592. contents = [
  593. self.formattree(inspect.getclasstree(classlist, 1), name)]
  594. for key, value in classes:
  595. contents.append(self.document(value, key, name, fdict, cdict))
  596. result = result + self.bigsection(
  597. 'Classes', '#ffffff', '#ee77aa', join(contents))
  598. if funcs:
  599. contents = []
  600. for key, value in funcs:
  601. contents.append(self.document(value, key, name, fdict, cdict))
  602. result = result + self.bigsection(
  603. 'Functions', '#ffffff', '#eeaa77', join(contents))
  604. if data:
  605. contents = []
  606. for key, value in data:
  607. contents.append(self.document(value, key))
  608. result = result + self.bigsection(
  609. 'Data', '#ffffff', '#55aa55', join(contents, '<br>\n'))
  610. if hasattr(object, '__author__'):
  611. contents = self.markup(str(object.__author__), self.preformat)
  612. result = result + self.bigsection(
  613. 'Author', '#ffffff', '#7799ee', contents)
  614. if hasattr(object, '__credits__'):
  615. contents = self.markup(str(object.__credits__), self.preformat)
  616. result = result + self.bigsection(
  617. 'Credits', '#ffffff', '#7799ee', contents)
  618. return result
  619. def docclass(self, object, name=None, mod=None, funcs={}, classes={},
  620. *ignored):
  621. """Produce HTML documentation for a class object."""
  622. realname = object.__name__
  623. name = name or realname
  624. bases = object.__bases__
  625. contents = []
  626. push = contents.append
  627. # Cute little class to pump out a horizontal rule between sections.
  628. class HorizontalRule:
  629. def __init__(self):
  630. self.needone = 0
  631. def maybe(self):
  632. if self.needone:
  633. push('<hr>\n')
  634. self.needone = 1
  635. hr = HorizontalRule()
  636. # List the mro, if non-trivial.
  637. mro = deque(inspect.getmro(object))
  638. if len(mro) > 2:
  639. hr.maybe()
  640. push('<dl><dt>Method resolution order:</dt>\n')
  641. for base in mro:
  642. push('<dd>%s</dd>\n' % self.classlink(base,
  643. object.__module__))
  644. push('</dl>\n')
  645. def spill(msg, attrs, predicate):
  646. ok, attrs = _split_list(attrs, predicate)
  647. if ok:
  648. hr.maybe()
  649. push(msg)
  650. for name, kind, homecls, value in ok:
  651. push(self.document(getattr(object, name), name, mod,
  652. funcs, classes, mdict, object))
  653. push('\n')
  654. return attrs
  655. def spilldescriptors(msg, attrs, predicate):
  656. ok, attrs = _split_list(attrs, predicate)
  657. if ok:
  658. hr.maybe()
  659. push(msg)
  660. for name, kind, homecls, value in ok:
  661. push(self._docdescriptor(name, value, mod))
  662. return attrs
  663. def spilldata(msg, attrs, predicate):
  664. ok, attrs = _split_list(attrs, predicate)
  665. if ok:
  666. hr.maybe()
  667. push(msg)
  668. for name, kind, homecls, value in ok:
  669. base = self.docother(getattr(object, name), name, mod)
  670. if callable(value) or inspect.isdatadescriptor(value):
  671. doc = getattr(value, "__doc__", None)
  672. else:
  673. doc = None
  674. if doc is None:
  675. push('<dl><dt>%s</dl>\n' % base)
  676. else:
  677. doc = self.markup(getdoc(value), self.preformat,
  678. funcs, classes, mdict)
  679. doc = '<dd><tt>%s</tt>' % doc
  680. push('<dl><dt>%s%s</dl>\n' % (base, doc))
  681. push('\n')
  682. return attrs
  683. attrs = filter(lambda (name, kind, cls, value): visiblename(name),
  684. classify_class_attrs(object))
  685. mdict = {}
  686. for key, kind, homecls, value in attrs:
  687. mdict[key] = anchor = '#' + name + '-' + key
  688. value = getattr(object, key)
  689. try:
  690. # The value may not be hashable (e.g., a data attr with
  691. # a dict or list value).
  692. mdict[value] = anchor
  693. except TypeError:
  694. pass
  695. while attrs:
  696. if mro:
  697. thisclass = mro.popleft()
  698. else:
  699. thisclass = attrs[0][2]
  700. attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
  701. if thisclass is __builtin__.object:
  702. attrs = inherited
  703. continue
  704. elif thisclass is object:
  705. tag = 'defined here'
  706. else:
  707. tag = 'inherited from %s' % self.classlink(thisclass,
  708. object.__module__)
  709. tag += ':<br>\n'
  710. # Sort attrs by name.
  711. try:
  712. attrs.sort(key=lambda t: t[0])
  713. except TypeError:
  714. attrs.sort(lambda t1, t2: cmp(t1[0], t2[0])) # 2.3 compat
  715. # Pump out the attrs, segregated by kind.
  716. attrs = spill('Methods %s' % tag, attrs,
  717. lambda t: t[1] == 'method')
  718. attrs = spill('Class methods %s' % tag, attrs,
  719. lambda t: t[1] == 'class method')
  720. attrs = spill('Static methods %s' % tag, attrs,
  721. lambda t: t[1] == 'static method')
  722. attrs = spilldescriptors('Data descriptors %s' % tag, attrs,
  723. lambda t: t[1] == 'data descriptor')
  724. attrs = spilldata('Data and other attributes %s' % tag, attrs,
  725. lambda t: t[1] == 'data')
  726. assert attrs == []
  727. attrs = inherited
  728. contents = ''.join(contents)
  729. if name == realname:
  730. title = '<a name="%s">class <strong>%s</strong></a>' % (
  731. name, realname)
  732. else:
  733. title = '<strong>%s</strong> = <a name="%s">class %s</a>' % (
  734. name, name, realname)
  735. if bases:
  736. parents = []
  737. for base in bases:
  738. parents.append(self.classlink(base, object.__module__))
  739. title = title + '(%s)' % join(parents, ', ')
  740. doc = self.markup(getdoc(object), self.preformat, funcs, classes, mdict)
  741. doc = doc and '<tt>%s<br>&nbsp;</tt>' % doc
  742. return self.section(title, '#000000', '#ffc8d8', contents, 3, doc)
  743. def formatvalue(self, object):
  744. """Format an argument default value as text."""
  745. return self.grey('=' + self.repr(object))
  746. def docroutine(self, object, name=None, mod=None,
  747. funcs={}, classes={}, methods={}, cl=None):
  748. """Produce HTML documentation for a function or method object."""
  749. realname = object.__name__
  750. name = name or realname
  751. anchor = (cl and cl.__name__ or '') + '-' + name
  752. note = ''
  753. skipdocs = 0
  754. if inspect.ismethod(object):
  755. imclass = object.im_class
  756. if cl:
  757. if imclass is not cl:
  758. note = ' from ' + self.classlink(imclass, mod)
  759. else:
  760. if object.im_self is not None:
  761. note = ' method of %s instance' % self.classlink(
  762. object.im_self.__class__, mod)
  763. else:
  764. note = ' unbound %s method' % self.classlink(imclass,mod)
  765. object = object.im_func
  766. if name == realname:
  767. title = '<a name="%s"><strong>%s</strong></a>' % (anchor, realname)
  768. else:
  769. if (cl and realname in cl.__dict__ and
  770. cl.__dict__[realname] is object):
  771. reallink = '<a href="#%s">%s</a>' % (
  772. cl.__name__ + '-' + realname, realname)
  773. skipdocs = 1
  774. else:
  775. reallink = realname
  776. title = '<a name="%s"><strong>%s</strong></a> = %s' % (
  777. anchor, name, reallink)
  778. if inspect.isfunction(object):
  779. args, varargs, varkw, defaults = inspect.getargspec(object)
  780. argspec = inspect.formatargspec(
  781. args, varargs, varkw, defaults, formatvalue=self.formatvalue)
  782. if realname == '<lambda>':
  783. title = '<strong>%s</strong> <em>lambda</em> ' % name
  784. argspec = argspec[1:-1] # remove parentheses
  785. else:
  786. argspec = '(...)'
  787. decl = title + argspec + (note and self.grey(
  788. '<font face="helvetica, arial">%s</font>' % note))
  789. if skipdocs:
  790. return '<dl><dt>%s</dt></dl>\n' % decl
  791. else:
  792. doc = self.markup(
  793. getdoc(object), self.preformat, funcs, classes, methods)
  794. doc = doc and '<dd><tt>%s</tt></dd>' % doc
  795. return '<dl><dt>%s</dt>%s</dl>\n' % (decl, doc)
  796. def _docdescriptor(self, name, value, mod):
  797. results = []
  798. push = results.append
  799. if name:
  800. push('<dl><dt><strong>%s</strong></dt>\n' % name)
  801. if value.__doc__ is not None:
  802. doc = self.markup(getdoc(value), self.preformat)
  803. push('<dd><tt>%s</tt></dd>\n' % doc)
  804. push('</dl>\n')
  805. return ''.join(results)
  806. def docproperty(self, object, name=None, mod=None, cl=None):
  807. """Produce html documentation for a property."""
  808. return self._docdescriptor(name, object, mod)
  809. def docother(self, object, name=None, mod=None, *ignored):
  810. """Produce HTML documentation for a data object."""
  811. lhs = name and '<strong>%s</strong> = ' % name or ''
  812. return lhs + self.repr(object)
  813. def docdata(self, object, name=None, mod=None, cl=None):
  814. """Produce html documentation for a data descriptor."""
  815. return self._docdescriptor(name, object, mod)
  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. for importer, name, ispkg in pkgutil.iter_modules([dir]):
  821. modpkgs.append((name, '', ispkg, name in shadowed))
  822. shadowed[name] = 1
  823. modpkgs.sort()
  824. contents = self.multicolumn(modpkgs, self.modpkglink)
  825. return self.bigsection(dir, '#ffffff', '#ee77aa', contents)
  826. # -------------------------------------------- text documentation generator
  827. class TextRepr(Repr):
  828. """Class for safely making a text representation of a Python object."""
  829. def __init__(self):
  830. Repr.__init__(self)
  831. self.maxlist = self.maxtuple = 20
  832. self.maxdict = 10
  833. self.maxstring = self.maxother = 100
  834. def repr1(self, x, level):
  835. if hasattr(type(x), '__name__'):
  836. methodname = 'repr_' + join(split(type(x).__name__), '_')
  837. if hasattr(self, methodname):
  838. return getattr(self, methodname)(x, level)
  839. return cram(stripid(repr(x)), self.maxother)
  840. def repr_string(self, x, level):
  841. test = cram(x, self.maxstring)
  842. testrepr = repr(test)
  843. if '\\' in test and '\\' not in replace(testrepr, r'\\', ''):
  844. # Backslashes are only literal in the string and are never
  845. # needed to make any special characters, so show a raw string.
  846. return 'r' + testrepr[0] + test + testrepr[0]
  847. return testrepr
  848. repr_str = repr_string
  849. def repr_instance(self, x, level):
  850. try:
  851. return cram(stripid(repr(x)), self.maxstring)
  852. except:
  853. return '<%s instance>' % x.__class__.__name__
  854. class TextDoc(Doc):
  855. """Formatter class for text documentation."""
  856. # ------------------------------------------- text formatting utilities
  857. _repr_instance = TextRepr()
  858. repr = _repr_instance.repr
  859. def bold(self, text):
  860. """Format a string in bold by overstriking."""
  861. return join(map(lambda ch: ch + '\b' + ch, text), '')
  862. def indent(self, text, prefix=' '):
  863. """Indent text by prepending a given prefix to each line."""
  864. if not text: return ''
  865. lines = split(text, '\n')
  866. lines = map(lambda line, prefix=prefix: prefix + line, lines)
  867. if lines: lines[-1] = rstrip(lines[-1])
  868. return join(lines, '\n')
  869. def section(self, title, contents):
  870. """Format a section with a given heading."""
  871. return self.bold(title) + '\n' + rstrip(self.indent(contents)) + '\n\n'
  872. # ---------------------------------------------- type-specific routines
  873. def formattree(self, tree, modname, parent=None, prefix=''):
  874. """Render in text a class tree as returned by inspect.getclasstree()."""
  875. result = ''
  876. for entry in tree:
  877. if type(entry) is type(()):
  878. c, bases = entry
  879. result = result + prefix + classname(c, modname)
  880. if bases and bases != (parent,):
  881. parents = map(lambda c, m=modname: classname(c, m), bases)
  882. result = result + '(%s)' % join(parents, ', ')
  883. result = result + '\n'
  884. elif type(entry) is type([]):
  885. result = result + self.formattree(
  886. entry, modname, c, prefix + ' ')
  887. return result
  888. def docmodule(self, object, name=None, mod=None):
  889. """Produce text documentation for a given module object."""
  890. name = object.__name__ # ignore the passed-in name
  891. synop, desc = splitdoc(getdoc(object))
  892. result = self.section('NAME', name + (synop and ' - ' + synop))
  893. try:
  894. all = object.__all__
  895. except AttributeError:
  896. all = None
  897. try:
  898. file = inspect.getabsfile(object)
  899. except TypeError:
  900. file = '(built-in)'
  901. result = result + self.section('FILE', file)
  902. docloc = self.getdocloc(object)
  903. if docloc is not None:
  904. result = result + self.section('MODULE DOCS', docloc)
  905. if desc:
  906. result = result + self.section('DESCRIPTION', desc)
  907. classes = []
  908. for key, value in inspect.getmembers(object, inspect.isclass):
  909. # if __all__ exists, believe it. Otherwise use old heuristic.
  910. if (all is not None
  911. or (inspect.getmodule(value) or object) is object):
  912. if visiblename(key, all):
  913. classes.append((key, value))
  914. funcs = []
  915. for key, value in inspect.getmembers(object, inspect.isroutine):
  916. # if __all__ exists, believe it. Otherwise use old heuristic.
  917. if (all is not None or
  918. inspect.isbuiltin(value) or inspect.getmodule(value) is object):
  919. if visiblename(key, all):
  920. funcs.append((key, value))
  921. data = []
  922. for key, value in inspect.getmembers(object, isdata):
  923. if visiblename(key, all):
  924. data.append((key, value))
  925. if hasattr(object, '__path__'):
  926. modpkgs = []
  927. for importer, modname, ispkg in pkgutil.iter_modules(object.__path__):
  928. if ispkg:
  929. modpkgs.append(modname + ' (package)')
  930. else:
  931. modpkgs.append(modname)
  932. modpkgs.sort()
  933. result = result + self.section(
  934. 'PACKAGE CONTENTS', join(modpkgs, '\n'))
  935. if classes:
  936. classlist = map(lambda (key, value): value, classes)
  937. contents = [self.formattree(
  938. inspect.getclasstree(classlist, 1), name)]
  939. for key, value in classes:
  940. contents.append(self.document(value, key, name))
  941. result = result + self.section('CLASSES', join(contents, '\n'))
  942. if funcs:
  943. contents = []
  944. for key, value in funcs:
  945. contents.append(self.document(value, key, name))
  946. result = result + self.section('FUNCTIONS', join(contents, '\n'))
  947. if data:
  948. contents = []
  949. for key, value in data:
  950. contents.append(self.docother(value, key, name, maxlen=70))
  951. result = result + self.section('DATA', join(contents, '\n'))
  952. if hasattr(object, '__version__'):
  953. version = str(object.__version__)
  954. if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
  955. version = strip(version[11:-1])
  956. result = result + self.section('VERSION', version)
  957. if hasattr(object, '__date__'):
  958. result = result + self.section('DATE', str(object.__date__))
  959. if hasattr(object, '__author__'):
  960. result = result + self.section('AUTHOR', str(object.__author__))
  961. if hasattr(object, '__credits__'):
  962. result = result + self.section('CREDITS', str(object.__credits__))
  963. return result
  964. def docclass(self, object, name=None, mod=None):
  965. """Produce text documentation for a given class object."""
  966. realname = object.__name__
  967. name = name or realname
  968. bases = object.__bases__
  969. def makename(c, m=object.__module__):
  970. return classname(c, m)
  971. if name == realname:
  972. title = 'class ' + self.bold(realname)
  973. else:
  974. title = self.bold(name) + ' = class ' + realname
  975. if bases:
  976. parents = map(makename, bases)
  977. title = title + '(%s)' % join(parents, ', ')
  978. doc = getdoc(object)
  979. contents = doc and [doc + '\n'] or []
  980. push = contents.append
  981. # List the mro, if non-trivial.
  982. mro = deque(inspect.getmro(object))
  983. if len(mro) > 2:
  984. push("Method resolution order:")
  985. for base in mro:
  986. push(' ' + makename(base))
  987. push('')
  988. # Cute little class to pump out a horizontal rule between sections.
  989. class HorizontalRule:
  990. def __init__(self):
  991. self.needone = 0
  992. def maybe(self):
  993. if self.needone:
  994. push('-' * 70)
  995. self.needone = 1
  996. hr = HorizontalRule()
  997. def spill(msg, attrs, predicate):
  998. ok, attrs = _split_list(attrs, predicate)
  999. if ok:
  1000. hr.maybe()
  1001. push(msg)
  1002. for name, kind, homecls, value in ok:
  1003. push(self.document(getattr(object, name),
  1004. name, mod, object))
  1005. return attrs
  1006. def spilldescriptors(msg, attrs, predicate):
  1007. ok, attrs = _split_list(attrs, predicate)
  1008. if ok:
  1009. hr.maybe()
  1010. push(msg)
  1011. for name, kind, homecls, value in ok:
  1012. push(self._docdescriptor(name, value, mod))
  1013. return attrs
  1014. def spilldata(msg, attrs, predicate):
  1015. ok, attrs = _split_list(attrs, predicate)
  1016. if ok:
  1017. hr.maybe()
  1018. push(msg)
  1019. for name, kind, homecls, value in ok:
  1020. if callable(value) or inspect.isdatadescriptor(value):
  1021. doc = getdoc(value)
  1022. else:
  1023. doc = None
  1024. push(self.docother(getattr(object, name),
  1025. name, mod, maxlen=70, doc=doc) + '\n')
  1026. return attrs
  1027. attrs = filter(lambda (name, kind, cls, value): visiblename(name),
  1028. classify_class_attrs(object))
  1029. while attrs:
  1030. if mro:
  1031. thisclass = mro.popleft()
  1032. else:
  1033. thisclass = attrs[0][2]
  1034. attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
  1035. if thisclass is __builtin__.object:
  1036. attrs = inherited
  1037. continue
  1038. elif thisclass is object:
  1039. tag = "defined here"
  1040. else:
  1041. tag = "inherited from %s" % classname(thisclass,
  1042. object.__module__)
  1043. filter(lambda t: not t[0].startswith('_'), attrs)
  1044. # Sort attrs by name.
  1045. attrs.sort()
  1046. # Pump out the attrs, segregated by kind.
  1047. attrs = spill("Methods %s:\n" % tag, attrs,
  1048. lambda t: t[1] == 'method')
  1049. attrs = spill("Class methods %s:\n" % tag, attrs,
  1050. lambda t: t[1] == 'class method')
  1051. attrs = spill("Static methods %s:\n" % tag, attrs,
  1052. lambda t: t[1] == 'static method')
  1053. attrs = spilldescriptors("Data descriptors %s:\n" % tag, attrs,
  1054. lambda t: t[1] == 'data descriptor')
  1055. attrs = spilldata("Data and other attributes %s:\n" % tag, attrs,
  1056. lambda t: t[1] == 'data')
  1057. assert attrs == []
  1058. attrs = inherited
  1059. contents = '\n'.join(contents)
  1060. if not contents:
  1061. return title + '\n'
  1062. return title + '\n' + self.indent(rstrip(contents), ' | ') + '\n'
  1063. def formatvalue(self, object):
  1064. """Format an argument default value as text."""
  1065. return '=' + self.repr(object)
  1066. def docroutine(self, object, name=None, mod=None, cl=None):
  1067. """Produce text documentation for a function or method object."""
  1068. realname = object.__name__
  1069. name = name or realname
  1070. note = ''
  1071. skipdocs = 0
  1072. if inspect.ismethod(object):
  1073. imclass = object.im_class
  1074. if cl:
  1075. if imclass is not cl:
  1076. note = ' from ' + classname(imclass, mod)
  1077. else:
  1078. if object.im_self is not None:
  1079. note = ' method of %s instance' % classname(
  1080. object.im_self.__class__, mod)
  1081. else:
  1082. note = ' unbound %s method' % classname(imclass,mod)
  1083. object = object.im_func
  1084. if name == realname:
  1085. title = self.bold(realname)
  1086. else:
  1087. if (cl and realname in cl.__dict__ and
  1088. cl.__dict__[realname] is object):
  1089. skipdocs = 1
  1090. title = self.bold(name) + ' = ' + realname
  1091. if inspect.isfunction(object):
  1092. args, varargs, varkw, defaults = inspect.getargspec(object)
  1093. argspec = inspect.formatargspec(
  1094. args, varargs, varkw, defaults, formatvalue=self.formatvalue)
  1095. if realname == '<lambda>':
  1096. title = self.bold(name) + ' lambda '

Large files files are truncated, but you can click here to view the full file