[FRPythoneers] pydoc patch

Theodore A. Roth troth at verinet.com
Mon Aug 6 16:20:39 MDT 2001


Hi all,

I've made some changes to the 2.1.1 pydoc source and was wondering if
anyone else thought the changes are useful enough to send to the pydoc
maintainer.

Here's what is in the patch:

- Change to allow running on a directory that is not the cwd.
  For example: I want to run pydoc on my src dir from my docs dir.
      `cd docs; pydoc -w ../src`
  This would fail before I changed pydoc. This also failed before:
      `cd docs; pydoc -w $PWD/../src`
  Both of the above work with the patch.

- Change to stop pydoc from showing all the method functions for base
  classes. For example:

      from Tkinter import *
      class MyButton(Button):
        """MyButton docs.
        """
        def __init__(self, text='press me'):
          """Init docs.
          """
          Button.__init__(self, text=text):
        def func(self, arg):
          """Func docs.
          """
          print arg

  Running pydoc on the above, would generate doc listings for _all_ the
  methods of the Button class, which I makes it very difficult to find
  the information for the MyButton methods.

  This change also stops pydoc from showing builtin functions which look
  as though they're part of the module in question, but really aren't.

  To stop this behaviour, I added the --no-show-base command line option.
  If the option isn't given, the old behaviour results.

- Change to allow pydoc to handle the RCS/CVS '$Date$' identifier with
  the __date__ global variable. Pydoc already handled the $Revision$
  identifier with the __version__ global, so it seems logical to handle
  the __date__ variable in the same manner.

Attached in my patch. If this patch is crap, let me know so I don't waste
any one else's time.

Ted

-------------- next part --------------
--- pydoc_2.1.1.py	Mon Aug  6 10:56:31 2001
+++ pydoc.py	Mon Aug  6 16:06:00 2001
@@ -115,12 +115,18 @@
             return re.sub(pattern, '>', text)
     return text
 
+# Show base is a command line option (--no-show-base)
+# Default is to show all elements of the base classes
+# or imported modules.
+show_base = 1
+
 def allmethods(cl):
     methods = {}
     for key, value in inspect.getmembers(cl, inspect.ismethod):
         methods[key] = 1
-    for base in cl.__bases__:
-        methods.update(allmethods(base)) # all your base are belong to us
+    if show_base:
+        for base in cl.__bases__:
+            methods.update(allmethods(base)) # all your base are belong to us
     for key in methods.keys():
         methods[key] = getattr(cl, key)
     return methods
@@ -498,7 +504,10 @@
                 version = strip(version[11:-1])
             info.append('version %s' % self.escape(version))
         if hasattr(object, '__date__'):
-            info.append(self.escape(str(object.__date__)))
+            date = str(object.__date__)
+            if date[:7] == '$' + 'Date: ' and date[-1:] == '$':
+                date = strip(date[7:-1])
+            info.append(self.escape(date))
         if info:
             head = head + ' (%s)' % join(info, ', ')
         result = self.heading(
@@ -521,7 +530,7 @@
                             cdict[key] = cdict[base] = modname + '.html#' + key
         funcs, fdict = [], {}
         for key, value in inspect.getmembers(object, inspect.isroutine):
-            if inspect.isbuiltin(value) or inspect.getmodule(value) is object:
+            if (inspect.isbuiltin(value) and show_base) or inspect.getmodule(value) is object:
                 funcs.append((key, value))
                 fdict[key] = '#-' + key
                 if inspect.isfunction(value): fdict[value] = fdict[key]
@@ -804,7 +813,7 @@
                 classes.append((key, value))
         funcs = []
         for key, value in inspect.getmembers(object, inspect.isroutine):
-            if inspect.isbuiltin(value) or inspect.getmodule(value) is object:
+            if (inspect.isbuiltin(value) and show_base) or inspect.getmodule(value) is object:
                 funcs.append((key, value))
         data = []
         for key, value in inspect.getmembers(object, isdata):
@@ -850,7 +859,10 @@
                 version = strip(version[11:-1])
             result = result + self.section('VERSION', version)
         if hasattr(object, '__date__'):
-            result = result + self.section('DATE', str(object.__date__))
+            date = str(object.__date__)
+            if date[:7] == '$' + 'Date: ' and date[-1:] == '$':
+                date = strip(date[7:-1])
+            result = result + self.section('DATE', date)
         if hasattr(object, '__author__'):
             result = result + self.section('AUTHOR', str(object.__author__))
         if hasattr(object, '__credits__'):
@@ -1131,6 +1143,7 @@
 
 def writedocs(dir, pkgpath='', done=None):
     """Write out HTML documentation for all modules in a directory tree."""
+    sys.path.append(dir) # so 'pydoc -w' can be run from other than dir
     if done is None: done = {}
     for file in os.listdir(dir):
         path = os.path.join(dir, file)
@@ -1811,7 +1824,7 @@
     sys.path.insert(0, '.')
 
     try:
-        opts, args = getopt.getopt(sys.argv[1:], 'gk:p:w')
+        opts, args = getopt.getopt(sys.argv[1:], 'gk:p:w', ['no-show-base'])
         writing = 0
 
         for opt, val in opts:
@@ -1834,6 +1847,9 @@
                 return
             if opt == '-w':
                 writing = 1
+            if opt == '--no-show-base':
+                global show_base
+                show_base = 0
 
         if not args: raise BadUsage
         for arg in args:
@@ -1873,6 +1889,12 @@
     Write out the HTML documentation for a module to a file in the current
     directory.  If <name> contains a '%s', it is treated as a filename; if
     it names a directory, documentation is written for all the contents.
-""" % (cmd, os.sep, cmd, cmd, cmd, cmd, os.sep)
+
+%s --no-show-base <name> ...
+    If the --no-show-base option is given, the output will not show method
+    functions for base classes.
+    The default behaviour is to show base methods. 
+
+""" % (cmd, os.sep, cmd, cmd, cmd, cmd, os.sep, cmd)
 
 if __name__ == '__main__': cli()


More information about the FRPythoneers mailing list