<s> from socket import _GLOBAL_DEFAULT_TIMEOUT <EOL> import time <EOL> from . . exceptions import TimeoutStateError <EOL> def current_time ( ) : <EOL> """<STR_LIT>""" <EOL> return time . time ( ) <EOL> _Default = object ( ) <EOL> class Timeout ( object ) : <EOL> """<STR_LIT>""" <EOL> DEFAULT_TIMEOUT = _GLOBAL_DEFAULT_TIMEOUT <EOL> def __init__ ( self , total = None , connect = _Default , read = _Default ) : <EOL> self . _connect = self . _validate_timeout ( connect , '<STR_LIT>' ) <EOL> self . _read = self . _validate_timeout ( read , '<STR_LIT>' ) <EOL> self . total = self . _validate_timeout ( total , '<STR_LIT>' ) <EOL> self . _start_connect = None <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> type ( self ) . __name__ , self . _connect , self . _read , self . total ) <EOL> @ classmethod <EOL> def _validate_timeout ( cls , value , name ) : <EOL> """<STR_LIT>""" <EOL> if value is _Default : <EOL> return cls . DEFAULT_TIMEOUT <EOL> if value is None or value is cls . DEFAULT_TIMEOUT : <EOL> return value <EOL> try : <EOL> float ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( name , value ) ) <EOL> try : <EOL> if value < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( name , value ) ) <EOL> except TypeError : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( name , value ) ) <EOL> return value <EOL> @ classmethod <EOL> def from_float ( cls , timeout ) : <EOL> """<STR_LIT>""" <EOL> return Timeout ( read = timeout , connect = timeout ) <EOL> def clone ( self ) : <EOL> """<STR_LIT>""" <EOL> return Timeout ( connect = self . _connect , read = self . _read , <EOL> total = self . total ) <EOL> def start_connect ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _start_connect is not None : <EOL> raise TimeoutStateError ( "<STR_LIT>" ) <EOL> self . _start_connect = current_time ( ) <EOL> return self . _start_connect <EOL> def get_connect_duration ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _start_connect is None : <EOL> raise TimeoutStateError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return current_time ( ) - self . _start_connect <EOL> @ property <EOL> def connect_timeout ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . total is None : <EOL> return self . _connect <EOL> if self . _connect is None or self . _connect is self . DEFAULT_TIMEOUT : <EOL> return self . total <EOL> return min ( self . _connect , self . total ) <EOL> @ property <EOL> def read_timeout ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . total is not None and <EOL> self . total is not self . DEFAULT_TIMEOUT and <EOL> self . _read is not None and <EOL> self . _read is not self . DEFAULT_TIMEOUT ) : <EOL> if self . _start_connect is None : <EOL> return self . _read <EOL> return max ( <NUM_LIT:0> , min ( self . total - self . get_connect_duration ( ) , <EOL> self . _read ) ) <EOL> elif self . total is not None and self . total is not self . DEFAULT_TIMEOUT : <EOL> return max ( <NUM_LIT:0> , self . total - self . get_connect_duration ( ) ) <EOL> else : <EOL> return self . _read </s>
