PageRenderTime 86ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/service/lib/py/thrift/Thrift.py

#
Python | 123 lines | 87 code | 15 blank | 21 comment | 18 complexity | 1bfed7cf216df0798a980a5145900d10 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. class TType:
  20. STOP = 0
  21. VOID = 1
  22. BOOL = 2
  23. BYTE = 3
  24. I08 = 3
  25. DOUBLE = 4
  26. I16 = 6
  27. I32 = 8
  28. I64 = 10
  29. STRING = 11
  30. UTF7 = 11
  31. STRUCT = 12
  32. MAP = 13
  33. SET = 14
  34. LIST = 15
  35. UTF8 = 16
  36. UTF16 = 17
  37. class TMessageType:
  38. CALL = 1
  39. REPLY = 2
  40. EXCEPTION = 3
  41. ONEWAY = 4
  42. class TProcessor:
  43. """Base class for procsessor, which works on two streams."""
  44. def process(iprot, oprot):
  45. pass
  46. class TException(Exception):
  47. """Base class for all thrift exceptions."""
  48. def __init__(self, message=None):
  49. Exception.__init__(self, message)
  50. self.message = message
  51. class TApplicationException(TException):
  52. """Application level thrift exceptions."""
  53. UNKNOWN = 0
  54. UNKNOWN_METHOD = 1
  55. INVALID_MESSAGE_TYPE = 2
  56. WRONG_METHOD_NAME = 3
  57. BAD_SEQUENCE_ID = 4
  58. MISSING_RESULT = 5
  59. def __init__(self, type=UNKNOWN, message=None):
  60. TException.__init__(self, message)
  61. self.type = type
  62. def __str__(self):
  63. if self.message:
  64. return self.message
  65. elif self.type == UNKNOWN_METHOD:
  66. return 'Unknown method'
  67. elif self.type == INVALID_MESSAGE_TYPE:
  68. return 'Invalid message type'
  69. elif self.type == WRONG_METHOD_NAME:
  70. return 'Wrong method name'
  71. elif self.type == BAD_SEQUENCE_ID:
  72. return 'Bad sequence ID'
  73. elif self.type == MISSING_RESULT:
  74. return 'Missing result'
  75. else:
  76. return 'Default (unknown) TApplicationException'
  77. def read(self, iprot):
  78. iprot.readStructBegin()
  79. while True:
  80. (fname, ftype, fid) = iprot.readFieldBegin()
  81. if ftype == TType.STOP:
  82. break
  83. if fid == 1:
  84. if ftype == TType.STRING:
  85. self.message = iprot.readString();
  86. else:
  87. iprot.skip(ftype)
  88. elif fid == 2:
  89. if ftype == TType.I32:
  90. self.type = iprot.readI32();
  91. else:
  92. iprot.skip(ftype)
  93. else:
  94. iprot.skip(ftype)
  95. iprot.readFieldEnd()
  96. iprot.readStructEnd()
  97. def write(self, oprot):
  98. oprot.writeStructBegin('TApplicationException')
  99. if self.message != None:
  100. oprot.writeFieldBegin('message', TType.STRING, 1)
  101. oprot.writeString(self.message)
  102. oprot.writeFieldEnd()
  103. if self.type != None:
  104. oprot.writeFieldBegin('type', TType.I32, 2)
  105. oprot.writeI32(self.type)
  106. oprot.writeFieldEnd()
  107. oprot.writeFieldStop()
  108. oprot.writeStructEnd()