<s> import logging <EOL> from logging . handlers import TimedRotatingFileHandler <EOL> import jasmin . vendor . txredisapi as redis <EOL> from twisted . internet import reactor <EOL> from twisted . internet import defer <EOL> from jasmin . redis . configs import RedisForJasminConfig <EOL> LOG_CATEGORY = "<STR_LIT>" <EOL> class RedisForJasminProtocol ( redis . RedisProtocol ) : <EOL> def connectionMade ( self ) : <EOL> redis . RedisProtocol . connectionMade ( self ) <EOL> self . factory . log . info ( "<STR_LIT>" ) <EOL> def execute_command ( self , * args , ** kwargs ) : <EOL> self . factory . log . debug ( '<STR_LIT>' , args ) <EOL> return redis . RedisProtocol . execute_command ( self , * args , ** kwargs ) <EOL> class RedisForJasminFactory ( redis . RedisFactory ) : <EOL> protocol = RedisForJasminProtocol <EOL> def startedConnecting ( self , connector ) : <EOL> redis . RedisFactory . startedConnecting ( self , connector ) <EOL> self . log . info ( '<STR_LIT>' ) <EOL> def clientConnectionLost ( self , connector , reason ) : <EOL> redis . RedisFactory . clientConnectionLost ( self , connector , reason ) <EOL> self . log . info ( '<STR_LIT>' , reason ) <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> redis . RedisFactory . clientConnectionFailed ( self , connector , reason ) <EOL> self . log . info ( '<STR_LIT>' , reason ) <EOL> def __init__ ( self , uuid , dbid , poolsize , isLazy = True , <EOL> handler = redis . ConnectionHandler , config = None ) : <EOL> redis . RedisFactory . __init__ ( self , uuid , dbid , poolsize , isLazy , handler ) <EOL> self . log = logging . getLogger ( LOG_CATEGORY ) <EOL> if config is not None : <EOL> self . log . setLevel ( config . log_level ) <EOL> handler = TimedRotatingFileHandler ( filename = config . log_file , <EOL> when = config . log_rotate ) <EOL> formatter = logging . Formatter ( config . log_format , config . log_date_format ) <EOL> handler . setFormatter ( formatter ) <EOL> else : <EOL> handler = logging . NullHandler ( ) <EOL> if len ( self . log . handlers ) != <NUM_LIT:1> : <EOL> self . log . addHandler ( handler ) <EOL> self . log . propagate = False <EOL> def makeConnection ( host , port , dbid , poolsize , reconnect , isLazy , _RedisForJasminConfig = None ) : <EOL> uuid = "<STR_LIT>" % ( host , port ) <EOL> factory = RedisForJasminFactory ( uuid , None , poolsize , isLazy , redis . ConnectionHandler , _RedisForJasminConfig ) <EOL> factory . continueTrying = reconnect <EOL> for _ in xrange ( poolsize ) : <EOL> reactor . connectTCP ( host , port , factory ) <EOL> if isLazy : <EOL> return factory . handler <EOL> else : <EOL> return factory . deferred <EOL> def SimpleConnection ( host = "<STR_LIT:127.0.0.1>" , port = <NUM_LIT> , dbid = None , reconnect = True ) : <EOL> return makeConnection ( host , port , dbid , <NUM_LIT:1> , reconnect , False ) <EOL> def ConnectionWithConfiguration ( _RedisForJasminConfig ) : <EOL> if _RedisForJasminConfig . password is not None : <EOL> dbid = None <EOL> else : <EOL> dbid = _RedisForJasminConfig . dbid <EOL> return makeConnection ( _RedisForJasminConfig . host , _RedisForJasminConfig . port , dbid , <EOL> _RedisForJasminConfig . poolsize , True , False , _RedisForJasminConfig ) <EOL> @ defer . inlineCallbacks <EOL> def main ( ) : <EOL> config = RedisForJasminConfig ( ) <EOL> rc = yield ConnectionWithConfiguration ( config ) <EOL> print rc <EOL> if config . password is not None : <EOL> rc . auth ( config . password ) <EOL> rc . select ( config . dbid ) <EOL> yield rc . set ( "<STR_LIT:foo>" , { '<STR_LIT:yes>' , '<STR_LIT>' } ) <EOL> yield rc . expire ( "<STR_LIT:foo>" , <NUM_LIT:2> ) <EOL> import time <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> v = yield rc . get ( "<STR_LIT:foo>" ) <EOL> print "<STR_LIT>" , repr ( v ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> v = yield rc . get ( "<STR_LIT:foo>" ) <EOL> print "<STR_LIT>" , repr ( v ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> v = yield rc . get ( "<STR_LIT:foo>" ) <EOL> print "<STR_LIT>" , repr ( v ) <EOL> msgid = "<STR_LIT>" <EOL> v = yield rc . get ( msgid ) <EOL> print "<STR_LIT>" % msgid , repr ( v ) <EOL> yield rc . disconnect ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) . addCallback ( lambda ign : reactor . stop ( ) ) <EOL> reactor . run ( ) </s>
