Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9e213e

Browse files
committed
Hide private names beginning with _ (but don't hide __special__ names).
Clean up section headings; make the bars on the left less fat. Adjust the display of properties slightly. Don't show stuff inherited from the base 'object' type.
1 parent b38bbbd commit d9e213e

1 file changed

Lines changed: 77 additions & 54 deletions

File tree

Lib/pydoc.py

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class or function within a module or module in a package. If the
4444
# the current directory is changed with os.chdir(), an incorrect
4545
# path will be displayed.
4646

47-
import sys, imp, os, re, types, inspect
47+
import sys, imp, os, re, types, inspect, __builtin__
4848
from repr import Repr
4949
from string import expandtabs, find, join, lower, split, strip, rfind, rstrip
5050

@@ -142,6 +142,15 @@ def _split_list(s, predicate):
142142
no.append(x)
143143
return yes, no
144144

145+
def visiblename(name):
146+
"""Decide whether to show documentation on a variable."""
147+
# Certain special names are redundant.
148+
if name in ['__builtins__', '__doc__', '__file__', '__path__',
149+
'__module__', '__name__']: return 0
150+
# Private names are hidden, but special names are displayed.
151+
if name.startswith('__') and name.endswith('__'): return 1
152+
return not name.startswith('_')
153+
145154
# ----------------------------------------------------- module manipulation
146155

147156
def ispackage(path):
@@ -337,9 +346,7 @@ def page(self, title, contents):
337346
return '''
338347
<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
339348
<html><head><title>Python: %s</title>
340-
<style type="text/css"><!--
341-
TT { font-family: lucidatypewriter, lucida console, courier }
342-
--></style></head><body bgcolor="#f0f0f8">
349+
</head><body bgcolor="#f0f0f8">
343350
%s
344351
</body></html>''' % (title, contents)
345352

@@ -354,12 +361,12 @@ def heading(self, title, fgcol, bgcol, extras=''):
354361
><font color="%s" face="helvetica, arial">%s</font></td></tr></table>
355362
''' % (bgcol, fgcol, title, fgcol, extras or '&nbsp;')
356363

357-
def section(self, title, fgcol, bgcol, contents, width=10,
358-
prelude='', marginalia=None, gap='&nbsp;&nbsp;'):
364+
def section(self, title, fgcol, bgcol, contents, width=6,
365+
prelude='', marginalia=None, gap='&nbsp;'):
359366
"""Format a section with a heading."""
360367
if marginalia is None:
361368
marginalia = '<tt>' + '&nbsp;' * width + '</tt>'
362-
result = '''
369+
result = '''<p>
363370
<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
364371
<tr bgcolor="%s">
365372
<td colspan=3 valign=bottom>&nbsp;<br>
@@ -529,8 +536,9 @@ def docmodule(self, object, name=None, mod=None, *ignored):
529536
classes, cdict = [], {}
530537
for key, value in inspect.getmembers(object, inspect.isclass):
531538
if (inspect.getmodule(value) or object) is object:
532-
classes.append((key, value))
533-
cdict[key] = cdict[value] = '#' + key
539+
if visiblename(key):
540+
classes.append((key, value))
541+
cdict[key] = cdict[value] = '#' + key
534542
for key, value in classes:
535543
for base in value.__bases__:
536544
key, modname = base.__name__, base.__module__
@@ -542,12 +550,13 @@ def docmodule(self, object, name=None, mod=None, *ignored):
542550
funcs, fdict = [], {}
543551
for key, value in inspect.getmembers(object, inspect.isroutine):
544552
if inspect.isbuiltin(value) or inspect.getmodule(value) is object:
545-
funcs.append((key, value))
546-
fdict[key] = '#-' + key
547-
if inspect.isfunction(value): fdict[value] = fdict[key]
553+
if visiblename(key):
554+
funcs.append((key, value))
555+
fdict[key] = '#-' + key
556+
if inspect.isfunction(value): fdict[value] = fdict[key]
548557
data = []
549558
for key, value in inspect.getmembers(object, isdata):
550-
if key not in ['__builtins__', '__doc__']:
559+
if visiblename(key):
551560
data.append((key, value))
552561

553562
doc = self.markup(getdoc(object), self.preformat, fdict, cdict)
@@ -560,11 +569,12 @@ def docmodule(self, object, name=None, mod=None, *ignored):
560569
for file in os.listdir(object.__path__[0]):
561570
path = os.path.join(object.__path__[0], file)
562571
modname = inspect.getmodulename(file)
563-
if modname and modname not in modnames:
564-
modpkgs.append((modname, name, 0, 0))
565-
modnames.append(modname)
566-
elif ispackage(path):
567-
modpkgs.append((file, name, 1, 0))
572+
if modname != '__init__':
573+
if modname and modname not in modnames:
574+
modpkgs.append((modname, name, 0, 0))
575+
modnames.append(modname)
576+
elif ispackage(path):
577+
modpkgs.append((file, name, 1, 0))
568578
modpkgs.sort()
569579
contents = self.multicolumn(modpkgs, self.modpkglink)
570580
result = result + self.bigsection(
@@ -658,12 +668,12 @@ def spillproperties(msg, attrs, predicate):
658668
doc = self.markup(value.__doc__, self.preformat,
659669
funcs, classes, mdict)
660670
push('<dd><tt>%s</tt></dd>\n' % doc)
661-
for attr, tag in [("fget", " getter"),
662-
("fset", " setter"),
663-
("fdel", " deleter")]:
671+
for attr, tag in [('fget', '<em>get</em>'),
672+
('fset', '<em>set</em>'),
673+
('fdel', '<em>delete</em>')]:
664674
func = getattr(value, attr)
665675
if func is not None:
666-
base = self.document(func, name + tag, mod,
676+
base = self.document(func, tag, mod,
667677
funcs, classes, mdict, object)
668678
push('<dd>%s</dd>\n' % base)
669679
push('</dl>\n')
@@ -690,7 +700,8 @@ def spilldata(msg, attrs, predicate):
690700
push('\n')
691701
return attrs
692702

693-
attrs = inspect.classify_class_attrs(object)
703+
attrs = filter(lambda (name, kind, cls, value): visiblename(name),
704+
inspect.classify_class_attrs(object))
694705
mdict = {}
695706
for key, kind, homecls, value in attrs:
696707
mdict[key] = anchor = '#' + name + '-' + key
@@ -709,26 +720,29 @@ def spilldata(msg, attrs, predicate):
709720
thisclass = attrs[0][2]
710721
attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
711722

712-
if thisclass is object:
713-
tag = "defined here"
723+
if thisclass is __builtin__.object:
724+
attrs = inherited
725+
continue
726+
elif thisclass is object:
727+
tag = 'defined here'
714728
else:
715-
tag = "inherited from %s" % self.classlink(thisclass,
716-
object.__module__)
729+
tag = 'inherited from %s' % self.classlink(thisclass,
730+
object.__module__)
717731
tag += ':<br>\n'
718732

719733
# Sort attrs by name.
720734
attrs.sort(lambda t1, t2: cmp(t1[0], t2[0]))
721735

722736
# Pump out the attrs, segregated by kind.
723-
attrs = spill("Methods %s" % tag, attrs,
737+
attrs = spill('Methods %s' % tag, attrs,
724738
lambda t: t[1] == 'method')
725-
attrs = spill("Class methods %s" % tag, attrs,
739+
attrs = spill('Class methods %s' % tag, attrs,
726740
lambda t: t[1] == 'class method')
727-
attrs = spill("Static methods %s" % tag, attrs,
741+
attrs = spill('Static methods %s' % tag, attrs,
728742
lambda t: t[1] == 'static method')
729-
attrs = spillproperties("Properties %s" % tag, attrs,
743+
attrs = spillproperties('Properties %s' % tag, attrs,
730744
lambda t: t[1] == 'property')
731-
attrs = spilldata("Data and non-method functions %s" % tag, attrs,
745+
attrs = spilldata('Data and other attributes %s' % tag, attrs,
732746
lambda t: t[1] == 'data')
733747
assert attrs == []
734748
attrs = inherited
@@ -747,9 +761,9 @@ def spilldata(msg, attrs, predicate):
747761
parents.append(self.classlink(base, object.__module__))
748762
title = title + '(%s)' % join(parents, ', ')
749763
doc = self.markup(getdoc(object), self.preformat, funcs, classes, mdict)
750-
doc = doc and '<tt>%s<br>&nbsp;</tt>' % doc or '&nbsp;'
764+
doc = doc and '<tt>%s<br>&nbsp;</tt>' % doc
751765

752-
return self.section(title, '#000000', '#ffc8d8', contents, 5, doc)
766+
return self.section(title, '#000000', '#ffc8d8', contents, 3, doc)
753767

754768
def formatvalue(self, object):
755769
"""Format an argument default value as text."""
@@ -935,25 +949,28 @@ def docmodule(self, object, name=None, mod=None):
935949
classes = []
936950
for key, value in inspect.getmembers(object, inspect.isclass):
937951
if (inspect.getmodule(value) or object) is object:
938-
classes.append((key, value))
952+
if visiblename(key):
953+
classes.append((key, value))
939954
funcs = []
940955
for key, value in inspect.getmembers(object, inspect.isroutine):
941956
if inspect.isbuiltin(value) or inspect.getmodule(value) is object:
942-
funcs.append((key, value))
957+
if visiblename(key):
958+
funcs.append((key, value))
943959
data = []
944960
for key, value in inspect.getmembers(object, isdata):
945-
if key not in ['__builtins__', '__doc__']:
961+
if visiblename(key):
946962
data.append((key, value))
947963

948964
if hasattr(object, '__path__'):
949965
modpkgs = []
950966
for file in os.listdir(object.__path__[0]):
951967
path = os.path.join(object.__path__[0], file)
952968
modname = inspect.getmodulename(file)
953-
if modname and modname not in modpkgs:
954-
modpkgs.append(modname)
955-
elif ispackage(path):
956-
modpkgs.append(file + ' (package)')
969+
if modname != '__init__':
970+
if modname and modname not in modpkgs:
971+
modpkgs.append(modname)
972+
elif ispackage(path):
973+
modpkgs.append(file + ' (package)')
957974
modpkgs.sort()
958975
result = result + self.section(
959976
'PACKAGE CONTENTS', join(modpkgs, '\n'))
@@ -1052,17 +1069,16 @@ def spillproperties(msg, attrs, predicate):
10521069
if doc:
10531070
push(self.indent(doc))
10541071
need_blank_after_doc = 1
1055-
for attr, tag in [("fget", " getter"),
1056-
("fset", " setter"),
1057-
("fdel", " deleter")]:
1072+
for attr, tag in [('fget', '<get>'),
1073+
('fset', '<set>'),
1074+
('fdel', '<delete>')]:
10581075
func = getattr(value, attr)
10591076
if func is not None:
10601077
if need_blank_after_doc:
10611078
push('')
10621079
need_blank_after_doc = 0
1063-
base = self.docother(func, name + tag, mod, 70)
1080+
base = self.document(func, tag, mod)
10641081
push(self.indent(base))
1065-
push('')
10661082
return attrs
10671083

10681084
def spilldata(msg, attrs, predicate):
@@ -1079,22 +1095,27 @@ def spilldata(msg, attrs, predicate):
10791095
name, mod, 70, doc) + '\n')
10801096
return attrs
10811097

1082-
attrs = inspect.classify_class_attrs(object)
1098+
attrs = filter(lambda (name, kind, cls, value): visiblename(name),
1099+
inspect.classify_class_attrs(object))
10831100
while attrs:
10841101
if mro:
10851102
thisclass = mro.pop(0)
10861103
else:
10871104
thisclass = attrs[0][2]
10881105
attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
10891106

1090-
if thisclass is object:
1107+
if thisclass is __builtin__.object:
1108+
attrs = inherited
1109+
continue
1110+
elif thisclass is object:
10911111
tag = "defined here"
10921112
else:
10931113
tag = "inherited from %s" % classname(thisclass,
10941114
object.__module__)
1115+
filter(lambda t: not t[0].startswith('_'), attrs)
10951116

10961117
# Sort attrs by name.
1097-
attrs.sort(lambda t1, t2: cmp(t1[0], t2[0]))
1118+
attrs.sort()
10981119

10991120
# Pump out the attrs, segregated by kind.
11001121
attrs = spill("Methods %s:\n" % tag, attrs,
@@ -1105,8 +1126,8 @@ def spilldata(msg, attrs, predicate):
11051126
lambda t: t[1] == 'static method')
11061127
attrs = spillproperties("Properties %s:\n" % tag, attrs,
11071128
lambda t: t[1] == 'property')
1108-
attrs = spilldata("Data and non-method functions %s:\n" % tag,
1109-
attrs, lambda t: t[1] == 'data')
1129+
attrs = spilldata("Data and other attributes %s:\n" % tag, attrs,
1130+
lambda t: t[1] == 'data')
11101131
assert attrs == []
11111132
attrs = inherited
11121133

@@ -1316,7 +1337,6 @@ def locate(path, forceload=0):
13161337
except AttributeError: return None
13171338
return object
13181339
else:
1319-
import __builtin__
13201340
if hasattr(__builtin__, path):
13211341
return getattr(__builtin__, path)
13221342

@@ -1371,8 +1391,11 @@ def writedocs(dir, pkgpath='', done=None):
13711391
elif os.path.isfile(path):
13721392
modname = inspect.getmodulename(path)
13731393
if modname:
1374-
modname = pkgpath + modname
1375-
if not modname in done:
1394+
if modname == '__init__':
1395+
modname = pkgpath[:-1] # remove trailing period
1396+
else:
1397+
modname = pkgpath + modname
1398+
if modname not in done:
13761399
done[modname] = 1
13771400
writedoc(modname)
13781401

0 commit comments

Comments
 (0)