<s> """<STR_LIT>""" <EOL> import time <EOL> import pymongo <EOL> def get_methods ( * objs ) : <EOL> return set ( <EOL> attr <EOL> for obj in objs <EOL> for attr in dir ( obj ) <EOL> if not attr . startswith ( '<STR_LIT:_>' ) <EOL> and hasattr ( getattr ( obj , attr ) , '<STR_LIT>' ) <EOL> ) <EOL> try : <EOL> from pymongo import MongoClient , MongoReplicaSetClient <EOL> except ImportError : <EOL> MongoClient , MongoReplicaSetClient = None , None <EOL> EXECUTABLE_MONGO_METHODS = get_methods ( pymongo . collection . Collection , <EOL> pymongo . database . Database , <EOL> pymongo . cursor . Cursor , <EOL> MongoClient , MongoReplicaSetClient , <EOL> pymongo ) <EOL> class Executable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , method , logger , wait_time = None ) : <EOL> self . method = method <EOL> self . logger = logger <EOL> self . wait_time = wait_time or <NUM_LIT> <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> start = time . time ( ) <EOL> i = <NUM_LIT:0> <EOL> while True : <EOL> try : <EOL> val = self . method ( * args , ** kwargs ) <EOL> if isinstance ( val , pymongo . cursor . Cursor ) : <EOL> return MongoProxy ( val , self . logger , self . wait_time ) <EOL> else : <EOL> return val <EOL> except pymongo . errors . AutoReconnect : <EOL> end = time . time ( ) <EOL> delta = end - start <EOL> if delta >= self . wait_time : <EOL> break <EOL> self . logger . warning ( '<STR_LIT>' <EOL> % ( i , delta ) ) <EOL> time . sleep ( min ( <NUM_LIT:5> , pow ( <NUM_LIT:2> , i ) ) ) <EOL> i += <NUM_LIT:1> <EOL> return self . method ( * args , ** kwargs ) <EOL> def __dir__ ( self ) : <EOL> return dir ( self . method ) <EOL> def __str__ ( self ) : <EOL> return self . method . __str__ ( ) <EOL> def __repr__ ( self ) : <EOL> return self . method . __repr__ ( ) <EOL> class MongoProxy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , conn , logger = None , wait_time = None ) : <EOL> """<STR_LIT>""" <EOL> if logger is None : <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> self . conn = conn <EOL> self . logger = logger <EOL> self . wait_time = wait_time <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> item = self . conn [ key ] <EOL> if hasattr ( item , '<STR_LIT>' ) : <EOL> return MongoProxy ( item , self . logger , self . wait_time ) <EOL> return item <EOL> def __getattr__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> attr = getattr ( self . conn , key ) <EOL> if hasattr ( attr , '<STR_LIT>' ) : <EOL> if key in EXECUTABLE_MONGO_METHODS : <EOL> return Executable ( attr , self . logger , self . wait_time ) <EOL> else : <EOL> return MongoProxy ( attr , self . logger , self . wait_time ) <EOL> return attr <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> return self . conn ( * args , ** kwargs ) <EOL> def __dir__ ( self ) : <EOL> return dir ( self . conn ) <EOL> def __iter__ ( self ) : <EOL> return self . conn . __iter__ ( ) <EOL> def __str__ ( self ) : <EOL> return self . conn . __str__ ( ) <EOL> def __repr__ ( self ) : <EOL> return self . conn . __repr__ ( ) <EOL> def __nonzero__ ( self ) : <EOL> return True </s>
