author | Tero Marttila <terom@fixme.fi> |
Sun, 08 Feb 2009 02:55:53 +0200 | |
branch | sites |
changeset 43 | fc11c4e86a82 |
parent 35 | 79ea0e2a6cc2 |
permissions | -rw-r--r-- |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
Handles mapping URLs to request handlers |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
import http |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
import handler |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
|
35
79ea0e2a6cc2
move pages/ -> sites/www.qmsk.net/pages/
Tero Marttila <terom@fixme.fi>
parents:
32
diff
changeset
|
8 |
class MapperError (http.ResponseError) : |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
URL could not be mapped |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
def __init__ (self, url) : |
35
79ea0e2a6cc2
move pages/ -> sites/www.qmsk.net/pages/
Tero Marttila <terom@fixme.fi>
parents:
32
diff
changeset
|
14 |
super(MapperError, self).__init__("URL not found: %s" % (url, ), status='404 Not Found') |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
|
31
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
16 |
class Mapper (handler.RequestHandler) : |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
""" |
31
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
18 |
Looks up the handler to use based on the URL |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
|
31
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
21 |
def handle_request (self, request) : |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
""" |
31
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
23 |
Map the given request |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
25 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
26 |
abstract |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
27 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
28 |
class Mapping (object) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
29 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
30 |
A mapping object for StaticMapping |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
31 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
32 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
33 |
def test (self, request) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
34 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
35 |
Either return a handler, or None |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
38 |
abstract |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
39 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
40 |
class RegexpMapping (object) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
41 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
42 |
A mapping object that uses regular expressions |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
43 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
44 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
45 |
def __init__ (self, regexp, handler) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
46 |
pass |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
47 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
48 |
def test (self, request) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
49 |
xxx |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
50 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
class SimpleMapping (object) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
52 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
53 |
A mapping object that uses simple expressions |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
54 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
55 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
56 |
def __init__ (self, expression, handler) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
pass |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
def test (self, request) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
xxx |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
61 |
|
35
79ea0e2a6cc2
move pages/ -> sites/www.qmsk.net/pages/
Tero Marttila <terom@fixme.fi>
parents:
32
diff
changeset
|
62 |
class StaticMapper (Mapper) : |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
Translates requests to handlers using a list of pre-determined Mapping's |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
def __init__ (self, mappings) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
# store |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
self.mappings = mappings |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
|
32
be954df4f0e8
move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents:
31
diff
changeset
|
71 |
def handle_request (self, request) : |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
72 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
73 |
Returns the appropriate handler |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
74 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
|
31
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
76 |
# find handler to use |
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
77 |
handler = None |
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
78 |
|
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
79 |
# just test each mapping in turn |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
80 |
for mapping in self.mappings : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
81 |
handler = mapping.test(request) |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
82 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
83 |
if handler : |
31
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
84 |
break |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
|
31
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
86 |
if not handler : |
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
87 |
# fail, not found |
35
79ea0e2a6cc2
move pages/ -> sites/www.qmsk.net/pages/
Tero Marttila <terom@fixme.fi>
parents:
32
diff
changeset
|
88 |
raise MapperError(request.get_page_name()) |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
|
31
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
90 |
# passthrough |
107062ebb6f9
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents:
30
diff
changeset
|
91 |
return handler.handle_request(request) |
30
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
92 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
93 |
# "friendly" names |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
94 |
map = SimpleMapping |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
95 |
mapre = RegexpMapping |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
96 |