lib/site.py
changeset 46 185504387370
parent 45 e94ab812c0c8
child 47 3d59c9eeffaa
equal deleted inserted replaced
45:e94ab812c0c8 46:185504387370
     1 """
       
     2     Per-site stuff
       
     3 """
       
     4 
       
     5 import imp
       
     6 
       
     7 import handler
       
     8 
       
     9 class Site (handler.RequestHandler) :
       
    10     """
       
    11         A site is a website and its configuration
       
    12 
       
    13         XXX: need to somehow communicate the site name to our downstream handler
       
    14     """
       
    15 
       
    16     def __init__ (self, name, handler) :
       
    17         """
       
    18             The given name must be like a valid hostname, e.g. 'www.qmsk.net'
       
    19         """
       
    20 
       
    21         # store
       
    22         self.name = name
       
    23         self.handler = handler
       
    24 
       
    25     def handle_request (self, request) :
       
    26         """
       
    27             Map the request through our handler...
       
    28         """
       
    29 
       
    30         return self.handler.handle_request(request)
       
    31 
       
    32 class SiteModule (Site) :
       
    33     """
       
    34         A site, represented as python module/package, with the following module attributes:
       
    35 
       
    36             handler     - the RequestHandler to use
       
    37     """
       
    38 
       
    39     def __init__ (self, name, module) :
       
    40         """
       
    41             Create the Site based on the given module
       
    42         """
       
    43 
       
    44         super(SiteModule, self).__init__(name,
       
    45             module.handler
       
    46         )
       
    47 
       
    48 class SiteModuleCollection (handler.RequestHandler) :
       
    49     """
       
    50         A collection of SiteModules, looking up the correct site to use based on the request hostname
       
    51     """
       
    52 
       
    53     def __init__ (self, path) :
       
    54         """
       
    55             Initialize to load site modules from the given path
       
    56         """
       
    57 
       
    58         self.path = path
       
    59         self.site_cache = dict()
       
    60 
       
    61     def handle_request (self, request) :
       
    62         """
       
    63             Lookup and return a Site object for the given request
       
    64         """
       
    65 
       
    66         # request hostnmae
       
    67         name = request.env['HTTP_HOST']
       
    68         
       
    69         # already loaded?
       
    70         if name in self.site_cache :
       
    71             site = self.site_cache[name]
       
    72 
       
    73         else :
       
    74             # first, we need to find it
       
    75             file, pathname, description = imp.find_module(name, [self.path])
       
    76 
       
    77             # then, we can load the module
       
    78             module = imp.load_module(name, file, pathname, description)
       
    79             
       
    80             # then build+cache the SiteModule
       
    81             site = self.site_cache[name] = SiteModule(name, module)
       
    82 
       
    83         # then execute the site's request handler
       
    84         return site.handle_request(request)
       
    85