-
Notifications
You must be signed in to change notification settings - Fork 0
/
berry.py
286 lines (221 loc) · 6.88 KB
/
berry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"Berry is a minimal DSL for building a WSGI application."
import sys
import re
import cgi
import urlparse
import traceback
debug = False
routes = []
middlewares = []
error_handlers = {}
before_request_hooks = []
parse_qs = urlparse.parse_qs or cgi.parse_qs
def app(env, start_response):
"The WSGI application."
request = Request(env, start_response)
response = Response(request)
return response.get()
class Response(object):
def __init__(self, request):
self.request = request
self.status = None
def get(self):
route, urlparams = self.request._dispatch()
if not route:
exception = NotFound()
status = exception.status
handler = exception.get_handler()
else:
status = (200, 'OK')
handler = route.handler
# Execute before_request hooks.
for hook in before_request_hooks:
hook(self.request)
try:
content = handler(self.request, *urlparams)
except Exception, exception:
if not isinstance(exception, HTTPError):
exception = AppError()
status = exception.status
handler = exception.get_handler()
self.status = status
headers = getattr(handler, 'headers', {})
if 'Content-Type' not in headers:
headers['Content-Type'] = 'text/html'
self.request._start_response('%s %s' % status, headers.items())
if status[0] == 200:
return content
else:
return handler(self.request)
class HTTPError(Exception):
"Base class for HTTP errors."
status = (500, 'Internal Server Error')
content = ""
headers = {}
def get_handler(self):
code = self.status[0]
if code in error_handlers:
handler = error_handlers[code]
else:
def handler(req):
return self.content
handler.headers = self.headers
return handler
class Redirect(HTTPError):
status = (303, 'See Other')
def __init__(self, url):
self.headers = {'Location': url}
class Forbidden(HTTPError):
status = (403, 'Forbidden')
content = "<h1>403 Forbidden</h1>"
class NotFound(HTTPError):
status = (404, 'Not Found')
content = "<h1>404 Not Found</h1>"
class AppError(HTTPError):
status = (500, 'Internal Server Error')
def __init__(self):
content = "<h1>500 Internal Server Error</h1>"
if debug:
exc_info = sys.exc_info()
tb = ''.join(traceback.format_exception(*exc_info))
content += "<pre><code>%s</code></pre>" % tb
self.content = content
class Request(object):
"Abstraction of the WSGI request."
# You can set custom properties here.
properties = {}
def __init__(self, env, start_response):
self.env = env
self._start_response = start_response
self.path = self.env.get('PATH_INFO', '').lstrip('/')
self.query = self.env.get('QUERY_STRING', '')
self.fullpath = '/' + self.path
if self.query:
self.fullpath += '?' + self.query
self.params = self._parse_params()
self.method = self.env.get('REQUEST_METHOD', 'GET').upper()
def __getattr__(self, name):
return self.properties.get(name)
def __setattr__(self, name, value):
self.properties[name] = value
def _dispatch(self):
"Dispatch the request."
for route in routes:
if route.method == self.method:
match = re.search(route.path, self.path, re.I)
if match is not None:
return route, list(match.groups())
return None, []
def _parse_params(self):
"Parse all form data."
params = {}
params.update(self._parse_get_params())
params.update(self._parse_post_params())
# parses fields like a[b] into dictionaries
params_ = {}
for key, value in params.iteritems():
d = _parse_param_as_dict(key, value)
for k, v in d.iteritems():
if isinstance(v, dict):
if not params_.get(k):
params_[k] = {}
params_[k] = _recursive_dict_update(params_[k], v)
elif isinstance(v, list):
if not params_.get(k):
params_[k] = []
params_[k].extend(v)
else:
params_[k] = v
return params_
def _parse_get_params(self):
"Parse form data passed through GET."
parsed = parse_qs(self.env['QUERY_STRING'], keep_blank_values=True)
params = {}
for key, val in parsed.items():
if len(val) == 0:
params[key] = ''
elif len(val) == 1:
params[key] = val[0]
else:
params[key] = val
return params
def _parse_post_params(self):
"Parse form data passed through POST."
parsed = cgi.FieldStorage(fp=self.env['wsgi.input'],
environ=self.env,
keep_blank_values=True)
params = {}
for key in parsed:
val = parsed[key]
if hasattr(val, 'filename') and val.filename:
params[key] = val
elif (isinstance(val, cgi.FieldStorage) or
isinstance(val, cgi.MiniFieldStorage)):
params[key] = val.value
else:
params[key] = [f.value for f in val]
return params
def error(code):
"A decorator for registering error handlers."
def register(handler):
error_handlers[code] = handler
return handler
return register
def header(key, value):
"A decorator for adding headers."
def register(handler):
if not getattr(handler, 'headers', {}):
handler.headers = {}
handler.headers[key.title()] = value
return handler
return register
def get(path):
"The decorator for GET."
def register(handler):
route = Route(path, handler, 'GET')
if (path, 'GET') not in [(r.path, r.method) for r in routes]:
routes.append(route)
return handler
return register
def post(path):
"The decorator for POST."
def register(handler):
route = Route(path, handler, 'POST')
if (path, 'POST') not in [(r.path, r.method) for r in routes]:
routes.append(route)
return handler
return register
class Route(object):
"Abstraction of routes."
def __init__(self, path, handler, method='GET'):
path = path.lstrip('/')
self.path = path
self.handler = handler
self.method = method.upper()
def before_request(hook):
"A decorator for adding hooks that execute before each request."
before_request_hooks.append(hook)
return hook
def _parse_param_as_dict(key, value):
match = re.compile('^(.+)\[([^\[\]]*)\]$').match(key)
if not match:
return {key: value}
k, v = match.groups()
if v == '':
if isinstance(value, list):
return _parse_param_as_dict(k, value)
return _parse_param_as_dict(k, [value])
return _parse_param_as_dict(k, {v: value})
def _recursive_dict_update(x, y):
for key, val in y.iteritems():
if isinstance(val, dict):
if not x.has_key(key):
x[key] = {}
x[key] = _recursive_dict_update(x[key], val)
elif isinstance(val, list):
if not x.has_key(key):
x[key] = []
x[key].extend(val)
else:
x[key] = val
return x