1 |
|
2 import os, os.path |
|
3 |
|
4 from lib import http, template, map |
|
5 |
|
6 import page, page_tree |
|
7 |
|
8 class PageMapper (map.Mapper) : |
|
9 """ |
|
10 Translates requests to handlers based on a filesystem directory containing various kinds of files |
|
11 """ |
|
12 |
|
13 # list of page handlers, by type |
|
14 PAGE_TYPES = [ |
|
15 ('html', page.HTMLPage ), |
|
16 (template.TEMPLATE_EXT, page.TemplatePage ), |
|
17 ] |
|
18 |
|
19 def __init__ (self, path, template) : |
|
20 """ |
|
21 Create, path is where the pages are stored. The list of pages is loaded from $path/list |
|
22 """ |
|
23 |
|
24 # store |
|
25 self.path = path |
|
26 self.template = template |
|
27 |
|
28 # load the page tree |
|
29 self.tree = page_tree.PageTree(path + '/list') |
|
30 |
|
31 def _lookup_page_type (self, url, path, filename, basename, extension, tail) : |
|
32 """ |
|
33 We found the file that we looked for, now get the correct type |
|
34 """ |
|
35 |
|
36 # find appropriate handler |
|
37 for handler_ext, type in self.PAGE_TYPES : |
|
38 # match against file extension? |
|
39 if handler_ext == extension : |
|
40 # found handler, return instance |
|
41 return type(self, url, path, basename, tail) |
|
42 |
|
43 # no handler found |
|
44 raise PageError("No handler found for page %r of type %r" % (url, extension)) |
|
45 |
|
46 def _lookup_page (self, name) : |
|
47 """ |
|
48 Look up and return a Page object for the given page, or raise an error |
|
49 """ |
|
50 |
|
51 # inital path |
|
52 path = self.path |
|
53 url_segments = [] |
|
54 |
|
55 # name segments |
|
56 segments = name.split('/') |
|
57 |
|
58 # iterate through the parts of the page segments |
|
59 while True : |
|
60 segment = None |
|
61 |
|
62 # pop segment |
|
63 if segments : |
|
64 segment = segments.pop(0) |
|
65 |
|
66 url_segments.append(segment) |
|
67 |
|
68 # translate empty -> index |
|
69 if not segment : |
|
70 segment = 'index' |
|
71 |
|
72 # look for it in the dir |
|
73 for filename in os.listdir(path) : |
|
74 # build full file path |
|
75 file_path = os.path.join(path, filename) |
|
76 |
|
77 # stat, recurse into subdirectory? |
|
78 if os.path.isdir(file_path) and filename == segment : |
|
79 # use new dir |
|
80 path = file_path |
|
81 |
|
82 # break for-loop to look at next segment |
|
83 break |
|
84 |
|
85 # split into basename + extension |
|
86 basename, extension = os.path.splitext(filename) |
|
87 |
|
88 # ...remove that dot |
|
89 extension = extension.lstrip('.') |
|
90 |
|
91 # match against requested page name? |
|
92 if basename == segment : |
|
93 # found the file we wanted |
|
94 return self._lookup_page_type('/'.join(url_segments), file_path, filename, basename, extension, '/'.join(segments)) |
|
95 |
|
96 else : |
|
97 # inspect next file in dir |
|
98 continue |
|
99 |
|
100 else : |
|
101 # did not find any dir or file, break out of while loop |
|
102 break |
|
103 |
|
104 # did not find the filename we were looking for in os.listdir |
|
105 raise map.MapperError(name) |
|
106 |
|
107 def handle_request (self, request) : |
|
108 """ |
|
109 Looks up the appropriate Page, and then renders it |
|
110 """ |
|
111 |
|
112 # determine the page name |
|
113 page_name = request.get_page_name() |
|
114 |
|
115 # get the page handler |
|
116 page = self._lookup_page(page_name) |
|
117 |
|
118 # pass on |
|
119 return page.handle_request(request) |
|
120 |
|