annotate simplewiki/handlers.py @ 5:b2fbb4f982da default tip

[mq]: edit
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 22:58:11 -0700
parents dd1c4916cbcd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 """
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 request handlers:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 these are instantiated for every request, then called
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
1
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
6 import os
4
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
7 from paste.fileapp import FileApp
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 from urlparse import urlparse
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 from webob import Response, exc
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 class HandlerMatchException(Exception):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 """the handler doesn't match the request"""
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 class Handler(object):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 methods = set(['GET']) # methods to listen to
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 handler_path = [] # path elements to match
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 @classmethod
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 def match(cls, app, request):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 # check the method
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 if request.method not in cls.methods:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 return None
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 # check the path
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 if request.environ['path'][:len(cls.handler_path)] != cls.handler_path:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 return None
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 try:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 return cls(app, request)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 except HandlerMatchException:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 return None
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 def __init__(self, app, request):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 self.app = app
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 self.request = request
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 self.application_path = urlparse(request.application_url)[2]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 def link(self, path=(), permanant=False):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 if isinstance(path, basestring):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 path = [ path ]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 path = [ i.strip('/') for i in path ]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 if permanant:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 application_url = [ self.request.application_url ]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 else:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 application_url = [ self.application_path ]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 path = application_url + path
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 return '/'.join(path)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 def redirect(self, location):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 raise exc.HTTPSeeOther(location=location)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53
1
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
54 class GenshiRenderer(Handler):
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
55
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
56 @classmethod
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
57 def match(cls, app, request):
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
58
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
59 # check the method
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
60 if request.method not in cls.methods:
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
61 return None
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
62
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
63 # check the path
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
64 path = request.environ['path']
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
65 if not path:
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
66 return None
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
67 if not path[-1].endswith('.html'):
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
68 return None
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
69
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
70
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
71 try:
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
72 return cls(app, request)
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
73 except HandlerMatchException:
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
74 return None
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76 def __init__(self, app, request):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 Handler.__init__(self, app, request)
1
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
78 self.template = os.path.join(app.directory, *request.environ['path'])
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
79 if not os.path.exists(self.template):
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
80 raise HandlerMatchException
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81 self.data = { 'request': request,
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82 'link': self.link }
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84 def __call__(self):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85 return getattr(self, self.request.method.title())()
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 def Get(self):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 template = self.app.loader.load(self.template)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 return Response(content_type='text/html',
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90 body=template.generate(**self.data).render('html'))
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91
2
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
92
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
93 class Index(Handler):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
94
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
95 template = 'index.html'
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
96
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
97 def __init__(self, app, request):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
98 Handler.__init__(self, app, request)
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
99 self.directory = os.path.join(app.directory, *request.environ['path'])
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
100 if not os.path.isdir(self.directory):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
101 raise HandlerMatchException
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
102 path = request.environ['path']
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
103 files = []
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
104 files = os.listdir(self.directory)
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
105 self.data = { 'request': request,
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
106 'link': self.link,
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
107 'directory': '/' + '/'.join(path),
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
108 'files': files }
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
109
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
110 def __call__(self):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
111 return getattr(self, self.request.method.title())()
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
112
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
113 def Get(self):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
114 if not self.request.path_info.endswith('/'):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
115 self.redirect(self.request.path_info + '/')
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
116 template = self.app.loader.load(self.template)
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
117 return Response(content_type='text/html',
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
118 body=template.generate(**self.data).render('html'))
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
119
3
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
120 class Post(Handler):
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
121 methods = set(['POST']) # methods to listen to
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
122
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
123 def __init__(self, app, request):
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
124 Handler.__init__(self, app, request)
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
125 if 'file' not in request.POST:
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
126 raise HandlerMatchException
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
127 self.file = self.request.POST['file']
5
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
128 filename = getattr(self.file, 'filename', '')
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
129 if filename:
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
130 content = self.file.file.read()
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
131 else:
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
132 content = self.file
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
133
3
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
134 self.location = request.path_info.rstrip('/')
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
135 path = os.path.join(self.app.directory, *self.request.environ['path'])
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
136 if os.path.isdir(path):
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
137 self.directory = path
5
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
138 self.filename = os.path.join(self.directory, filename)
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
139 self.location += '/' + filename
3
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
140 else:
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
141 self.directory = os.path.dirname(path)
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
142 self.filename = path
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
143
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
144 f = file(self.filename, 'wb')
5
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
145 f.write(content)
3
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
146 f.close()
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
147
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
148 def __call__(self):
56ab6b90cd1a [mq]: post
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
149 self.redirect(self.location)
4
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
150
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
151 class FileServer(Handler):
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
152 methods = set(['GET']) # methods to listen to
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
153
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
154 def __init__(self, app, request):
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
155 Handler.__init__(self, app, request)
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
156 self.file = os.path.join(self.app.directory, *request.environ['path'])
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
157 if not os.path.exists(self.file):
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
158 raise HandlerMatchException
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
159
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
160 def __call__(self):
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
161 return FileApp(self.file)
dd1c4916cbcd [mq]: fileserver
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
162
5
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
163 class EditView(Handler):
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
164 methods = set(['GET'])
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
165 template = 'edit.html'
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
166
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
167 def __init__(self, app, request):
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
168 if 'edit' not in request.GET:
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
169 raise HandlerMatchException
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
170
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
171 Handler.__init__(self, app, request)
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
172
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
173 self.file = os.path.join(self.app.directory, *request.environ['path'])
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
174 if not os.path.exists(self.file):
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
175 raise HandlerMatchException
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
176 self.data = { 'request': request,
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
177 'link': self.link,
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
178 'file': '/' + '/'.join(request.environ['path']),
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
179 'content': file(self.file).read()
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
180 }
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
181
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
182 def __call__(self):
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
183 return getattr(self, self.request.method.title())()
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
184
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
185 def Get(self):
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
186 template = self.app.loader.load(self.template)
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
187 return Response(content_type='text/html',
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
188 body=template.generate(**self.data).render('html'))