author | Tero Marttila <terom@fixme.fi> |
Sat, 07 Feb 2009 01:33:30 +0200 | |
changeset 11 | fa216534ae45 |
parent 10 | d83b10c210e3 |
child 12 | 2abc5ace0b15 |
permissions | -rw-r--r-- |
5 | 1 |
|
7
d6a8258bd90e
YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
2 |
""" |
d6a8258bd90e
YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
3 |
Handling page requests |
d6a8258bd90e
YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
4 |
""" |
d6a8258bd90e
YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
5 |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
6 |
# for filesystem ops |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
7 |
import os, os.path |
7
d6a8258bd90e
YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
8 |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
9 |
# for ResponseError |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
10 |
import http |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
11 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
12 |
# for TemplatePage |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
13 |
import template |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
14 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
15 |
# path to directory containing the page heirarcy |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
16 |
PAGE_DIR = "pages" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
17 |
|
9 | 18 |
# path to directory containing the list of visible pages |
19 |
PAGE_LIST_FILE = os.path.join(PAGE_DIR, "list") |
|
20 |
||
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
21 |
class PageError (http.ResponseError) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
22 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
23 |
Error looking up/handling a page |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
24 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
25 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
26 |
pass |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
27 |
|
11 | 28 |
class PageInfo (object) : |
29 |
""" |
|
30 |
Contains metainformation about a page |
|
31 |
""" |
|
32 |
||
33 |
def __init__ (self, parent, name, title, children=None) : |
|
34 |
""" |
|
35 |
Initialize, children defaults to empty list |
|
36 |
""" |
|
37 |
||
38 |
# store |
|
39 |
self.parent = parent |
|
40 |
self.name = name |
|
41 |
self.title = title |
|
42 |
self.children = children if children else [] |
|
43 |
||
44 |
# no url get |
|
45 |
self._url = None |
|
46 |
||
47 |
def set_parent (self, parent) : |
|
48 |
""" |
|
49 |
Set a parent where non was set before |
|
50 |
""" |
|
51 |
||
52 |
assert self.parent is None |
|
53 |
||
54 |
self.parent = parent |
|
55 |
||
56 |
def add_child (self, child) : |
|
57 |
""" |
|
58 |
Add a PageInfo child |
|
59 |
""" |
|
60 |
||
61 |
self.children.append(child) |
|
62 |
||
63 |
def get_child (self, name) : |
|
64 |
""" |
|
65 |
Look up a child by name, returning None if not found |
|
66 |
""" |
|
67 |
||
68 |
return dict((c.name, c) for c in self.children).get(name) |
|
69 |
||
70 |
@property |
|
71 |
def url (self) : |
|
72 |
""" |
|
73 |
Build this page's URL |
|
74 |
""" |
|
75 |
||
76 |
# cached? |
|
77 |
if self._url : |
|
78 |
return self._url |
|
79 |
||
80 |
# collect URL segments in reverse order |
|
81 |
segments = [] |
|
82 |
||
83 |
# add empty segment if dir |
|
84 |
if self.children : |
|
85 |
segments.append('') |
|
86 |
||
87 |
# iterate over ancestry |
|
88 |
item = self |
|
89 |
||
90 |
# add all parent names, but not root's |
|
91 |
while item and item.parent : |
|
92 |
segments.append(item.name) |
|
93 |
||
94 |
item = item.parent |
|
95 |
||
96 |
# reverse segments |
|
97 |
segments.reverse() |
|
98 |
||
99 |
# join |
|
100 |
url = '/'.join(segments) |
|
101 |
||
102 |
# cache |
|
103 |
self._url = url |
|
104 |
||
105 |
# done |
|
106 |
return url |
|
107 |
||
108 |
class PageTree (object) : |
|
9 | 109 |
""" |
110 |
The list of pages |
|
111 |
""" |
|
112 |
||
113 |
def __init__ (self) : |
|
114 |
""" |
|
11 | 115 |
Empty PageList, must call load_page_list to initialize, once |
9 | 116 |
""" |
117 |
||
11 | 118 |
def _load (self, path=PAGE_LIST_FILE) : |
9 | 119 |
""" |
120 |
Processes the lines in the given file |
|
121 |
""" |
|
11 | 122 |
|
123 |
# collect the page list |
|
124 |
pages = [] |
|
125 |
||
126 |
# stack of (indent, PageInfo) items |
|
127 |
stack = [] |
|
128 |
||
129 |
# the previous item processed, None for first one |
|
130 |
prev = None |
|
9 | 131 |
|
132 |
for line in open(path, 'rb') : |
|
11 | 133 |
indent = 0 |
134 |
||
135 |
# count indent |
|
136 |
for char in line : |
|
137 |
# tabs break things |
|
138 |
assert char != '\t' |
|
139 |
||
140 |
# increment up to first non-space char |
|
141 |
if char == ' ' : |
|
142 |
indent += 1 |
|
143 |
||
144 |
elif char == ':' : |
|
145 |
indent = 0 |
|
146 |
break |
|
147 |
||
148 |
else : |
|
149 |
break |
|
150 |
||
151 |
# strip whitespace |
|
9 | 152 |
line = line.strip() |
11 | 153 |
|
9 | 154 |
# ignore empty lines |
155 |
if not line : |
|
156 |
continue |
|
157 |
||
158 |
# parse line |
|
159 |
url, title = line.split(':') |
|
160 |
||
11 | 161 |
# remove whitespace |
162 |
url = url.strip() |
|
163 |
title = title.strip() |
|
164 |
||
165 |
# create PageInfo item without parent |
|
166 |
item = PageInfo(None, url, title) |
|
9 | 167 |
|
11 | 168 |
# are we the first item? |
169 |
if not prev : |
|
170 |
assert url == '', "Page list must begin with root item" |
|
171 |
||
172 |
# root node does not have a parent |
|
173 |
parent = None |
|
174 |
||
175 |
# set root |
|
176 |
self.root = item |
|
9 | 177 |
|
11 | 178 |
# tee hee hee |
179 |
self.root.add_child(self.root) |
|
9 | 180 |
|
11 | 181 |
# initialize stack |
182 |
stack.append((0, self.root)) |
|
183 |
||
184 |
else : |
|
185 |
# peek stack |
|
186 |
stack_indent, stack_parent = stack[-1] |
|
187 |
||
188 |
# new indent level? |
|
189 |
if indent > stack_indent : |
|
190 |
# set parent to previous item, and push new indent level + parent to stack |
|
191 |
parent = prev |
|
192 |
||
193 |
# push new indent level + its parent |
|
194 |
stack.append((indent, parent)) |
|
195 |
||
196 |
# same indent level as previous |
|
197 |
elif indent == stack_indent : |
|
198 |
# parent is the one of the current stack level, stack doesn't change |
|
199 |
parent = stack_parent |
|
200 |
||
201 |
# unravel stack |
|
202 |
elif indent < stack_indent : |
|
203 |
while True : |
|
204 |
# remove current stack level |
|
205 |
stack.pop(-1) |
|
206 |
||
207 |
# peek next level |
|
208 |
stack_indent, stack_parent = stack[-1] |
|
209 |
||
210 |
# found the level to return to? |
|
211 |
if stack_indent == indent : |
|
212 |
# restore prev |
|
213 |
parent = stack_parent |
|
214 |
||
215 |
break |
|
216 |
||
217 |
elif stack_indent < indent : |
|
218 |
assert False, "Bad un-indent" |
|
219 |
||
220 |
# add to parent? |
|
221 |
if parent : |
|
222 |
item.set_parent(parent) |
|
223 |
parent.add_child(item) |
|
224 |
||
225 |
# update prev |
|
226 |
prev = item |
|
9 | 227 |
|
11 | 228 |
# get root |
229 |
assert hasattr(self, 'root'), "No root item found!" |
|
230 |
||
231 |
def get_page (self, url) : |
|
232 |
""" |
|
233 |
Lookup the given page URL, and return the matching PageInfo object, or None, if not found |
|
234 |
""" |
|
235 |
||
236 |
# start from root |
|
237 |
node = self.root |
|
238 |
||
239 |
# traverse the object tree |
|
240 |
for segment in url.split('/') : |
|
241 |
if segment : |
|
242 |
node = node.get_child(segment) |
|
243 |
||
244 |
if not node : |
|
245 |
return None |
|
9 | 246 |
|
247 |
# return |
|
11 | 248 |
return node |
249 |
||
250 |
def get_siblings (self, url) : |
|
251 |
""" |
|
252 |
Get the list of siblings for the given url, including the given page itself |
|
253 |
""" |
|
254 |
||
255 |
# look up the page itself |
|
256 |
page = self.get_page(url) |
|
257 |
||
258 |
# specialcase root/unknown node |
|
259 |
if page and page.parent : |
|
260 |
return page.parent.children |
|
9 | 261 |
|
11 | 262 |
else : |
263 |
return self.root.children |
|
264 |
||
265 |
def dump (self) : |
|
266 |
""" |
|
267 |
Returns a string representation of the tree |
|
268 |
""" |
|
269 |
||
270 |
def _print_node (indent, node) : |
|
271 |
return '\n'.join('%s%s' % (' '*indent, line) for line in [ |
|
272 |
"%-15s : %s" % (node.name, node.title) |
|
273 |
] + [ |
|
274 |
_print_node(indent + 4, child) for child in node.children |
|
275 |
]) |
|
9 | 276 |
|
11 | 277 |
return _print_node(0, self.root) |
278 |
||
279 |
# global singleton PageList instance |
|
280 |
page_tree = PageTree() |
|
281 |
||
282 |
def load_page_tree () : |
|
283 |
""" |
|
284 |
Load the global singleton PageInfo instance |
|
285 |
""" |
|
286 |
||
287 |
page_tree._load() |
|
288 |
||
289 |
# XXX: should inherit from PageInfo |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
290 |
class Page (object) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
291 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
292 |
This object represents the information about our attempt to render some specific page |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
293 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
294 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
295 |
def __init__ (self, url, path, basename, url_tail) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
296 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
297 |
Initialize the page at the given location |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
298 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
299 |
@param url the URL leading to this page |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
300 |
@param path the filesystem path to this page's file |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
301 |
@param basename the filesystem name of this page's file, without the file extension |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
302 |
@param url_trail trailing URL for this page |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
303 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
304 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
305 |
# store |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
306 |
self.url = url |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
307 |
self.path = path |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
308 |
self.basename = basename |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
309 |
self.url_tail = url_tail |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
310 |
|
10
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
311 |
# unbound |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
312 |
self.request = None |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
313 |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
314 |
# sub-init |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
315 |
self._init() |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
316 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
317 |
def _init (self) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
318 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
319 |
Do initial data loading, etc |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
320 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
321 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
322 |
pass |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
323 |
|
10
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
324 |
def bind_request (self, request) : |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
325 |
""" |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
326 |
Bind this page-render to the given request |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
327 |
""" |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
328 |
|
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
329 |
self.request = request |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
330 |
|
9 | 331 |
@property |
332 |
def title (self) : |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
333 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
334 |
Return the page's title |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
335 |
|
9 | 336 |
Defaults to the retreiving the page title from page_list, or basename in Titlecase. |
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
337 |
""" |
9 | 338 |
|
339 |
# lookup in page_list |
|
11 | 340 |
page_info = page_tree.get_page(self.url) |
9 | 341 |
|
342 |
# fallback to titlecase |
|
11 | 343 |
if page_info : |
344 |
title = page_info.title |
|
345 |
||
346 |
else : |
|
9 | 347 |
title = self.basename.title() |
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
348 |
|
9 | 349 |
return title |
350 |
||
351 |
@property |
|
352 |
def content (self) : |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
353 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
354 |
Return the page content as a string |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
355 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
356 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
357 |
abstract |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
358 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
359 |
class HTMLPage (Page) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
360 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
361 |
A simple .html page that's just passed through directly |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
362 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
363 |
|
9 | 364 |
@property |
365 |
def content (self) : |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
366 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
367 |
Opens the .html file, reads and returns contents |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
368 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
369 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
370 |
return open(self.path, 'rb').read() |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
371 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
372 |
class TemplatePage (Page) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
373 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
374 |
A template that's rendered using our template library |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
375 |
""" |
9 | 376 |
|
377 |
@property |
|
378 |
def content (self) : |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
379 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
380 |
Loads the .tmpl file, and renders it |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
381 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
382 |
|
10
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
383 |
return template.render_file(self.path, |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
384 |
request = self.request, |
11 | 385 |
page_tree = page_tree |
10
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
386 |
) |
7
d6a8258bd90e
YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
387 |
|
d6a8258bd90e
YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
388 |
# list of page handlers, by type |
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
389 |
TYPE_HANDLERS = [ |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
390 |
('html', HTMLPage ), |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
391 |
(template.TEMPLATE_EXT, TemplatePage ), |
7
d6a8258bd90e
YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
392 |
] |
d6a8258bd90e
YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
393 |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
394 |
def _lookup_handler (url, path, filename, basename, extension, tail) : |
5 | 395 |
""" |
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
396 |
We found the file that we looked for, now get its handler |
5 | 397 |
""" |
398 |
||
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
399 |
# find appropriate handler |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
400 |
for handler_ext, handler in TYPE_HANDLERS : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
401 |
# match against file extension? |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
402 |
if handler_ext == extension : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
403 |
# found handler, return instance |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
404 |
return handler(url, path, basename, tail) |
5 | 405 |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
406 |
# no handler found |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
407 |
raise PageError("No handler found for page %r of type %r" % (url, extension)) |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
408 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
409 |
def lookup (name) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
410 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
411 |
Look up and return a Page object for the given page, or raise an error |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
412 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
413 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
414 |
# inital path |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
415 |
path = PAGE_DIR |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
416 |
url_segments = [] |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
417 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
418 |
# name segments |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
419 |
segments = name.split('/') |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
420 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
421 |
# iterate through the parts of the page segments |
10
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
422 |
while True : |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
423 |
segment = None |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
424 |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
425 |
# pop segment |
10
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
426 |
if segments : |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
427 |
segment = segments.pop(0) |
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
428 |
|
10
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
429 |
url_segments.append(segment) |
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
430 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
431 |
# translate empty -> index |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
432 |
if not segment : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
433 |
segment = 'index' |
10
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
434 |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
435 |
# look for it in the dir |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
436 |
for filename in os.listdir(path) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
437 |
# build full file path |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
438 |
file_path = os.path.join(path, filename) |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
439 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
440 |
# stat, recurse into subdirectory? |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
441 |
if os.path.isdir(file_path) and filename == segment : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
442 |
# use new dir |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
443 |
path = file_path |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
444 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
445 |
# break for-loop to look at next segment |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
446 |
break |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
447 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
448 |
# split into basename + extension |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
449 |
basename, extension = os.path.splitext(filename) |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
450 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
451 |
# ...remove that dot |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
452 |
extension = extension.lstrip('.') |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
453 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
454 |
# match against requested page name? |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
455 |
if basename == segment : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
456 |
# found the file we wanted |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
457 |
return _lookup_handler('/'.join(url_segments), file_path, filename, basename, extension, '/'.join(segments)) |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
458 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
459 |
else : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
460 |
# inspect next file in dir |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
461 |
continue |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
462 |
|
10
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
463 |
else : |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
464 |
# did not find any dir or file, break out of while loop |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
465 |
break |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
466 |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
467 |
# did not find the filename we were looking for in os.listdir |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
468 |
raise PageError("Page not found: %s" % name, status='404 Not Found') |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
469 |