lib/site.py
branchsites
changeset 31 107062ebb6f9
parent 30 a86a25a9f75b
child 36 02d4040d5946
--- a/lib/site.py	Sat Feb 07 06:54:52 2009 +0200
+++ b/lib/site.py	Sat Feb 07 16:33:27 2009 +0200
@@ -4,53 +4,82 @@
 
 import imp
 
-SITE_DIR = "sites"
+import handler
 
-class Site (object) :
+class Site (handler.RequestHandler) :
     """
         A site is a website and its configuration
+
+        XXX: need to somehow communicate the site name to our downstream handler
     """
 
-    def __init__ (self, name) :
+    def __init__ (self, name, handler) :
         """
             The given name must be like a valid hostname, e.g. 'www.qmsk.net'
         """
 
         # store
         self.name = name
+        self.handler = handler
 
-        # load the site
-        self._load()
-
-    def _load (self) :
+    def handle_request (self, request) :
         """
-            Loads this site, as a python module (i.e. dir with __init__.py)
+            Map the request through our handler...
         """
 
-        # first, we need to find it
-        file, pathname, description = imp.find_module(self.name, [SITE_DIR])
+        return self.handler.handle_request(request)
 
-        # then, we can load it
-        self.module = imp.load_module(self.name, file, pathname, description)
-        
-        # create our mapper
-        self.mapper = self.module.build_mapper()
+class SiteModule (Site) :
+    """
+        A site, represented as python module/package, with the following module attributes:
 
-    def get_mapper (self) :
+            handler     - the RequestHandler to use
+    """
+
+    def __init__ (self, name, module) :
         """
-            Return the Mapper for this site
+            Create the Site based on the given module
         """
 
-        return self.mapper
+        super(SiteModule, self).__init__(name,
+            module.handler
+        )
 
-def lookup (request) :
+class SiteModuleCollection (handler.RequestHandler) :
     """
-        Lookup and return a Site object for the given request
+        A collection of SiteModules, looking up the correct site to use based on the request hostname
     """
 
-    # request hostnmae
-    hostname = request.env.get('HTTP_POST')
+    def __init__ (self, path) :
+        """
+            Initialize to load site modules from the given path
+        """
 
-    # XXX: hardcoded for now
-    return Site("www.qmsk.net")
+        self.path = path
+        self.site_cache = dict()
 
+    def handle_request (self, request) :
+        """
+            Lookup and return a Site object for the given request
+        """
+
+        # request hostnmae
+        name = request.env.get('HTTP_HOST')
+        
+        # already loaded?
+        if name in self.site_cache :
+            site = self.site_cache[name]
+
+        else :
+            # first, we need to find it
+            file, pathname, description = imp.find_module(name, [self.path])
+
+            # then, we can load the module
+            module = imp.load_module(name, file, pathname, description)
+            
+            # then build+cache the SiteModule
+            site = self.site_cache[name] = SiteModule(name, module)
+
+        # then execute the site's request handler
+        return site.handle_request(request)
+