PyXR

C:\Python24\Lib\site-packages\pyxr \ pageText.py



0001 # Copyright 2003, Grant T. Olson, see License.txt for details
0002 
0003 from __future__ import generators
0004 from misc import urlToPath, pathToUrl, cacheLocation, pathToFileName, pythonpath, virtualLocation, version, styles
0005 
0006 from astToHtml import astToHtml
0007 
0008 unhandledError = """
0009 
0010 <h2>I Don't even know what happened here</h2>
0011 <br />
0012 <pre>
0013 TRACEBACK<font color='red'>
0014 =======================================================================
0015 %s
0016 =======================================================================
0017 </font>
0018 </pre>
0019 """
0020 
0021 pageNotFound = """
0022 <h2>You have tried to access a nonexistant file %s.</h2>
0023 <a href="/">Click here</a> to go home.
0024 """
0025 
0026 
0027 
0028 import sys
0029 class lineNumberWriter:
0030     """
0031     Adds line numbers to output every time we hit a newline
0032     """
0033     def __init__(self, output=sys.stdout.write):
0034         self.output = output
0035         self.lineNumber = 1
0036         self.writeNumber()
0037     def writeNumber(self):
0038         self.output("\n<a name='%04d'><a href='#%04d'><span class='lineNumber'>%04d</span></a></a> " % \
0039                     (self.lineNumber, self.lineNumber, self.lineNumber) )
0040         self.lineNumber += 1
0041 
0042     def write(self, text):
0043         lines = text.split("\n")
0044         self.output(lines[0])
0045         for line in lines[1:]:
0046             self.writeNumber()
0047             self.output(line)
0048 
0049 
0050 class pageText:
0051     def __init__(self, baseHREF="", write=sys.stdout.write):
0052         import sys
0053         self.write = write
0054         self.baseHREF = baseHREF
0055         def longestFirst(x,y):
0056             if len(x) > len(y):
0057                 return -1
0058             elif len(x) == len(y):
0059                 return 0
0060             else: return 1
0061         self.pythonpath =  [x for x in pythonpath if x]
0062         self.pythonpath.sort(longestFirst)
0063 
0064 
0065     def writePageHeader(self, title=""):
0066         self.write("""
0067             <html><head><title>%s</title>
0068             </head>
0069             <body>
0070             %s
0071             """ % (title,styles.stylesheet))
0072 
0073     def writePageFooter(self):
0074         self.write(styles.footer)
0075 
0076     def writeInfoPage(self):
0077         self.write("""<html>
0078             <head><title>PyXR Info</title></head>
0079             <body>
0080             <h1>General info about PyXR</h1>
0081             <ul>
0082             <li>Pybrowse is a cross-referenced hypertext sourcecode viewer for
0083             <a href='http://www.python.org/'Python</a>, similar to
0084             <a href='http://lxr.linux.no/'>lxr</a> for C (although I'm guessing
0085             implementation is quite different).
0086             <li>Unlike pydoc (which is great) PyXR grabs info via AST trees, not
0087             introspection (which is also great in Python)
0088             <li>Created by <i>logistix</i>
0089             <li>Tested on Win2K, XP, and OpenBSD.
0090             </ul>
0091             </body>
0092             </head>
0093             """)
0094         
0095     def writePathText(self, path):
0096         import os
0097         self.write("<h2>\n")
0098         startOfPath = ""
0099         for start in self.pythonpath:
0100             if path.upper().startswith(start.upper()):
0101                 startOfPath = start
0102                 break
0103         if not startOfPath:
0104             raise RuntimeError("Invalid Path")
0105 
0106         self.write("<a href='%s'>%s</a>\n" % (pathToUrl(startOfPath), startOfPath))
0107         wholePath = startOfPath
0108         path = path[len(startOfPath):].split(os.sep)
0109         for item in [x for x in path if x]:
0110             self.write(" %s " % os.sep)
0111             wholePath = os.path.join(wholePath, item)
0112             self.write("<a href='%s'>%s</a>\n" % (pathToUrl(wholePath), item))
0113         self.write("</h2>\n")
0114         
0115     def writeRootText(self):
0116         self.writePageHeader("Title\n")
0117         self.write("<h1>Welcome to PyXR!</h1>\n")
0118         self.write("<h2>Pythonpath:</h2>\n")
0119         self.write("<ul>\n")
0120         for dir in self.pythonpath:
0121             if dir:
0122                 self.write("<li><a href='%s/'>%s</a></li>\n" %  (pathToUrl(dir), dir))
0123         self.write("</ul>\n")
0124         self.writePageFooter()
0125     
0126     def writeFileText(self, path):
0127         import tokenize
0128         
0129         self.lastName = ""
0130         self.currentPos = (0,0)
0131         self.fullName = []
0132         self.writePageHeader("File- %s\n" % path)
0133         self.write(styles.header)
0134         self.writePathText(path)
0135         self.write("<hr><pre>\n")
0136 
0137         tmpPrintCollection = []        
0138         tmpPrint = astToHtml(path, lineNumberWriter(self.write).write)
0139         tmpPrint.process()
0140        
0141         self.write("</pre><hr>\n")
0142         self.writePageFooter()
0143 
0144     def writeDirText(self):
0145         self.write("<h2>\n")
0146         url = ""
0147         for item in path:
0148             if url and url[-1] != "/": url += "/"
0149             url += item
0150             self.write("<a href='%s'>%s</a>\n" % (url, item))
0151             if item[-1] == "/" or item[-3:] == '.py':
0152                 self.write("&nbsp;&nbsp;\n")
0153             else:
0154                 self.write("&nbsp;/&nbsp;\n")
0155         self.write("</h2>\n")
0156         
0157     def writePackageText(self, path):
0158         def getSourceFiles(files):
0159             return [x for x in files if x[-3:] == ".py"]
0160         def getPackages(currentPath, files):
0161             for aFile in files:
0162                 fileName = os.path.join(currentPath, aFile)
0163                 mode = os.stat(fileName)[stat.ST_MODE]
0164                 if stat.S_ISDIR(mode):
0165                     if os.path.exists( os.path.join(fileName, "__init__.py")):
0166                         yield pathToUrl(fileName)
0167             
0168         import os, stat
0169         filePath = path
0170         if os.path.exists(filePath) and os.path.isdir(filePath):
0171             files = os.listdir(filePath)
0172         else:
0173             print >> sys.stderr, "\nDirectory '%s' does not exist. Skipping" % filePath
0174             files = []
0175 
0176         self.writePageHeader("PyXR: Package %s\n" % filePath)
0177         self.write(styles.header)
0178         self.writePathText(path)
0179         self.write("<h2>Subpackages:</h2>\n")
0180         self.write("<ul>")
0181         for aFile in getPackages(filePath, files):
0182             unqualifiedName = os.path.split(aFile)[-1]
0183             self.write("<li><a href='%s/'>%s</a></li>\n" % ( aFile, unqualifiedName))
0184         self.write("</ul>")
0185 
0186         self.write("<h2>Modules</h2>")
0187         self.write("<ul>")
0188         for aFile in getSourceFiles(files):
0189             self.write("<li><a href='%s.html'>%s</a></li>\n" % ( aFile, aFile))
0190         self.write("</ul>")
0191 
0192         initFile =os.path.join(path,"__init__.py")
0193         if os.path.exists(initFile):
0194             self.write("<h2>Init code</h2>\n")
0195             self.write("<hr><pre>\n")
0196       
0197             tmpPrint = astToHtml(initFile, lineNumberWriter(self.write).write)
0198             tmpPrint.process()
0199             self.write("</pre><hr>\n")
0200         self.writePageFooter()
0201 
0202 if __name__ == '__main__':
0203     pt = pageText("/pysrc")
0204     pt.writeFileText("/c/python22/lib/base64.py")
0205     pt.writePathText("/c/python22/lib/")
0206     pt.writeRootText()
0207 

Generated by PyXR 0.9.4
SourceForge.net Logo