# -*- coding: utf-8 -*-

import contextlib

from thriftpy.protocol import TCyBinaryProtocolFactory
from thriftpy.thrift import TProcessor, TClient
from thriftpy.transport import TSocket, TBufferedTransport, TServerSocket
from thriftpy.server import TThreadedServer


@contextlib.contextmanager
def make_client(service, host, port, proto_factory=TCyBinaryProtocolFactory()):
    try:
        transport = TBufferedTransport(TSocket(host, port))
        protocol = proto_factory.get_protocol(transport)
        transport.open()
        yield TClient(service, protocol)
    finally:
        transport.close()


def make_server(service, handler, host, port,
                proto_factory=TCyBinaryProtocolFactory()):
    processor = TProcessor(service, handler)
    transport = TServerSocket(host=host, port=port)
    server = TThreadedServer(processor, transport,
                             iprot_factory=proto_factory)
    return server

@@ -264,35 +264,8 @@
 r%0A%0A%0A
-@contextlib.contextmanager%0A
 def 
@@ -344,21 +344,8 @@
 )):%0A
-    try:%0A    
     
@@ -396,28 +396,24 @@
  port))%0A    
-  
-  
 protocol = p
@@ -449,28 +449,24 @@
 nsport)%0A    
-  
-  
 transport.op
@@ -478,17 +478,14 @@
     
-    yield
+return
  TCl
@@ -512,47 +512,8 @@
 ol)%0A
-    finally:%0A        transport.close()%0A
 %0A%0Ade
@@ -821,16 +821,16 @@
 actory)%0A
-
     retu
@@ -835,12 +835,374 @@
 turn server%0A
+%0A%0A@contextlib.contextmanager%0Adef client_context(service, host, port,%0A                   proto_factory=TCyBinaryProtocolFactory()):%0A    try:%0A        transport = TBufferedTransport(TSocket(host, port))%0A        protocol = proto_factory.get_protocol(transport)%0A        transport.open()%0A        yield TClient(service, protocol)%0A    finally:%0A        transport.close()%0A

split client with raw client and client context