#NEWLINE# def connect( host , port , protocol , * proto_args , **proto_kwargs ) : #NEWLINE# #INDENT# 'connect(host, port, protocol, *proto_args, **proto_kwargs)\n       Initiate a TCP connection to the given host and port using the given \n       protocol class. *proto_args and **proto_kwargs are passed to the protocol\n       constructor, and the last positional argument will be a Deferred for the\n       result of the task. The protocol constructor must take at least this one\n       argument.\n    ' #NEWLINE# deferredResult = defer . Deferred ( ) #NEWLINE# proto_args += ( deferredResult , ) #NEWLINE# if ( 'timeout' in proto_kwargs ) : #NEWLINE# #INDENT# timeout = proto_kwargs . pop ( 'timeout' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# timeout = None #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# import config #NEWLINE# reactor = config . reactor #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# from twisted.internet import reactor #NEWLINE# #UNINDENT# connector = ClientCreator ( reactor , protocol , * proto_args , ** proto_kwargs ) #NEWLINE# deferredConnect = connector . connectTCP ( host , port ) #NEWLINE# from kitt.decorators import debugCall #NEWLINE# if ( ( 'debug' in proto_kwargs ) and ( proto_kwargs [ 'debug' ] == True ) ) : #NEWLINE# #INDENT# deferredResult . errback = debugCall ( deferredResult . errback ) #NEWLINE# deferredResult . callback = debugCall ( deferredResult . callback ) #NEWLINE# #UNINDENT# deferredConnect . addErrback ( ( lambda failure : ( deferredResult . called or deferredResult . errback ( failure ) ) ) ) #NEWLINE# if timeout : #NEWLINE# #INDENT# reactor . callLater ( timeout , cancelTask , deferredResult ) #NEWLINE# #UNINDENT# def injectServer( outcome ) : #NEWLINE# #INDENT# if isinstance ( outcome , dict ) : #NEWLINE# #INDENT# outcome [ 'server' ] = proto_kwargs . get ( 'hostname' , host ) #NEWLINE# outcome [ 'port' ] = port #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( isinstance ( outcome , Failure ) and outcome . check ( DroneCommandFailed ) ) : #NEWLINE# #INDENT# outcome . value . resultContext [ 'server' ] = proto_kwargs . get ( 'hostname' , host ) #NEWLINE# outcome . value . resultContext [ 'port' ] = port #NEWLINE# #UNINDENT# #UNINDENT# return outcome #NEWLINE# #UNINDENT# if ( ( 'debug' in proto_kwargs ) and ( proto_kwargs [ 'debug' ] == True ) ) : #NEWLINE# #INDENT# injectServer = debugCall ( injectServer ) #NEWLINE# #UNINDENT# deferredResult . addBoth ( injectServer ) #NEWLINE# return deferredResult