0001 # Copyright 2003, Grant T. Olson, see License.txt for details 0002 0003 from __future__ import generators 0004 0005 import BaseHTTPServer 0006 import sys 0007 from misc import urlToPath, pathToUrl, cacheLocation, pathToFileName, pythonpath, virtualLocation 0008 from pageText import pageText, unhandledError, pageNotFound 0009 0010 0011 class sourceHandler(BaseHTTPServer.BaseHTTPRequestHandler): 0012 useCache = 1 0013 def validatePath(self): 0014 currentPath = self.path 0015 if currentPath == "/info.html": return currentPath 0016 if currentPath[-5:] == '.html': 0017 currentPath = currentPath[:-5] 0018 dir, modules = None, None 0019 #make sure file is somewhere in pythonpath 0020 #and not an arbitrary system file 0021 for path in self.pythonpath: 0022 if currentPath.startswith(path): 0023 dir, modules = path, currentPath[len(path):].split("/") 0024 break 0025 if not dir: return None 0026 import os 0027 if not os.access(urlToPath(currentPath), os.F_OK): return None 0028 return urlToPath(currentPath) 0029 0030 def writeRoot(self): 0031 pageText(write=self.wfile.write).writeRootText() 0032 0033 def writeInfo(self): 0034 pageText(write=self.wfile.write).writeInfoPage() 0035 0036 def writePath(self, path): 0037 pageText(write=self.wfile.write).writeDirText() 0038 0039 def writePackage(self): 0040 pageText(write=self.wfile.write).writePackageText(urlToPath(self.path)) 0041 0042 def writeFile(self): 0043 pageText(write=self.wfile.write).writeFileText(urlToPath(self.path[:-5])) 0044 0045 def do_GET(self): 0046 self.setPythonPath() 0047 self.send_response(200) 0048 self.send_header('Content-Type', 'text/html') 0049 self.end_headers() 0050 try: 0051 if self.path == "/" or self.path == virtualLocation or self.path == virtualLocation + "/": 0052 self.writeRoot() 0053 else: 0054 checkPath = self.validatePath() 0055 if checkPath: 0056 if checkPath == "/info.html": 0057 self.writeInfo() 0058 elif checkPath[-3:] == '.py': 0059 self.writeFile() 0060 else: 0061 if self.path[-1] != "/": self.path += "/" #normalized dirs 0062 self.writePackage() 0063 else: 0064 self.send_error(404, pageNotFound % self.path) 0065 except: 0066 import traceback 0067 errorText = unhandledError % reduce(lambda x,y: x + y, \ 0068 traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) 0069 self.send_error(500, errorText) 0070 raise 0071 def setPythonPath(self): 0072 """ Windows version: get python path""" 0073 def longestFirst(x,y): 0074 if len(x) > len(y): 0075 return -1 0076 elif len(x) == len(y): 0077 return 0 0078 else: return 1 0079 self.pythonpath = [pathToUrl(x) + "/" for x in pythonpath if x] 0080 self.pythonpath.sort(longestFirst) 0081 0082 if __name__ == "__main__": 0083 import sys 0084 import astToHtml 0085 import lexicalTree 0086 astToHtml = astToHtml.astToHtml 0087 lexicalTree = lexicalTree.lexicalTree 0088 repeats = 100 0089 0090 testServer = BaseHTTPServer.HTTPServer( ("", 8088), sourceHandler) 0091 0092 if len(sys.argv) == 1: 0093 import nsNode 0094 nsNode.loadTree() 0095 elif len(sys.argv) == 2: 0096 print "loading index(a minute or two)" 0097 import nsNode 0098 nsNode.loadTreeFromFile(sys.argv[1]) 0099 else: 0100 print "Invalid syntax" 0101 sys.exit() 0102 print "Done generating text. Starting Server" 0103 0104 for x in range(repeats, 0, -1): 0105 print "Handling %s more pages before I quit..." % x 0106 testServer.handle_request() 0107 testServer.server_close() 0108 0109 print "Okay. I'm done now." 0110 0111
Generated by PyXR 0.9.4