From: David Reiss Date: Sat, 31 Jan 2009 21:59:27 +0000 (+0000) Subject: python: Add TFileObjectTransport X-Git-Tag: 0.2.0~344 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=1fe17774eb1059bc784a5c3be46b208a2fc0a63c;p=common%2Fthrift.git python: Add TFileObjectTransport TFileObjectTransport is a Thrift transport that wraps a Python-style file-like object. This is necessary to add methods like isOpen and readAll. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@739637 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/py/src/transport/TTransport.py b/lib/py/src/transport/TTransport.py index 67f97ba5..a83ac256 100644 --- a/lib/py/src/transport/TTransport.py +++ b/lib/py/src/transport/TTransport.py @@ -289,3 +289,25 @@ class TFramedTransport(TTransportBase, CReadableTransport): prefix += self.__rbuf.getvalue() self.__rbuf = StringIO(prefix) return self.__rbuf + + +class TFileObjectTransport(TTransportBase): + """Wraps a file-like object to make it work as a Thrift transport.""" + + def __init__(self, fileobj): + self.fileobj = fileobj + + def isOpen(self): + return True + + def close(self): + self.fileobj.close() + + def read(self, sz): + return self.fileobj.read(sz) + + def write(self, buf): + self.fileobj.write(buf) + + def flush(self): + self.fileobj.flush()