pydoc.Helper.help() ignores input/output init parameters (issue 940286) - Code Review (original) (raw)
OLD
NEW
1 #!/usr/bin/env python3
1 #!/usr/bin/env python3
2 """Generate Python documentation in HTML or text for interactive use.
2 """Generate Python documentation in HTML or text for interactive use.
3
3
4 In the Python interpreter, do "from pydoc import help" to provide online
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.
5 help. Calling help(thing) on a Python object documents the object.
6
6
7 Or, at the shell command line outside of Python:
7 Or, at the shell command line outside of Python:
8
8
9 Run "pydoc " to show documentation on something. may be
9 Run "pydoc " to show documentation on something. may be
10 the name of a function, module, package, or a dotted reference to a
10 the name of a function, module, package, or a dotted reference to a
(...skipping 1292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
1303 repr = self.repr(object)
1303 repr = self.repr(object)
1304 if maxlen:
1304 if maxlen:
1305 line = (name and name + ' = ' or '') + repr
1305 line = (name and name + ' = ' or '') + repr
1306 chop = maxlen - len(line)
1306 chop = maxlen - len(line)
1307 if chop < 0: repr = repr[:chop] + '...'
1307 if chop < 0: repr = repr[:chop] + '...'
1308 line = (name and self.bold(name) + ' = ' or '') + repr
1308 line = (name and self.bold(name) + ' = ' or '') + repr
1309 if doc is not None:
1309 if doc is not None:
1310 line += '\n' + self.indent(str(doc))
1310 line += '\n' + self.indent(str(doc))
1311 return line
1311 return line
1312
1312
1313 class PlainTextDoc(TextDoc):
1314 """Subclass of TextDoc which overrides string formatting"""
1315 def bold(self,text):
1316 return text
1317
1313 # --------------------------------------------------------- user interfaces
1318 # --------------------------------------------------------- user interfaces
1314
1319
1315 def pager(text):
1320 def pager(text):
1316 """The first time this is called, determine what kind of pager to use."""
1321 """The first time this is called, determine what kind of pager to use."""
1317 global pager
1322 global pager
1318 pager = getpager()
1323 pager = getpager()
1319 pager(text)
1324 pager(text)
1320
1325
1321 def getpager():
1326 def getpager():
1322 """Decide what method to use for paging through text."""
1327 """Decide what method to use for paging through text."""
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
1457 try: object = getattr(object, part)
1462 try: object = getattr(object, part)
1458 except AttributeError: return None
1463 except AttributeError: return None
1459 return object
1464 return object
1460 else:
1465 else:
1461 if hasattr(builtins, path):
1466 if hasattr(builtins, path):
1462 return getattr(builtins, path)
1467 return getattr(builtins, path)
1463
1468
1464 # --------------------------------------- interactive interpreter interface
1469 # --------------------------------------- interactive interpreter interface
1465
1470
1466 text = TextDoc()
1471 text = TextDoc()
1472 plaintext = PlainTextDoc()
1467 html = HTMLDoc()
1473 html = HTMLDoc()
1468
1474
1469 def resolve(thing, forceload=0):
1475 def resolve(thing, forceload=0):
1470 """Given an object or a path to an object, get the object and its name."""
1476 """Given an object or a path to an object, get the object and its name."""
1471 if isinstance(thing, str):
1477 if isinstance(thing, str):
1472 object = locate(thing, forceload)
1478 object = locate(thing, forceload)
1473 if not object:
1479 if not object:
1474 raise ImportError('no Python documentation found for %r' % thing)
1480 raise ImportError('no Python documentation found for %r' % thing)
1475 return object, thing
1481 return object, thing
1476 else:
1482 else:
1477 return thing, getattr(thing, '__name__', None)
1483 return thing, getattr(thing, '__name__', None)
1478
1484
1479 def render_doc(thing, title='Python Library Documentation: %s', forceload=0):
1485 def render_doc(thing, title='Python Library Documentation: %s', forceload=0, ren derer=None):
1480 """Render text documentation, given an object or a path to an object."""
1486 """Render text documentation, given an object or a path to an object."""
1487 if renderer is None:
1488 renderer = text
1481 object, name = resolve(thing, forceload)
1489 object, name = resolve(thing, forceload)
1482 desc = describe(object)
1490 desc = describe(object)
1483 module = inspect.getmodule(object)
1491 module = inspect.getmodule(object)
1484 if name and '.' in name:
1492 if name and '.' in name:
1485 desc += ' in ' + name[:name.rfind('.')]
1493 desc += ' in ' + name[:name.rfind('.')]
1486 elif module and module is not object:
1494 elif module and module is not object:
1487 desc += ' in module ' + module.__name__
1495 desc += ' in module ' + module.__name__
1488
1496
1489 if not (inspect.ismodule(object) or
1497 if not (inspect.ismodule(object) or
1490 inspect.isclass(object) or
1498 inspect.isclass(object) or
1491 inspect.isroutine(object) or
1499 inspect.isroutine(object) or
1492 inspect.isgetsetdescriptor(object) or
1500 inspect.isgetsetdescriptor(object) or
1493 inspect.ismemberdescriptor(object) or
1501 inspect.ismemberdescriptor(object) or
1494 isinstance(object, property)):
1502 isinstance(object, property)):
1495 # If the passed object is a piece of data or an instance,
1503 # If the passed object is a piece of data or an instance,
1496 # document its available methods instead of its value.
1504 # document its available methods instead of its value.
1497 object = type(object)
1505 object = type(object)
1498 desc += ' object'
1506 desc += ' object'
1499 return title % desc + '\n\n' + text.document(object, name)
1507 return title % desc + '\n\n' + renderer.document(object, name)
1500
1508
1501 def doc(thing, title='Python Library Documentation: %s', forceload=0):
1509 def doc(thing, title='Python Library Documentation: %s', forceload=0, output=Non e):
1502 """Display text documentation, given an object or a path to an object."""
1510 """Display text documentation, given an object or a path to an object."""
1503 try:
1511 try:
1504 pager(render_doc(thing, title, forceload))
1512 if output:
1513 output.write(render_doc(thing, title, forceload, plaintext))
1514 else:
1515 pager(render_doc(thing, title, forceload))
1505 except (ImportError, ErrorDuringImport) as value:
1516 except (ImportError, ErrorDuringImport) as value:
1506 print(value)
1517 print(value)
1507
1518
1508 def writedoc(thing, forceload=0):
1519 def writedoc(thing, forceload=0):
1509 """Write HTML documentation to a file in the current directory."""
1520 """Write HTML documentation to a file in the current directory."""
1510 try:
1521 try:
1511 object, name = resolve(thing, forceload)
1522 object, name = resolve(thing, forceload)
1512 page = html.page(describe(object), html.document(object, name))
1523 page = html.page(describe(object), html.document(object, name))
1513 file = open(name + '.html', 'w', encoding='utf-8')
1524 file = open(name + '.html', 'w', encoding='utf-8')
1514 file.write(page)
1525 file.write(page)
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
1747 if request == 'help': self.intro()
1758 if request == 'help': self.intro()
1748 elif request == 'keywords': self.listkeywords()
1759 elif request == 'keywords': self.listkeywords()
1749 elif request == 'symbols': self.listsymbols()
1760 elif request == 'symbols': self.listsymbols()
1750 elif request == 'topics': self.listtopics()
1761 elif request == 'topics': self.listtopics()
1751 elif request == 'modules': self.listmodules()
1762 elif request == 'modules': self.listmodules()
1752 elif request[:8] == 'modules ':
1763 elif request[:8] == 'modules ':
1753 self.listmodules(request.split()[1])
1764 self.listmodules(request.split()[1])
1754 elif request in self.symbols: self.showsymbol(request)
1765 elif request in self.symbols: self.showsymbol(request)
1755 elif request in self.keywords: self.showtopic(request)
1766 elif request in self.keywords: self.showtopic(request)
1756 elif request in self.topics: self.showtopic(request)
1767 elif request in self.topics: self.showtopic(request)
1757 elif request: doc(request, 'Help on %s:')
1768 elif request: doc(request, 'Help on %s:',output=self._output)
1758 elif isinstance(request, Helper): self()
1769 elif isinstance(request, Helper): self()
1759 else: doc(request, 'Help on %s:')
1770 else: doc(request, 'Help on %s:',output=self._output)
1760 self.output.write('\n')
1771 self.output.write('\n')
1761
1772
1762 def intro(self):
1773 def intro(self):
1763 self.output.write('''
1774 self.output.write('''
1764 Welcome to Python %s! This is the online help utility.
1775 Welcome to Python %s! This is the online help utility.
1765
1776
1766 If this is your first time using Python, you should definitely check out
1777 If this is your first time using Python, you should definitely check out
1767 the tutorial on the Internet at http://docs.python.org/tutorial/.
1778 the tutorial on the Internet at http://docs.python.org/tutorial/.
1768
1779
1769 Enter the name of any module, keyword, or topic to get help on writing
1780 Enter the name of any module, keyword, or topic to get help on writing
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
2319 %s -g
2330 %s -g
2320 Pop up a graphical interface for finding and serving documentation.
2331 Pop up a graphical interface for finding and serving documentation.
2321
2332
2322 %s -w ...
2333 %s -w ...
2323 Write out the HTML documentation for a module to a file in the current
2334 Write out the HTML documentation for a module to a file in the current
2324 directory. If contains a '%s', it is treated as a filename; if
2335 directory. If contains a '%s', it is treated as a filename; if
2325 it names a directory, documentation is written for all the contents.
2336 it names a directory, documentation is written for all the contents.
2326 """ % (cmd, os.sep, cmd, cmd, cmd, cmd, os.sep))
2337 """ % (cmd, os.sep, cmd, cmd, cmd, cmd, os.sep))
2327
2338
2328 if __name__ == '__main__': cli()
2339 if __name__ == '__main__': cli()
OLD
NEW