-
Notifications
You must be signed in to change notification settings - Fork 2
/
Service.py
executable file
·261 lines (220 loc) · 7.09 KB
/
Service.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
#!/usr/bin/env python
"""
Copyright 2007-2012 David Garcia Garzon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import webob
import sys, os
import decorator
class HttpError(Exception) :
def __init__(self, status, message) :
self.status = status
self.message = message
class NotFound(HttpError) :
def __init__(self, message) :
HttpError.__init__(self, "404 Not Found", message)
class Forbidden(HttpError) :
def __init__(self, message) :
HttpError.__init__(self, "403 Forbidden", message)
class BadRequest(HttpError) :
def __init__(self, message) :
HttpError.__init__(self, "400 Bad Request", message)
class query(object) :
"""Decorator to enhance functions"""
def __init__(self, content_type='text/plain') :
self.content_type = content_type
def __call__(self, f, **kw) :
f.content_type = self.content_type
return f
def wrapper(*args, **kwd) :
return f(*args, **kwd)
wrapper.__name__ = f.__name__
wrapper.__doc__ = f.__doc__
wrapper.__dict__.update(f.__dict__)
return wrapper
class Reload:
""" Module reload middleware """
def __init__(self, app):
# TODO: Check is one of our apps
self.app = app
self.mtimes = mtimes = {}
for name in self.app._modules:
__import__(name)
moduleFile = sys.modules[name].__file__
self.mtimes[name] = (
moduleFile, os.stat(moduleFile).st_mtime)
def __call__(self, environ, start_response):
for name, (path, mtime) in self.mtimes.iteritems():
if os.stat(path).st_mtime == mtime : continue
print 'Reloading', name, path
execfile(path, sys.modules[name].__dict__)
self.mtimes[name] = path, os.stat(path).st_mtime
return self.app(environ, start_response)
class Service :
def __init__(self, modules=None) :
if modules is not None :
self._modules = modules
def wrapper(aWrapper) :
"""This decorator simplifies the definition of
wrapper functions to be cascaded. The decorators
receive the wrapped function as second argument."""
def decorator(wrapped) :
def inner(self, *args, **kwd) :
return aWrapper(self, wrapped, *args, **kwd)
return inner
return decorator
@wrapper
def _webobWrap(self, f, environ, start_response) :
"""This decorator takes a webob based application, receiving
a webob.Request and returning an webob.Response, into a wsgi
compatible call method.
"""
request = webob.Request(environ)
# Untested
if request.charset is None:
request.charset = 'utf8'
response = f(self,request)
return response(environ, start_response)
@wrapper
def _handleErrors(self, f, request) :
"""This decorator wraps a webob app and turns any raised
exception into proper a HTTP error response.
"""
try :
return f(self,request)
except HttpError as e :
return webob.Response(
"%s: %s\n"%(e.__class__.__name__, e.message),
status = e.status,
content_type ='text/plain',
)
except Exception as e :
return webob.Response(
"%s: %s\n"%(e.__class__.__name__, e),
status = "500 Internal Server Error",
content_type = 'text/plain',
)
# TODO: Not unittested
@wrapper
def _handleAffero(self, f, request, module=None) :
"""This decorator helps to acomplish the affero licence
this code is licenced under, by providing a service 'affero'
to get the source.
"""
nextLevel = request.path_info_peek()
if nextLevel == "affero" :
source = __file__ if module is None else module.__file__
if source.endswith("pyc") :
source = source[:-1]
return webob.Response(
file(source).read(),
content_disposition = 'filename="%s"'%(
os.path.basename(source)),
content_type = 'text/plain',
)
if module is None : return f(self,request)
return f(self,request, module)
@wrapper
def _chooseModule(self, f, request) :
"""This wrapper consumes one path level of the request
and checks that it matches the name of one of the
published modules.
"""
moduleName = request.path_info_pop()
if moduleName not in self._modules :
raise NotFound("Bad service %s"%moduleName)
module = __import__(moduleName)
return f(self, request, module)
@wrapper
def _chooseTarget(self, f, request, module) :
"""This wrapper consume one path level from the request
and ensures that it matches a valid element within
the module.
"""
moduleName = module.__name__
targetName = request.path_info_pop()
if not targetName :
raise BadRequest("Specify a subservice within '%s'"%(
moduleName))
if targetName.startswith("_") :
raise Forbidden("Private object")
if targetName not in module.__dict__ :
raise NotFound("Bad function %s.%s"%(
moduleName, targetName))
target = module.__dict__[targetName]
if target.__class__.__name__ == 'module' :
raise NotFound("Bad function %s.%s"%(
moduleName, targetName))
return f(self, request, target=target)
@wrapper
def _handleData(self, f, request, target) :
if callable(target) : return f(self, request, target)
return webob.Response(
str(target),
content_type = 'text/plain',
)
@_webobWrap
@_handleErrors
@_handleAffero
@_chooseModule
@_handleAffero
@_chooseTarget
@_handleData
def __call__(self, request, target):
""" Handle request """
# TODO: Multiple valued
requestVar = "request"
paramnames = target.func_code.co_varnames
hasRequest = requestVar in paramnames and paramnames.index(requestVar)==0
nDefaults = len(target.func_defaults or ())
nDeclared = target.func_code.co_argcount
required = paramnames[1 if hasRequest else 0:nDeclared-nDefaults]
declared = paramnames[:nDeclared]
hasKeyword = target.func_code.co_flags & 0x08
missing = [
p for p in required
if p not in request.params
]
if missing :
raise BadRequest("Missing parameters: %s"%(
", ".join(missing)))
exceed = [
p for p in request.params
if p not in declared
]
if hasRequest and requestVar in request.params :
raise BadRequest("Unavailable parameter: %s"%requestVar)
if exceed and not hasKeyword:
raise BadRequest("Unavailable parameter: %s"%(
", ".join(exceed)))
if hasRequest :
result = target(request=request, **request.params)
else :
result = target(**request.params)
if isinstance(result, basestring) :
content_type = getattr(target, 'content_type', 'text/plain')
return webob.Response(
result,
content_type = content_type,
)
return result
if __name__=="__main__" :
services = sys.argv[1:] or ["TestingService"]
application = Reload(Service(services))
print "Loading server"
from wsgiref.simple_server import make_server
httpd = make_server(
'localhost', # Host name.
8051, # port
application, # application object
)
httpd.serve_forever()