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