Thrift.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. import sys
  20. class TType:
  21. STOP = 0
  22. VOID = 1
  23. BOOL = 2
  24. BYTE = 3
  25. I08 = 3
  26. DOUBLE = 4
  27. I16 = 6
  28. I32 = 8
  29. I64 = 10
  30. STRING = 11
  31. UTF7 = 11
  32. STRUCT = 12
  33. MAP = 13
  34. SET = 14
  35. LIST = 15
  36. UTF8 = 16
  37. UTF16 = 17
  38. _VALUES_TO_NAMES = ('STOP',
  39. 'VOID',
  40. 'BOOL',
  41. 'BYTE',
  42. 'DOUBLE',
  43. None,
  44. 'I16',
  45. None,
  46. 'I32',
  47. None,
  48. 'I64',
  49. 'STRING',
  50. 'STRUCT',
  51. 'MAP',
  52. 'SET',
  53. 'LIST',
  54. 'UTF8',
  55. 'UTF16')
  56. class TMessageType:
  57. CALL = 1
  58. REPLY = 2
  59. EXCEPTION = 3
  60. ONEWAY = 4
  61. class TProcessor:
  62. """Base class for procsessor, which works on two streams."""
  63. def process(iprot, oprot):
  64. pass
  65. class TException(Exception):
  66. """Base class for all thrift exceptions."""
  67. # BaseException.message is deprecated in Python v[2.6,3.0)
  68. if (2, 6, 0) <= sys.version_info < (3, 0):
  69. def _get_message(self):
  70. return self._message
  71. def _set_message(self, message):
  72. self._message = message
  73. message = property(_get_message, _set_message)
  74. def __init__(self, message=None):
  75. Exception.__init__(self, message)
  76. self.message = message
  77. class TApplicationException(TException):
  78. """Application level thrift exceptions."""
  79. UNKNOWN = 0
  80. UNKNOWN_METHOD = 1
  81. INVALID_MESSAGE_TYPE = 2
  82. WRONG_METHOD_NAME = 3
  83. BAD_SEQUENCE_ID = 4
  84. MISSING_RESULT = 5
  85. INTERNAL_ERROR = 6
  86. PROTOCOL_ERROR = 7
  87. INVALID_TRANSFORM = 8
  88. INVALID_PROTOCOL = 9
  89. UNSUPPORTED_CLIENT_TYPE = 10
  90. def __init__(self, type=UNKNOWN, message=None):
  91. TException.__init__(self, message)
  92. self.type = type
  93. def __str__(self):
  94. if self.message:
  95. return self.message
  96. elif self.type == self.UNKNOWN_METHOD:
  97. return 'Unknown method'
  98. elif self.type == self.INVALID_MESSAGE_TYPE:
  99. return 'Invalid message type'
  100. elif self.type == self.WRONG_METHOD_NAME:
  101. return 'Wrong method name'
  102. elif self.type == self.BAD_SEQUENCE_ID:
  103. return 'Bad sequence ID'
  104. elif self.type == self.MISSING_RESULT:
  105. return 'Missing result'
  106. elif self.type == self.INTERNAL_ERROR:
  107. return 'Internal error'
  108. elif self.type == self.PROTOCOL_ERROR:
  109. return 'Protocol error'
  110. elif self.type == self.INVALID_TRANSFORM:
  111. return 'Invalid transform'
  112. elif self.type == self.INVALID_PROTOCOL:
  113. return 'Invalid protocol'
  114. elif self.type == self.UNSUPPORTED_CLIENT_TYPE:
  115. return 'Unsupported client type'
  116. else:
  117. return 'Default (unknown) TApplicationException'
  118. def read(self, iprot):
  119. iprot.readStructBegin()
  120. while True:
  121. (fname, ftype, fid) = iprot.readFieldBegin()
  122. if ftype == TType.STOP:
  123. break
  124. if fid == 1:
  125. if ftype == TType.STRING:
  126. self.message = iprot.readString()
  127. else:
  128. iprot.skip(ftype)
  129. elif fid == 2:
  130. if ftype == TType.I32:
  131. self.type = iprot.readI32()
  132. else:
  133. iprot.skip(ftype)
  134. else:
  135. iprot.skip(ftype)
  136. iprot.readFieldEnd()
  137. iprot.readStructEnd()
  138. def write(self, oprot):
  139. oprot.writeStructBegin('TApplicationException')
  140. if self.message is not None:
  141. oprot.writeFieldBegin('message', TType.STRING, 1)
  142. oprot.writeString(self.message)
  143. oprot.writeFieldEnd()
  144. if self.type is not None:
  145. oprot.writeFieldBegin('type', TType.I32, 2)
  146. oprot.writeI32(self.type)
  147. oprot.writeFieldEnd()
  148. oprot.writeFieldStop()
  149. oprot.writeStructEnd()