<s> import sys , os . path <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . abspath ( os . path . split ( sys . argv [ <NUM_LIT:0> ] ) [ <NUM_LIT:0> ] ) , '<STR_LIT>' ) ) <EOL> import tutorial . Calculator <EOL> from tutorial . ttypes import * <EOL> from thrift . transport import TTwisted , TTransport <EOL> from thrift . protocol import TBinaryProtocol <EOL> from twisted . internet import reactor , defer <EOL> from twisted . internet . protocol import ClientCreator <EOL> import txamqp . spec <EOL> from txamqp . client import TwistedDelegate <EOL> from txamqp . contrib . thrift . transport import TwistedAMQPTransport <EOL> from txamqp . contrib . thrift . protocol import ThriftAMQClient <EOL> servicesExchange = "<STR_LIT>" <EOL> responsesExchange = "<STR_LIT>" <EOL> calculatorQueue = "<STR_LIT>" <EOL> calculatorKey = "<STR_LIT>" <EOL> def gotPing ( _ ) : <EOL> print "<STR_LIT>" <EOL> def gotAddResults ( results ) : <EOL> print "<STR_LIT>" <EOL> print results <EOL> def gotCalculateResults ( results ) : <EOL> print "<STR_LIT>" <EOL> print results <EOL> def gotCalculateErrors ( error ) : <EOL> error . trap ( InvalidOperation ) <EOL> print "<STR_LIT>" <EOL> print error . value . why <EOL> def gotTransportError ( error ) : <EOL> error . trap ( TTransport . TTransportException ) <EOL> print "<STR_LIT>" <EOL> print error . value . message <EOL> @ defer . inlineCallbacks <EOL> def prepareClient ( client , username , password ) : <EOL> yield client . authenticate ( username , password ) <EOL> channel = yield client . channel ( <NUM_LIT:1> ) <EOL> yield channel . channel_open ( ) <EOL> yield channel . exchange_declare ( exchange = servicesExchange , type = "<STR_LIT>" ) <EOL> yield channel . exchange_declare ( exchange = responsesExchange , type = "<STR_LIT>" ) <EOL> pfactory = TBinaryProtocol . TBinaryProtocolFactory ( ) <EOL> thriftClient = yield client . createThriftClient ( responsesExchange , <EOL> servicesExchange , calculatorKey , tutorial . Calculator . Client , <EOL> iprot_factory = pfactory , oprot_factory = pfactory ) <EOL> defer . returnValue ( thriftClient ) <EOL> def gotClient ( client ) : <EOL> d1 = client . ping ( ) . addCallback ( gotPing ) . addErrback ( gotTransportError ) <EOL> d2 = client . add ( <NUM_LIT:1> , <NUM_LIT:2> ) . addCallback ( gotAddResults ) . addErrback ( gotTransportError ) <EOL> w = Work ( num1 = <NUM_LIT:2> , num2 = <NUM_LIT:3> , op = Operation . ADD ) <EOL> d3 = client . calculate ( <NUM_LIT:1> , w ) . addCallbacks ( gotCalculateResults , <EOL> gotCalculateErrors ) . addErrback ( gotTransportError ) <EOL> w = Work ( num1 = <NUM_LIT:2> , num2 = <NUM_LIT:3> , op = Operation . SUBTRACT ) <EOL> d4 = client . calculate ( <NUM_LIT:2> , w ) . addCallbacks ( gotCalculateResults , <EOL> gotCalculateErrors ) . addErrback ( gotTransportError ) <EOL> w = Work ( num1 = <NUM_LIT:2> , num2 = <NUM_LIT:3> , op = Operation . MULTIPLY ) <EOL> d5 = client . calculate ( <NUM_LIT:3> , w ) . addCallbacks ( gotCalculateResults , <EOL> gotCalculateErrors ) . addErrback ( gotTransportError ) <EOL> w = Work ( num1 = <NUM_LIT:2> , num2 = <NUM_LIT:3> , op = Operation . DIVIDE ) <EOL> d6 = client . calculate ( <NUM_LIT:4> , w ) . addCallbacks ( gotCalculateResults , <EOL> gotCalculateErrors ) . addErrback ( gotTransportError ) <EOL> w = Work ( num1 = <NUM_LIT:2> , num2 = <NUM_LIT:0> , op = Operation . DIVIDE ) <EOL> d7 = client . calculate ( <NUM_LIT:5> , w ) . addCallbacks ( gotCalculateResults , <EOL> gotCalculateErrors ) . addErrback ( gotTransportError ) <EOL> d8 = client . zip ( ) <EOL> dl = defer . DeferredList ( [ d1 , d2 , d3 , d4 , d5 , d6 , d7 , d8 ] ) <EOL> dl . addCallback ( lambda _ : reactor . stop ( ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> if len ( sys . argv ) != <NUM_LIT:7> : <EOL> print "<STR_LIT>" % sys . argv [ <NUM_LIT:0> ] <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> host = sys . argv [ <NUM_LIT:1> ] <EOL> port = int ( sys . argv [ <NUM_LIT:2> ] ) <EOL> vhost = sys . argv [ <NUM_LIT:3> ] <EOL> username = sys . argv [ <NUM_LIT:4> ] <EOL> password = sys . argv [ <NUM_LIT:5> ] <EOL> specFile = sys . argv [ <NUM_LIT:6> ] <EOL> spec = txamqp . spec . load ( specFile ) <EOL> delegate = TwistedDelegate ( ) <EOL> d = ClientCreator ( reactor , ThriftAMQClient , delegate , vhost , <EOL> spec ) . connectTCP ( host , port ) <EOL> d . addCallback ( prepareClient , username , password ) . addCallback ( gotClient ) <EOL> reactor . run ( ) </s>
