<s> import json <EOL> from py2neo . compat import ustr <EOL> from py2neo . database import Graph , Cursor , cypher_request <EOL> from py2neo . database . status import GraphError <EOL> from py2neo . packages . httpstream . packages . urimagic import percent_encode , URI <EOL> from py2neo . types import Path , Relatable , remote <EOL> from py2neo . util import raise_from <EOL> from . util import NodePointer , BatchError <EOL> def _create_query ( p , unique = False ) : <EOL> initial_match_clause = [ ] <EOL> path , values , params = [ ] , [ ] , { } <EOL> def append_node ( i , node ) : <EOL> remote_node = remote ( node ) <EOL> if node is None : <EOL> path . append ( "<STR_LIT>" . format ( i ) ) <EOL> values . append ( "<STR_LIT>" . format ( i ) ) <EOL> elif remote_node : <EOL> path . append ( "<STR_LIT>" . format ( i ) ) <EOL> initial_match_clause . append ( "<STR_LIT>" . format ( i ) ) <EOL> params [ "<STR_LIT>" . format ( i ) ] = remote_node . _id <EOL> values . append ( "<STR_LIT>" . format ( i ) ) <EOL> else : <EOL> path . append ( "<STR_LIT>" . format ( i ) ) <EOL> params [ "<STR_LIT>" . format ( i ) ] = dict ( node ) <EOL> values . append ( "<STR_LIT>" . format ( i ) ) <EOL> def append_relationship ( i , relationship ) : <EOL> if relationship : <EOL> path . append ( "<STR_LIT>" . format ( i , relationship . type ( ) ) ) <EOL> params [ "<STR_LIT>" . format ( i ) ] = dict ( relationship ) <EOL> values . append ( "<STR_LIT>" . format ( i ) ) <EOL> else : <EOL> path . append ( "<STR_LIT>" . format ( i , relationship . type ( ) ) ) <EOL> values . append ( "<STR_LIT>" . format ( i ) ) <EOL> nodes = p . nodes ( ) <EOL> append_node ( <NUM_LIT:0> , nodes [ <NUM_LIT:0> ] ) <EOL> for i , relationship in enumerate ( p . relationships ( ) ) : <EOL> append_relationship ( i , relationship ) <EOL> append_node ( i + <NUM_LIT:1> , nodes [ i + <NUM_LIT:1> ] ) <EOL> clauses = list ( initial_match_clause ) <EOL> if unique : <EOL> clauses . append ( "<STR_LIT>" . format ( "<STR_LIT>" . join ( path ) ) ) <EOL> else : <EOL> clauses . append ( "<STR_LIT>" . format ( "<STR_LIT>" . join ( path ) ) ) <EOL> clauses . append ( "<STR_LIT>" ) <EOL> query = "<STR_LIT:\n>" . join ( clauses ) <EOL> return query , params <EOL> class Target ( object ) : <EOL> """<STR_LIT>""" <EOL> entity = None <EOL> segments = None <EOL> def __init__ ( self , entity , * segments ) : <EOL> self . entity = entity <EOL> self . segments = segments <EOL> @ property <EOL> def uri_string ( self ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . entity , int ) : <EOL> uri_string = "<STR_LIT>" % self . entity <EOL> elif isinstance ( self . entity , NodePointer ) : <EOL> uri_string = "<STR_LIT>" % self . entity . address <EOL> else : <EOL> remote_entity = remote ( self . entity ) <EOL> if remote_entity : <EOL> uri_string = remote_entity . ref <EOL> else : <EOL> uri_string = ustr ( self . entity ) <EOL> if self . segments : <EOL> if not uri_string . endswith ( "<STR_LIT:/>" ) : <EOL> uri_string += "<STR_LIT:/>" <EOL> uri_string += "<STR_LIT:/>" . join ( map ( percent_encode , self . segments ) ) <EOL> return uri_string <EOL> class Job ( Relatable ) : <EOL> """<STR_LIT>""" <EOL> graph = None <EOL> raw_result = False <EOL> method = None <EOL> target = None <EOL> body = None <EOL> finished = False <EOL> def __init__ ( self , method , target , body = None ) : <EOL> self . method = method <EOL> self . target = target <EOL> self . body = body <EOL> def __repr__ ( self ) : <EOL> parts = [ self . method , self . target . uri_string ] <EOL> if self . body is not None : <EOL> parts . append ( json . dumps ( self . body , separators = "<STR_LIT>" ) ) <EOL> return "<STR_LIT:U+0020>" . join ( parts ) <EOL> def __eq__ ( self , other ) : <EOL> return self is other <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( id ( self ) ) <EOL> def __iter__ ( self ) : <EOL> yield "<STR_LIT>" , self . method <EOL> yield "<STR_LIT:to>" , self . target . uri_string <EOL> if self . body is not None : <EOL> yield "<STR_LIT:body>" , self . body <EOL> class JobResult ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def hydrate ( cls , data , batch ) : <EOL> graph = getattr ( batch , "<STR_LIT>" , None ) <EOL> job_id = data [ "<STR_LIT:id>" ] <EOL> uri = data [ "<STR_LIT>" ] <EOL> status_code = data . get ( "<STR_LIT:status>" ) <EOL> location = data . get ( "<STR_LIT:location>" ) <EOL> if graph is None or batch [ job_id ] . raw_result : <EOL> body = data . get ( "<STR_LIT:body>" ) <EOL> else : <EOL> body = None <EOL> try : <EOL> body = graph . _hydrate ( data . get ( "<STR_LIT:body>" ) ) <EOL> except GraphError as error : <EOL> message = "<STR_LIT>" % ( <EOL> job_id , error . __class__ . __name__ , ustr ( error ) ) <EOL> raise_from ( BatchError ( message , batch , job_id , status_code , uri , location ) , error ) <EOL> else : <EOL> if isinstance ( body , Cursor ) : <EOL> if body . forward ( ) : <EOL> record = body . current ( ) <EOL> width = len ( record ) <EOL> if width == <NUM_LIT:1> : <EOL> body = record [ <NUM_LIT:0> ] <EOL> else : <EOL> body = record <EOL> else : <EOL> body = None <EOL> return cls ( batch , job_id , uri , status_code , location , body ) <EOL> batch = None <EOL> job_id = None <EOL> uri = None <EOL> status_code = None <EOL> location = None <EOL> content = None <EOL> def __init__ ( self , batch , job_id , uri , status_code = None , location = None , content = None ) : <EOL> self . batch = batch <EOL> self . job_id = job_id <EOL> self . uri = URI ( uri ) <EOL> self . status_code = status_code or <NUM_LIT:200> <EOL> self . location = URI ( location ) <EOL> self . content = content <EOL> def __repr__ ( self ) : <EOL> parts = [ "<STR_LIT:{>" + ustr ( self . job_id ) + "<STR_LIT:}>" , ustr ( self . status_code ) ] <EOL> if self . content is not None : <EOL> parts . append ( repr ( self . content ) ) <EOL> return "<STR_LIT:U+0020>" . join ( parts ) <EOL> @ property <EOL> def graph ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . batch . graph <EOL> @ property <EOL> def job ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . batch [ self . job_id ] <EOL> class CypherJob ( Job ) : <EOL> target = Target ( "<STR_LIT>" ) <EOL> def __init__ ( self , statement , parameters = None ) : <EOL> Job . __init__ ( self , "<STR_LIT:POST>" , self . target , <EOL> { "<STR_LIT>" : [ cypher_request ( statement , parameters ) ] } ) <EOL> class PullPropertiesJob ( Job ) : <EOL> raw_result = True <EOL> def __init__ ( self , entity ) : <EOL> Job . __init__ ( self , "<STR_LIT:GET>" , Target ( entity , "<STR_LIT>" ) ) <EOL> class PullNodeLabelsJob ( Job ) : <EOL> raw_result = True <EOL> def __init__ ( self , node ) : <EOL> Job . __init__ ( self , "<STR_LIT:GET>" , Target ( node , "<STR_LIT>" ) ) <EOL> class PullRelationshipJob ( Job ) : <EOL> raw_result = True <EOL> def __init__ ( self , relationship ) : <EOL> Job . __init__ ( self , "<STR_LIT:GET>" , Target ( relationship ) ) <EOL> class PushPropertyJob ( Job ) : <EOL> def __init__ ( self , entity , key , value ) : <EOL> Job . __init__ ( self , "<STR_LIT>" , Target ( entity , "<STR_LIT>" , key ) , value ) <EOL> class PushPropertiesJob ( Job ) : <EOL> def __init__ ( self , entity , properties ) : <EOL> Job . __init__ ( self , "<STR_LIT>" , Target ( entity , "<STR_LIT>" ) , dict ( properties ) ) <EOL> class PushNodeLabelsJob ( Job ) : <EOL> def __init__ ( self , node , labels ) : <EOL> Job . __init__ ( self , "<STR_LIT>" , Target ( node , "<STR_LIT>" ) , set ( labels ) ) <EOL> class CreateNodeJob ( Job ) : <EOL> target = Target ( "<STR_LIT>" ) <EOL> def __init__ ( self , ** properties ) : <EOL> Job . __init__ ( self , "<STR_LIT:POST>" , self . target , properties ) <EOL> class CreateRelationshipJob ( Job ) : <EOL> def __init__ ( self , start_node , type , end_node , ** properties ) : <EOL> body = { "<STR_LIT:type>" : type , "<STR_LIT:to>" : Target ( end_node ) . uri_string } <EOL> if properties : <EOL> body [ "<STR_LIT:data>" ] = properties <EOL> Job . __init__ ( self , "<STR_LIT:POST>" , Target ( start_node , "<STR_LIT>" ) , body ) <EOL> class CreatePathJob ( CypherJob ) : <EOL> def __init__ ( self , * entities ) : <EOL> if isinstance ( entities [ <NUM_LIT:0> ] , Graph ) : <EOL> self . graph , entities = entities [ <NUM_LIT:0> ] , entities [ <NUM_LIT:1> : ] <EOL> path = Path ( * ( entity or { } for entity in entities ) ) <EOL> CypherJob . __init__ ( self , * _create_query ( path ) ) <EOL> class CreateUniquePathJob ( CypherJob ) : <EOL> def __init__ ( self , * entities ) : <EOL> if isinstance ( entities [ <NUM_LIT:0> ] , Graph ) : <EOL> self . graph , entities = entities [ <NUM_LIT:0> ] , entities [ <NUM_LIT:1> : ] <EOL> path = Path ( * ( entity or { } for entity in entities ) ) <EOL> CypherJob . __init__ ( self , * _create_query ( path , unique = True ) ) <EOL> class DeleteEntityJob ( Job ) : <EOL> def __init__ ( self , entity ) : <EOL> Job . __init__ ( self , "<STR_LIT>" , Target ( entity ) ) <EOL> class DeletePropertyJob ( Job ) : <EOL> def __init__ ( self , entity , key ) : <EOL> Job . __init__ ( self , "<STR_LIT>" , Target ( entity , "<STR_LIT>" , key ) ) <EOL> class DeletePropertiesJob ( Job ) : <EOL> def __init__ ( self , entity ) : <EOL> Job . __init__ ( self , "<STR_LIT>" , Target ( entity , "<STR_LIT>" ) ) <EOL> class AddNodeLabelsJob ( Job ) : <EOL> def __init__ ( self , node , * labels ) : <EOL> Job . __init__ ( self , "<STR_LIT:POST>" , Target ( node , "<STR_LIT>" ) , set ( labels ) ) <EOL> class RemoveNodeLabelJob ( Job ) : <EOL> def __init__ ( self , entity , label ) : <EOL> Job . __init__ ( self , "<STR_LIT>" , Target ( entity , "<STR_LIT>" , label ) ) </s>
