<s> import logging <EOL> from goblin import connection <EOL> from goblin . constants import EDGE_TRAVERSAL <EOL> from goblin . _compat import ( <EOL> array_types , integer_types , float_types , string_types , add_metaclass ) <EOL> from goblin . exceptions import ( <EOL> ElementDefinitionException , GoblinQueryError , ValidationError ) <EOL> from goblin . gremlin import GremlinMethod <EOL> from . element import Element , ElementMetaClass , edge_types <EOL> from . query import V <EOL> logger = logging . getLogger ( __name__ ) <EOL> class EdgeMetaClass ( ElementMetaClass ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( mcs , name , bases , body ) : <EOL> body [ '<STR_LIT>' ] = body . pop ( '<STR_LIT>' , None ) <EOL> klass = super ( EdgeMetaClass , mcs ) . __new__ ( mcs , name , bases , body ) <EOL> if not klass . __abstract__ : <EOL> label = klass . get_label ( ) <EOL> if label in edge_types and str ( edge_types [ label ] ) != str ( klass ) : <EOL> logger . debug ( <EOL> ElementDefinitionException ( <EOL> """<STR_LIT>""" % ( label , mcs , name , bases , body ) ) ) <EOL> else : <EOL> edge_types [ label ] = klass <EOL> return klass <EOL> @ add_metaclass ( EdgeMetaClass ) <EOL> class Edge ( Element ) : <EOL> """<STR_LIT>""" <EOL> __abstract__ = True <EOL> __exclusive__ = False <EOL> _label = None <EOL> gremlin_path = '<STR_LIT>' <EOL> _save_edge = GremlinMethod ( ) <EOL> _delete_edge = GremlinMethod ( ) <EOL> _get_edges_between = GremlinMethod ( classmethod = True ) <EOL> _find_edge_by_value = GremlinMethod ( classmethod = True ) <EOL> FACTORY_CLASS = None <EOL> def __init__ ( self , outV , inV , ** values ) : <EOL> """<STR_LIT>""" <EOL> self . _outV = outV <EOL> self . _inV = inV <EOL> super ( Edge , self ) . __init__ ( ** values ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> self . __class__ . __name__ , self . __class__ . get_label ( ) , <EOL> getattr ( self , '<STR_LIT>' , None ) , getattr ( self , '<STR_LIT>' , { } ) , <EOL> getattr ( self , '<STR_LIT>' , { } ) ) <EOL> def __getstate__ ( self ) : <EOL> state = { u'<STR_LIT>' : self . id , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : str ( self . outV ( ) . id ) , <EOL> u'<STR_LIT>' : str ( self . inV ( ) . id ) , <EOL> u'<STR_LIT>' : self . get_label ( ) , <EOL> u'<STR_LIT>' : self . as_save_params ( ) } <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> data = self . translate_db_fields ( state ) <EOL> self . __init__ ( state [ '<STR_LIT>' ] , state [ '<STR_LIT>' ] , ** data ) <EOL> return self <EOL> @ classmethod <EOL> def find_by_value ( cls , field , value , as_dict = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> _field = cls . get_property_by_name ( field ) <EOL> _label = cls . get_label ( ) <EOL> value_type = False <EOL> if isinstance ( value , integer_types + float_types ) : <EOL> value_type = True <EOL> future = connection . get_future ( kwargs ) <EOL> future_results = cls . _find_edge_by_value ( <EOL> value_type = value_type , <EOL> elabel = _label , <EOL> field = _field , <EOL> val = value <EOL> ) <EOL> def by_value_handler ( data ) : <EOL> if data is None : <EOL> data = [ ] <EOL> if as_dict : <EOL> data = { v . _id : v for v in data } <EOL> return data <EOL> def on_find_by_value ( f ) : <EOL> try : <EOL> stream = f . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> stream . add_handler ( by_value_handler ) <EOL> future . set_result ( stream ) <EOL> future_results . add_done_callback ( on_find_by_value ) <EOL> return future <EOL> @ classmethod <EOL> def all ( cls , ids = None , as_dict = False , * args , ** kwargs ) : <EOL> return super ( Edge , cls ) . all ( <EOL> EDGE_TRAVERSAL , ids = ids , as_dict = as_dict , * args , ** kwargs ) <EOL> @ classmethod <EOL> def get_label ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . _type_name ( cls . _label ) <EOL> @ classmethod <EOL> def get_between ( cls , outV , inV , page_num = None , per_page = None ) : <EOL> """<STR_LIT>""" <EOL> return cls . _get_edges_between ( out_v = outV , <EOL> in_v = inV , <EOL> elabel = cls . get_label ( ) , <EOL> page_num = page_num , <EOL> per_page = per_page ) <EOL> def validate ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _id is None : <EOL> if self . _inV is None : <EOL> raise ValidationError ( <EOL> '<STR_LIT>' ) <EOL> if self . _outV is None : <EOL> raise ValidationError ( <EOL> '<STR_LIT>' ) <EOL> super ( Edge , self ) . validate ( ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( Edge , self ) . save ( ) <EOL> future = connection . get_future ( kwargs ) <EOL> future_result = self . _save_edge ( self . _outV , <EOL> self . _inV , <EOL> self . get_label ( ) , <EOL> self . as_save_params ( ) , <EOL> exclusive = self . __exclusive__ , <EOL> ** kwargs ) <EOL> def on_read ( f2 ) : <EOL> try : <EOL> result = f2 . result ( ) [ <NUM_LIT:0> ] <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> future . set_result ( result ) <EOL> def on_save ( f ) : <EOL> try : <EOL> stream = f . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> future_read = stream . read ( ) <EOL> future_read . add_done_callback ( on_read ) <EOL> future_result . add_done_callback ( on_save ) <EOL> return future <EOL> def _reload_values ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> reloaded_values = { } <EOL> future = connection . get_future ( kwargs ) <EOL> future_result = connection . execute_query ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : self . _id } , ** kwargs ) <EOL> def on_read ( f2 ) : <EOL> try : <EOL> result = f2 . result ( ) <EOL> result = result . data [ <NUM_LIT:0> ] <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> if result : <EOL> reloaded_values [ '<STR_LIT:id>' ] = result [ '<STR_LIT:id>' ] <EOL> for name , value in result . get ( '<STR_LIT>' , { } ) . items ( ) : <EOL> reloaded_values [ name ] = value <EOL> if result [ '<STR_LIT:id>' ] : <EOL> setattr ( self , '<STR_LIT:id>' , result [ '<STR_LIT:id>' ] ) <EOL> future . set_result ( reloaded_values ) <EOL> else : <EOL> future . set_result ( { } ) <EOL> def on_reload ( f ) : <EOL> try : <EOL> stream = f . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> future_read = stream . read ( ) <EOL> future_read . add_done_callback ( on_read ) <EOL> future_result . add_done_callback ( on_reload ) <EOL> return future <EOL> @ classmethod <EOL> def create ( cls , outV , inV , label = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> edge = super ( Edge , cls ) . create ( outV , inV , * args , ** kwargs ) <EOL> return edge <EOL> def delete ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . __abstract__ : <EOL> raise GoblinQueryError ( '<STR_LIT>' ) <EOL> if self . _id is None : <EOL> return self <EOL> future = connection . get_future ( kwargs ) <EOL> future_result = self . _delete_edge ( ) <EOL> def on_read ( f2 ) : <EOL> try : <EOL> result = f2 . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> future . set_result ( result ) <EOL> def on_delete ( f ) : <EOL> try : <EOL> stream = f . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> future_read = stream . read ( ) <EOL> future_read . add_done_callback ( on_read ) <EOL> future_result . add_done_callback ( on_delete ) <EOL> return future <EOL> def _simple_traversal ( self , operation , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> deserialize = kwargs . pop ( '<STR_LIT>' , True ) <EOL> def edge_traversal_handler ( data ) : <EOL> if deserialize : <EOL> data = [ Element . deserialize ( d ) for d in data ] <EOL> return data <EOL> future_results = connection . execute_query ( <EOL> '<STR_LIT>' % operation , { '<STR_LIT:id>' : self . id } , <EOL> handler = edge_traversal_handler , ** kwargs ) <EOL> return future_results <EOL> def inV ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from goblin . models . vertex import Vertex <EOL> future = connection . get_future ( kwargs ) <EOL> if self . _inV is None : <EOL> future_results = self . _simple_traversal ( '<STR_LIT>' , ** kwargs ) <EOL> def on_traversal ( f ) : <EOL> try : <EOL> result = f . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> self . _inV = result [ <NUM_LIT:0> ] <EOL> if isinstance ( self . _inV , string_types + integer_types ) : <EOL> future_results = V ( self . _inV ) . get ( ** kwargs ) <EOL> def on_get ( f2 ) : <EOL> try : <EOL> result = f2 . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> self . _inV = result <EOL> future_results . add_done_callback ( on_get ) <EOL> future . set_result ( self . _inV ) <EOL> future_results . add_done_callback ( on_traversal ) <EOL> elif isinstance ( self . _inV , string_types + integer_types ) : <EOL> future_results = V ( self . _inV ) . get ( ** kwargs ) <EOL> def on_get ( f2 ) : <EOL> try : <EOL> result = f2 . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> self . _inV = result <EOL> future . set_result ( self . _inV ) <EOL> future_results . add_done_callback ( on_get ) <EOL> else : <EOL> future . set_result ( self . _inV ) <EOL> return future <EOL> def outV ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from goblin . models . vertex import Vertex <EOL> future = connection . get_future ( kwargs ) <EOL> if self . _inV is None : <EOL> future_results = self . _simple_traversal ( '<STR_LIT>' , ** kwargs ) <EOL> def on_traversal ( f ) : <EOL> try : <EOL> result = f . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> self . _outV = result [ <NUM_LIT:0> ] <EOL> if isinstance ( self . _outV , string_types + integer_types ) : <EOL> future_results = V ( self . _outV ) . get ( ** kwargs ) <EOL> def on_get ( f2 ) : <EOL> try : <EOL> result = f2 . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> self . _outV = result <EOL> future_results . add_done_callback ( on_get ) <EOL> future . set_result ( self . _outV ) <EOL> future_results . add_done_callback ( on_traversal ) <EOL> elif isinstance ( self . _outV , string_types + integer_types ) : <EOL> future_results = V ( self . _outV ) . get ( ** kwargs ) <EOL> def on_get ( f2 ) : <EOL> try : <EOL> result = f2 . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> self . _outV = result <EOL> future . set_result ( self . _outV ) <EOL> future_results . add_done_callback ( on_get ) <EOL> else : <EOL> future . set_result ( self . _outV ) <EOL> return future </s>
