author | Tero Marttila <terom@fixme.fi> |
Sat, 07 Feb 2009 06:54:52 +0200 | |
branch | sites |
changeset 30 | a86a25a9f75b |
child 31 | 107062ebb6f9 |
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 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
class MappingError (http.ResponseError) : |
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) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
super(MappingError, self).__init__("URL not found: %s" % (url, ), status='404 Not Found') |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
16 |
class Mapper (object) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
18 |
Translates requests to handlers |
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 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
21 |
def map_request (self, request) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
23 |
Map the given request, returning a Handler |
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 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
62 |
class StaticMapping (Mapper) : |
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 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
def map_request (self, request) : |
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 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
76 |
# 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
|
77 |
for mapping in self.mappings : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
78 |
handler = mapping.test(request) |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
79 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
80 |
if handler : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
81 |
return handler |
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 |
# fail, not found |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
84 |
raise MappingError(request.get_page_name()) |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
class FilesystemMapper (Mapper) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
Translates requests to handlers based on a filesystem directory containing various kinds of files |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
def __init__ (self, path) : |
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 |
Create, path is where the pages are stored |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
94 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
95 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
96 |
# store |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
97 |
self.path = path |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
98 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
99 |
def map_request (self, request) : |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
100 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
101 |
Looks up the appropriate Page, and then returns a generic Handler |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
102 |
""" |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
103 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
104 |
# XXX: harcoded |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
105 |
return handler.Handler(handler.handle_request) |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
106 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
107 |
# "friendly" names |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
108 |
fstree = FilesystemMapper |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
109 |
|
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
map = SimpleMapping |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
111 |
mapre = RegexpMapping |
a86a25a9f75b
route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
112 |