terom@29: """ terom@29: Per-site stuff terom@29: """ terom@29: terom@30: import imp terom@30: terom@46: from qmsk.web import handler terom@29: terom@31: class Site (handler.RequestHandler) : terom@29: """ terom@29: A site is a website and its configuration terom@31: terom@31: XXX: need to somehow communicate the site name to our downstream handler terom@29: """ terom@29: terom@31: def __init__ (self, name, handler) : terom@30: """ terom@30: The given name must be like a valid hostname, e.g. 'www.qmsk.net' terom@30: """ terom@29: terom@30: # store terom@30: self.name = name terom@31: self.handler = handler terom@29: terom@31: def handle_request (self, request) : terom@30: """ terom@31: Map the request through our handler... terom@30: """ terom@30: terom@31: return self.handler.handle_request(request) terom@30: terom@31: class SiteModule (Site) : terom@31: """ terom@31: A site, represented as python module/package, with the following module attributes: terom@30: terom@31: handler - the RequestHandler to use terom@31: """ terom@31: terom@31: def __init__ (self, name, module) : terom@30: """ terom@31: Create the Site based on the given module terom@30: """ terom@30: terom@31: super(SiteModule, self).__init__(name, terom@31: module.handler terom@31: ) terom@30: terom@31: class SiteModuleCollection (handler.RequestHandler) : terom@30: """ terom@31: A collection of SiteModules, looking up the correct site to use based on the request hostname terom@30: """ terom@30: terom@31: def __init__ (self, path) : terom@31: """ terom@31: Initialize to load site modules from the given path terom@31: """ terom@30: terom@31: self.path = path terom@31: self.site_cache = dict() terom@30: terom@31: def handle_request (self, request) : terom@31: """ terom@31: Lookup and return a Site object for the given request terom@31: """ terom@31: terom@31: # request hostnmae terom@36: name = request.env['HTTP_HOST'] terom@31: terom@31: # already loaded? terom@31: if name in self.site_cache : terom@31: site = self.site_cache[name] terom@31: terom@31: else : terom@31: # first, we need to find it terom@31: file, pathname, description = imp.find_module(name, [self.path]) terom@31: terom@31: # then, we can load the module terom@31: module = imp.load_module(name, file, pathname, description) terom@31: terom@31: # then build+cache the SiteModule terom@31: site = self.site_cache[name] = SiteModule(name, module) terom@31: terom@31: # then execute the site's request handler terom@31: return site.handle_request(request) terom@31: