-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpx_json_server.py
374 lines (305 loc) · 9.72 KB
/
px_json_server.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import sys
import itertools
from collections import OrderedDict
import urllib2
from urllib2 import urlopen
import urlparse
import os
from cStringIO import StringIO
import shelve
import itertools
import re
import cherrypy as cp
import pydatacube
import pydatacube.pcaxis
import pydatacube.jsonstat
jsonp_callback_check = re.compile("^[a-zA-Z0-9_]+$")
def jsonp_tool(callback_name='callback'):
def jsonp_handler(*args, **kwargs):
request = cp.serving.request
orig_handler = request._jsonp_inner_handler
if callback_name not in request.params:
return orig_handler(*args, **kwargs)
callback = request.params.pop(callback_name)
if not jsonp_callback_check.match(callback):
raise ValueError("Invalid JSONP callback name")
value = orig_handler(*args, **kwargs)
ct = cp.serving.response.headers['Content-Type']
if not ct.startswith("application/json"):
return value
cp.serving.response.headers['Content-Type'] = "application/javascript"
if isinstance(value, basestring):
return "%s(%s)"%(callback, str(value))
# We probably have an iterator
return itertools.chain((callback, '('), value, (')'))
request = cp.serving.request
request._jsonp_inner_handler = request.handler
request.handler = jsonp_handler
cp.tools.jsonp = cp.Tool('before_handler', jsonp_tool, priority=31)
def json_expose(func):
func = cp.tools.json_out()(func)
func.exposed = True
return func
def is_exposed(obj):
if getattr(obj, 'func_name', False) == 'index':
return False
if callable(obj) and getattr(obj, 'exposed', False):
return True
if hasattr(obj, 'index'):
idx = getattr(obj, 'index')
if callable(idx) and getattr(idx, 'exposed', False):
return True
return False
HAL_BLACKLIST = {'favicon_ico': True}
def default_hal_dir(obj):
for name in dir(obj):
if name.startswith('__'):
continue
if name in HAL_BLACKLIST:
continue
yield (name, getattr(obj, name))
def object_hal_links(obj, dirrer=default_hal_dir):
links = {}
if is_exposed(obj):
links['self'] = {'href': cp.url(relative=False)}
for name, value in dirrer(obj):
if not is_exposed(value):
continue
link = {'href': cp.url(name, relative=False)}
links[name] = link
return links
class DictExposer(object):
def __init__(self, mydict):
self._dict = mydict
@json_expose
def index(self):
objects = dict()
for key, value in self._dict.iteritems():
if hasattr(value, '_preview'):
entry = OrderedDict(value._preview().iteritems())
else:
entry = OrderedDict()
entry['_links'] = OrderedDict()
entry['_links']['self'] = {
'href': cp.url(key, relative=False)
}
objects[key] = entry
ret = OrderedDict()
ret['_embedded'] = objects
ret['_links'] = object_hal_links(self)
return ret
def __getattr__(self, attr):
try:
return self._dict[attr]
except KeyError:
raise AttributeError("No item '%s'"%(attr))
class ResourceServer(object):
def __init__(self, resources=None):
if resources is None:
resources = {}
self._resources = resources
self.resources = DictExposer(self._resources)
@json_expose
def index(self):
ret = {}
ret['_links'] = object_hal_links(self)
return ret
class CubeResource(object):
MAX_ENTRIES=1000
def __init__(self, lazycube):
# A "lazy" cube so that we don't have to keep
# it in memory
# TODO: May be confusing
self._lazycube = lazycube
cube = self._lazycube()
# Cache here so we don't have to deserialize
# the large cube-object every time
self._specification = cube.specification
self._metadata = cube.metadata
@json_expose
def index(self):
spec = OrderedDict(self._specification)
spec['_links'] = object_hal_links(self)
return spec
@json_expose
def entries(self, start=0, end=None,
dimension_labels=False, category_labels=False):
# TODO: No need to really iterate if
# pydatacube would support slicing
if end is None:
end = self._specification['length']
end = int(end)
start = int(start)
if end - start > self.MAX_ENTRIES:
raise ValueError("No more than %i entries allowed at a time. Use 'start' and 'end' parameters to limit the selection."%self.MAX_ENTRIES)
entry_iter = self._lazycube().toEntries(
dimension_labels=dimension_labels,
category_labels=category_labels)
entry_iter = itertools.islice(entry_iter, start, end)
return list(map(OrderedDict, entry_iter))
@json_expose
def table(self, start=0, end=None, labels=False):
# TODO: No need to really iterate if
# pydatacube would support slicing
if end is None:
end = self._specification['length']
end = int(end)
start = int(start)
if end - start > self.MAX_ENTRIES:
raise ValueError("No more than %i entries allowed at a time. Use 'start' and 'end' parameters to limit the selection."%self.MAX_ENTRIES)
entry_iter = self._lazycube().toTable(labels=labels)
entry_iter = itertools.islice(entry_iter, start, end)
return list(map(list, entry_iter))
@json_expose
def columns(self,
start=0, end=None,
dimension_labels=False, category_labels=False,
collapse_unique=True):
if end is None:
end = self._specification['length']
end = int(end)
start = int(start)
if end - start > self.MAX_ENTRIES:
raise ValueError("No more than %i entries allowed at a time. Use 'start' and 'end' parameters to limit the selection."%self.MAX_ENTRIES)
return self._lazycube().toColumns(
start=start, end=end,
dimension_labels=dimension_labels,
category_labels=category_labels,
collapse_unique=collapse_unique)
@json_expose
def group_for_columns(self, as_values=None,
dimension_labels=False, category_labels=False):
if as_values is not None:
as_values = as_values.split(',')
groups = self._lazycube().group_for(*as_values)
groupcols = []
for group in groups:
if len(group) > self.MAX_ENTRIES:
raise ValueError("No more than %i entries allowed at a time. Use 'start' and 'end' parameters to limit the selection."%self.MAX_ENTRIES)
col = group.toColumns(
dimension_labels=dimension_labels,
category_labels=category_labels)
groupcols.append(col)
return groupcols
# TODO: Expose only if can be converted?
@json_expose
def jsonstat(self):
return pydatacube.jsonstat.to_jsonstat(self._lazycube())
def __filter(self, **kwargs):
filters = {}
for dim, catstr in kwargs.iteritems():
filters[dim] = catstr.split(',')
return CubeResource(lambda: self._lazycube().filter(**filters))
def __getattr__(self, attr):
parts = attr.split('&')
if parts[0] != 'filter':
return object.__getattr__(self, attr)
args = []
kwargs = {}
for part in parts[1:]:
split = part.split('=', 1)
if len(split) == 1:
args.append(split[0])
else:
kwargs[split[0]] = split[1]
return self.__filter(*args, **kwargs)
def _preview(self):
return {'metadata': self._metadata}
class PxResource(CubeResource):
def __init__(self, data, metadata, datastore):
px_data = data.read()
data = StringIO(px_data)
datastore['cube'] = pydatacube.pcaxis.to_cube(data)
CubeResource.__init__(self, lambda: datastore['cube'])
datastore['pc-axis'] = px_data
@cp.expose
def pc_axis(self):
return datastore['pc-axis']
def fetch_px_resource(spec, datastore):
metadata = {}
if 'file' in spec:
data = open(spec['file'])
url = "file://"+spec['file']
else:
url = spec['url']
data = urlopen(spec['url'])
if 'id' not in spec:
parsed = urlparse.urlparse(url)
basename = os.path.basename(parsed.path)
basename = os.path.splitext(basename)[0]
id = '%s:%s'%(parsed.netloc, basename)
else:
id = spec['id']
metadata = dict(
id=id
)
return id, PxResource(data, metadata, datastore(id))
class _Substore(object):
def __init__(self, prefix, backend):
self._prefix = prefix
self._backend = backend
def _getid(self, attr):
return str(self._prefix + '/' + attr)
def __getitem__(self, attr):
return self._backend[self._getid(attr)]
def __setitem__(self, attr, value):
self._backend[self._getid(attr)] = value
class PrefixStore(object):
def __init__(self, backend):
self._backend = backend
def __call__(self, id):
return _Substore(id, self._backend)
def serve_px_resources(resources):
SERVER_ROOT = os.path.dirname(os.path.abspath('__file__'))
# TODO: Figure out nicer persistence
# TODO: Really not necessary to recreate every time!
shelve_file_path = SERVER_ROOT + "/px_json_server.shelve"
backend = shelve.open(shelve_file_path, flag='c', protocol=-1, writeback=True)
storer = PrefixStore(backend)
px_resources = {}
for spec in resources:
try:
id, px_resource = fetch_px_resource(spec, storer)
px_resources[id] = px_resource
except urllib2.HTTPError, e:
print >>sys.stderr, "Fetching file failed", e, spec
except pydatacube.pcaxis.PxSyntaxError, e:
print >>sys.stderr, "Px parsing failed:", e, spec
backend.sync()
server = ResourceServer(px_resources)
import string
dispatch = cp.dispatch.Dispatcher(translate=string.maketrans('', ''))
def CORS():
cp.response.headers["Access-Control-Allow-Origin"] = "*"
cp.tools.CORS = cp.Tool('before_finalize', CORS)
config = {
'global': {
'SERVER_ROOT_DIR': SERVER_ROOT
},
'/': {
'request.dispatch': dispatch,
'tools.CORS.on': True,
'tools.jsonp.on': True
},
'/browser': {
'tools.staticdir.on': True,
'tools.staticdir.root': SERVER_ROOT,
'tools.staticdir.dir': 'browser',
'tools.staticdir.index': 'index.html'
}
}
cp.config.update(config)
app = cp.tree.mount(server, '/', config=config)
conffilepath = os.path.join(SERVER_ROOT, 'px_json_server.conf')
if os.path.exists(conffilepath):
cp.config.update(conffilepath)
app.merge(conffilepath)
if hasattr(cp.engine, 'signals'):
# Conditional for older cherrpy versions.
# Not even sure what this does.
cp.engine.signals.subscribe()
cp.engine.start()
cp.engine.block()
if __name__ == '__main__':
import json
serve_px_resources(json.load(open(sys.argv[1])))