<s> import utils <EOL> from manager_rest import storage_manager <EOL> from constants import ENTITY_TYPES <EOL> def get_entity_context ( plan , deployment_id , entity_type , entity_id ) : <EOL> entity_context_by_type = { <EOL> ENTITY_TYPES . NODE : NodeContext , <EOL> ENTITY_TYPES . RELATIONSHIP : RelationshipContext , <EOL> ENTITY_TYPES . PROPERTY : PropertyContext , <EOL> ENTITY_TYPES . OPERATION : _operation_context <EOL> } <EOL> context = entity_context_by_type [ entity_type ] <EOL> return context ( plan , deployment_id , * utils . get_entity_keys ( entity_id ) ) <EOL> def _operation_context ( plan , deployment_id , * entity_keys ) : <EOL> if entity_keys [ <NUM_LIT:2> ] == utils . pluralize ( ENTITY_TYPES . RELATIONSHIP ) : <EOL> entity_context = RelationshipInterfaceOperationContext <EOL> else : <EOL> entity_context = NodeInterfaceOperationContext <EOL> return entity_context ( plan , deployment_id , * entity_keys ) <EOL> class EntityContextBase ( object ) : <EOL> NODES = utils . pluralize ( ENTITY_TYPES . NODE ) <EOL> RELATIONSHIPS = utils . pluralize ( ENTITY_TYPES . RELATIONSHIP ) <EOL> OPERATIONS = utils . pluralize ( ENTITY_TYPES . OPERATION ) <EOL> PROPERTIES = utils . pluralize ( ENTITY_TYPES . PROPERTY ) <EOL> PLUGINS = '<STR_LIT>' <EOL> def __init__ ( self , plan , deployment_id , entity_type , node_id ) : <EOL> self . sm = storage_manager . get_storage_manager ( ) <EOL> self . _deployment_id = deployment_id <EOL> self . _entity_type = entity_type <EOL> self . _node_id = node_id <EOL> self . _plan = plan <EOL> self . _raw_node = utils . get_raw_node ( self . blueprint , self . _node_id ) <EOL> @ property <EOL> def blueprint ( self ) : <EOL> return self . _plan <EOL> @ property <EOL> def entity_id ( self ) : <EOL> raise NotImplementedError <EOL> @ property <EOL> def entity_type ( self ) : <EOL> return self . _entity_type <EOL> @ property <EOL> def storage_node ( self ) : <EOL> return self . sm . get_node ( self . _deployment_id , self . _node_id ) or { } <EOL> @ property <EOL> def storage_node_instance ( self ) : <EOL> old_node_instances = self . sm . get_node_instance ( <EOL> filters = { '<STR_LIT>' : self . _deployment_id , <EOL> '<STR_LIT>' : self . _node_id } <EOL> ) <EOL> return old_node_instances [ <NUM_LIT:0> ] if old_node_instances else { } <EOL> @ property <EOL> def raw_node ( self ) : <EOL> return self . _raw_node <EOL> @ property <EOL> def raw_node_id ( self ) : <EOL> return self . raw_node [ '<STR_LIT:id>' ] <EOL> @ property <EOL> def storage_node_id ( self ) : <EOL> return self . storage_node . id <EOL> @ property <EOL> def storage_entity_value ( self ) : <EOL> raise NotImplementedError <EOL> @ property <EOL> def raw_entity_value ( self ) : <EOL> raise NotImplementedError <EOL> @ property <EOL> def deployment_id ( self ) : <EOL> return self . _deployment_id <EOL> class NodeContext ( EntityContextBase ) : <EOL> def __init__ ( self , <EOL> plan , <EOL> deployment_id , <EOL> nodes_key , <EOL> node_id , <EOL> * modification_breadcrumbs ) : <EOL> super ( NodeContext , self ) . __init__ ( plan , <EOL> deployment_id , <EOL> ENTITY_TYPES . NODE , <EOL> node_id ) <EOL> entity_keys = [ nodes_key , node_id ] <EOL> entity_keys . extend ( modification_breadcrumbs ) <EOL> self . _entity_id = '<STR_LIT::>' . join ( entity_keys ) <EOL> @ property <EOL> def raw_entity_value ( self ) : <EOL> return self . raw_node <EOL> @ property <EOL> def storage_entity_value ( self ) : <EOL> return self . storage_node <EOL> @ property <EOL> def entity_id ( self ) : <EOL> return self . _entity_id <EOL> class RelationshipContext ( EntityContextBase ) : <EOL> def __init__ ( self , <EOL> plan , <EOL> deployment_id , <EOL> nodes_key , <EOL> node_id , <EOL> relationships_key , <EOL> relationship_index , <EOL> * modification_breadcrumbs ) : <EOL> super ( RelationshipContext , self ) . __init__ ( plan , <EOL> deployment_id , <EOL> ENTITY_TYPES . RELATIONSHIP , <EOL> node_id ) <EOL> self . _relationship_index = utils . parse_index ( relationship_index ) <EOL> self . _modification_breadcrumbs = modification_breadcrumbs <EOL> self . _raw_target_node = utils . get_raw_node ( self . blueprint , <EOL> self . target_id ) <EOL> entity_keys = [ nodes_key , node_id , relationships_key , <EOL> relationship_index ] <EOL> entity_keys . extend ( modification_breadcrumbs ) <EOL> self . _entity_id = '<STR_LIT::>' . join ( entity_keys ) <EOL> @ property <EOL> def target_id ( self ) : <EOL> return self . raw_entity_value . get ( '<STR_LIT>' ) or self . storage_entity_value . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def storage_target_node ( self ) : <EOL> return self . sm . get_node ( self . _deployment_id , self . target_id ) or { } <EOL> @ property <EOL> def raw_target_node ( self ) : <EOL> return self . _raw_target_node <EOL> @ property <EOL> def raw_entity_value ( self ) : <EOL> if len ( self . raw_node [ '<STR_LIT>' ] ) > self . relationship_index : <EOL> return self . raw_node [ '<STR_LIT>' ] [ self . relationship_index ] <EOL> else : <EOL> return { } <EOL> @ property <EOL> def storage_entity_value ( self ) : <EOL> if len ( self . storage_node . relationships ) > self . relationship_index : <EOL> return self . storage_node . relationships [ self . relationship_index ] <EOL> else : <EOL> return { } <EOL> @ property <EOL> def relationship_index ( self ) : <EOL> return self . _relationship_index <EOL> @ property <EOL> def modification_breadcrumbs ( self ) : <EOL> return self . _modification_breadcrumbs <EOL> @ property <EOL> def entity_id ( self ) : <EOL> return self . _entity_id <EOL> class PropertyContext ( EntityContextBase ) : <EOL> def __init__ ( self , <EOL> plan , <EOL> deployment_id , <EOL> nodes_key , <EOL> node_id , <EOL> properties_key , <EOL> property_id , <EOL> * modification_breadcrumbs ) : <EOL> super ( PropertyContext , self ) . __init__ ( plan , <EOL> deployment_id , <EOL> ENTITY_TYPES . PROPERTY , <EOL> node_id ) <EOL> self . _property_id = property_id <EOL> self . _modification_breadcrumbs = modification_breadcrumbs <EOL> entity_keys = [ nodes_key , node_id , properties_key , property_id ] <EOL> entity_keys . extend ( modification_breadcrumbs ) <EOL> self . _entity_id = '<STR_LIT::>' . join ( entity_keys ) <EOL> @ property <EOL> def raw_entity_value ( self ) : <EOL> return utils . traverse_object ( self . raw_entity , <EOL> self . _modification_breadcrumbs ) <EOL> @ property <EOL> def storage_entity_value ( self ) : <EOL> return utils . traverse_object ( self . storage_entity , <EOL> self . _modification_breadcrumbs ) <EOL> @ property <EOL> def raw_entity ( self ) : <EOL> return self . raw_node [ '<STR_LIT>' ] [ self . property_id ] <EOL> @ property <EOL> def storage_entity ( self ) : <EOL> return self . storage_node . properties [ self . property_id ] <EOL> @ property <EOL> def property_id ( self ) : <EOL> return self . _property_id <EOL> @ property <EOL> def entity_id ( self ) : <EOL> return self . _entity_id <EOL> @ property <EOL> def modification_breadcrumbs ( self ) : <EOL> return self . _modification_breadcrumbs <EOL> class NodeInterfaceOperationContext ( EntityContextBase ) : <EOL> def __init__ ( self , <EOL> plan , <EOL> deployment_id , <EOL> nodes_key , <EOL> node_id , <EOL> operations_key , <EOL> operation_id , <EOL> * modification_breadcrumbs ) : <EOL> super ( NodeInterfaceOperationContext , self ) . __init__ ( <EOL> plan , <EOL> deployment_id , <EOL> ENTITY_TYPES . OPERATION , <EOL> node_id ) <EOL> self . _operation_id = operation_id <EOL> self . _modification_breadcrumbs = modification_breadcrumbs <EOL> entity_keys = [ nodes_key , node_id , operation_id ] <EOL> entity_keys . extend ( modification_breadcrumbs ) <EOL> self . _entity_id = '<STR_LIT::>' . join ( entity_keys ) <EOL> @ property <EOL> def raw_entity_value ( self ) : <EOL> return utils . traverse_object ( self . raw_entity , <EOL> self . modification_breadcrumbs ) <EOL> @ property <EOL> def storage_entity_value ( self ) : <EOL> return utils . traverse_object ( self . storage_entity , <EOL> self . _modification_breadcrumbs ) <EOL> @ property <EOL> def raw_entity ( self ) : <EOL> return self . raw_node [ self . OPERATIONS ] [ self . operation_id ] <EOL> @ property <EOL> def storage_entity ( self ) : <EOL> return self . storage_node . operations [ self . operation_id ] <EOL> @ property <EOL> def modification_breadcrumbs ( self ) : <EOL> return self . _modification_breadcrumbs <EOL> @ property <EOL> def operation_id ( self ) : <EOL> return self . _operation_id <EOL> @ property <EOL> def entity_id ( self ) : <EOL> return self . _entity_id <EOL> class RelationshipInterfaceOperationContext ( EntityContextBase ) : <EOL> def __init__ ( self , <EOL> plan , <EOL> deployment_id , <EOL> nodes_key , <EOL> node_id , <EOL> relatonships_key , <EOL> relationship_index , <EOL> operations_key , <EOL> operation_id , <EOL> * modification_breadcrumbs ) : <EOL> super ( RelationshipInterfaceOperationContext , self ) . __init__ ( <EOL> plan , <EOL> deployment_id , <EOL> ENTITY_TYPES . OPERATION , <EOL> node_id ) <EOL> self . _relationships_index = utils . parse_index ( relationship_index ) <EOL> self . _operations_key = operations_key <EOL> self . _operation_id = operation_id <EOL> self . _modification_breadcrumbs = modification_breadcrumbs <EOL> entity_keys = [ nodes_key , node_id , relatonships_key , <EOL> relationship_index , operation_id ] <EOL> entity_keys . extend ( modification_breadcrumbs ) <EOL> self . _entity_id = '<STR_LIT::>' . join ( entity_keys ) <EOL> @ property <EOL> def operations_key ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _operations_key <EOL> @ property <EOL> def raw_operations ( self ) : <EOL> return self . raw_relationship [ self . operations_key ] <EOL> @ property <EOL> def storage_operations ( self ) : <EOL> return self . storage_relationship [ self . operations_key ] <EOL> @ property <EOL> def relationship_index ( self ) : <EOL> return self . _relationships_index <EOL> @ property <EOL> def raw_relationship ( self ) : <EOL> return self . raw_node [ self . RELATIONSHIPS ] [ self . _relationships_index ] <EOL> @ property <EOL> def storage_relationship ( self ) : <EOL> return self . storage_node . relationships [ self . _relationships_index ] <EOL> @ property <EOL> def modification_breadcrumbs ( self ) : <EOL> return self . _modification_breadcrumbs <EOL> @ property <EOL> def raw_entity ( self ) : <EOL> return self . raw_operations . get ( self . operation_id ) <EOL> @ property <EOL> def storage_entity ( self ) : <EOL> return self . storage_operations . get ( self . operation_id ) <EOL> @ property <EOL> def raw_entity_value ( self ) : <EOL> return utils . traverse_object ( self . raw_entity , <EOL> self . modification_breadcrumbs ) <EOL> @ property <EOL> def storage_entity_value ( self ) : <EOL> return utils . traverse_object ( self . storage_entity , <EOL> self . modification_breadcrumbs ) <EOL> @ property <EOL> def operation_id ( self ) : <EOL> return self . _operation_id <EOL> @ property <EOL> def entity_id ( self ) : <EOL> return self . _entity_id </s>
