# -*- coding: utf-8 -*- import cgi import os import sys import patchtracker.Conf as Conf from patchtracker.Templates import ErrorTemplate, LetterTocTemplate, FrontPageTemplate, SearchResultsTemplate from patchtracker.CacheObject import CacheObject, CacheMissException import patchtracker.DB as DB from patchtracker.DB import PatchTrackerDB import patchtracker.SourceArchive as SourceArchive class ReqHandlerException(Exception): def __init__(self, msg, code="500 Oh noes"): Exception.__init__(self, msg) self.status=code class Cmd: def __init__(self): self.content_type = 'text/html' self.status = "200 OK" class ErrorCmd(Cmd): def __init__(self, msg, code="500 Oh noes"): Cmd.__init__(self) self.status = code self.msg = msg def output(self): return str(ErrorTemplate(self.msg)) class IndexCmd(Cmd): def __init__(self, args): Cmd.__init__(self) if len(args) < 1 or not len(args[0]): raise ReqHandlerException("please provide a letter on which to index") else: self.db = PatchTrackerDB() self.letter = args[0] self.toc = self.db.findLetterToc(self.letter) def output(self): return str(LetterTocTemplate(self.letter, self.toc)) class MaintCmd(Cmd): def __init__(self, args): Cmd.__init__(self) if len(args) < 1 or not len(args[0]): raise ReqHandlerException("please provide a email address on which to index") else: self.db = PatchTrackerDB() self.email = args[0] self.toc = self.db.findCollection(email=self.email) def output(self): return str(SearchResultsTemplate(self.email, "maintainer email", self.toc)) class JumpCmd(Cmd): def __init__(self, env): Cmd.__init__(self) form = cgi.FieldStorage(fp=env['wsgi.input'],environ=env) self.name = form.getfirst("package") self.uri = "%s/package/%s"%(Conf.root_url, self.name) self.status = "302 Try this other place kthx" def output(self): return "" class FrontPageCmd(Cmd): def __init__(self): Cmd.__init__(self) self.db = PatchTrackerDB() self.index = self.db.findIndices() def output(self): return str(FrontPageTemplate(self.index)) class CmdHandler: def __init__(self, env): self.headers = [] uri = Conf.root_url+env['PATH_INFO'] self.cacheobj = None #print "Accept:",env['HTTP_ACCEPT'] args = uri[len(Conf.root_url)+1:].split("/") cmdarg = args[0] cacheable = False if cmdarg == "index": self.cmd = IndexCmd(args[1:]) elif cmdarg == "jump": self.cmd = JumpCmd(env) self.headers.append( ('Location', self.cmd.uri) ) elif cmdarg == "email": self.cmd = MaintCmd(args[1:]) elif not len(cmdarg): self.cmd = FrontPageCmd() else: self.cmd = ErrorCmd("invalid command/location '%s'"%(cmdarg), "404 Not found") if Conf.caching and cacheable: self.cacheobj = CacheObject(key=uri) self.headers.append( ('Content-type', self.cmd.content_type) ) self.status = self.cmd.status def output(self): result = None try: if self.cacheobj: result = self.cacheobj.get() else: result = self.cmd.output() except CacheMissException: result = self.cmd.output() self.cacheobj.put(result) return result if __name__ == '__main__': fake_env = { 'PATH_INFO': sys.argv[1] } cmdh = CmdHandler(fake_env) print "Status: %s\nHeaders: %s"%(cmdh.status, cmdh.headers) print cmdh.output()