Completed
Branch master (477316)
by Michael
08:56
created

thrift.Thrift.TApplicationException.read()   B

Complexity

Conditions 7

Size

Total Lines 20
Code Lines 17

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 17
dl 20
loc 20
rs 8
c 0
b 0
f 0
cc 7
nop 2
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
20
import sys
21
22
23
class TType:
24
  STOP   = 0
25
  VOID   = 1
26
  BOOL   = 2
27
  BYTE   = 3
28
  I08    = 3
29
  DOUBLE = 4
30
  I16    = 6
31
  I32    = 8
32
  I64    = 10
33
  STRING = 11
34
  UTF7   = 11
35
  STRUCT = 12
36
  MAP    = 13
37
  SET    = 14
38
  LIST   = 15
39
  UTF8   = 16
40
  UTF16  = 17
41
42
  _VALUES_TO_NAMES = ('STOP',
43
                      'VOID',
44
                      'BOOL',
45
                      'BYTE',
46
                      'DOUBLE',
47
                      None,
48
                      'I16',
49
                      None,
50
                      'I32',
51
                      None,
52
                     'I64',
53
                     'STRING',
54
                     'STRUCT',
55
                     'MAP',
56
                     'SET',
57
                     'LIST',
58
                     'UTF8',
59
                     'UTF16')
60
61
62
class TMessageType:
63
  CALL = 1
64
  REPLY = 2
65
  EXCEPTION = 3
66
  ONEWAY = 4
67
68
69
class TProcessor:
70
  """Base class for procsessor, which works on two streams."""
71
72
  def process(iprot, oprot):
73
    pass
74
75
76
class TException(Exception):
77
  """Base class for all thrift exceptions."""
78
79
  # BaseException.message is deprecated in Python v[2.6,3.0)
80
  if (2, 6, 0) <= sys.version_info < (3, 0):
81
    def _get_message(self):
82
      return self._message
83
84
    def _set_message(self, message):
85
      self._message = message
86
    message = property(_get_message, _set_message)
87
88
  def __init__(self, message=None):
89
    Exception.__init__(self, message)
90
    self.message = message
91
92
93
class TApplicationException(TException):
94
  """Application level thrift exceptions."""
95
96
  UNKNOWN = 0
97
  UNKNOWN_METHOD = 1
98
  INVALID_MESSAGE_TYPE = 2
99
  WRONG_METHOD_NAME = 3
100
  BAD_SEQUENCE_ID = 4
101
  MISSING_RESULT = 5
102
  INTERNAL_ERROR = 6
103
  PROTOCOL_ERROR = 7
104
105
  def __init__(self, type=UNKNOWN, message=None):
106
    TException.__init__(self, message)
107
    self.type = type
108
109
  def __str__(self):
110
    if self.message:
111
      return self.message
112
    elif self.type == self.UNKNOWN_METHOD:
113
      return 'Unknown method'
114
    elif self.type == self.INVALID_MESSAGE_TYPE:
115
      return 'Invalid message type'
116
    elif self.type == self.WRONG_METHOD_NAME:
117
      return 'Wrong method name'
118
    elif self.type == self.BAD_SEQUENCE_ID:
119
      return 'Bad sequence ID'
120
    elif self.type == self.MISSING_RESULT:
121
      return 'Missing result'
122
    else:
123
      return 'Default (unknown) TApplicationException'
124
125
  def read(self, iprot):
126
    iprot.readStructBegin()
127
    while True:
128
      (fname, ftype, fid) = iprot.readFieldBegin()
129
      if ftype == TType.STOP:
130
        break
131
      if fid == 1:
132
        if ftype == TType.STRING:
133
          self.message = iprot.readString()
134
        else:
135
          iprot.skip(ftype)
136
      elif fid == 2:
137
        if ftype == TType.I32:
138
          self.type = iprot.readI32()
139
        else:
140
          iprot.skip(ftype)
141
      else:
142
        iprot.skip(ftype)
143
      iprot.readFieldEnd()
144
    iprot.readStructEnd()
145
146
  def write(self, oprot):
147
    oprot.writeStructBegin('TApplicationException')
148
    if self.message is not None:
149
      oprot.writeFieldBegin('message', TType.STRING, 1)
150
      oprot.writeString(self.message)
151
      oprot.writeFieldEnd()
152
    if self.type is not None:
153
      oprot.writeFieldBegin('type', TType.I32, 2)
154
      oprot.writeI32(self.type)
155
      oprot.writeFieldEnd()
156
    oprot.writeFieldStop()
157
    oprot.writeStructEnd()
158