<s> from __future__ import absolute_import <EOL> import os <EOL> import json <EOL> import hashlib <EOL> import rdflib <EOL> import requests <EOL> from celery import Celery <EOL> from flask import Flask , make_response <EOL> from flask . ext . restful import Api , Resource , reqparse <EOL> from niquery . utils import NS <EOL> from niquery . query import AskQuery , SelectQuery <EOL> def create_app ( environment = None ) : <EOL> """<STR_LIT>""" <EOL> app = Flask ( __name__ ) <EOL> if not environment : <EOL> environment = os . environ . get ( '<STR_LIT>' ) <EOL> if not environment : <EOL> environment = '<STR_LIT>' <EOL> app . config . from_object ( <EOL> '<STR_LIT>' . format ( environment . capitalize ( ) ) ) <EOL> return app <EOL> def make_celery ( app ) : <EOL> celery = Celery ( app . import_name , broker = app . config [ '<STR_LIT>' ] ) <EOL> celery . conf . update ( app . config ) <EOL> TaskBase = celery . Task <EOL> class ContextTask ( TaskBase ) : <EOL> abstract = True <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> with app . app_context ( ) : <EOL> return TaskBase . __call__ ( self , * args , ** kwargs ) <EOL> celery . Task = ContextTask <EOL> return celery <EOL> app = create_app ( ) <EOL> api = Api ( app ) <EOL> @ api . representation ( '<STR_LIT>' ) <EOL> def turtle ( data , code , headers = None ) : <EOL> g = rdflib . ConjunctiveGraph ( ) <EOL> g . parse ( data = json . dumps ( data ) , format = '<STR_LIT>' ) <EOL> resp = make_response ( g . serialize ( format = '<STR_LIT>' ) , code ) <EOL> resp . headers . extend ( headers or { } ) <EOL> return resp <EOL> celery = make_celery ( app ) <EOL> @ celery . task ( name = "<STR_LIT>" ) <EOL> def add ( x , y ) : <EOL> return x + y <EOL> @ celery . task ( name = '<STR_LIT>' ) <EOL> def bet ( record ) : <EOL> import nipype <EOL> from nipype . interfaces . fsl import BET <EOL> from nipype . utils . filemanip import hash_infile <EOL> nipype . config . enable_provenance ( ) <EOL> in_file_uri = record [ '<STR_LIT>' ] <EOL> os . chdir ( '<STR_LIT>' ) <EOL> fname = '<STR_LIT>' <EOL> with open ( fname , '<STR_LIT:wb>' ) as fd : <EOL> response = requests . get ( in_file_uri , stream = True ) <EOL> if not response . ok : <EOL> response . raise_for_status ( ) <EOL> for chunk in response . iter_content ( <NUM_LIT> ) : <EOL> fd . write ( chunk ) <EOL> sha = hash_infile ( os . path . abspath ( fname ) , crypto = hashlib . sha512 ) <EOL> select = SelectQuery ( config = app . config ) <EOL> res = select . execute_select ( '<STR_LIT>' ) <EOL> sha_recs = res [ res . sha512 . str . contains ( sha ) ] <EOL> bet_recs = res [ res . interface . str . contains ( '<STR_LIT>' ) ] <EOL> results = dict ( ) <EOL> if sha_recs . empty or ( not sha_recs . empty and bet_recs . empty ) : <EOL> better = BET ( ) <EOL> better . inputs . in_file = os . path . abspath ( fname ) <EOL> result = better . run ( ) <EOL> prov = result . provenance . rdf ( ) . serialize ( format = '<STR_LIT>' ) <EOL> results . update ( { '<STR_LIT>' : prov } ) <EOL> else : <EOL> results . update ( { '<STR_LIT>' : res . to_json ( orient = '<STR_LIT>' ) } ) <EOL> return results <EOL> parser = reqparse . RequestParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> class Validate ( Resource ) : <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> ask = AskQuery ( ) <EOL> return ask . sparql_meta . to_dict ( outtype = '<STR_LIT>' ) <EOL> class ValidateResult ( Resource ) : <EOL> def get ( self , task_id ) : <EOL> """<STR_LIT>""" <EOL> ask = AskQuery ( ) <EOL> qid = "<STR_LIT>" . format ( task_id ) <EOL> result = ask . describe_query ( qid ) <EOL> return result . to_dict ( ) <EOL> class Inference ( Resource ) : <EOL> def get ( self ) : <EOL> pass <EOL> class InferenceResult ( Resource ) : <EOL> def get ( self , task_id ) : <EOL> pass <EOL> class Compute ( Resource ) : <EOL> def get ( self ) : <EOL> select = SelectQuery ( config = app . config ) <EOL> compute = select . sparql_meta . query_type == str ( NS . niq . ComputeQuery ) <EOL> result = select . sparql_meta [ compute ] <EOL> return result . to_dict ( outtype = '<STR_LIT>' ) <EOL> def post ( self ) : <EOL> args = parser . parse_args ( ) <EOL> result = [ ] <EOL> select = SelectQuery ( ) <EOL> df = select . execute_select ( args [ '<STR_LIT>' ] , <EOL> turtle_str = args [ '<STR_LIT>' ] ) <EOL> for record in df . to_dict ( outtype = '<STR_LIT>' ) : <EOL> task_id = record [ '<STR_LIT>' ] <EOL> task = celery . tasks [ task_id ] <EOL> res = task . apply_async ( [ record ] ) <EOL> record . update ( { "<STR_LIT>" : res . task_id } ) <EOL> result . append ( record ) <EOL> return result <EOL> class ComputeResult ( Resource ) : <EOL> def get ( self , task_id ) : <EOL> async = celery . AsyncResult ( task_id ) <EOL> result = { '<STR_LIT>' : async . id , <EOL> '<STR_LIT>' : async . state } <EOL> if async . ready ( ) : <EOL> prov = async . get ( timeout = <NUM_LIT:1.0> ) <EOL> graph = json . loads ( prov [ '<STR_LIT>' ] ) <EOL> store = SelectQuery ( config = app . config ) . get_graph ( ) . store <EOL> sparql_store = rdflib . ConjunctiveGraph ( store = store , <EOL> identifier = NS . iri [ async . id ] ) <EOL> g = rdflib . Graph ( ) <EOL> g . parse ( data = prov [ '<STR_LIT>' ] , format = '<STR_LIT>' ) <EOL> g . skolemize ( new_graph = sparql_store ) <EOL> result . update ( { '<STR_LIT>' : NS . iri [ async . id ] , <EOL> '<STR_LIT>' : graph } ) <EOL> return result <EOL> api . add_resource ( Validate , '<STR_LIT>' ) <EOL> api . add_resource ( ValidateResult , '<STR_LIT>' ) <EOL> api . add_resource ( Inference , '<STR_LIT>' ) <EOL> api . add_resource ( InferenceResult , '<STR_LIT>' ) <EOL> api . add_resource ( Compute , '<STR_LIT>' , '<STR_LIT>' ) <EOL> api . add_resource ( ComputeResult , '<STR_LIT>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> app . run ( host = app . config [ '<STR_LIT>' ] , port = app . config [ '<STR_LIT>' ] ) </s>
