<s> from clay import config <EOL> import threading <EOL> import random <EOL> log = config . get_logger ( '<STR_LIT>' ) <EOL> class DatabaseContext ( object ) : <EOL> def __init__ ( self , servers , dbapi_name ) : <EOL> '''<STR_LIT>''' <EOL> self . servers = servers <EOL> self . tlocal = threading . local ( ) <EOL> self . tlocal . dbconn = None <EOL> if not dbapi_name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( '<STR_LIT>' % dbapi_name ) <EOL> self . dbapi_name = dbapi_name <EOL> self . dbapi = __import__ ( dbapi_name ) <EOL> def __enter__ ( self ) : <EOL> server = random . choice ( self . servers ) <EOL> conn = self . dbapi . connect ( ** server ) <EOL> self . tlocal . dbconn = conn <EOL> return conn <EOL> def __exit__ ( self , exc_type , exc_value , exc_traceback ) : <EOL> self . tlocal . dbconn . close ( ) <EOL> self . tlocal . dbconn = None <EOL> if exc_type is not None : <EOL> raise <EOL> def __str__ ( self ) : <EOL> if self . dbconn is not None : <EOL> return '<STR_LIT>' % ( self . dbapi_name , self . servers ) <EOL> else : <EOL> return '<STR_LIT>' % ( self . dbapi_name , self . servers ) <EOL> read = DatabaseContext ( config . get ( '<STR_LIT>' ) , config . get ( '<STR_LIT>' ) ) <EOL> write = DatabaseContext ( config . get ( '<STR_LIT>' ) , config . get ( '<STR_LIT>' ) ) </s>
