<s> """<STR_LIT>""" <EOL> import re <EOL> from bulbs . config import Config , DEBUG , ERROR <EOL> from bulbs . registry import Registry <EOL> from bulbs . utils import get_logger <EOL> from bulbs . json import JSONTypeSystem <EOL> from bulbs . base import Client , Response , Result <EOL> from bulbs . rest import Request , RESPONSE_HANDLERS , server_error <EOL> from bulbs . utils import json , build_path , get_file_path , urlsplit <EOL> from bulbs . groovy import GroovyScripts <EOL> from . cypher import Cypher , Yaml <EOL> NEO4J_URI = "<STR_LIT>" <EOL> log = get_logger ( __name__ ) <EOL> vertex_path = "<STR_LIT>" <EOL> edge_path = "<STR_LIT>" <EOL> index_path = "<STR_LIT:index>" <EOL> gremlin_path = "<STR_LIT>" <EOL> cypher_path = "<STR_LIT>" <EOL> class Neo4jResult ( Result ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , result , config ) : <EOL> self . config = config <EOL> self . raw = result <EOL> self . data = self . _get_data ( result ) <EOL> self . type_map = dict ( node = "<STR_LIT>" , relationship = "<STR_LIT>" ) <EOL> def get_id ( self ) : <EOL> """<STR_LIT>""" <EOL> uri = self . raw . get ( '<STR_LIT>' ) <EOL> return self . _parse_id ( uri ) <EOL> def get_type ( self ) : <EOL> """<STR_LIT>""" <EOL> uri = self . get_uri ( ) <EOL> neo4j_type = self . _parse_type ( uri ) <EOL> return self . type_map [ neo4j_type ] <EOL> def get_data ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data <EOL> def get_uri ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . raw . get ( '<STR_LIT>' ) <EOL> def get_outV ( self ) : <EOL> """<STR_LIT>""" <EOL> uri = self . raw . get ( '<STR_LIT:start>' ) <EOL> return self . _parse_id ( uri ) <EOL> def get_inV ( self ) : <EOL> """<STR_LIT>""" <EOL> uri = self . raw . get ( '<STR_LIT:end>' ) <EOL> return self . _parse_id ( uri ) <EOL> def get_label ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . raw . get ( '<STR_LIT:type>' ) <EOL> def get_index_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . raw . get ( '<STR_LIT:name>' ) <EOL> def get_index_class ( self ) : <EOL> """<STR_LIT>""" <EOL> uri = self . raw . get ( '<STR_LIT>' ) <EOL> neo4j_type = self . _parse_index_type ( uri ) <EOL> return self . type_map [ neo4j_type ] <EOL> def get ( self , attribute ) : <EOL> """<STR_LIT>""" <EOL> return self . raw [ attribute ] <EOL> def _get_data ( self , result ) : <EOL> if type ( result ) is dict : <EOL> return result . get ( '<STR_LIT:data>' ) <EOL> def _parse_id ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> if uri : <EOL> _id = int ( uri . rpartition ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] ) <EOL> return _id <EOL> def _parse_type ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> if uri : <EOL> root_uri = uri . rpartition ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] <EOL> neo4j_type = root_uri . rpartition ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> return neo4j_type <EOL> def _parse_index_type ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> if uri : <EOL> path = urlsplit ( uri ) . path <EOL> segments = path . split ( "<STR_LIT:/>" ) <EOL> neo4j_type = segments [ - <NUM_LIT:4> ] <EOL> return neo4j_type <EOL> class Neo4jResponse ( Response ) : <EOL> """<STR_LIT>""" <EOL> result_class = Neo4jResult <EOL> def __init__ ( self , response , config ) : <EOL> self . config = config <EOL> self . handle_response ( response ) <EOL> self . headers = self . get_headers ( response ) <EOL> self . content = self . get_content ( response ) <EOL> self . results , self . total_size = self . get_results ( ) <EOL> self . raw = self . _maybe_get_raw ( response , config ) <EOL> def _maybe_get_raw ( self , response , config ) : <EOL> """<STR_LIT>""" <EOL> if config . log_level == DEBUG : <EOL> return response <EOL> def handle_response ( self , response ) : <EOL> """<STR_LIT>""" <EOL> headers , content = response <EOL> if re . search ( b"<STR_LIT>" , content ) : <EOL> server_error ( response ) <EOL> response_handler = RESPONSE_HANDLERS . get ( headers . status ) <EOL> response_handler ( response ) <EOL> def get_headers ( self , response ) : <EOL> """<STR_LIT>""" <EOL> headers , content = response <EOL> return headers <EOL> def get_content ( self , response ) : <EOL> """<STR_LIT>""" <EOL> headers , content = response <EOL> if content : <EOL> content = json . loads ( content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> return content <EOL> def get_results ( self ) : <EOL> """<STR_LIT>""" <EOL> if type ( self . content ) == list : <EOL> results = ( self . result_class ( result , self . config ) for result in self . content ) <EOL> total_size = len ( self . content ) <EOL> elif self . content and self . content != "<STR_LIT:null>" : <EOL> results = self . result_class ( self . content , self . config ) <EOL> total_size = <NUM_LIT:1> <EOL> else : <EOL> results = None <EOL> total_size = <NUM_LIT:0> <EOL> return results , total_size <EOL> def _set_index_name ( self , index_name ) : <EOL> """<STR_LIT>""" <EOL> self . results . raw [ '<STR_LIT:name>' ] = index_name <EOL> class Neo4jRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> response_class = Neo4jResponse <EOL> class Neo4jClient ( Client ) : <EOL> """<STR_LIT>""" <EOL> default_uri = NEO4J_URI <EOL> request_class = Neo4jRequest <EOL> def __init__ ( self , config = None ) : <EOL> self . config = config or Config ( self . default_uri ) <EOL> self . registry = Registry ( self . config ) <EOL> self . type_system = JSONTypeSystem ( ) <EOL> self . request = self . request_class ( self . config , self . type_system . content_type ) <EOL> self . scripts = GroovyScripts ( self . config ) <EOL> scripts_file = get_file_path ( __file__ , "<STR_LIT>" ) <EOL> self . scripts . update ( scripts_file ) <EOL> self . registry . add_scripts ( "<STR_LIT>" , self . scripts ) <EOL> def gremlin ( self , script , params = None ) : <EOL> """<STR_LIT>""" <EOL> path = gremlin_path <EOL> params = dict ( script = script , params = params ) <EOL> return self . request . post ( path , params ) <EOL> def cypher ( self , query , params = None ) : <EOL> """<STR_LIT>""" <EOL> path = cypher_path <EOL> params = dict ( query = query , params = params ) <EOL> resp = self . request . post ( path , params ) <EOL> resp . total_size = len ( resp . results . data ) <EOL> resp . results = ( Neo4jResult ( result [ <NUM_LIT:0> ] , self . config ) for result in resp . results . data ) <EOL> return resp <EOL> def create_vertex ( self , data , keys = None ) : <EOL> """<STR_LIT>""" <EOL> if keys or self . config . autoindex is True : <EOL> index_name = self . config . vertex_index <EOL> return self . create_indexed_vertex ( data , index_name , keys = keys ) <EOL> path = vertex_path <EOL> params = self . _remove_null_values ( data ) <EOL> return self . request . post ( path , params ) <EOL> def get_vertex ( self , _id ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( vertex_path , _id ) <EOL> params = None <EOL> return self . request . get ( path , params ) <EOL> def get_all_vertices ( self ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( "<STR_LIT>" ) <EOL> params = None <EOL> return self . gremlin ( script , params ) <EOL> def update_vertex ( self , _id , data , keys = None ) : <EOL> """<STR_LIT>""" <EOL> if keys or self . config . autoindex is True : <EOL> index_name = self . config . vertex_index <EOL> return self . update_indexed_vertex ( _id , data , index_name , keys = keys ) <EOL> path = self . _build_vertex_path ( _id , "<STR_LIT>" ) <EOL> params = self . _remove_null_values ( data ) <EOL> return self . request . put ( path , params ) <EOL> def delete_vertex ( self , _id ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( "<STR_LIT>" ) <EOL> params = dict ( _id = _id ) <EOL> return self . gremlin ( script , params ) <EOL> def create_edge ( self , outV , label , inV , data = None , keys = None ) : <EOL> """<STR_LIT>""" <EOL> if keys or self . config . autoindex is True : <EOL> index_name = self . config . edge_index <EOL> return self . create_indexed_edge ( outV , label , inV , data , index_name , keys = keys ) <EOL> data = self . _remove_null_values ( data ) <EOL> inV_uri = self . _build_vertex_uri ( inV ) <EOL> path = build_path ( vertex_path , outV , "<STR_LIT>" ) <EOL> params = { '<STR_LIT:to>' : inV_uri , '<STR_LIT:type>' : label , '<STR_LIT:data>' : data } <EOL> return self . request . post ( path , params ) <EOL> def get_edge ( self , _id ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( edge_path , _id ) <EOL> params = None <EOL> return self . request . get ( path , params ) <EOL> def get_all_edges ( self ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( "<STR_LIT>" ) <EOL> params = None <EOL> return self . gremlin ( script , params ) <EOL> def update_edge ( self , _id , data , keys = None ) : <EOL> """<STR_LIT>""" <EOL> if keys or self . config . autoindex is True : <EOL> index_name = self . config . edge_index <EOL> return self . update_indexed_edge ( _id , data , index_name , keys = keys ) <EOL> path = build_path ( edge_path , _id , "<STR_LIT>" ) <EOL> params = self . _remove_null_values ( data ) <EOL> return self . request . put ( path , params ) <EOL> def delete_edge ( self , _id ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( edge_path , _id ) <EOL> params = None <EOL> return self . request . delete ( path , params ) <EOL> def outE ( self , _id , label = None , start = None , limit = None ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( '<STR_LIT>' ) <EOL> params = dict ( _id = _id , label = label , start = start , limit = limit ) <EOL> return self . gremlin ( script , params ) <EOL> def inE ( self , _id , label = None , start = None , limit = None ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( '<STR_LIT>' ) <EOL> params = dict ( _id = _id , label = label , start = start , limit = limit ) <EOL> return self . gremlin ( script , params ) <EOL> def bothE ( self , _id , label = None , start = None , limit = None ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( '<STR_LIT>' ) <EOL> params = dict ( _id = _id , label = label , start = start , limit = limit ) <EOL> return self . gremlin ( script , params ) <EOL> def outV ( self , _id , label = None , start = None , limit = None ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( '<STR_LIT>' ) <EOL> params = dict ( _id = _id , label = label , start = start , limit = limit ) <EOL> return self . gremlin ( script , params ) <EOL> def inV ( self , _id , label = None , start = None , limit = None ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( '<STR_LIT>' ) <EOL> params = dict ( _id = _id , label = label , start = start , limit = limit ) <EOL> return self . gremlin ( script , params ) <EOL> def bothV ( self , _id , label = None , start = None , limit = None ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( '<STR_LIT>' ) <EOL> params = dict ( _id = _id , label = label , start = start , limit = limit ) <EOL> return self . gremlin ( script , params ) <EOL> def create_vertex_index ( self , index_name , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> default_config = { '<STR_LIT:type>' : "<STR_LIT>" , '<STR_LIT>' : "<STR_LIT>" } <EOL> index_config = kwds . pop ( "<STR_LIT>" , default_config ) <EOL> path = build_path ( index_path , vertex_path ) <EOL> params = dict ( name = index_name , config = index_config ) <EOL> resp = self . request . post ( path , params ) <EOL> resp . _set_index_name ( index_name ) <EOL> return resp <EOL> def get_vertex_indices ( self ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( index_path , vertex_path ) <EOL> params = None <EOL> return self . request . get ( path , params ) <EOL> def get_vertex_index ( self , index_name ) : <EOL> """<STR_LIT>""" <EOL> resp = self . get_vertex_indices ( ) <EOL> resp . results = self . _get_index_results ( index_name , resp ) <EOL> if resp . results : <EOL> resp . _set_index_name ( index_name ) <EOL> return resp <EOL> def get_or_create_vertex_index ( self , index_name , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> return self . create_vertex_index ( index_name , * args , ** kwds ) <EOL> def delete_vertex_index ( self , index_name ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( index_path , vertex_path , index_name ) <EOL> params = None <EOL> return self . request . delete ( path , params ) <EOL> def create_edge_index ( self , index_name , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> default_config = { '<STR_LIT:type>' : "<STR_LIT>" , '<STR_LIT>' : "<STR_LIT>" } <EOL> index_config = kwds . pop ( "<STR_LIT>" , default_config ) <EOL> path = build_path ( index_path , edge_path ) <EOL> params = dict ( name = index_name , config = index_config ) <EOL> resp = self . request . post ( path , params ) <EOL> resp . _set_index_name ( index_name ) <EOL> return resp <EOL> def get_edge_indices ( self ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( index_path , edge_path ) <EOL> params = None <EOL> return self . request . get ( path , params ) <EOL> def get_edge_index ( self , index_name ) : <EOL> """<STR_LIT>""" <EOL> resp = self . get_edge_indices ( ) <EOL> resp . results = self . _get_index_results ( index_name , resp ) <EOL> if resp . results : <EOL> resp . _set_index_name ( index_name ) <EOL> return resp <EOL> def get_or_create_edge_index ( self , index_name , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> return self . create_edge_index ( index_name , * args , ** kwds ) <EOL> def delete_edge_index ( self , index_name ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( index_path , edge_path , index_name ) <EOL> params = None <EOL> return self . request . delete ( path , params ) <EOL> def put_vertex ( self , index_name , key , value , _id ) : <EOL> """<STR_LIT>""" <EOL> uri = "<STR_LIT>" % ( self . config . root_uri , vertex_path , _id ) <EOL> path = build_path ( index_path , vertex_path , index_name ) <EOL> params = dict ( key = key , value = value , uri = uri ) <EOL> return self . request . post ( path , params ) <EOL> def lookup_vertex ( self , index_name , key , value ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( index_path , vertex_path , index_name , key , value ) <EOL> params = None <EOL> return self . request . get ( path , params ) <EOL> def create_unique_vertex ( self , index_name , key , value , data = None ) : <EOL> """<STR_LIT>""" <EOL> data = { } if data is None else data <EOL> data = self . _remove_null_values ( data ) <EOL> path = ( build_path ( index_path , vertex_path , index_name ) + <EOL> '<STR_LIT>' ) <EOL> params = { '<STR_LIT:key>' : key , '<STR_LIT:value>' : value , '<STR_LIT>' : data } <EOL> return self . request . post ( path , params ) <EOL> def query_vertex ( self , index_name , query ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( index_path , vertex_path , index_name ) <EOL> params = dict ( query = query ) <EOL> return self . request . get ( path , params ) <EOL> def remove_vertex ( self , index_name , _id , key = None , value = None ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( index_path , vertex_path , index_name , key , value , _id ) <EOL> params = None <EOL> return self . request . delete ( path , params ) <EOL> def put_edge ( self , index_name , key , value , _id ) : <EOL> """<STR_LIT>""" <EOL> uri = "<STR_LIT>" % ( self . config . root_uri , edge_path , _id ) <EOL> path = build_path ( index_path , edge_path , index_name ) <EOL> params = dict ( key = key , value = value , uri = uri ) <EOL> return self . request . post ( path , params ) <EOL> def lookup_edge ( self , index_name , key , value ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( index_path , edge_path , index_name , key , value ) <EOL> params = None <EOL> return self . request . get ( path , params ) <EOL> def query_edge ( self , index_name , query ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( index_path , edge_path , index_name ) <EOL> params = dict ( query = query ) <EOL> return self . request . get ( path , params ) <EOL> def remove_edge ( self , index_name , _id , key = None , value = None ) : <EOL> """<STR_LIT>""" <EOL> path = build_path ( index_path , edge_path , index_name , key , value , _id ) <EOL> params = None <EOL> return self . request . delete ( path , params ) <EOL> def create_indexed_vertex ( self , data , index_name , keys = None ) : <EOL> """<STR_LIT>""" <EOL> data = self . _remove_null_values ( data ) <EOL> params = dict ( data = data , index_name = index_name , keys = keys ) <EOL> script = self . scripts . get ( "<STR_LIT>" ) <EOL> return self . gremlin ( script , params ) <EOL> def update_indexed_vertex ( self , _id , data , index_name , keys = None ) : <EOL> """<STR_LIT>""" <EOL> data = self . _remove_null_values ( data ) <EOL> params = dict ( _id = _id , data = data , index_name = index_name , keys = keys ) <EOL> script = self . scripts . get ( "<STR_LIT>" ) <EOL> return self . gremlin ( script , params ) <EOL> def create_indexed_edge ( self , outV , label , inV , data , index_name , keys = None ) : <EOL> """<STR_LIT>""" <EOL> data = self . _remove_null_values ( data ) <EOL> edge_params = dict ( outV = outV , label = label , inV = inV , label_var = self . config . label_var ) <EOL> params = dict ( data = data , index_name = index_name , keys = keys ) <EOL> params . update ( edge_params ) <EOL> script = self . scripts . get ( "<STR_LIT>" ) <EOL> return self . gremlin ( script , params ) <EOL> def update_indexed_edge ( self , _id , data , index_name , keys = None ) : <EOL> """<STR_LIT>""" <EOL> data = self . _remove_null_values ( data ) <EOL> params = dict ( _id = _id , data = data , index_name = index_name , keys = keys ) <EOL> script = self . scripts . get ( "<STR_LIT>" ) <EOL> return self . gremlin ( script , params ) <EOL> def set_metadata ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( "<STR_LIT>" ) <EOL> params = dict ( key = key , value = value ) <EOL> return self . gremlin ( script , params ) <EOL> def get_metadata ( self , key , default_value = None ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( "<STR_LIT>" ) <EOL> params = dict ( key = key , default_value = default_value ) <EOL> return self . gremlin ( script , params ) <EOL> def remove_metadata ( self , key ) : <EOL> """<STR_LIT>""" <EOL> script = self . scripts . get ( "<STR_LIT>" ) <EOL> params = dict ( key = key ) <EOL> return self . gremlin ( script , params ) <EOL> def _remove_null_values ( self , data ) : <EOL> """<STR_LIT>""" <EOL> data = data or { } <EOL> clean_data = [ ( k , data [ k ] ) for k in data if data [ k ] is not None ] <EOL> return dict ( clean_data ) <EOL> def _get_index_results ( self , index_name , resp ) : <EOL> """<STR_LIT>""" <EOL> if resp . content and index_name in resp . content : <EOL> result = resp . content [ index_name ] <EOL> return Neo4jResult ( result , self . config ) <EOL> def _placeholder ( self , _id ) : <EOL> pattern = "<STR_LIT>" <EOL> match = re . search ( pattern , str ( _id ) ) <EOL> if match : <EOL> placeholder = match . group ( ) <EOL> return placeholder <EOL> def _build_vertex_path ( self , _id , * args ) : <EOL> placeholder = self . _placeholder ( _id ) <EOL> if placeholder : <EOL> segments = [ placeholder ] <EOL> else : <EOL> segments = [ vertex_path , _id ] <EOL> segments = segments + list ( args ) <EOL> return build_path ( * segments ) <EOL> def _build_vertex_uri ( self , _id , * args ) : <EOL> placeholder = self . _placeholder ( _id ) <EOL> if placeholder : <EOL> return placeholder <EOL> root_uri = self . config . root_uri . rstrip ( "<STR_LIT:/>" ) <EOL> segments = [ vertex_path , _id ] + list ( args ) <EOL> path = build_path ( * segments ) <EOL> uri = "<STR_LIT>" % ( root_uri , path ) <EOL> return uri <EOL> def _build_edge_path ( self , _id ) : <EOL> return self . _placeholder ( _id ) or build_path ( edge_path , _id ) <EOL> def _build_edge_uri ( self , _id ) : <EOL> pass </s>
