THRIFT-1480. py: remove tabs, adjust whitespace and address PEP8 warnings
This patch addresses a host of PEP8 lint problems.
Patch: Will Pierce
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1226890 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/py/src/transport/THttpClient.py b/lib/py/src/transport/THttpClient.py
index 5026978..ad94d11 100644
--- a/lib/py/src/transport/THttpClient.py
+++ b/lib/py/src/transport/THttpClient.py
@@ -17,16 +17,17 @@
# under the License.
#
-from TTransport import *
+import httplib
+import socket
+import urlparse
+import warnings
+
from cStringIO import StringIO
-import urlparse
-import httplib
-import warnings
-import socket
+from TTransport import *
+
class THttpClient(TTransportBase):
-
"""Http implementation of TTransport base."""
def __init__(self, uri_or_host, port=None, path=None):
@@ -35,10 +36,13 @@
THttpClient(host, port, path) - deprecated
THttpClient(uri)
- Only the second supports https."""
-
+ Only the second supports https.
+ """
if port is not None:
- warnings.warn("Please use the THttpClient('http://host:port/path') syntax", DeprecationWarning, stacklevel=2)
+ warnings.warn(
+ "Please use the THttpClient('http://host:port/path') syntax",
+ DeprecationWarning,
+ stacklevel=2)
self.host = uri_or_host
self.port = port
assert path
@@ -71,7 +75,7 @@
self.__http = None
def isOpen(self):
- return self.__http != None
+ return self.__http is not None
def setTimeout(self, ms):
if not hasattr(socket, 'getdefaulttimeout'):
@@ -80,7 +84,7 @@
if ms is None:
self.__timeout = None
else:
- self.__timeout = ms/1000.0
+ self.__timeout = ms / 1000.0
def read(self, sz):
return self.__http.file.read(sz)
@@ -100,7 +104,7 @@
def flush(self):
if self.isOpen():
self.close()
- self.open();
+ self.open()
# Pull data out of buffer
data = self.__wbuf.getvalue()
diff --git a/lib/py/src/transport/TSSLSocket.py b/lib/py/src/transport/TSSLSocket.py
index be35844..6d79ac6 100644
--- a/lib/py/src/transport/TSSLSocket.py
+++ b/lib/py/src/transport/TSSLSocket.py
@@ -16,6 +16,7 @@
# specific language governing permissions and limitations
# under the License.
#
+
import os
import socket
import ssl
@@ -23,28 +24,35 @@
from thrift.transport import TSocket
from thrift.transport.TTransport import TTransportException
+
class TSSLSocket(TSocket.TSocket):
"""
SSL implementation of client-side TSocket
This class creates outbound sockets wrapped using the
python standard ssl module for encrypted connections.
-
+
The protocol used is set using the class variable
SSL_VERSION, which must be one of ssl.PROTOCOL_* and
defaults to ssl.PROTOCOL_TLSv1 for greatest security.
"""
SSL_VERSION = ssl.PROTOCOL_TLSv1
- def __init__(self, host='localhost', port=9090, validate=True, ca_certs=None, unix_socket=None):
- """
- @param validate: Set to False to disable SSL certificate validation entirely.
+ def __init__(self,
+ host='localhost',
+ port=9090,
+ validate=True,
+ ca_certs=None,
+ unix_socket=None):
+ """Create SSL TSocket
+
+ @param validate: Set to False to disable SSL certificate validation
@type validate: bool
@param ca_certs: Filename to the Certificate Authority pem file, possibly a
file downloaded from: http://curl.haxx.se/ca/cacert.pem This is passed to
the ssl_wrap function as the 'ca_certs' parameter.
@type ca_certs: str
-
+
Raises an IOError exception if validate is True and the ca_certs file is
None, not present or unreadable.
"""
@@ -58,18 +66,23 @@
self.ca_certs = ca_certs
if validate:
if ca_certs is None or not os.access(ca_certs, os.R_OK):
- raise IOError('Certificate Authority ca_certs file "%s" is not readable, cannot validate SSL certificates.' % (ca_certs))
+ raise IOError('Certificate Authority ca_certs file "%s" '
+ 'is not readable, cannot validate SSL '
+ 'certificates.' % (ca_certs))
TSocket.TSocket.__init__(self, host, port, unix_socket)
def open(self):
try:
res0 = self._resolveAddr()
for res in res0:
- sock_family, sock_type= res[0:2]
+ sock_family, sock_type = res[0:2]
ip_port = res[4]
plain_sock = socket.socket(sock_family, sock_type)
- self.handle = ssl.wrap_socket(plain_sock, ssl_version=self.SSL_VERSION,
- do_handshake_on_connect=True, ca_certs=self.ca_certs, cert_reqs=self.cert_reqs)
+ self.handle = ssl.wrap_socket(plain_sock,
+ ssl_version=self.SSL_VERSION,
+ do_handshake_on_connect=True,
+ ca_certs=self.ca_certs,
+ cert_reqs=self.cert_reqs)
self.handle.settimeout(self._timeout)
try:
self.handle.connect(ip_port)
@@ -84,7 +97,8 @@
message = 'Could not connect to secure socket %s' % self._unix_socket
else:
message = 'Could not connect to %s:%d' % (self.host, self.port)
- raise TTransportException(type=TTransportException.NOT_OPEN, message=message)
+ raise TTransportException(type=TTransportException.NOT_OPEN,
+ message=message)
if self.validate:
self._validate_cert()
@@ -93,13 +107,15 @@
commonName of the certificate to ensure it matches the hostname we
used to make this connection. Does not support subjectAltName records
in certificates.
-
- raises TTransportException if the certificate fails validation."""
+
+ raises TTransportException if the certificate fails validation.
+ """
cert = self.handle.getpeercert()
self.peercert = cert
if 'subject' not in cert:
- raise TTransportException(type=TTransportException.NOT_OPEN,
- message='No SSL certificate found from %s:%s' % (self.host, self.port))
+ raise TTransportException(
+ type=TTransportException.NOT_OPEN,
+ message='No SSL certificate found from %s:%s' % (self.host, self.port))
fields = cert['subject']
for field in fields:
# ensure structure we get back is what we expect
@@ -115,29 +131,38 @@
if certhost == self.host:
# success, cert commonName matches desired hostname
self.is_valid = True
- return
+ return
else:
- raise TTransportException(type=TTransportException.UNKNOWN,
- message='Host name we connected to "%s" doesn\'t match certificate provided commonName "%s"' % (self.host, certhost))
- raise TTransportException(type=TTransportException.UNKNOWN,
- message='Could not validate SSL certificate from host "%s". Cert=%s' % (self.host, cert))
+ raise TTransportException(
+ type=TTransportException.UNKNOWN,
+ message='Hostname we connected to "%s" doesn\'t match certificate '
+ 'provided commonName "%s"' % (self.host, certhost))
+ raise TTransportException(
+ type=TTransportException.UNKNOWN,
+ message='Could not validate SSL certificate from '
+ 'host "%s". Cert=%s' % (self.host, cert))
+
class TSSLServerSocket(TSocket.TServerSocket):
- """
- SSL implementation of TServerSocket
+ """SSL implementation of TServerSocket
This uses the ssl module's wrap_socket() method to provide SSL
negotiated encryption.
"""
SSL_VERSION = ssl.PROTOCOL_TLSv1
- def __init__(self, host=None, port=9090, certfile='cert.pem', unix_socket=None):
+ def __init__(self,
+ host=None,
+ port=9090,
+ certfile='cert.pem',
+ unix_socket=None):
"""Initialize a TSSLServerSocket
-
- @param certfile: The filename of the server certificate file, defaults to cert.pem
+
+ @param certfile: filename of the server certificate, defaults to cert.pem
@type certfile: str
- @param host: The hostname or IP to bind the listen socket to, i.e. 'localhost' for only allowing
- local network connections. Pass None to bind to all interfaces.
+ @param host: The hostname or IP to bind the listen socket to,
+ i.e. 'localhost' for only allowing local network connections.
+ Pass None to bind to all interfaces.
@type host: str
@param port: The port to listen on for inbound connections.
@type port: int
@@ -147,10 +172,11 @@
def setCertfile(self, certfile):
"""Set or change the server certificate file used to wrap new connections.
-
- @param certfile: The filename of the server certificate, i.e. '/etc/certs/server.pem'
+
+ @param certfile: The filename of the server certificate,
+ i.e. '/etc/certs/server.pem'
@type certfile: str
-
+
Raises an IOError exception if the certfile is not present or unreadable.
"""
if not os.access(certfile, os.R_OK):
@@ -166,11 +192,11 @@
# failed handshake/ssl wrap, close socket to client
plain_client.close()
# raise ssl_exc
- # We can't raise the exception, because it kills most TServer derived serve()
- # methods.
+ # We can't raise the exception, because it kills most TServer derived
+ # serve() methods.
# Instead, return None, and let the TServer instance deal with it in
# other exception handling. (but TSimpleServer dies anyway)
- return None
+ return None
result = TSocket.TSocket()
result.setHandle(client)
return result
diff --git a/lib/py/src/transport/TSocket.py b/lib/py/src/transport/TSocket.py
index 4e0e187..9e2b384 100644
--- a/lib/py/src/transport/TSocket.py
+++ b/lib/py/src/transport/TSocket.py
@@ -17,24 +17,33 @@
# under the License.
#
-from TTransport import *
-import os
import errno
+import os
import socket
import sys
+from TTransport import *
+
+
class TSocketBase(TTransportBase):
def _resolveAddr(self):
if self._unix_socket is not None:
- return [(socket.AF_UNIX, socket.SOCK_STREAM, None, None, self._unix_socket)]
+ return [(socket.AF_UNIX, socket.SOCK_STREAM, None, None,
+ self._unix_socket)]
else:
- return socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG)
+ return socket.getaddrinfo(self.host,
+ self.port,
+ socket.AF_UNSPEC,
+ socket.SOCK_STREAM,
+ 0,
+ socket.AI_PASSIVE | socket.AI_ADDRCONFIG)
def close(self):
if self.handle:
self.handle.close()
self.handle = None
+
class TSocket(TSocketBase):
"""Socket implementation of TTransport base."""
@@ -46,7 +55,6 @@
@param unix_socket(str) The filename of a unix socket to connect to.
(host and port will be ignored.)
"""
-
self.host = host
self.port = port
self.handle = None
@@ -63,7 +71,7 @@
if ms is None:
self._timeout = None
else:
- self._timeout = ms/1000.0
+ self._timeout = ms / 1000.0
if self.handle is not None:
self.handle.settimeout(self._timeout)
@@ -87,7 +95,8 @@
message = 'Could not connect to socket %s' % self._unix_socket
else:
message = 'Could not connect to %s:%d' % (self.host, self.port)
- raise TTransportException(type=TTransportException.NOT_OPEN, message=message)
+ raise TTransportException(type=TTransportException.NOT_OPEN,
+ message=message)
def read(self, sz):
try:
@@ -105,24 +114,28 @@
else:
raise
if len(buff) == 0:
- raise TTransportException(type=TTransportException.END_OF_FILE, message='TSocket read 0 bytes')
+ raise TTransportException(type=TTransportException.END_OF_FILE,
+ message='TSocket read 0 bytes')
return buff
def write(self, buff):
if not self.handle:
- raise TTransportException(type=TTransportException.NOT_OPEN, message='Transport not open')
+ raise TTransportException(type=TTransportException.NOT_OPEN,
+ message='Transport not open')
sent = 0
have = len(buff)
while sent < have:
plus = self.handle.send(buff)
if plus == 0:
- raise TTransportException(type=TTransportException.END_OF_FILE, message='TSocket sent 0 bytes')
+ raise TTransportException(type=TTransportException.END_OF_FILE,
+ message='TSocket sent 0 bytes')
sent += plus
buff = buff[plus:]
def flush(self):
pass
+
class TServerSocket(TSocketBase, TServerTransportBase):
"""Socket implementation of TServerTransport base."""
diff --git a/lib/py/src/transport/TTransport.py b/lib/py/src/transport/TTransport.py
index 12e51a9..4481371 100644
--- a/lib/py/src/transport/TTransport.py
+++ b/lib/py/src/transport/TTransport.py
@@ -18,11 +18,11 @@
#
from cStringIO import StringIO
-from struct import pack,unpack
+from struct import pack, unpack
from thrift.Thrift import TException
-class TTransportException(TException):
+class TTransportException(TException):
"""Custom Transport Exception class"""
UNKNOWN = 0
@@ -35,8 +35,8 @@
TException.__init__(self, message)
self.type = type
-class TTransportBase:
+class TTransportBase:
"""Base class for Thrift transport layer."""
def isOpen(self):
@@ -55,7 +55,7 @@
buff = ''
have = 0
while (have < sz):
- chunk = self.read(sz-have)
+ chunk = self.read(sz - have)
have += len(chunk)
buff += chunk
@@ -70,6 +70,7 @@
def flush(self):
pass
+
# This class should be thought of as an interface.
class CReadableTransport:
"""base class for transports that are readable from C"""
@@ -98,8 +99,8 @@
"""
pass
-class TServerTransportBase:
+class TServerTransportBase:
"""Base class for Thrift server transports."""
def listen(self):
@@ -111,15 +112,15 @@
def close(self):
pass
-class TTransportFactoryBase:
+class TTransportFactoryBase:
"""Base class for a Transport Factory"""
def getTransport(self, trans):
return trans
-class TBufferedTransportFactory:
+class TBufferedTransportFactory:
"""Factory transport that builds buffered transports"""
def getTransport(self, trans):
@@ -127,17 +128,15 @@
return buffered
-class TBufferedTransport(TTransportBase,CReadableTransport):
-
+class TBufferedTransport(TTransportBase, CReadableTransport):
"""Class that wraps another transport and buffers its I/O.
The implementation uses a (configurable) fixed-size read buffer
but buffers all writes until a flush is performed.
"""
-
DEFAULT_BUFFER = 4096
- def __init__(self, trans, rbuf_size = DEFAULT_BUFFER):
+ def __init__(self, trans, rbuf_size=DEFAULT_BUFFER):
self.__trans = trans
self.__wbuf = StringIO()
self.__rbuf = StringIO("")
@@ -188,6 +187,7 @@
self.__rbuf = StringIO(retstring)
return self.__rbuf
+
class TMemoryBuffer(TTransportBase, CReadableTransport):
"""Wraps a cStringIO object as a TTransport.
@@ -237,8 +237,8 @@
# only one shot at reading...
raise EOFError()
-class TFramedTransportFactory:
+class TFramedTransportFactory:
"""Factory transport that builds framed transports"""
def getTransport(self, trans):
@@ -247,7 +247,6 @@
class TFramedTransport(TTransportBase, CReadableTransport):
-
"""Class that wraps another transport and frames its I/O when writing."""
def __init__(self, trans,):
diff --git a/lib/py/src/transport/TTwisted.py b/lib/py/src/transport/TTwisted.py
index b6dcb4e..3ce3eb2 100644
--- a/lib/py/src/transport/TTwisted.py
+++ b/lib/py/src/transport/TTwisted.py
@@ -16,6 +16,9 @@
# specific language governing permissions and limitations
# under the License.
#
+
+from cStringIO import StringIO
+
from zope.interface import implements, Interface, Attribute
from twisted.internet.protocol import Protocol, ServerFactory, ClientFactory, \
connectionDone
@@ -25,7 +28,6 @@
from twisted.web import server, resource, http
from thrift.transport import TTransport
-from cStringIO import StringIO
class TMessageSenderTransport(TTransport.TTransportBase):
@@ -79,7 +81,7 @@
self.started.callback(self.client)
def connectionLost(self, reason=connectionDone):
- for k,v in self.client._reqs.iteritems():
+ for k, v in self.client._reqs.iteritems():
tex = TTransport.TTransportException(
type=TTransport.TTransportException.END_OF_FILE,
message='Connection closed')
diff --git a/lib/py/src/transport/TZlibTransport.py b/lib/py/src/transport/TZlibTransport.py
index 784d4e1..a2f42a5 100644
--- a/lib/py/src/transport/TZlibTransport.py
+++ b/lib/py/src/transport/TZlibTransport.py
@@ -16,50 +16,49 @@
# specific language governing permissions and limitations
# under the License.
#
-'''
-TZlibTransport provides a compressed transport and transport factory
+
+"""TZlibTransport provides a compressed transport and transport factory
class, using the python standard library zlib module to implement
data compression.
-'''
+"""
from __future__ import division
import zlib
from cStringIO import StringIO
from TTransport import TTransportBase, CReadableTransport
+
class TZlibTransportFactory(object):
- '''
- Factory transport that builds zlib compressed transports.
-
+ """Factory transport that builds zlib compressed transports.
+
This factory caches the last single client/transport that it was passed
and returns the same TZlibTransport object that was created.
-
+
This caching means the TServer class will get the _same_ transport
object for both input and output transports from this factory.
(For non-threaded scenarios only, since the cache only holds one object)
-
+
The purpose of this caching is to allocate only one TZlibTransport where
only one is really needed (since it must have separate read/write buffers),
and makes the statistics from getCompSavings() and getCompRatio()
easier to understand.
- '''
-
+ """
# class scoped cache of last transport given and zlibtransport returned
_last_trans = None
_last_z = None
def getTransport(self, trans, compresslevel=9):
- '''Wrap a transport , trans, with the TZlibTransport
+ """Wrap a transport, trans, with the TZlibTransport
compressed transport class, returning a new
transport to the caller.
-
+
@param compresslevel: The zlib compression level, ranging
from 0 (no compression) to 9 (best compression). Defaults to 9.
@type compresslevel: int
-
+
This method returns a TZlibTransport which wraps the
passed C{trans} TTransport derived instance.
- '''
+ """
if trans == self._last_trans:
return self._last_z
ztrans = TZlibTransport(trans, compresslevel)
@@ -69,27 +68,24 @@
class TZlibTransport(TTransportBase, CReadableTransport):
- '''
- Class that wraps a transport with zlib, compressing writes
+ """Class that wraps a transport with zlib, compressing writes
and decompresses reads, using the python standard
library zlib module.
- '''
-
+ """
# Read buffer size for the python fastbinary C extension,
# the TBinaryProtocolAccelerated class.
DEFAULT_BUFFSIZE = 4096
def __init__(self, trans, compresslevel=9):
- '''
- Create a new TZlibTransport, wrapping C{trans}, another
+ """Create a new TZlibTransport, wrapping C{trans}, another
TTransport derived object.
-
+
@param trans: A thrift transport object, i.e. a TSocket() object.
@type trans: TTransport
@param compresslevel: The zlib compression level, ranging
from 0 (no compression) to 9 (best compression). Default is 9.
@type compresslevel: int
- '''
+ """
self.__trans = trans
self.compresslevel = compresslevel
self.__rbuf = StringIO()
@@ -98,49 +94,45 @@
self._init_stats()
def _reinit_buffers(self):
- '''
- Internal method to initialize/reset the internal StringIO objects
+ """Internal method to initialize/reset the internal StringIO objects
for read and write buffers.
- '''
+ """
self.__rbuf = StringIO()
self.__wbuf = StringIO()
def _init_stats(self):
- '''
- Internal method to reset the internal statistics counters
+ """Internal method to reset the internal statistics counters
for compression ratios and bandwidth savings.
- '''
+ """
self.bytes_in = 0
self.bytes_out = 0
self.bytes_in_comp = 0
self.bytes_out_comp = 0
def _init_zlib(self):
- '''
- Internal method for setting up the zlib compression and
+ """Internal method for setting up the zlib compression and
decompression objects.
- '''
+ """
self._zcomp_read = zlib.decompressobj()
self._zcomp_write = zlib.compressobj(self.compresslevel)
def getCompRatio(self):
- '''
- Get the current measured compression ratios (in,out) from
+ """Get the current measured compression ratios (in,out) from
this transport.
-
- Returns a tuple of:
+
+ Returns a tuple of:
(inbound_compression_ratio, outbound_compression_ratio)
-
+
The compression ratios are computed as:
compressed / uncompressed
E.g., data that compresses by 10x will have a ratio of: 0.10
and data that compresses to half of ts original size will
have a ratio of 0.5
-
+
None is returned if no bytes have yet been processed in
a particular direction.
- '''
+ """
r_percent, w_percent = (None, None)
if self.bytes_in > 0:
r_percent = self.bytes_in_comp / self.bytes_in
@@ -149,23 +141,22 @@
return (r_percent, w_percent)
def getCompSavings(self):
- '''
- Get the current count of saved bytes due to data
+ """Get the current count of saved bytes due to data
compression.
-
+
Returns a tuple of:
(inbound_saved_bytes, outbound_saved_bytes)
-
+
Note: if compression is actually expanding your
data (only likely with very tiny thrift objects), then
the values returned will be negative.
- '''
+ """
r_saved = self.bytes_in - self.bytes_in_comp
w_saved = self.bytes_out - self.bytes_out_comp
return (r_saved, w_saved)
def isOpen(self):
- '''Return the underlying transport's open status'''
+ """Return the underlying transport's open status"""
return self.__trans.isOpen()
def open(self):
@@ -174,25 +165,24 @@
return self.__trans.open()
def listen(self):
- '''Invoke the underlying transport's listen() method'''
+ """Invoke the underlying transport's listen() method"""
self.__trans.listen()
def accept(self):
- '''Accept connections on the underlying transport'''
+ """Accept connections on the underlying transport"""
return self.__trans.accept()
def close(self):
- '''Close the underlying transport,'''
+ """Close the underlying transport,"""
self._reinit_buffers()
self._init_zlib()
return self.__trans.close()
def read(self, sz):
- '''
- Read up to sz bytes from the decompressed bytes buffer, and
+ """Read up to sz bytes from the decompressed bytes buffer, and
read from the underlying transport if the decompression
buffer is empty.
- '''
+ """
ret = self.__rbuf.read(sz)
if len(ret) > 0:
return ret
@@ -204,10 +194,9 @@
return ret
def readComp(self, sz):
- '''
- Read compressed data from the underlying transport, then
+ """Read compressed data from the underlying transport, then
decompress it and append it to the internal StringIO read buffer
- '''
+ """
zbuf = self.__trans.read(sz)
zbuf = self._zcomp_read.unconsumed_tail + zbuf
buf = self._zcomp_read.decompress(zbuf)
@@ -220,17 +209,15 @@
return True
def write(self, buf):
- '''
- Write some bytes, putting them into the internal write
+ """Write some bytes, putting them into the internal write
buffer for eventual compression.
- '''
+ """
self.__wbuf.write(buf)
def flush(self):
- '''
- Flush any queued up data in the write buffer and ensure the
+ """Flush any queued up data in the write buffer and ensure the
compression buffer is flushed out to the underlying transport
- '''
+ """
wout = self.__wbuf.getvalue()
if len(wout) > 0:
zbuf = self._zcomp_write.compress(wout)
@@ -247,11 +234,11 @@
@property
def cstringio_buf(self):
- '''Implement the CReadableTransport interface'''
+ """Implement the CReadableTransport interface"""
return self.__rbuf
def cstringio_refill(self, partialread, reqlen):
- '''Implement the CReadableTransport interface for refill'''
+ """Implement the CReadableTransport interface for refill"""
retstring = partialread
if reqlen < self.DEFAULT_BUFFSIZE:
retstring += self.read(self.DEFAULT_BUFFSIZE)
diff --git a/lib/py/src/transport/__init__.py b/lib/py/src/transport/__init__.py
index 46e54fe..c9596d9 100644
--- a/lib/py/src/transport/__init__.py
+++ b/lib/py/src/transport/__init__.py
@@ -17,4 +17,4 @@
# under the License.
#
-__all__ = ['TTransport', 'TSocket', 'THttpClient','TZlibTransport']
+__all__ = ['TTransport', 'TSocket', 'THttpClient', 'TZlibTransport']