def handle_array_query(): array_name, indexers = split_array_base(request.path) arr = get_array(array_name, indexers) base_url = request.url_root[:-1] #no query params if len(request.values) == 0: return html_array(arr, base_url, array_name, indexers) q_req = request.values['r'] if q_req == 'data.json': return Response(nd.as_py(nd.format_json(arr).view_scalars(ndt.bytes)), mimetype='application/json') elif q_req == 'datashape': content_type = 'text/plain; charset=utf-8' return arr.dshape elif q_req == 'dyndtype': content_type = 'application/json; charset=utf-8' body = str(arr.dtype) return Response(body, mimetype='application/json') elif q_req == 'dynddebug': return arr.debug_repr() elif q_req == 'create_session': session = compute_session(app.array_provider, base_url, add_indexers_to_url(array_name, indexers)) app.sessions[session.session_name] = session content_type, body = session.creation_response() return Response(body, mimetype='application/json') else: abort(400, "Unknown Blaze server request %s" % q_req)
def handle_array_query(): array_name, indexers = split_array_base(request.path.rstrip('/')) arr = get_array(array_name, indexers) base_url = request.url_root[:-1] #no query params # NOTE: len(request.values) was failing within werkzeug if len(list(request.values)) == 0: return html_array(arr, base_url, array_name, indexers) q_req = request.values['r'] if q_req == 'data.json': dat = arr.ddesc.dynd_arr() return Response(nd.as_py(nd.format_json(dat).view_scalars(ndt.bytes)), mimetype='application/json') elif q_req == 'datashape': content_type = 'text/plain; charset=utf-8' return str(arr.dshape) elif q_req == 'dyndtype': content_type = 'application/json; charset=utf-8' body = str(arr.dtype) return Response(body, mimetype='application/json') elif q_req == 'dynddebug': return arr.debug_repr() elif q_req == 'create_session': session = compute_session(base_url, add_indexers_to_url(array_name, indexers)) app.sessions[session.session_name] = session content_type, body = session.creation_response() return Response(body, mimetype='application/json') else: abort(400, "Unknown Blaze server request %s" % q_req)
def create_json(self, name): a = nd.array([[1, 2, 3], [1, 2]]) with open(os.path.join(self.arrdir, '%s.json' % name), 'w') as f: f.write(nd.as_py(nd.format_json(a))) with open(os.path.join(self.arrdir, '%s.array' % name), 'w') as f: f.write('type: json\n') f.write('import: {}\n') f.write('datashape: "var, var, int32"\n')
def create_json(self, name): a = nd.array([[1, 2, 3], [1, 2]]) with open(os.path.join(self.arrdir, '%s.json' % name), 'w') as f: f.write(nd.as_py(nd.format_json(a))) with open(os.path.join(self.arrdir, '%s.array' % name), 'w') as f: f.write('type: json\n') f.write('import: {}\n') f.write('datashape: "var * var * int32"\n')
def handle_array_query(self, environ, start_response): print('Handling array query') try: array_name, indexers = split_array_base(environ['PATH_INFO']) arr = self.get_array(array_name, indexers) base_url = wsgi_reconstruct_base_url(environ) request_method = environ['REQUEST_METHOD'] if request_method == 'GET' and environ['QUERY_STRING'] == '': # This version of the array information is for human consumption content_type = 'text/html; charset=utf-8' body = self.html_array(arr, base_url, array_name, indexers) else: if request_method == 'GET': q = parse_qs(environ['QUERY_STRING']) elif request_method == 'POST': # the environment variable CONTENT_LENGTH may be empty or missing try: request_body_size = int(environ.get('CONTENT_LENGTH', 0)) except (ValueError): request_body_size = 0 request_body = environ['wsgi.input'].read(request_body_size) q = parse_qs(request_body) else: status = '404 Not Found' response_headers = [('content-type', 'text/plain')] start_response(status, response_headers) return ['Unsupported request method'] print q if not q.has_key('r'): status = '400 Bad Request' response_headers = [('content-type', 'text/plain')] start_response(status, response_headers, sys.exc_info()) return ['Blaze server request requires the ?r= query request type'] q_req = q['r'][0] if q_req == 'data.json': content_type = 'application/json; charset=utf-8' body = nd.as_py(nd.format_json(arr).view_scalars(ndt.bytes)) elif q_req == 'datashape': content_type = 'text/plain; charset=utf-8' body = arr.dshape elif q_req == 'dyndtype': content_type = 'application/json; charset=utf-8' body = str(arr.dtype) elif q_req == 'dynddebug': content_type = 'text/plain; charset=utf-8' body = arr.debug_repr() elif q_req == 'create_session': session = compute_session(self.array_provider, base_url, add_indexers_to_url(array_name, indexers)) self.sessions[session.session_name] = session content_type, body = session.creation_response() else: status = '400 Bad Request' response_headers = [('content-type', 'text/plain')] start_response(status, response_headers, sys.exc_info()) return ['Unknown Blaze server request ?r=%s' % q['r'][0]] except: traceback.print_exc() status = '404 Not Found' response_headers = [('content-type', 'text/plain')] start_response(status, response_headers, sys.exc_info()) return ['Error getting Blaze Array\n\n' + traceback.format_exc()] status = '200 OK' response_headers = [ ('content-type', content_type), ('content-length', str(len(body))) ] start_response(status, response_headers) return [body]
def data(datasets, name): """ Basic indexing API Allows remote indexing of datasets. Takes indexing data as JSON Takes requests like Example ------- For the following array: [['Alice', 100], ['Bob', 200], ['Charlie', 300]] schema = '{name: string, amount: int32}' And the following url: /data/table-name.json POST-data: {'index': [{'start': 0, 'step': 3}, 'name']} and returns responses like {"name": "table-name", "index": [0, "name"], "datashape": "3 * string", "data": ["Alice", "Bob", "Charlie"]} """ if request.headers['content-type'] != 'application/json': return ("Expected JSON data", 404) try: data = json.loads(request.data) except ValueError: return ("Bad JSON. Got %s " % request.data, 404) try: dset = datasets[name] except KeyError: return ("Dataset %s not found" % name, 404) try: index = parse_index(data['index']) except ValueError: return ("Bad index", 404) try: rv = dset[index] except RuntimeError: return ("Bad index: %s" % (str(index)), 404) if isinstance(rv, Iterator): rv = list(rv) dshape = dset.dshape.subshape[index] rv = json.loads(str(nd.format_json(nd.array(rv, type=str(dshape)), tuple=True))) response = {'name': name, 'index': data['index'], 'datashape': str(dshape), 'data': rv} return jsonify(response)