<s> __author__ = '<STR_LIT>' <EOL> import struct <EOL> import sys <EOL> from . . exceptions import PyOrientBadMethodCallException , PyOrientCommandException , PyOrientNullRecordException <EOL> from . . otypes import OrientRecord , OrientRecordLink <EOL> from . . hexdump import hexdump <EOL> from . . constants import BOOLEAN , BYTE , BYTES , CHAR , FIELD_BOOLEAN , FIELD_BYTE , FIELD_INT , FIELD_RECORD , FIELD_SHORT , FIELD_STRING , FIELD_TYPE_LINK , INT , LINK , LONG , RECORD , SHORT , STRING , STRINGS <EOL> from . . utils import is_debug_active <EOL> from . . orient import OrientSocket <EOL> from . . serializations import OrientSerialization <EOL> class BaseMessage ( object ) : <EOL> def __init__ ( self , sock = OrientSocket ) : <EOL> """<STR_LIT>""" <EOL> sock . get_connection ( ) <EOL> self . _orientSocket = sock <EOL> self . _protocol = self . _orientSocket . protocol <EOL> self . _session_id = self . _orientSocket . session_id <EOL> self . _auth_token = self . _orientSocket . auth_token <EOL> self . _request_token = False <EOL> self . _header = [ ] <EOL> """<STR_LIT>""" <EOL> self . _body = [ ] <EOL> """<STR_LIT>""" <EOL> self . _fields_definition = [ ] <EOL> """<STR_LIT>""" <EOL> self . _command = chr ( <NUM_LIT:0> ) <EOL> self . _db_opened = self . _orientSocket . db_opened <EOL> self . _connected = self . _orientSocket . connected <EOL> self . _serializer = None <EOL> self . _output_buffer = b'<STR_LIT>' <EOL> self . _input_buffer = b'<STR_LIT>' <EOL> self . _callback = None <EOL> self . _push_callback = None <EOL> self . _need_token = True <EOL> global in_transaction <EOL> in_transaction = False <EOL> def get_serializer ( self ) : <EOL> """<STR_LIT>""" <EOL> return OrientSerialization . get_impl ( self . _orientSocket . serialization_type ) <EOL> def get_orient_socket_instance ( self ) : <EOL> return self . _orientSocket <EOL> def is_connected ( self ) : <EOL> return self . _connected is True <EOL> def database_opened ( self ) : <EOL> return self . _db_opened <EOL> def get_cluster_map ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _cluster_map <EOL> def set_session_token ( self , token = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if token != '<STR_LIT>' and token is not None : <EOL> if type ( token ) is bool : <EOL> self . _request_token = token <EOL> elif type ( token ) is str or type ( token ) is bytes : <EOL> self . _request_token = True <EOL> self . _auth_token = token <EOL> self . _db_opened = True <EOL> self . _connected = True <EOL> self . _update_socket_token ( ) <EOL> return self <EOL> def get_session_token ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _auth_token <EOL> def _update_socket_id ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _orientSocket . session_id = self . _session_id <EOL> return self <EOL> def _update_socket_token ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _orientSocket . auth_token = self . _auth_token <EOL> return self <EOL> def _reset_fields_definition ( self ) : <EOL> self . _fields_definition = [ ] <EOL> def prepare ( self , * args ) : <EOL> self . _fields_definition . insert ( <NUM_LIT:1> , ( FIELD_INT , self . _session_id ) ) <EOL> """<STR_LIT>""" <EOL> if self . _need_token and self . _request_token is True : <EOL> self . _fields_definition . insert ( <EOL> <NUM_LIT:2> , ( FIELD_STRING , self . _auth_token ) <EOL> ) <EOL> self . _output_buffer = b'<STR_LIT>' . join ( <EOL> self . _encode_field ( x ) for x in self . _fields_definition <EOL> ) <EOL> return self <EOL> def get_protocol ( self ) : <EOL> if self . _protocol < <NUM_LIT:0> : <EOL> self . _protocol = self . _orientSocket . protocol <EOL> return self . _protocol <EOL> def _decode_header ( self ) : <EOL> self . _header = [ self . _decode_field ( FIELD_BYTE ) , <EOL> self . _decode_field ( FIELD_INT ) ] <EOL> from . connection import ConnectMessage <EOL> from . database import DbOpenMessage <EOL> """<STR_LIT>""" <EOL> if not isinstance ( self , ( ConnectMessage , DbOpenMessage ) ) and self . _request_token is True : <EOL> token_refresh = self . _decode_field ( FIELD_STRING ) <EOL> if token_refresh != b'<STR_LIT>' : <EOL> self . _auth_token = token_refresh <EOL> self . _update_socket_token ( ) <EOL> if self . _header [ <NUM_LIT:0> ] == <NUM_LIT:1> : <EOL> exception_class = b'<STR_LIT>' <EOL> exception_message = b'<STR_LIT>' <EOL> more = self . _decode_field ( FIELD_BOOLEAN ) <EOL> while more : <EOL> exception_class += self . _decode_field ( FIELD_STRING ) <EOL> exception_message += self . _decode_field ( FIELD_STRING ) <EOL> more = self . _decode_field ( FIELD_BOOLEAN ) <EOL> if self . get_protocol ( ) > <NUM_LIT> : <EOL> serialized_exception = self . _decode_field ( FIELD_STRING ) <EOL> del serialized_exception <EOL> raise PyOrientCommandException ( <EOL> exception_class . decode ( '<STR_LIT:utf8>' ) , <EOL> [ exception_message . decode ( '<STR_LIT:utf8>' ) ] <EOL> ) <EOL> elif self . _header [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> push_command_id = self . _decode_field ( FIELD_BYTE ) <EOL> push_message = self . _decode_field ( FIELD_STRING ) <EOL> _ , payload = self . get_serializer ( ) . decode ( push_message ) <EOL> if self . _push_callback : <EOL> self . _push_callback ( push_command_id , payload ) <EOL> end_flag = self . _decode_field ( FIELD_BYTE ) <EOL> while end_flag == <NUM_LIT:3> : <EOL> self . _decode_field ( FIELD_INT ) <EOL> self . _decode_field ( FIELD_BYTE ) <EOL> self . _cluster_map . set_hi_availability_list ( <EOL> self . _decode_field ( FIELD_STRING ) <EOL> ) <EOL> """<STR_LIT>""" <EOL> end_flag = self . _decode_field ( FIELD_BYTE ) <EOL> self . _header [ <NUM_LIT:1> ] = self . _decode_field ( FIELD_INT ) <EOL> pass <EOL> def _decode_body ( self ) : <EOL> for field in self . _fields_definition : <EOL> self . _body . append ( self . _decode_field ( field ) ) <EOL> self . _reset_fields_definition ( ) <EOL> return self <EOL> def _decode_all ( self ) : <EOL> self . _decode_header ( ) <EOL> self . _decode_body ( ) <EOL> def fetch_response ( self , * _continue ) : <EOL> """<STR_LIT>""" <EOL> if len ( _continue ) is not <NUM_LIT:0> : <EOL> self . _body = [ ] <EOL> self . _decode_body ( ) <EOL> self . dump_streams ( ) <EOL> elif len ( self . _body ) is <NUM_LIT:0> : <EOL> self . _decode_all ( ) <EOL> self . dump_streams ( ) <EOL> return self . _body <EOL> def dump_streams ( self ) : <EOL> if is_debug_active ( ) : <EOL> if len ( self . _output_buffer ) : <EOL> print ( "<STR_LIT>" ) <EOL> hexdump ( self . _output_buffer ) <EOL> if len ( self . _input_buffer ) : <EOL> print ( "<STR_LIT>" ) <EOL> hexdump ( self . _input_buffer ) <EOL> def _append ( self , field ) : <EOL> """<STR_LIT>""" <EOL> self . _fields_definition . append ( field ) <EOL> return self <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" + hexdump ( self . _output_buffer , '<STR_LIT>' ) + "<STR_LIT>" + hexdump ( self . _input_buffer , '<STR_LIT>' ) <EOL> def send ( self ) : <EOL> if self . _orientSocket . in_transaction is False : <EOL> self . _orientSocket . write ( self . _output_buffer ) <EOL> self . _reset_fields_definition ( ) <EOL> if is_debug_active ( ) : <EOL> self . dump_streams ( ) <EOL> self . _output_buffer = b"<STR_LIT>" <EOL> return self <EOL> def close ( self ) : <EOL> self . _orientSocket . close ( ) <EOL> @ staticmethod <EOL> def _encode_field ( field ) : <EOL> t , v = field <EOL> _content = None <EOL> if t [ '<STR_LIT:type>' ] == INT : <EOL> _content = struct . pack ( "<STR_LIT>" , v ) <EOL> elif t [ '<STR_LIT:type>' ] == SHORT : <EOL> _content = struct . pack ( "<STR_LIT>" , v ) <EOL> elif t [ '<STR_LIT:type>' ] == LONG : <EOL> _content = struct . pack ( "<STR_LIT>" , v ) <EOL> elif t [ '<STR_LIT:type>' ] == BOOLEAN : <EOL> if sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> : <EOL> _content = chr ( <NUM_LIT:1> ) if v else chr ( <NUM_LIT:0> ) <EOL> else : <EOL> _content = bytes ( [ <NUM_LIT:1> ] ) if v else bytes ( [ <NUM_LIT:0> ] ) <EOL> elif t [ '<STR_LIT:type>' ] == BYTE : <EOL> if sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> : <EOL> _content = v <EOL> else : <EOL> _content = bytes ( [ ord ( v ) ] ) <EOL> elif t [ '<STR_LIT:type>' ] == BYTES : <EOL> _content = struct . pack ( "<STR_LIT>" , len ( v ) ) + v <EOL> elif t [ '<STR_LIT:type>' ] == STRING : <EOL> if sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> if isinstance ( v , str ) : <EOL> v = v . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> if isinstance ( v , unicode ) : <EOL> v = v . encode ( '<STR_LIT:utf-8>' ) <EOL> _content = struct . pack ( "<STR_LIT>" , len ( v ) ) + v <EOL> elif t [ '<STR_LIT:type>' ] == STRINGS : <EOL> _content = b'<STR_LIT>' <EOL> for s in v : <EOL> if sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> if isinstance ( s , str ) : <EOL> s = s . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> if isinstance ( s , unicode ) : <EOL> s = s . encode ( '<STR_LIT:utf-8>' ) <EOL> _content += struct . pack ( "<STR_LIT>" , len ( s ) ) + s <EOL> return _content <EOL> def _decode_field ( self , _type ) : <EOL> _value = b"<STR_LIT>" <EOL> if _type [ '<STR_LIT>' ] is not None : <EOL> _value = self . _orientSocket . read ( _type [ '<STR_LIT>' ] ) <EOL> if _type [ '<STR_LIT:type>' ] == STRING or _type [ '<STR_LIT:type>' ] == BYTES : <EOL> _len = struct . unpack ( '<STR_LIT>' , _value ) [ <NUM_LIT:0> ] <EOL> if _len == - <NUM_LIT:1> or _len == <NUM_LIT:0> : <EOL> _decoded_string = b'<STR_LIT>' <EOL> else : <EOL> _decoded_string = self . _orientSocket . read ( _len ) <EOL> self . _input_buffer += _value <EOL> self . _input_buffer += _decoded_string <EOL> return _decoded_string <EOL> elif _type [ '<STR_LIT:type>' ] == RECORD : <EOL> record_type = self . _decode_field ( _type [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> rid = "<STR_LIT:#>" + str ( self . _decode_field ( _type [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) ) <EOL> rid += "<STR_LIT::>" + str ( self . _decode_field ( _type [ '<STR_LIT>' ] [ <NUM_LIT:2> ] ) ) <EOL> version = self . _decode_field ( _type [ '<STR_LIT>' ] [ <NUM_LIT:3> ] ) <EOL> content = self . _decode_field ( _type [ '<STR_LIT>' ] [ <NUM_LIT:4> ] ) <EOL> return { '<STR_LIT>' : rid , '<STR_LIT>' : record_type , <EOL> '<STR_LIT:content>' : content , '<STR_LIT:version>' : version } <EOL> elif _type [ '<STR_LIT:type>' ] == LINK : <EOL> rid = "<STR_LIT:#>" + str ( self . _decode_field ( _type [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) ) <EOL> rid += "<STR_LIT::>" + str ( self . _decode_field ( _type [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) ) <EOL> return rid <EOL> else : <EOL> self . _input_buffer += _value <EOL> if _type [ '<STR_LIT:type>' ] == BOOLEAN : <EOL> return ord ( _value ) == <NUM_LIT:1> <EOL> elif _type [ '<STR_LIT:type>' ] == BYTE : <EOL> return ord ( _value ) <EOL> elif _type [ '<STR_LIT:type>' ] == CHAR : <EOL> return _value <EOL> elif _type [ '<STR_LIT:type>' ] == SHORT : <EOL> return struct . unpack ( '<STR_LIT>' , _value ) [ <NUM_LIT:0> ] <EOL> elif _type [ '<STR_LIT:type>' ] == INT : <EOL> return struct . unpack ( '<STR_LIT>' , _value ) [ <NUM_LIT:0> ] <EOL> elif _type [ '<STR_LIT:type>' ] == LONG : <EOL> return struct . unpack ( '<STR_LIT>' , _value ) [ <NUM_LIT:0> ] <EOL> def _read_async_records ( self ) : <EOL> """<STR_LIT>""" <EOL> _status = self . _decode_field ( FIELD_BYTE ) <EOL> while _status != <NUM_LIT:0> : <EOL> try : <EOL> if not hasattr ( self . _callback , '<STR_LIT>' ) : <EOL> raise AttributeError ( ) <EOL> _record = self . _read_record ( ) <EOL> if _status == <NUM_LIT:1> : <EOL> self . _callback ( _record ) <EOL> elif _status == <NUM_LIT:2> : <EOL> self . _callback ( _record ) <EOL> except AttributeError : <EOL> raise PyOrientBadMethodCallException ( <EOL> str ( self . _callback ) + "<STR_LIT>" , [ ] ) <EOL> finally : <EOL> _status = self . _decode_field ( FIELD_BYTE ) <EOL> def _read_record ( self ) : <EOL> """<STR_LIT>""" <EOL> marker = self . _decode_field ( FIELD_SHORT ) <EOL> if marker is - <NUM_LIT:2> : <EOL> raise PyOrientNullRecordException ( '<STR_LIT>' , [ ] ) <EOL> elif marker is - <NUM_LIT:3> : <EOL> res = OrientRecordLink ( self . _decode_field ( FIELD_TYPE_LINK ) ) <EOL> else : <EOL> __res = self . _decode_field ( FIELD_RECORD ) <EOL> class_name , data = self . get_serializer ( ) . decode ( __res [ '<STR_LIT:content>' ] . rstrip ( ) ) <EOL> res = OrientRecord ( <EOL> dict ( <EOL> __o_storage = data , <EOL> __o_class = class_name , <EOL> __version = __res [ '<STR_LIT:version>' ] , <EOL> __rid = __res [ '<STR_LIT>' ] <EOL> ) <EOL> ) <EOL> self . dump_streams ( ) <EOL> self . _output_buffer = b'<STR_LIT>' <EOL> self . _input_buffer = b'<STR_LIT>' <EOL> return res </s>
