annotate simplewiki/handlers.py @ 2:2464e2051b78

[mq]: index
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 21:00:46 -0700
parents 4c83f7715993
children 56ab6b90cd1a
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 template = self.app.loader.load(self.template)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 return Response(content_type='text/html',
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 body=template.generate(**self.data).render('html'))
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90
2
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
91
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
92 class Index(Handler):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
93
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
94 template = 'index.html'
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
95
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
96 def __init__(self, app, request):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
97 Handler.__init__(self, app, request)
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
98 self.directory = os.path.join(app.directory, *request.environ['path'])
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
99 if not os.path.isdir(self.directory):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
100 raise HandlerMatchException
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
101 path = request.environ['path']
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
102 files = []
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
103 files = os.listdir(self.directory)
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
104 self.data = { 'request': request,
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
105 'link': self.link,
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
106 'directory': '/' + '/'.join(path),
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
107 'files': files }
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
108
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
109 def __call__(self):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
110 return getattr(self, self.request.method.title())()
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
111
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
112 def Get(self):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
113 if not self.request.path_info.endswith('/'):
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
114 self.redirect(self.request.path_info + '/')
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
115 template = self.app.loader.load(self.template)
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
116 return Response(content_type='text/html',
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
117 body=template.generate(**self.data).render('html'))
2464e2051b78 [mq]: index
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
118