<s> import json <EOL> import select <EOL> import socket <EOL> import time <EOL> class OVSDBClient ( object ) : <EOL> class ConnectionError ( Exception ) : <EOL> def __init__ ( self , hostname , port , errno , message ) : <EOL> message = "<STR_LIT>" % ( hostname , port , errno , message ) <EOL> super ( self . __class__ , self ) . __init__ ( message ) <EOL> class ReceiveTimeoutError ( Exception ) : <EOL> def __init__ ( self , timeout , buff ) : <EOL> if not buff : <EOL> buff = "<STR_LIT>" <EOL> message = "<STR_LIT>" "<STR_LIT>" % ( timeout , buff ) <EOL> super ( self . __class__ , self ) . __init__ ( message ) <EOL> class RequestTimeoutError ( Exception ) : <EOL> def __init__ ( self , formatted_json , timeout , buff ) : <EOL> if not buff : <EOL> buff = "<STR_LIT>" <EOL> message = "<STR_LIT>" "<STR_LIT>" % ( timeout , formatted_json , buff ) <EOL> super ( self . __class__ , self ) . __init__ ( message ) <EOL> def __init__ ( self , hostname = None , port = None , request_timeout = <NUM_LIT:5> ) : <EOL> self . _socket = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> try : <EOL> self . _socket . connect ( ( hostname , port ) ) <EOL> except socket . error as ( errno , message ) : <EOL> raise OVSDBClient . ConnectionError ( hostname , port , errno , message ) <EOL> self . _request_timeout = request_timeout <EOL> @ property <EOL> def buffer ( self , max_chunk_size = <NUM_LIT> ) : <EOL> try : <EOL> ready_to_read , ready_to_write , in_error = select . select ( [ self . _socket ] , [ ] , [ ] , <NUM_LIT:0> ) <EOL> if self . _socket in ready_to_read : <EOL> self . _buffer += self . _socket . recv ( max_chunk_size ) <EOL> else : <EOL> self . _buffer += "<STR_LIT>" <EOL> except socket . error : <EOL> self . _buffer += "<STR_LIT>" <EOL> return self . _buffer <EOL> def receive ( self ) : <EOL> self . _flush_buffer ( ) <EOL> message = "<STR_LIT>" <EOL> start = time . time ( ) <EOL> while len ( message ) == <NUM_LIT:0> or message . count ( "<STR_LIT:}>" ) < message . count ( "<STR_LIT:{>" ) : <EOL> message = self . buffer <EOL> duration = time . time ( ) - start <EOL> if duration > self . _request_timeout : <EOL> raise OVSDBClient . ReceiveTimeoutError ( duration , message ) <EOL> return json . loads ( message ) <EOL> def request ( self , dict ) : <EOL> message = json . dumps ( dict ) <EOL> self . _flush_buffer ( ) <EOL> self . _socket . send ( message ) <EOL> response = "<STR_LIT>" <EOL> start = time . time ( ) <EOL> while len ( response ) == <NUM_LIT:0> or response . count ( "<STR_LIT:}>" ) < response . count ( "<STR_LIT:{>" ) : <EOL> response = self . buffer <EOL> duration = time . time ( ) - start <EOL> if duration > self . _request_timeout : <EOL> raise OVSDBClient . RequestTimeoutError ( <EOL> message , duration , response ) <EOL> return json . loads ( response ) <EOL> def send ( self , dict ) : <EOL> self . _flush_buffer ( ) <EOL> self . _socket . send ( json . dumps ( dict ) ) <EOL> def _flush_buffer ( self ) : <EOL> self . _buffer = "<STR_LIT>" </s>
