def getmethod(module, remainder): path = get_path(module, remainder) if request.method == 'GET': response = get_docs_or_tree(path, module, remainder) return jsonify(response) elif request.method == 'POST': if not request.json: response = {'status':'error','data':{}} response['data'] = 'Post datatype was not json' return jsonify(response), 400 else: req = request.json #Request is well formatted response = {'status':'success','data':{}} call = getfromdict(libraryfunctions, path) if not 'arguments' in req.keys(): print 'Error' postarguments = req['arguments'] #Iterate over the arguments that are required and extract them. args = [] argorder = getargorder(call) for a in argorder: #Check if the arg is a URL or raw data try: v = postarguments[a] except: a_key = '{}_href'.format(a) v = postarguments[a_key] try: v = gettoken(v) #Grab the data via the token except: return {'status':'failure', 'message':'Unable to get data from url: {}'.format(v)} #Convert to a Python object try: pythonarg = ast.literal_eval(v) except: pythonarg = v if isinstance(pythonarg, list): pythonarg = np.array(pythonarg) args.append(pythonarg) kwargs = {} for k, v in postarguments.iteritems(): #Skip arguments that do not have defaults, handled above if k in argorder: continue if '_href' in k and k.split('_')[0] in argorder: continue if '_href' in k: try: v = gettoken(v) k = k.split('_')[0] #clean the _href off except: return {'status':'failure', 'message':'Unable to get data from url: {}'.format(v)} #Convert to a python object try: pythonarg = ast.literal_eval(v) except: pythonarg = v #Cast to a ndarray if isinstance(v, list): v = np.array(v) kwargs[k] = v #Explicit duck typing - this is pysal specific... #Make the call and get the return items try: funcreturn = call(*args, **kwargs) except: try: for i,a in enumerate(args): if isinstance(a, list): args[i] = np.array(args[i]).reshape(-1,1) if isinstance(a, np.ndarray): args[i] = a.reshape(-1,1) funcreturn = call(*args, **kwargs) except: for i, a in enumerate(args): if isinstance(a,list): args[i] = np.array(args[i]).reshape(1,-1) funcreturn = call(*args, **kwargs) pdata = cPickle.dumps(funcreturn, cPickle.HIGHEST_PROTOCOL) #datahashvalues = '{}_{}'.format(time.time(), funcreturn) datahash = hashlib.sha256(pdata).hexdigest() insertrow = UserPyObj(current_user.get_id(), pdata, path[-1], datahash=datahash) db.session.add(insertrow) db.session.commit() #Extract the response (attributes) from the function return and assign href for each try: attributes = inspect.getmembers(funcreturn, lambda a:not(inspect.isroutine(a))) publicattributes = [a for a in attributes if not (a[0].startswith('_') and a[0].startswith('__'))] except: pass #Build the response response['data'] = {} for a in publicattributes: response['data'][a[0]] = {'type':'{}'.format(type(a[1])), 'href':basedataurl + '/mydata/{}/{}'.format(datahash,a[0])} response['data']['full_results'] = {'type':'obj', 'href':basedataurl + '/mydata/{}/full'.format(datahash)} response['data']['raw'] = {'type':'python_object', 'href':basedataurl + '/mydata/{}/raw'.format(datahash)} return jsonify(response)
def getmethod(module, remainder): path = get_path(module, remainder) if request.method == 'GET': response = get_docs_or_tree(path, module, remainder) return jsonify(response) elif request.method == 'POST': if not request.json: response = {'status': 'error', 'data': {}} response['data'] = 'Post datatype was not json' return jsonify(response), 400 else: req = request.json #Request is well formatted response = {'status': 'success', 'data': {}} call = getfromdict(libraryfunctions, path) if not 'arguments' in req.keys(): print 'Error' postarguments = req['arguments'] #Iterate over the arguments that are required and extract them. args = [] argorder = getargorder(call) for a in argorder: #Check if the arg is a URL or raw data try: v = postarguments[a] except: a_key = '{}_href'.format(a) v = postarguments[a_key] try: v = gettoken(v) #Grab the data via the token except: return { 'status': 'failure', 'message': 'Unable to get data from url: {}'.format(v) } #Convert to a Python object try: pythonarg = ast.literal_eval(v) except: pythonarg = v if isinstance(pythonarg, list): pythonarg = np.array(pythonarg) args.append(pythonarg) kwargs = {} for k, v in postarguments.iteritems(): #Skip arguments that do not have defaults, handled above if k in argorder: continue if '_href' in k and k.split('_')[0] in argorder: continue if '_href' in k: try: v = gettoken(v) k = k.split('_')[0] #clean the _href off except: return { 'status': 'failure', 'message': 'Unable to get data from url: {}'.format(v) } #Convert to a python object try: pythonarg = ast.literal_eval(v) except: pythonarg = v #Cast to a ndarray if isinstance(v, list): v = np.array(v) kwargs[k] = v #Explicit duck typing - this is pysal specific... #Make the call and get the return items try: funcreturn = call(*args, **kwargs) except: try: for i, a in enumerate(args): if isinstance(a, list): args[i] = np.array(args[i]).reshape(-1, 1) if isinstance(a, np.ndarray): args[i] = a.reshape(-1, 1) funcreturn = call(*args, **kwargs) except: for i, a in enumerate(args): if isinstance(a, list): args[i] = np.array(args[i]).reshape(1, -1) funcreturn = call(*args, **kwargs) pdata = cPickle.dumps(funcreturn, cPickle.HIGHEST_PROTOCOL) #datahashvalues = '{}_{}'.format(time.time(), funcreturn) datahash = hashlib.sha256(pdata).hexdigest() insertrow = UserPyObj(current_user.get_id(), pdata, path[-1], datahash=datahash) db.session.add(insertrow) db.session.commit() #Extract the response (attributes) from the function return and assign href for each try: attributes = inspect.getmembers( funcreturn, lambda a: not (inspect.isroutine(a))) publicattributes = [ a for a in attributes if not (a[0].startswith('_') and a[0].startswith('__')) ] except: pass #Build the response response['data'] = {} for a in publicattributes: response['data'][a[0]] = { 'type': '{}'.format(type(a[1])), 'href': basedataurl + '/mydata/{}/{}'.format(datahash, a[0]) } response['data']['full_results'] = { 'type': 'obj', 'href': basedataurl + '/mydata/{}/full'.format(datahash) } response['data']['raw'] = { 'type': 'python_object', 'href': basedataurl + '/mydata/{}/raw'.format(datahash) } return jsonify(response)
def getmethod(module, remainder): path = get_path(module, remainder) if request.method == 'GET': response = get_docs_or_tree(path, module, remainder) return jsonify(response) elif request.method == 'POST': if not request.values: response = {'status':'error','data':{}} response['data'] = 'Empty POST request.' return jsonify(response), 400 else: postarguments = request.values #Request is well formatted response = {'status':'success','data':{}} call = getfromdict(libraryfunctions, path) docs = getfromdict(librarydocs, path) resp = docs['responses'].keys() #Iterate over the arguments that are required and extract them. args = [] argorder = getargorder(call) for a in argorder: #Check if the arg is a URL or raw data try: v = postarguments[a] except: a_key = '{}_href'.format(a) v = postarguments[a_key] try: v = gettoken(v) #Grab the data via the token except: return {'status':'failure', 'message':'Unable to get data from url: {}'.format(v)} #Convert to a Python object try: pythonarg = ast.literal_eval(v) except: pythonarg = v dtype = docs['parameters']['arguments'][a]['type'] if dtype == 'array' or docs == 'string': if isinstance(pythonarg, list): pythonarg = np.array(pythonarg, dtype=np.float).T #if docs['parameters']['arguments'][a]['type'] == 'array' or: #if isinstance(pythonarg, list): #pythonarg = np.array(pythonarg, dtype=np.float).T #except: pass args.append(pythonarg) kwargs = {} for k, v in postarguments.iteritems(): #Skip arguments that do not have defaults, handled above if k in argorder: continue if '_href' in k and k.split('_')[0] in argorder: continue if '_href' in k: try: v = gettoken(v) k = k[:-5] #k = k.split('_')[0] #clean the _href off except: return {'status':'failure', 'message':'Unable to get data from url: {}'.format(v)} print k #Convert to a python object try: pythonarg = ast.literal_eval(v) except: pythonarg = v #Cast to a ndarray if isinstance(pythonarg, list): v = np.array(pythonarg, dtype=np.float) kwargs[k] = pythonarg print kwargs #Explicit duck typing - this is pysal specific... #Make the call and get the return items try: funcreturn = call(*args, **kwargs) except: for i, a in enumerate(args): if isinstance(a, list): args[i] = np.array(args[i]).reshape(-1) if isinstance(a, np.ndarray): args[i] = a.reshape(-1) funcreturn = call(*args, **kwargs) ''' except: try: for i,a in enumerate(args): if isinstance(a, list): args[i] = np.array(args[i]).reshape(-1,1) if isinstance(a, np.ndarray): args[i] = a.reshape(-1,1) print 'HERE' funcreturn = call(*args, **kwargs) except: try: for i, a in enumerate(args): if isinstance(a, list): args[i] = np.arrat(args[i]).reshape(-1) if isinstance(a, np.ndarray): args[i] = a.reshape(-1) print args[0].shape print 'HERE2' funcreturn = call(*args, **kwargs) except: for i, a in enumerate(args): if isinstance(a,list): args[i] = np.array(args[i]).reshape(1,-1) print 'HERE3' funcreturn = call(*args, **kwargs) ''' response['data'] = {} #Unpack the func return and get piped into the DB pdata = base64.b64encode(cPickle.dumps(funcreturn, cPickle.HIGHEST_PROTOCOL)) datahash = hashlib.sha224(pdata).hexdigest() insertrow = UserPyObj(userid=current_user.get_id(), pyobj=funcreturn, dataname=path[-1], datahash=datahash) db.session.add(insertrow) #Build the return response #In some instances resp can be multiple keys, e.g. from a response in the form return x, y response['data'][resp[0]] = {'href':baseurl + '/data/{}/'.format(datahash)} #Extract the response (attributes) from the function return and assign href for each try: attributes = inspect.getmembers(funcreturn, lambda a:not(inspect.isroutine(a))) publicattributes = [a for a in attributes if not (a[0].startswith('_') or a[0].startswith('__'))] except: pass #Build the response for a in publicattributes: attr_name = '_{}'.format(a[0]) response['data'][a[0]] = {'href':baseurl + '/data/{}/'.format(datahash + attr_name)} insertrow = UserPyObj(userid=current_user.get_id(), pyobj = a[1], dataname = a[0], datahash = datahash + attr_name) db.session.add(insertrow) db.session.commit() return jsonify(response)