def load_from_file(self): filename = self.filename if not os.path.isfile(filename): raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND mtime = os.path.getmtime(filename) # check cache code = self.cache_get(filename, mtime) # check for precompiled file if not code: code = self.cfile_get(filename, mtime) # finally parse and compile if not code: dir, fname = path_split(self.filename) source = _psp.parse(fname, dir) code = compile(source, filename, "exec") # store in cache self.cache_store(filename, mtime, code) self.code = code
def display_code(self): """ Display a niceliy HTML-formatted side-by-side of what PSP generated next to orinial code. """ req, filename = self.req, self.filename # Because of caching, source code is most often not # available in this object, so we read it here # (instead of trying to get it in __init__ somewhere) dir, fname = path_split(filename) source = open(filename).read().splitlines() pycode = _psp.parse(fname, dir).splitlines() source = [s.rstrip() for s in source] pycode = [s.rstrip() for s in pycode] req.write("<table>\n<tr>") for s in ("", " PSP-produced Python Code:", " %s:" % filename): req.write("<td><tt>%s</tt></td>" % s) req.write("</tr>\n") n = 1 for line in pycode: req.write("<tr>") left = escape(line).replace("\t", " "*4).replace(" ", " ") if len(source) < n: right = "" else: right = escape(source[n-1]).replace("\t", " "*4).replace(" ", " ") for s in ("%d. " % n, "<font color=blue>%s</font>" % left, " <font color=green>%s</font>" % right): req.write("<td><tt>%s</tt></td>" % s) req.write("</tr>\n") n += 1 req.write("</table>\n")
def parse(filename, dir=None): if dir: return _psp.parse(filename, dir) else: return _psp.parse(filename)