2 Per-site stuff |
2 Per-site stuff |
3 """ |
3 """ |
4 |
4 |
5 import imp |
5 import imp |
6 |
6 |
7 SITE_DIR = "sites" |
7 import handler |
8 |
8 |
9 class Site (object) : |
9 class Site (handler.RequestHandler) : |
10 """ |
10 """ |
11 A site is a website and its configuration |
11 A site is a website and its configuration |
|
12 |
|
13 XXX: need to somehow communicate the site name to our downstream handler |
12 """ |
14 """ |
13 |
15 |
14 def __init__ (self, name) : |
16 def __init__ (self, name, handler) : |
15 """ |
17 """ |
16 The given name must be like a valid hostname, e.g. 'www.qmsk.net' |
18 The given name must be like a valid hostname, e.g. 'www.qmsk.net' |
17 """ |
19 """ |
18 |
20 |
19 # store |
21 # store |
20 self.name = name |
22 self.name = name |
|
23 self.handler = handler |
21 |
24 |
22 # load the site |
25 def handle_request (self, request) : |
23 self._load() |
|
24 |
|
25 def _load (self) : |
|
26 """ |
26 """ |
27 Loads this site, as a python module (i.e. dir with __init__.py) |
27 Map the request through our handler... |
28 """ |
28 """ |
29 |
29 |
30 # first, we need to find it |
30 return self.handler.handle_request(request) |
31 file, pathname, description = imp.find_module(self.name, [SITE_DIR]) |
|
32 |
31 |
33 # then, we can load it |
32 class SiteModule (Site) : |
34 self.module = imp.load_module(self.name, file, pathname, description) |
33 """ |
35 |
34 A site, represented as python module/package, with the following module attributes: |
36 # create our mapper |
|
37 self.mapper = self.module.build_mapper() |
|
38 |
35 |
39 def get_mapper (self) : |
36 handler - the RequestHandler to use |
|
37 """ |
|
38 |
|
39 def __init__ (self, name, module) : |
40 """ |
40 """ |
41 Return the Mapper for this site |
41 Create the Site based on the given module |
42 """ |
42 """ |
43 |
43 |
44 return self.mapper |
44 super(SiteModule, self).__init__(name, |
|
45 module.handler |
|
46 ) |
45 |
47 |
46 def lookup (request) : |
48 class SiteModuleCollection (handler.RequestHandler) : |
47 """ |
49 """ |
48 Lookup and return a Site object for the given request |
50 A collection of SiteModules, looking up the correct site to use based on the request hostname |
49 """ |
51 """ |
50 |
52 |
51 # request hostnmae |
53 def __init__ (self, path) : |
52 hostname = request.env.get('HTTP_POST') |
54 """ |
|
55 Initialize to load site modules from the given path |
|
56 """ |
53 |
57 |
54 # XXX: hardcoded for now |
58 self.path = path |
55 return Site("www.qmsk.net") |
59 self.site_cache = dict() |
56 |
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.get('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 |