Permalink
Browse files

Update lib/py/scribe for compatibility with new thrift python code ge…

…neration

Thrift has recently changed its python code generation. So that code that was previously written as
log_entry = scribe.LogEntry(dict(category=category, message=sys.stdin.read()))
needed to be changed to
log_entry = scribe.LogEntry(category=category, message=sys.stdin.read()).

The lib/py/scribe directory contains the thrift gen-py code for scribe which eventually gets copied to the python site-packages directory.
This will act as a temporary fix till we figure out a way to fix autoconf to automatically use the thrift generated python code in src/gen-py/scribe instead of manually checking in thrift generated python code as we are doing now.
Please follow the discussion on the scribe-server mailing list for progress on the autoconf issue
  • Loading branch information...
1 parent 74768b1 commit 9f3769963a1d07c227892a5f8ae18bfa61f20068 groys committed Jan 16, 2010
Showing with 158 additions and 44 deletions.
  1. +1 −1 lib/py/scribe/constants.py
  2. +27 −8 lib/py/scribe/scribe-remote
  3. +82 −22 lib/py/scribe/scribe.py
  4. +48 −13 lib/py/scribe/ttypes.py
@@ -4,6 +4,6 @@
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#
-from thrift.protocol.TProtocol import *
+from thrift.Thrift import *
from ttypes import *
@@ -7,16 +7,18 @@
import sys
import pprint
+from urlparse import urlparse
from thrift.transport import TTransport
from thrift.transport import TSocket
+from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol
import scribe
from ttypes import *
if len(sys.argv) <= 1 or sys.argv[1] == '--help':
print ''
- print 'Usage: ' + sys.argv[0] + ' [-h host:port] [-f[ramed]] function [arg1 [arg2...]]'
+ print 'Usage: ' + sys.argv[0] + ' [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]'
print ''
print 'Functions:'
print ' ResultCode Log( messages)'
@@ -26,14 +28,28 @@ if len(sys.argv) <= 1 or sys.argv[1] == '--help':
pp = pprint.PrettyPrinter(indent = 2)
host = 'localhost'
port = 9090
+uri = ''
framed = False
+http = False
argi = 1
-if sys.argv[1] == '-h':
- parts = sys.argv[2].split(':')
+if sys.argv[argi] == '-h':
+ parts = sys.argv[argi+1].split(':')
host = parts[0]
port = int(parts[1])
- argi = 3
+ argi += 2
+
+if sys.argv[argi] == '-u':
+ url = urlparse(sys.argv[argi+1])
+ parts = url[1].split(':')
+ host = parts[0]
+ if len(parts) > 1:
+ port = int(parts[1])
+ else:
+ port = 80
+ uri = url[2]
+ http = True
+ argi += 2
if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':
framed = True
@@ -42,11 +58,14 @@ if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':
cmd = sys.argv[argi]
args = sys.argv[argi+1:]
-socket = TSocket.TSocket(host, port)
-if framed:
- transport = TTransport.TFramedTransport(socket)
+if http:
+ transport = THttpClient.THttpClient(host, port, uri)
else:
- transport = TTransport.TBufferedTransport(socket)
+ socket = TSocket.TSocket(host, port)
+ if framed:
+ transport = TTransport.TFramedTransport(socket)
+ else:
+ transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = scribe.Client(protocol)
transport.open()
View
@@ -4,13 +4,24 @@
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#
-from thrift.protocol.TProtocol import *
+from thrift.Thrift import *
import fb303.FacebookService
from ttypes import *
from thrift.Thrift import TProcessor
+from thrift.transport import TTransport
+from thrift.protocol import TBinaryProtocol
+try:
+ from thrift.protocol import fastbinary
+except:
+ fastbinary = None
+
class Iface(fb303.FacebookService.Iface):
def Log(self, messages):
+ """
+ Parameters:
+ - messages
+ """
pass
@@ -19,6 +30,10 @@ def __init__(self, iprot, oprot=None):
fb303.FacebookService.Client.__init__(self, iprot, oprot)
def Log(self, messages):
+ """
+ Parameters:
+ - messages
+ """
self.send_Log(messages)
return self.recv_Log()
@@ -32,12 +47,17 @@ def send_Log(self, messages):
def recv_Log(self, ):
(fname, mtype, rseqid) = self._iprot.readMessageBegin()
+ if mtype == TMessageType.EXCEPTION:
+ x = TApplicationException()
+ x.read(self._iprot)
+ self._iprot.readMessageEnd()
+ raise x
result = Log_result()
result.read(self._iprot)
self._iprot.readMessageEnd()
if result.success != None:
return result.success
- raise Exception("Log failed: unknown result");
+ raise TApplicationException(TApplicationException.MISSING_RESULT, "Log failed: unknown result");
class Processor(fb303.FacebookService.Processor, Iface, TProcessor):
@@ -46,9 +66,16 @@ def __init__(self, handler):
self._processMap["Log"] = Processor.process_Log
def process(self, iprot, oprot):
- (name, type, seqid) = iprot.readMessageBegin()
+ (name, type, seqid) = iprot.readMessageBegin()
if name not in self._processMap:
- print 'Unknown function %s' % (name)
+ iprot.skip(TType.STRUCT)
+ iprot.readMessageEnd()
+ x = TApplicationException(TApplicationException.UNKNOWN_METHOD, 'Unknown function %s' % (name))
+ oprot.writeMessageBegin(name, TMessageType.EXCEPTION, seqid)
+ x.write(oprot)
+ oprot.writeMessageEnd()
+ oprot.trans.flush()
+ return
else:
self._processMap[name](self, seqid, iprot, oprot)
return True
@@ -68,14 +95,23 @@ def process_Log(self, seqid, iprot, oprot):
# HELPER FUNCTIONS AND STRUCTURES
class Log_args:
+ """
+ Attributes:
+ - messages
+ """
+
+ thrift_spec = (
+ None, # 0
+ (1, TType.LIST, 'messages', (TType.STRUCT,(LogEntry, LogEntry.thrift_spec)), None, ), # 1
+ )
- def __init__(self, d=None):
- self.messages = None
- if isinstance(d, dict):
- if 'messages' in d:
- self.messages = d['messages']
+ def __init__(self, messages=None,):
+ self.messages = messages
def read(self, iprot):
+ if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
+ fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
+ return
iprot.readStructBegin()
while True:
(fname, ftype, fid) = iprot.readFieldBegin()
@@ -98,6 +134,9 @@ def read(self, iprot):
iprot.readStructEnd()
def write(self, oprot):
+ if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
+ oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
+ return
oprot.writeStructBegin('Log_args')
if self.messages != None:
oprot.writeFieldBegin('messages', TType.LIST, 1)
@@ -109,21 +148,34 @@ def write(self, oprot):
oprot.writeFieldStop()
oprot.writeStructEnd()
- def __str__(self):
- return str(self.__dict__)
+ def __repr__(self):
+ L = ['%s=%r' % (key, value)
+ for key, value in self.__dict__.iteritems()]
+ return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
+
+ def __eq__(self, other):
+ return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
- def __repr__(self):
- return repr(self.__dict__)
+ def __ne__(self, other):
+ return not (self == other)
class Log_result:
+ """
+ Attributes:
+ - success
+ """
- def __init__(self, d=None):
- self.success = None
- if isinstance(d, dict):
- if 'success' in d:
- self.success = d['success']
+ thrift_spec = (
+ (0, TType.I32, 'success', None, None, ), # 0
+ )
+
+ def __init__(self, success=None,):
+ self.success = success
def read(self, iprot):
+ if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
+ fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
+ return
iprot.readStructBegin()
while True:
(fname, ftype, fid) = iprot.readFieldBegin()
@@ -140,6 +192,9 @@ def read(self, iprot):
iprot.readStructEnd()
def write(self, oprot):
+ if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
+ oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
+ return
oprot.writeStructBegin('Log_result')
if self.success != None:
oprot.writeFieldBegin('success', TType.I32, 0)
@@ -148,10 +203,15 @@ def write(self, oprot):
oprot.writeFieldStop()
oprot.writeStructEnd()
- def __str__(self):
- return str(self.__dict__)
+ def __repr__(self):
+ L = ['%s=%r' % (key, value)
+ for key, value in self.__dict__.iteritems()]
+ return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
+
+ def __eq__(self, other):
+ return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
- def __repr__(self):
- return repr(self.__dict__)
+ def __ne__(self, other):
+ return not (self == other)
View
@@ -4,26 +4,53 @@
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#
-from thrift.protocol.TProtocol import *
+from thrift.Thrift import *
import fb303.ttypes
+from thrift.transport import TTransport
+from thrift.protocol import TBinaryProtocol
+try:
+ from thrift.protocol import fastbinary
+except:
+ fastbinary = None
+
+
class ResultCode:
OK = 0
TRY_LATER = 1
+ _VALUES_TO_NAMES = {
+ 0: "OK",
+ 1: "TRY_LATER",
+ }
+
+ _NAMES_TO_VALUES = {
+ "OK": 0,
+ "TRY_LATER": 1,
+ }
+
class LogEntry:
+ """
+ Attributes:
+ - category
+ - message
+ """
+
+ thrift_spec = (
+ None, # 0
+ (1, TType.STRING, 'category', None, None, ), # 1
+ (2, TType.STRING, 'message', None, None, ), # 2
+ )
- def __init__(self, d=None):
- self.category = None
- self.message = None
- if isinstance(d, dict):
- if 'category' in d:
- self.category = d['category']
- if 'message' in d:
- self.message = d['message']
+ def __init__(self, category=None, message=None,):
+ self.category = category
+ self.message = message
def read(self, iprot):
+ if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
+ fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
+ return
iprot.readStructBegin()
while True:
(fname, ftype, fid) = iprot.readFieldBegin()
@@ -45,6 +72,9 @@ def read(self, iprot):
iprot.readStructEnd()
def write(self, oprot):
+ if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
+ oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
+ return
oprot.writeStructBegin('LogEntry')
if self.category != None:
oprot.writeFieldBegin('category', TType.STRING, 1)
@@ -57,9 +87,14 @@ def write(self, oprot):
oprot.writeFieldStop()
oprot.writeStructEnd()
- def __str__(self):
- return str(self.__dict__)
+ def __repr__(self):
+ L = ['%s=%r' % (key, value)
+ for key, value in self.__dict__.iteritems()]
+ return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
+
+ def __eq__(self, other):
+ return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
- def __repr__(self):
- return repr(self.__dict__)
+ def __ne__(self, other):
+ return not (self == other)

0 comments on commit 9f37699

Please sign in to comment.