Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
    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]