<s> __author__ = '<STR_LIT>' <EOL> import httplib <EOL> import platform <EOL> import re <EOL> import socket <EOL> import sys <EOL> import time <EOL> try : <EOL> import ssl <EOL> __has_ssl__ = True <EOL> except Exception : <EOL> __has_ssl__ = False <EOL> ssl = None <EOL> import scalyr_agent . json_lib as json_lib <EOL> import scalyr_agent . scalyr_logging as scalyr_logging <EOL> import scalyr_agent . util as scalyr_util <EOL> from scalyr_agent . connection import ConnectionFactory <EOL> from cStringIO import StringIO <EOL> log = scalyr_logging . getLogger ( __name__ ) <EOL> class ScalyrClientSession ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server , api_key , agent_version , quiet = False , request_deadline = <NUM_LIT> , ca_file = None , use_requests_lib = False ) : <EOL> """<STR_LIT>""" <EOL> if not quiet : <EOL> log . info ( '<STR_LIT>' % server ) <EOL> self . __full_address = server <EOL> parsed_server = re . match ( '<STR_LIT>' , server . lower ( ) ) <EOL> if parsed_server is None : <EOL> raise Exception ( '<STR_LIT>' % server ) <EOL> self . __connection = None <EOL> self . __use_requests = use_requests_lib <EOL> self . __api_key = api_key <EOL> self . __session_id = scalyr_util . create_unique_id ( ) <EOL> self . __quiet = quiet <EOL> if not quiet : <EOL> log . info ( '<STR_LIT>' % ( self . __session_id , scalyr_util . get_pid_tid ( ) ) ) <EOL> self . __last_success = None <EOL> self . __agent_version = agent_version <EOL> self . __last_connection_close = None <EOL> self . __standard_headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : ScalyrClientSession . __get_user_agent ( agent_version ) <EOL> } <EOL> self . __request_deadline = request_deadline <EOL> self . total_requests_sent = <NUM_LIT:0> <EOL> self . total_requests_failed = <NUM_LIT:0> <EOL> self . total_request_bytes_sent = <NUM_LIT:0> <EOL> self . total_response_bytes_received = <NUM_LIT:0> <EOL> self . total_request_latency_secs = <NUM_LIT:0> <EOL> self . total_connections_created = <NUM_LIT:0> <EOL> self . __ca_file = ca_file <EOL> def ping ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . send ( self . add_events_request ( ) ) [ <NUM_LIT:0> ] <EOL> def __send_request ( self , request_path , body = None , body_func = None , is_post = True , block_on_response = True ) : <EOL> """<STR_LIT>""" <EOL> current_time = time . time ( ) <EOL> if self . __last_connection_close is not None and current_time - self . __last_connection_close < <NUM_LIT:30> : <EOL> return self . __wrap_response_if_necessary ( '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT>' , block_on_response ) <EOL> self . total_requests_sent += <NUM_LIT:1> <EOL> was_sent = False <EOL> try : <EOL> try : <EOL> if self . __connection is None : <EOL> self . __connection = ConnectionFactory . connection ( self . __full_address , self . __request_deadline , <EOL> self . __ca_file , self . __standard_headers , <EOL> self . __use_requests , quiet = self . __quiet ) <EOL> self . total_connections_created += <NUM_LIT:1> <EOL> except Exception , e : <EOL> return self . __wrap_response_if_necessary ( '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT>' , block_on_response ) <EOL> if is_post : <EOL> if body is None : <EOL> body_str = body_func ( ) <EOL> else : <EOL> body_str = body <EOL> else : <EOL> body_str = "<STR_LIT>" <EOL> self . total_request_bytes_sent += len ( body_str ) + len ( request_path ) <EOL> try : <EOL> if is_post : <EOL> log . log ( scalyr_logging . DEBUG_LEVEL_5 , '<STR_LIT>' , request_path , body_str ) <EOL> self . __connection . post ( request_path , body = body_str ) <EOL> else : <EOL> log . log ( scalyr_logging . DEBUG_LEVEL_5 , '<STR_LIT>' , request_path ) <EOL> self . __connection . get ( request_path ) <EOL> except Exception , error : <EOL> if hasattr ( error , '<STR_LIT>' ) and error . errno is not None : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , self . __full_address , error . errno , str ( error ) , <EOL> error_code = '<STR_LIT>' ) <EOL> else : <EOL> log . exception ( '<STR_LIT>' , <EOL> error_code = '<STR_LIT>' ) <EOL> return self . __wrap_response_if_necessary ( '<STR_LIT>' , len ( body_str ) , '<STR_LIT>' , block_on_response ) <EOL> was_sent = True <EOL> def receive_response ( ) : <EOL> return self . __receive_response ( body_str , current_time ) <EOL> if not block_on_response : <EOL> return receive_response <EOL> else : <EOL> return receive_response ( ) <EOL> finally : <EOL> if not was_sent : <EOL> self . total_request_latency_secs += ( time . time ( ) - current_time ) <EOL> self . total_requests_failed += <NUM_LIT:1> <EOL> self . close ( current_time = current_time ) <EOL> def __receive_response ( self , body_str , send_time ) : <EOL> """<STR_LIT>""" <EOL> response = '<STR_LIT>' <EOL> was_success = False <EOL> bytes_received = <NUM_LIT:0> <EOL> try : <EOL> try : <EOL> status_code = self . __connection . status_code ( ) <EOL> response = self . __connection . response ( ) <EOL> bytes_received = len ( response ) <EOL> except Exception , error : <EOL> if hasattr ( error , '<STR_LIT>' ) and error . errno is not None : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , self . __full_address , error . errno , str ( error ) , <EOL> error_code = '<STR_LIT>' ) <EOL> else : <EOL> log . exception ( '<STR_LIT>' , <EOL> error_code = '<STR_LIT>' ) <EOL> return '<STR_LIT>' , len ( body_str ) , response <EOL> log . log ( scalyr_logging . DEBUG_LEVEL_5 , '<STR_LIT>' , response ) <EOL> if status_code == <NUM_LIT> : <EOL> log . error ( '<STR_LIT>' , <EOL> error_code = '<STR_LIT>' ) <EOL> return '<STR_LIT>' , len ( body_str ) , response <EOL> if len ( response ) == <NUM_LIT:0> : <EOL> log . error ( '<STR_LIT>' , <EOL> error_code = '<STR_LIT>' ) <EOL> return '<STR_LIT>' , len ( body_str ) , response <EOL> try : <EOL> response_as_json = json_lib . parse ( response ) <EOL> except Exception : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , scalyr_util . remove_newlines_and_truncate ( response , <NUM_LIT:1000> ) , <EOL> error_code = '<STR_LIT>' ) <EOL> return '<STR_LIT>' , len ( body_str ) , response <EOL> self . __last_success = send_time <EOL> if '<STR_LIT:status>' in response_as_json : <EOL> status = response_as_json [ '<STR_LIT:status>' ] <EOL> if status == '<STR_LIT:success>' : <EOL> was_success = True <EOL> elif status == '<STR_LIT>' : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , self . __full_address , <EOL> error_code = '<STR_LIT>' ) <EOL> else : <EOL> log . error ( '<STR_LIT>' , <EOL> self . __full_address , status , error_code = '<STR_LIT>' ) <EOL> return status , len ( body_str ) , response <EOL> else : <EOL> log . error ( '<STR_LIT>' , <EOL> scalyr_util . remove_newlines_and_truncate ( response , <NUM_LIT:1000> ) , error_code = '<STR_LIT>' ) <EOL> return '<STR_LIT>' , len ( body_str ) , response <EOL> finally : <EOL> self . total_request_latency_secs += ( time . time ( ) - send_time ) <EOL> if not was_success : <EOL> self . total_requests_failed += <NUM_LIT:1> <EOL> self . close ( current_time = send_time ) <EOL> self . total_response_bytes_received += bytes_received <EOL> def __wrap_response_if_necessary ( self , status_message , bytes_sent , response , block_on_response ) : <EOL> """<STR_LIT>""" <EOL> if block_on_response : <EOL> return status_message , bytes_sent , response <EOL> def wrap ( ) : <EOL> return status_message , bytes_sent , response <EOL> return wrap <EOL> def send ( self , add_events_request , block_on_response = True ) : <EOL> """<STR_LIT>""" <EOL> current_time = time . time ( ) <EOL> def generate_body ( ) : <EOL> add_events_request . set_client_time ( current_time ) <EOL> return add_events_request . get_payload ( ) <EOL> return self . __send_request ( '<STR_LIT>' , body_func = generate_body , block_on_response = block_on_response ) <EOL> def close ( self , current_time = None ) : <EOL> """<STR_LIT>""" <EOL> if self . __connection is not None : <EOL> if current_time is None : <EOL> current_time = time . time ( ) <EOL> self . __connection . close ( ) <EOL> self . __connection = None <EOL> self . __last_connection_close = current_time <EOL> def add_events_request ( self , session_info = None , max_size = <NUM_LIT:1> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> body = { <EOL> '<STR_LIT>' : self . __api_key , <EOL> '<STR_LIT>' : self . __session_id , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> if session_info is not None : <EOL> body [ '<STR_LIT>' ] = session_info <EOL> return AddEventsRequest ( body , max_size = max_size ) <EOL> @ staticmethod <EOL> def __get_user_agent ( agent_version ) : <EOL> """<STR_LIT>""" <EOL> python_version = sys . version_info <EOL> if len ( python_version ) >= <NUM_LIT:5> : <EOL> python_version_str = '<STR_LIT>' % ( python_version [ <NUM_LIT:0> ] , python_version [ <NUM_LIT:1> ] , python_version [ <NUM_LIT:2> ] ) <EOL> else : <EOL> python_version_str = '<STR_LIT>' <EOL> platform_value = None <EOL> try : <EOL> distribution = platform . dist ( ) <EOL> if len ( distribution [ <NUM_LIT:0> ] ) > <NUM_LIT:0> : <EOL> platform_value = '<STR_LIT>' % ( distribution [ <NUM_LIT:0> ] , distribution [ <NUM_LIT:1> ] ) <EOL> except Exception : <EOL> platform_value = None <EOL> if platform_value is None : <EOL> try : <EOL> mac_ver = platform . mac_ver ( ) [ <NUM_LIT:0> ] <EOL> if len ( mac_ver ) > <NUM_LIT:0> : <EOL> platform_value = '<STR_LIT>' % mac_ver <EOL> except Exception : <EOL> platform_value = None <EOL> if platform_value is None : <EOL> platform_value = platform . platform ( terse = <NUM_LIT:1> ) <EOL> if __has_ssl__ : <EOL> ssl_str = '<STR_LIT>' <EOL> else : <EOL> ssl_str = '<STR_LIT>' <EOL> return '<STR_LIT>' % ( platform_value , python_version_str , agent_version , ssl_str ) <EOL> def perform_agent_version_check ( self , track = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> url_path = "<STR_LIT>" % ( self . __agent_version , track ) <EOL> return self . __send_request ( url_path , is_post = False ) <EOL> class EventSequencer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . __previous_sequence_id = None <EOL> self . __previous_sequence_number = None <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __previous_sequence_id = None <EOL> self . __previous_sequence_number = None <EOL> def get_memento ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . __previous_sequence_id , self . __previous_sequence_number ) <EOL> def restore_from_memento ( self , memento ) : <EOL> """<STR_LIT>""" <EOL> self . __previous_sequence_id = memento [ <NUM_LIT:0> ] <EOL> self . __previous_sequence_number = memento [ <NUM_LIT:1> ] <EOL> def add_sequence_fields ( self , event , sequence_id , sequence_number ) : <EOL> """<STR_LIT>""" <EOL> if sequence_id is not None and sequence_number is not None : <EOL> if sequence_id != self . __previous_sequence_id : <EOL> event . set_sequence_id ( sequence_id ) <EOL> self . __previous_sequence_id = sequence_id <EOL> self . __previous_sequence_number = None <EOL> if self . __previous_sequence_number is None : <EOL> event . set_sequence_number ( sequence_number ) <EOL> else : <EOL> event . set_sequence_number_delta ( sequence_number - self . __previous_sequence_number ) <EOL> self . __previous_sequence_number = sequence_number <EOL> class AddEventsRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , base_body , max_size = <NUM_LIT:1> * <NUM_LIT> * <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> assert len ( base_body ) > <NUM_LIT:0> , "<STR_LIT>" <EOL> assert not '<STR_LIT>' in base_body , "<STR_LIT>" <EOL> assert not '<STR_LIT>' in base_body , "<STR_LIT>" <EOL> string_buffer = StringIO ( ) <EOL> json_lib . serialize ( base_body , output = string_buffer , use_fast_encoding = True ) <EOL> _rewind_past_close_curly ( string_buffer ) <EOL> string_buffer . write ( '<STR_LIT>' ) <EOL> self . __post_fix_buffer = PostFixBuffer ( '<STR_LIT>' ) <EOL> self . __post_fix_buffer . set_client_timestamp ( time . time ( ) ) <EOL> self . __buffer = string_buffer <EOL> self . __max_size = max_size <EOL> self . __events_added = <NUM_LIT:0> <EOL> self . __body = None <EOL> self . __event_sequencer = EventSequencer ( ) <EOL> self . __timing_data = dict ( ) <EOL> @ property <EOL> def current_size ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __buffer is not None : <EOL> return self . __buffer . tell ( ) + self . __post_fix_buffer . length <EOL> else : <EOL> return len ( self . __body ) <EOL> def add_thread ( self , thread_id , thread_name ) : <EOL> """<STR_LIT>""" <EOL> available_size_for_post_fix = self . __max_size - self . __buffer . tell ( ) <EOL> return self . __post_fix_buffer . add_thread_entry ( thread_id , thread_name , <EOL> fail_if_buffer_exceeds = available_size_for_post_fix ) <EOL> def add_event ( self , event , timestamp = None , sequence_id = None , sequence_number = None ) : <EOL> """<STR_LIT>""" <EOL> start_pos = self . __buffer . tell ( ) <EOL> if self . __events_added > <NUM_LIT:0> : <EOL> self . __buffer . write ( '<STR_LIT:U+002C>' ) <EOL> if timestamp is None : <EOL> timestamp = self . __get_timestamp ( ) <EOL> memento = self . __event_sequencer . get_memento ( ) <EOL> self . __event_sequencer . add_sequence_fields ( event , sequence_id , sequence_number ) <EOL> event . set_timestamp ( timestamp ) <EOL> event . serialize ( self . __buffer ) <EOL> if self . current_size > self . __max_size : <EOL> self . __buffer . truncate ( start_pos ) <EOL> self . __event_sequencer . restore_from_memento ( memento ) <EOL> return False <EOL> self . __events_added += <NUM_LIT:1> <EOL> return True <EOL> @ property <EOL> def num_events ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __events_added <EOL> def set_client_time ( self , current_time ) : <EOL> """<STR_LIT>""" <EOL> original_postfix_length = self . __post_fix_buffer . length <EOL> self . __post_fix_buffer . set_client_timestamp ( current_time ) <EOL> if self . __body is not None : <EOL> rebuild_buffer = StringIO ( ) <EOL> rebuild_buffer . write ( self . __body ) <EOL> self . __body = None <EOL> rebuild_buffer . seek ( - <NUM_LIT:1> * original_postfix_length , <NUM_LIT:2> ) <EOL> rebuild_buffer . truncate ( ) <EOL> rebuild_buffer . write ( self . __post_fix_buffer . content ( ) ) <EOL> self . __body = rebuild_buffer . getvalue ( ) <EOL> rebuild_buffer . close ( ) <EOL> def get_payload ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __body is None : <EOL> self . __buffer . write ( self . __post_fix_buffer . content ( ) ) <EOL> self . __body = self . __buffer . getvalue ( ) <EOL> self . __buffer . close ( ) <EOL> self . __buffer = None <EOL> return self . __body <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __body = None <EOL> self . __buffer = None <EOL> def increment_timing_data ( self , ** key_values ) : <EOL> """<STR_LIT>""" <EOL> for key , value in key_values . iteritems ( ) : <EOL> if key in self . __timing_data : <EOL> amount = self . __timing_data [ key ] <EOL> else : <EOL> amount = <NUM_LIT:0.0> <EOL> amount += value <EOL> self . __timing_data [ key ] = amount <EOL> def get_timing_data ( self ) : <EOL> """<STR_LIT>""" <EOL> output_buffer = StringIO ( ) <EOL> first_time = True <EOL> for key , value in self . __timing_data . iteritems ( ) : <EOL> if not first_time : <EOL> output_buffer . write ( '<STR_LIT:U+0020>' ) <EOL> else : <EOL> first_time = False <EOL> output_buffer . write ( key ) <EOL> output_buffer . write ( '<STR_LIT:=>' ) <EOL> output_buffer . write ( str ( value ) ) <EOL> return output_buffer . getvalue ( ) <EOL> def __get_timestamp ( self ) : <EOL> """<STR_LIT>""" <EOL> global __last_time_stamp__ <EOL> base_timestamp = long ( time . time ( ) * <NUM_LIT> L ) <EOL> if __last_time_stamp__ is not None and base_timestamp <= __last_time_stamp__ : <EOL> base_timestamp = __last_time_stamp__ + <NUM_LIT:1> L <EOL> __last_time_stamp__ = base_timestamp <EOL> return base_timestamp <EOL> @ property <EOL> def total_events ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __events_added <EOL> def position ( self ) : <EOL> """<STR_LIT>""" <EOL> return AddEventsRequest . Position ( self . __events_added , self . __buffer . tell ( ) , self . __post_fix_buffer . position ) <EOL> def set_position ( self , position ) : <EOL> """<STR_LIT>""" <EOL> self . __events_added = position . events_added <EOL> self . __buffer . truncate ( position . buffer_size ) <EOL> self . __post_fix_buffer . set_position ( position . postfix_buffer_position ) <EOL> self . __event_sequencer . reset ( ) <EOL> class Position ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , events_added , buffer_size , postfix_buffer_position ) : <EOL> self . events_added = events_added <EOL> self . buffer_size = buffer_size <EOL> self . postfix_buffer_position = postfix_buffer_position <EOL> def _calculate_per_thread_extra_bytes ( ) : <EOL> """<STR_LIT>""" <EOL> sizes_by_entries = [ ] <EOL> threads = [ ] <EOL> test_string = '<STR_LIT:A>' <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> sizes_by_entries . append ( len ( json_lib . serialize ( threads ) ) ) <EOL> threads . append ( { '<STR_LIT:id>' : test_string , '<STR_LIT:name>' : test_string } ) <EOL> test_string_len = len ( json_lib . serialize ( test_string ) ) <EOL> result = [ ] <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:3> ) : <EOL> result . append ( sizes_by_entries [ i ] - sizes_by_entries [ i - <NUM_LIT:1> ] - <NUM_LIT:2> * test_string_len ) <EOL> return result <EOL> class PostFixBuffer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , format_string ) : <EOL> """<STR_LIT>""" <EOL> assert ( '<STR_LIT>' in format_string ) <EOL> assert ( '<STR_LIT>' in format_string ) <EOL> self . __threads = [ ] <EOL> self . __client_timestamp = <NUM_LIT:0> <EOL> self . __format = format_string <EOL> self . __current_size = len ( self . content ( ) ) <EOL> __per_thread_extra_bytes = _calculate_per_thread_extra_bytes ( ) <EOL> @ property <EOL> def length ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __current_size <EOL> def content ( self , cache_size = True ) : <EOL> """<STR_LIT>""" <EOL> result = self . __format . replace ( '<STR_LIT>' , str ( self . __client_timestamp ) ) <EOL> result = result . replace ( '<STR_LIT>' , json_lib . serialize ( self . __threads ) ) <EOL> if cache_size : <EOL> self . __current_size = len ( result ) <EOL> return result <EOL> def set_client_timestamp ( self , timestamp , fail_if_buffer_exceeds = None ) : <EOL> """<STR_LIT>""" <EOL> new_timestamp = int ( timestamp ) <EOL> size_difference = len ( str ( new_timestamp ) ) - len ( str ( self . __client_timestamp ) ) <EOL> if fail_if_buffer_exceeds is not None and self . __current_size + size_difference > fail_if_buffer_exceeds : <EOL> return False <EOL> self . __current_size += size_difference <EOL> self . __client_timestamp = new_timestamp <EOL> return True <EOL> def add_thread_entry ( self , thread_id , thread_name , fail_if_buffer_exceeds = None ) : <EOL> """<STR_LIT>""" <EOL> size_difference = len ( json_lib . serialize ( thread_name ) ) + len ( json_lib . serialize ( thread_id ) ) <EOL> num_threads = len ( self . __threads ) <EOL> if num_threads < <NUM_LIT:1> : <EOL> size_difference += PostFixBuffer . __per_thread_extra_bytes [ <NUM_LIT:0> ] <EOL> else : <EOL> size_difference += PostFixBuffer . __per_thread_extra_bytes [ <NUM_LIT:1> ] <EOL> if fail_if_buffer_exceeds is not None and self . __current_size + size_difference > fail_if_buffer_exceeds : <EOL> return False <EOL> self . __current_size += size_difference <EOL> self . __threads . append ( { '<STR_LIT:id>' : thread_id , '<STR_LIT:name>' : thread_name } ) <EOL> return True <EOL> @ property <EOL> def position ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . __current_size , self . __client_timestamp , len ( self . __threads ) ] <EOL> def set_position ( self , position ) : <EOL> """<STR_LIT>""" <EOL> self . __current_size = position [ <NUM_LIT:0> ] <EOL> self . __client_timestamp = position [ <NUM_LIT:1> ] <EOL> assert ( len ( self . __threads ) >= position [ <NUM_LIT:2> ] ) <EOL> if position [ <NUM_LIT:2> ] < len ( self . __threads ) : <EOL> self . __threads = self . __threads [ <NUM_LIT:0> : position [ <NUM_LIT:2> ] ] <EOL> class Event ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , thread_id = None , attrs = None , base = None ) : <EOL> """<STR_LIT>""" <EOL> self . __attrs = attrs <EOL> if ( attrs is not None or thread_id is not None ) and base is not None : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if base is not None : <EOL> self . __serialization_base = base . __serialization_base <EOL> self . __attrs = base . __attrs <EOL> else : <EOL> tmp_buffer = StringIO ( ) <EOL> tmp_buffer . write ( '<STR_LIT:{>' ) <EOL> if thread_id is not None : <EOL> tmp_buffer . write ( '<STR_LIT>' ) <EOL> json_lib . serialize ( thread_id , use_fast_encoding = True , output = tmp_buffer ) <EOL> tmp_buffer . write ( '<STR_LIT:U+002CU+0020>' ) <EOL> if attrs is not None : <EOL> tmp_buffer . write ( '<STR_LIT>' ) <EOL> json_lib . serialize ( attrs , use_fast_encoding = True , output = tmp_buffer ) <EOL> _rewind_past_close_curly ( tmp_buffer ) <EOL> tmp_buffer . write ( '<STR_LIT:U+002C>' ) <EOL> else : <EOL> tmp_buffer . write ( '<STR_LIT>' ) <EOL> tmp_buffer . write ( '<STR_LIT>' ) <EOL> self . __serialization_base = tmp_buffer . getvalue ( ) <EOL> self . __message = None <EOL> self . __timestamp = None <EOL> self . __sequence_id = None <EOL> self . __sequence_number = None <EOL> self . __sequence_number_delta = None <EOL> self . __sampling_rate = None <EOL> self . __has_non_optimal_fields = False <EOL> self . __num_optimal_fields = <NUM_LIT:0> <EOL> @ property <EOL> def attrs ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __attrs <EOL> def set_message ( self , message ) : <EOL> """<STR_LIT>""" <EOL> if self . __message is None and message is not None : <EOL> self . __num_optimal_fields += <NUM_LIT:1> <EOL> if message is unicode : <EOL> self . __message = message . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> self . __message = message <EOL> return self <EOL> @ property <EOL> def message ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __message <EOL> def set_timestamp ( self , timestamp ) : <EOL> """<STR_LIT>""" <EOL> if self . __timestamp is None and timestamp is not None : <EOL> self . __num_optimal_fields += <NUM_LIT:1> <EOL> self . __timestamp = '<STR_LIT>' % str ( timestamp ) <EOL> return self <EOL> @ property <EOL> def timestamp ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __timestamp is not None : <EOL> return long ( self . __timestamp [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> else : <EOL> return None <EOL> def set_sequence_id ( self , sequence_id ) : <EOL> """<STR_LIT>""" <EOL> self . __sequence_id = '<STR_LIT>' % str ( sequence_id ) <EOL> self . __has_non_optimal_fields = True <EOL> return self <EOL> @ property <EOL> def sequence_id ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __sequence_id is not None : <EOL> return self . __sequence_id [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> self . __has_non_optimal_fields = True <EOL> return None <EOL> def set_sequence_number ( self , sequence_number ) : <EOL> """<STR_LIT>""" <EOL> self . __has_non_optimal_fields = True <EOL> self . __sequence_number = str ( sequence_number ) <EOL> return self <EOL> @ property <EOL> def sequence_number ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __sequence_number is not None : <EOL> return long ( self . __sequence_number ) <EOL> else : <EOL> return None <EOL> def set_sequence_number_delta ( self , sequence_number_delta ) : <EOL> """<STR_LIT>""" <EOL> if self . __sequence_number_delta is None and sequence_number_delta is not None : <EOL> self . __num_optimal_fields += <NUM_LIT:1> <EOL> self . __sequence_number_delta = str ( sequence_number_delta ) <EOL> return self <EOL> @ property <EOL> def sequence_number_delta ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __sequence_number_delta is not None : <EOL> return long ( self . __sequence_number_delta ) <EOL> else : <EOL> return None <EOL> def set_sampling_rate ( self , rate ) : <EOL> """<STR_LIT>""" <EOL> self . __has_non_optimal_fields = True <EOL> self . __sampling_rate = str ( rate ) <EOL> return self <EOL> def serialize ( self , output_buffer ) : <EOL> """<STR_LIT>""" <EOL> output_buffer . write ( self . __serialization_base ) <EOL> json_lib . serialize_as_length_prefixed_string ( self . __message , output_buffer ) <EOL> if not self . __has_non_optimal_fields and self . __num_optimal_fields == <NUM_LIT:3> : <EOL> output_buffer . write ( '<STR_LIT:}>' ) <EOL> output_buffer . write ( '<STR_LIT>' ) <EOL> output_buffer . write ( self . __sequence_number_delta ) <EOL> output_buffer . write ( '<STR_LIT>' ) <EOL> output_buffer . write ( self . __timestamp ) <EOL> else : <EOL> self . __write_field_if_not_none ( '<STR_LIT>' , self . __sampling_rate , output_buffer ) <EOL> output_buffer . write ( '<STR_LIT:}>' ) <EOL> self . __write_field_if_not_none ( '<STR_LIT>' , self . __timestamp , output_buffer ) <EOL> self . __write_field_if_not_none ( '<STR_LIT>' , self . __sequence_id , output_buffer ) <EOL> self . __write_field_if_not_none ( '<STR_LIT>' , self . __sequence_number , output_buffer ) <EOL> self . __write_field_if_not_none ( '<STR_LIT>' , self . __sequence_number_delta , output_buffer ) <EOL> output_buffer . write ( '<STR_LIT:}>' ) <EOL> def __write_field_if_not_none ( self , field_name , field_value , output_buffer ) : <EOL> """<STR_LIT>""" <EOL> if field_value is not None : <EOL> output_buffer . write ( field_name ) <EOL> output_buffer . write ( field_value ) <EOL> def _rewind_past_close_curly ( output_buffer ) : <EOL> """<STR_LIT>""" <EOL> location = output_buffer . tell ( ) <EOL> while location > <NUM_LIT:0> : <EOL> location -= <NUM_LIT:1> <EOL> output_buffer . seek ( location ) <EOL> if output_buffer . read ( <NUM_LIT:1> ) == '<STR_LIT:}>' : <EOL> break <EOL> last_char = None <EOL> while location > <NUM_LIT:0> : <EOL> location -= <NUM_LIT:1> <EOL> output_buffer . seek ( location ) <EOL> last_char = output_buffer . read ( <NUM_LIT:1> ) <EOL> if not last_char . isspace ( ) : <EOL> break <EOL> if location > <NUM_LIT:0> and last_char == '<STR_LIT:U+002C>' : <EOL> location -= <NUM_LIT:1> <EOL> if location < <NUM_LIT:0> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> location += <NUM_LIT:1> <EOL> output_buffer . seek ( location ) <EOL> output_buffer . truncate ( ) <EOL> __last_time_stamp__ = None <EOL> class HTTPConnectionWithTimeout ( httplib . HTTPConnection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port , timeout ) : <EOL> self . __timeout = timeout <EOL> httplib . HTTPConnection . __init__ ( self , host , port ) <EOL> def connect ( self ) : <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> self . sock = socket . create_connection ( ( self . host , self . port ) , self . __timeout ) <EOL> else : <EOL> self . sock = create_connection_helper ( self . host , self . port , timeout = self . __timeout ) <EOL> if hasattr ( self , '<STR_LIT>' ) and self . _tunnel_host : <EOL> self . _tunnel ( ) <EOL> class HTTPSConnectionWithTimeoutAndVerification ( httplib . HTTPSConnection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port , timeout , ca_file , has_ssl ) : <EOL> """<STR_LIT>""" <EOL> if not has_ssl and ca_file is not None : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . __timeout = timeout <EOL> self . __ca_file = ca_file <EOL> self . __has_ssl = has_ssl <EOL> httplib . HTTPSConnection . __init__ ( self , host , port ) <EOL> def connect ( self ) : <EOL> if not self . __has_ssl : <EOL> old_timeout = None <EOL> try : <EOL> old_timeout = socket . getdefaulttimeout ( ) <EOL> socket . setdefaulttimeout ( self . __timeout ) <EOL> httplib . HTTPSConnection . connect ( self ) <EOL> return <EOL> finally : <EOL> socket . setdefaulttimeout ( old_timeout ) <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> self . sock = socket . create_connection ( ( self . host , self . port ) , self . __timeout ) <EOL> else : <EOL> self . sock = create_connection_helper ( self . host , self . port , timeout = self . __timeout ) <EOL> if hasattr ( self , '<STR_LIT>' ) and self . _tunnel_host : <EOL> self . _tunnel ( ) <EOL> if self . __ca_file is not None : <EOL> self . sock = ssl . wrap_socket ( self . sock , ca_certs = self . __ca_file , cert_reqs = ssl . CERT_REQUIRED ) <EOL> else : <EOL> self . sock = ssl . wrap_socket ( self . sock , cert_reqs = ssl . CERT_NONE ) <EOL> def create_connection_helper ( host , port , timeout = None , source_address = None ) : <EOL> """<STR_LIT>""" <EOL> err = None <EOL> for res in socket . getaddrinfo ( host , port , <NUM_LIT:0> , socket . SOCK_STREAM ) : <EOL> af , socktype , proto , canonname , sa = res <EOL> sock = None <EOL> try : <EOL> sock = socket . socket ( af , socktype , proto ) <EOL> if timeout is not None : <EOL> sock . settimeout ( timeout ) <EOL> if source_address is not None : <EOL> sock . bind ( source_address ) <EOL> sock . connect ( sa ) <EOL> return sock <EOL> except socket . error , _ : <EOL> err = _ <EOL> if sock is not None : <EOL> sock . close ( ) <EOL> if err is not None : <EOL> raise err <EOL> else : <EOL> raise socket . error ( "<STR_LIT>" ) </s>
