From 10308cb975ac090584068d0470b81e41555b2f35 Mon Sep 17 00:00:00 2001 From: Nobuaki Sukegawa Date: Wed, 3 Feb 2016 01:57:03 +0900 Subject: THRIFT-3596 Better conformance to PEP8 This closes #832 --- contrib/async-test/test-leaf.py | 15 ++-- contrib/fb303/py/fb303/FacebookBase.py | 77 +++++++++---------- .../fb303/py/fb303_scripts/fb303_simple_mgmt.py | 39 +++++----- contrib/fb303/py/setup.py | 43 ++++++----- contrib/parse_profiling.py | 12 +-- contrib/zeromq/TZmqClient.py | 81 ++++++++++---------- contrib/zeromq/TZmqServer.py | 87 +++++++++++----------- contrib/zeromq/test-client.py | 40 +++++----- contrib/zeromq/test-server.py | 32 ++++---- 9 files changed, 215 insertions(+), 211 deletions(-) (limited to 'contrib') diff --git a/contrib/async-test/test-leaf.py b/contrib/async-test/test-leaf.py index 8b7c3e3f5..4ea4a9b8c 100755 --- a/contrib/async-test/test-leaf.py +++ b/contrib/async-test/test-leaf.py @@ -7,16 +7,17 @@ from thrift.protocol import TBinaryProtocol from thrift.server import THttpServer from aggr import Aggr + class AggrHandler(Aggr.Iface): - def __init__(self): - self.values = [] + def __init__(self): + self.values = [] - def addValue(self, value): - self.values.append(value) + def addValue(self, value): + self.values.append(value) - def getValues(self, ): - time.sleep(1) - return self.values + def getValues(self, ): + time.sleep(1) + return self.values processor = Aggr.Processor(AggrHandler()) pfactory = TBinaryProtocol.TBinaryProtocolFactory() diff --git a/contrib/fb303/py/fb303/FacebookBase.py b/contrib/fb303/py/fb303/FacebookBase.py index 685ff20f3..07db10cd3 100644 --- a/contrib/fb303/py/fb303/FacebookBase.py +++ b/contrib/fb303/py/fb303/FacebookBase.py @@ -24,59 +24,60 @@ import FacebookService import thrift.reflection.limited from ttypes import fb_status + class FacebookBase(FacebookService.Iface): - def __init__(self, name): - self.name = name - self.alive = int(time.time()) - self.counters = {} + def __init__(self, name): + self.name = name + self.alive = int(time.time()) + self.counters = {} - def getName(self, ): - return self.name + def getName(self, ): + return self.name - def getVersion(self, ): - return '' + def getVersion(self, ): + return '' - def getStatus(self, ): - return fb_status.ALIVE + def getStatus(self, ): + return fb_status.ALIVE - def getCounters(self): - return self.counters + def getCounters(self): + return self.counters - def resetCounter(self, key): - self.counters[key] = 0 + def resetCounter(self, key): + self.counters[key] = 0 - def getCounter(self, key): - if self.counters.has_key(key): - return self.counters[key] - return 0 + def getCounter(self, key): + if self.counters.has_key(key): + return self.counters[key] + return 0 - def incrementCounter(self, key): - self.counters[key] = self.getCounter(key) + 1 + def incrementCounter(self, key): + self.counters[key] = self.getCounter(key) + 1 - def setOption(self, key, value): - pass + def setOption(self, key, value): + pass - def getOption(self, key): - return "" + def getOption(self, key): + return "" - def getOptions(self): - return {} + def getOptions(self): + return {} - def getOptions(self): - return {} + def getOptions(self): + return {} - def aliveSince(self): - return self.alive + def aliveSince(self): + return self.alive - def getCpuProfile(self, duration): - return "" + def getCpuProfile(self, duration): + return "" - def getLimitedReflection(self): - return thrift.reflection.limited.Service() + def getLimitedReflection(self): + return thrift.reflection.limited.Service() - def reinitialize(self): - pass + def reinitialize(self): + pass - def shutdown(self): - pass + def shutdown(self): + pass diff --git a/contrib/fb303/py/fb303_scripts/fb303_simple_mgmt.py b/contrib/fb303/py/fb303_scripts/fb303_simple_mgmt.py index 4f8ce9933..4b1c25728 100644 --- a/contrib/fb303/py/fb303_scripts/fb303_simple_mgmt.py +++ b/contrib/fb303/py/fb303_scripts/fb303_simple_mgmt.py @@ -19,7 +19,8 @@ # under the License. # -import sys, os +import sys +import os from optparse import OptionParser from thrift.Thrift import * @@ -31,11 +32,12 @@ from thrift.protocol import TBinaryProtocol from fb303 import * from fb303.ttypes import * + def service_ctrl( - command, - port, - trans_factory = None, - prot_factory = None): + command, + port, + trans_factory=None, + prot_factory=None): """ service_ctrl is a generic function to execute standard fb303 functions @@ -66,19 +68,19 @@ def service_ctrl( return 3 # scalar commands - if command in ["version","alive","name"]: + if command in ["version", "alive", "name"]: try: - result = fb303_wrapper(command, port, trans_factory, prot_factory) + result = fb303_wrapper(command, port, trans_factory, prot_factory) print result return 0 except: - print "failed to get ",command + print "failed to get ", command return 3 # counters if command in ["counters"]: try: - counters = fb303_wrapper('counters', port, trans_factory, prot_factory) + counters = fb303_wrapper('counters', port, trans_factory, prot_factory) for counter in counters: print "%s: %d" % (counter, counters[counter]) return 0 @@ -86,11 +88,10 @@ def service_ctrl( print "failed to get counters" return 3 - # Only root should be able to run the following commands if os.getuid() == 0: # async commands - if command in ["stop","reload"] : + if command in ["stop", "reload"]: try: fb303_wrapper(command, port, trans_factory, prot_factory) return 0 @@ -98,23 +99,21 @@ def service_ctrl( print "failed to tell the service to ", command return 3 else: - if command in ["stop","reload"]: + if command in ["stop", "reload"]: print "root privileges are required to stop or reload the service." return 4 print "The following commands are available:" - for command in ["counters","name","version","alive","status"]: + for command in ["counters", "name", "version", "alive", "status"]: print "\t%s" % command print "The following commands are available for users with root privileges:" - for command in ["stop","reload"]: + for command in ["stop", "reload"]: print "\t%s" % command + return 0 - return 0; - - -def fb303_wrapper(command, port, trans_factory = None, prot_factory = None): +def fb303_wrapper(command, port, trans_factory=None, prot_factory=None): sock = TSocket.TSocket('localhost', port) # use input transport factory if provided @@ -179,11 +178,11 @@ def main(): # parse command line options parser = OptionParser() - commands=["stop","counters","status","reload","version","name","alive"] + commands = ["stop", "counters", "status", "reload", "version", "name", "alive"] parser.add_option("-c", "--command", dest="command", help="execute this API", choices=commands, default="status") - parser.add_option("-p","--port",dest="port",help="the service's port", + parser.add_option("-p", "--port", dest="port", help="the service's port", default=9082) (options, args) = parser.parse_args() diff --git a/contrib/fb303/py/setup.py b/contrib/fb303/py/setup.py index 6710c8f61..4321ce258 100644 --- a/contrib/fb303/py/setup.py +++ b/contrib/fb303/py/setup.py @@ -24,26 +24,25 @@ try: from setuptools import setup, Extension except: from distutils.core import setup, Extension, Command - -setup(name = 'thrift_fb303', - version = '1.0.0-dev', - description = 'Python bindings for the Apache Thrift FB303', - author = ['Thrift Developers'], - author_email = ['dev@thrift.apache.org'], - url = 'http://thrift.apache.org', - license = 'Apache License 2.0', - packages = [ - 'fb303', - 'fb303_scripts', - ], - classifiers = [ - 'Development Status :: 5 - Production/Stable', - 'Environment :: Console', - 'Intended Audience :: Developers', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Topic :: Software Development :: Libraries', - 'Topic :: System :: Networking' - ], -) +setup(name='thrift_fb303', + version='1.0.0-dev', + description='Python bindings for the Apache Thrift FB303', + author=['Thrift Developers'], + author_email=['dev@thrift.apache.org'], + url='http://thrift.apache.org', + license='Apache License 2.0', + packages=[ + 'fb303', + 'fb303_scripts', + ], + classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Environment :: Console', + 'Intended Audience :: Developers', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Topic :: Software Development :: Libraries', + 'Topic :: System :: Networking' + ], + ) diff --git a/contrib/parse_profiling.py b/contrib/parse_profiling.py index 3d46fb832..0be5f29ed 100755 --- a/contrib/parse_profiling.py +++ b/contrib/parse_profiling.py @@ -46,6 +46,8 @@ class AddressInfo(object): g_addrs_by_filename = {} + + def get_address(filename, address): """ Retrieve an AddressInfo object for the specified object file and address. @@ -103,12 +105,12 @@ def translate_file_addresses(filename, addresses, options): idx = file_and_line.rfind(':') if idx < 0: msg = 'expected file and line number from addr2line; got %r' % \ - (file_and_line,) + (file_and_line,) msg += '\nfile=%r, address=%r' % (filename, address.address) raise Exception(msg) address.sourceFile = file_and_line[:idx] - address.sourceLine = file_and_line[idx+1:] + address.sourceLine = file_and_line[idx + 1:] (remaining_out, cmd_err) = proc.communicate() retcode = proc.wait() @@ -180,7 +182,7 @@ def process_file(in_file, out_file, options): virt_call_regex = re.compile(r'^\s*T_VIRTUAL_CALL: (\d+) calls on (.*):$') gen_prot_regex = re.compile( - r'^\s*T_GENERIC_PROTOCOL: (\d+) calls to (.*) with a (.*):$') + r'^\s*T_GENERIC_PROTOCOL: (\d+) calls to (.*) with a (.*):$') bt_regex = re.compile(r'^\s*#(\d+)\s*(.*) \[(0x[0-9A-Za-z]+)\]$') # Parse all of the input, and store it as Entry objects @@ -209,7 +211,7 @@ def process_file(in_file, out_file, options): # "_Z" to the type name to make it look like an external name. type_name = '_Z' + type_name header = 'T_VIRTUAL_CALL: %d calls on "%s"' % \ - (num_calls, type_name) + (num_calls, type_name) if current_entry is not None: entries.append(current_entry) current_entry = Entry(header) @@ -224,7 +226,7 @@ def process_file(in_file, out_file, options): type_name1 = '_Z' + type_name1 type_name2 = '_Z' + type_name2 header = 'T_GENERIC_PROTOCOL: %d calls to "%s" with a "%s"' % \ - (num_calls, type_name1, type_name2) + (num_calls, type_name1, type_name2) if current_entry is not None: entries.append(current_entry) current_entry = Entry(header) diff --git a/contrib/zeromq/TZmqClient.py b/contrib/zeromq/TZmqClient.py index d56069733..1bd60a1e5 100644 --- a/contrib/zeromq/TZmqClient.py +++ b/contrib/zeromq/TZmqClient.py @@ -20,44 +20,45 @@ import zmq from cStringIO import StringIO from thrift.transport.TTransport import TTransportBase, CReadableTransport + class TZmqClient(TTransportBase, CReadableTransport): - def __init__(self, ctx, endpoint, sock_type): - self._sock = ctx.socket(sock_type) - self._endpoint = endpoint - self._wbuf = StringIO() - self._rbuf = StringIO() - - def open(self): - self._sock.connect(self._endpoint) - - def read(self, size): - ret = self._rbuf.read(size) - if len(ret) != 0: - return ret - self._read_message() - return self._rbuf.read(size) - - def _read_message(self): - msg = self._sock.recv() - self._rbuf = StringIO(msg) - - def write(self, buf): - self._wbuf.write(buf) - - def flush(self): - msg = self._wbuf.getvalue() - self._wbuf = StringIO() - self._sock.send(msg) - - # Implement the CReadableTransport interface. - @property - def cstringio_buf(self): - return self._rbuf - - # NOTE: This will probably not actually work. - def cstringio_refill(self, prefix, reqlen): - while len(prefix) < reqlen: - self.read_message() - prefix += self._rbuf.getvalue() - self._rbuf = StringIO(prefix) - return self._rbuf + def __init__(self, ctx, endpoint, sock_type): + self._sock = ctx.socket(sock_type) + self._endpoint = endpoint + self._wbuf = StringIO() + self._rbuf = StringIO() + + def open(self): + self._sock.connect(self._endpoint) + + def read(self, size): + ret = self._rbuf.read(size) + if len(ret) != 0: + return ret + self._read_message() + return self._rbuf.read(size) + + def _read_message(self): + msg = self._sock.recv() + self._rbuf = StringIO(msg) + + def write(self, buf): + self._wbuf.write(buf) + + def flush(self): + msg = self._wbuf.getvalue() + self._wbuf = StringIO() + self._sock.send(msg) + + # Implement the CReadableTransport interface. + @property + def cstringio_buf(self): + return self._rbuf + + # NOTE: This will probably not actually work. + def cstringio_refill(self, prefix, reqlen): + while len(prefix) < reqlen: + self.read_message() + prefix += self._rbuf.getvalue() + self._rbuf = StringIO(prefix) + return self._rbuf diff --git a/contrib/zeromq/TZmqServer.py b/contrib/zeromq/TZmqServer.py index c83cc8d5d..15c1543ac 100644 --- a/contrib/zeromq/TZmqServer.py +++ b/contrib/zeromq/TZmqServer.py @@ -21,58 +21,59 @@ import zmq import thrift.server.TServer import thrift.transport.TTransport + class TZmqServer(thrift.server.TServer.TServer): - def __init__(self, processor, ctx, endpoint, sock_type): - thrift.server.TServer.TServer.__init__(self, processor, None) - self.zmq_type = sock_type - self.socket = ctx.socket(sock_type) - self.socket.bind(endpoint) + def __init__(self, processor, ctx, endpoint, sock_type): + thrift.server.TServer.TServer.__init__(self, processor, None) + self.zmq_type = sock_type + self.socket = ctx.socket(sock_type) + self.socket.bind(endpoint) - def serveOne(self): - msg = self.socket.recv() - itrans = thrift.transport.TTransport.TMemoryBuffer(msg) - otrans = thrift.transport.TTransport.TMemoryBuffer() - iprot = self.inputProtocolFactory.getProtocol(itrans) - oprot = self.outputProtocolFactory.getProtocol(otrans) + def serveOne(self): + msg = self.socket.recv() + itrans = thrift.transport.TTransport.TMemoryBuffer(msg) + otrans = thrift.transport.TTransport.TMemoryBuffer() + iprot = self.inputProtocolFactory.getProtocol(itrans) + oprot = self.outputProtocolFactory.getProtocol(otrans) - try: - self.processor.process(iprot, oprot) - except Exception: - logging.exception("Exception while processing request") - # Fall through and send back a response, even if empty or incomplete. + try: + self.processor.process(iprot, oprot) + except Exception: + logging.exception("Exception while processing request") + # Fall through and send back a response, even if empty or incomplete. - if self.zmq_type == zmq.REP: - msg = otrans.getvalue() - self.socket.send(msg) + if self.zmq_type == zmq.REP: + msg = otrans.getvalue() + self.socket.send(msg) - def serve(self): - while True: - self.serveOne() + def serve(self): + while True: + self.serveOne() class TZmqMultiServer(object): - def __init__(self): - self.servers = [] + def __init__(self): + self.servers = [] - def serveOne(self, timeout = -1): - self._serveActive(self._setupPoll(), timeout) + def serveOne(self, timeout=-1): + self._serveActive(self._setupPoll(), timeout) - def serveForever(self): - poll_info = self._setupPoll() - while True: - self._serveActive(poll_info, -1) + def serveForever(self): + poll_info = self._setupPoll() + while True: + self._serveActive(poll_info, -1) - def _setupPoll(self): - server_map = {} - poller = zmq.Poller() - for server in self.servers: - server_map[server.socket] = server - poller.register(server.socket, zmq.POLLIN) - return (server_map, poller) + def _setupPoll(self): + server_map = {} + poller = zmq.Poller() + for server in self.servers: + server_map[server.socket] = server + poller.register(server.socket, zmq.POLLIN) + return (server_map, poller) - def _serveActive(self, poll_info, timeout): - (server_map, poller) = poll_info - ready = dict(poller.poll()) - for sock, state in ready.items(): - assert (state & zmq.POLLIN) != 0 - server_map[sock].serveOne() + def _serveActive(self, poll_info, timeout): + (server_map, poller) = poll_info + ready = dict(poller.poll()) + for sock, state in ready.items(): + assert (state & zmq.POLLIN) != 0 + server_map[sock].serveOne() diff --git a/contrib/zeromq/test-client.py b/contrib/zeromq/test-client.py index 1886d9cab..753b132d8 100755 --- a/contrib/zeromq/test-client.py +++ b/contrib/zeromq/test-client.py @@ -9,28 +9,28 @@ import storage.Storage def main(args): - endpoint = "tcp://127.0.0.1:9090" - socktype = zmq.REQ - incr = 0 - if len(args) > 1: - incr = int(args[1]) - if incr: - socktype = zmq.DOWNSTREAM - endpoint = "tcp://127.0.0.1:9091" + endpoint = "tcp://127.0.0.1:9090" + socktype = zmq.REQ + incr = 0 + if len(args) > 1: + incr = int(args[1]) + if incr: + socktype = zmq.DOWNSTREAM + endpoint = "tcp://127.0.0.1:9091" - ctx = zmq.Context() - transport = TZmqClient.TZmqClient(ctx, endpoint, socktype) - protocol = thrift.protocol.TBinaryProtocol.TBinaryProtocolAccelerated(transport) - client = storage.Storage.Client(protocol) - transport.open() + ctx = zmq.Context() + transport = TZmqClient.TZmqClient(ctx, endpoint, socktype) + protocol = thrift.protocol.TBinaryProtocol.TBinaryProtocolAccelerated(transport) + client = storage.Storage.Client(protocol) + transport.open() - if incr: - client.incr(incr) - time.sleep(0.05) - else: - value = client.get() - print value + if incr: + client.incr(incr) + time.sleep(0.05) + else: + value = client.get() + print value if __name__ == "__main__": - main(sys.argv) + main(sys.argv) diff --git a/contrib/zeromq/test-server.py b/contrib/zeromq/test-server.py index 5767b71fe..c7804d317 100755 --- a/contrib/zeromq/test-server.py +++ b/contrib/zeromq/test-server.py @@ -6,28 +6,28 @@ import storage.Storage class StorageHandler(storage.Storage.Iface): - def __init__(self): - self.value = 0 + def __init__(self): + self.value = 0 - def incr(self, amount): - self.value += amount + def incr(self, amount): + self.value += amount - def get(self): - return self.value + def get(self): + return self.value def main(): - handler = StorageHandler() - processor = storage.Storage.Processor(handler) + handler = StorageHandler() + processor = storage.Storage.Processor(handler) - ctx = zmq.Context() - reqrep_server = TZmqServer.TZmqServer(processor, ctx, "tcp://0.0.0.0:9090", zmq.REP) - oneway_server = TZmqServer.TZmqServer(processor, ctx, "tcp://0.0.0.0:9091", zmq.UPSTREAM) - multiserver = TZmqServer.TZmqMultiServer() - multiserver.servers.append(reqrep_server) - multiserver.servers.append(oneway_server) - multiserver.serveForever() + ctx = zmq.Context() + reqrep_server = TZmqServer.TZmqServer(processor, ctx, "tcp://0.0.0.0:9090", zmq.REP) + oneway_server = TZmqServer.TZmqServer(processor, ctx, "tcp://0.0.0.0:9091", zmq.UPSTREAM) + multiserver = TZmqServer.TZmqMultiServer() + multiserver.servers.append(reqrep_server) + multiserver.servers.append(oneway_server) + multiserver.serveForever() if __name__ == "__main__": - main() + main() -- cgit v1.2.1