annotate simplewiki/handlers.py @ 1:4c83f7715993

[mq]: renderer
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 18:11:04 -0700
parents d5102c881cb5
children 2464e2051b78
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
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 from urlparse import urlparse
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 from webob import Response, exc
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 class HandlerMatchException(Exception):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 """the handler doesn't match the request"""
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 class Handler(object):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 methods = set(['GET']) # methods to listen to
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 handler_path = [] # path elements to match
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 @classmethod
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 def match(cls, app, request):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 # check the method
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 if request.method not in cls.methods:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 return None
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 # check the path
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 if request.environ['path'][:len(cls.handler_path)] != cls.handler_path:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 return None
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 try:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 return cls(app, request)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 except HandlerMatchException:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 return None
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 def __init__(self, app, request):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 self.app = app
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 self.request = request
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 self.application_path = urlparse(request.application_url)[2]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 def link(self, path=(), permanant=False):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 if isinstance(path, basestring):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 path = [ path ]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 path = [ i.strip('/') for i in path ]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 if permanant:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 application_url = [ self.request.application_url ]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 else:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 application_url = [ self.application_path ]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 path = application_url + path
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 return '/'.join(path)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 def redirect(self, location):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 raise exc.HTTPSeeOther(location=location)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52
1
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
53 class GenshiRenderer(Handler):
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
54
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
55 @classmethod
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
56 def match(cls, app, request):
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
57
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
58 # check the method
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
59 if request.method not in cls.methods:
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
60 return None
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
61
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
62 # check the path
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
63 path = request.environ['path']
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
64 if not path:
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
65 return None
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
66 if not path[-1].endswith('.html'):
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
67 return None
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
68
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 try:
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
71 return cls(app, request)
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
72 except HandlerMatchException:
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
73 return None
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 def __init__(self, app, request):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76 Handler.__init__(self, app, request)
1
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
77 self.template = os.path.join(app.directory, *request.environ['path'])
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
78 if not os.path.exists(self.template):
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
79 raise HandlerMatchException
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80 self.data = { 'request': request,
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81 'link': self.link }
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83 def __call__(self):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84 return getattr(self, self.request.method.title())()
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86 def Get(self):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 # needs to have self.template set
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