viewing pydoc output in FireFox

Jeff Epler jepler at unpythonic.net
Sat Oct 16 21:49:03 EDT 2004


I don't know.  My mozilla (firefox 0.9.3 on linux) displays ".py" files
from the file: URLs as text without any special configuration.

Here's a patch to pydoc to serve .py source files over http as
text/plain.  It should be fairly easy to apply manually if you don't
have a unix-style "patch" program on your windows machine (just remove
the lines marked with "-" and add the ones marked with "+", at around
the line numbers given in the '@@" line).

I tested it minimally (again, on my linux machine with firefox 0.9.3)
and it works nicely.  Modules and packages display their source code,
and non-.py files are no longer linked.  instead you see text like
    /usr/lib/python2.3/lib-dynload/_tkinter.so (Shared module)

Please feel free to use these changes under the terms of the python
license.

--- /usr/lib/python2.3/pydoc.py	2004-05-07 09:52:46.000000000 -0500
+++ ./pydoc.py	2004-10-16 20:38:59.618007763 -0500
@@ -515,11 +515,10 @@
         head = '<big><big><strong>%s</strong></big></big>' % linkedname
         try:
             path = inspect.getabsfile(object)
-            url = path
-            if sys.platform == 'win32':
-                import nturl2path
-                url = nturl2path.pathname2url(path)
-            filelink = '<a href="file:%s">%s</a>' % (url, path)
+            if not path.endswith(".py"):
+                filelink = "%s (Shared module)" % path
+            else:
+                filelink = '<a href="/%s.txt">%s</a>' % (".".join(parts), path)
         except TypeError:
             filelink = '(built-in)'
         info = []
@@ -1812,8 +1811,28 @@
                 self.wfile.write(html.page(title, contents))
             except IOError: pass
 
+        def send_text(self, contents):
+            try:
+                self.send_response(200)
+                self.send_header('Content-Type', 'text/plain')
+                self.end_headers()
+                self.wfile.write(contents)
+            except IOError: pass
+
         def do_GET(self):
             path = self.path
+            if path.endswith(".txt"):
+                path = path[:-4]
+                if path[:1] == '/': path = path[1:]
+                try:
+                    obj = locate(path, forceload=1)
+                except ErrorDuringImport, value:
+                    self.send_document(path, html.escape(str(value)))
+                    return
+                f = inspect.getabsfile(obj)
+                    
+                self.send_text(open(f).read())
+                return
             if path[-5:] == '.html': path = path[:-5]
             if path[:1] == '/': path = path[1:]
             if path and path != '.':
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20041016/a40fe877/attachment.sig>


More information about the Python-list mailing list