23 from math
import frexp, ldexp
24 from atlas
import Object, Messages
30 float_pos_pos_type = 3
31 float_pos_neg_type = 4
32 float_neg_pos_type = 5
33 float_neg_neg_type = 6
34 float_special_type = 7
38 variable_string_type = 11
39 variable_list_type = 12
40 variable_map_type = 13
44 gen = GenerateBinary1()
45 return gen.encode(obj)
47 def encode_pos_int(value):
51 if value>0x7F: v = v | 0x80
54 if not value:
return res
65 def encode(self, value):
68 def decode_pos_int(self, input):
72 value = value + (ord(ch) & ~0x80) * multiplier
73 multiplier = multiplier * 128
76 def encode_name(self, value):
77 return encode_pos_int(len(value)) + value
79 def encode_string(self, value):
80 return encode_pos_int(string_type) + \
81 encode_pos_int(len(value)) + \
84 def encode_int(self, value):
86 return encode_pos_int(int_pos_type) +\
89 return encode_pos_int(int_neg_type) +\
90 encode_pos_int(-value)
92 def encode_float(self, value):
93 mant, exp = frexp(value)
94 mant = int(mant *2**53)
98 type_str = encode_pos_int(float_pos_pos_type)
100 type_str = encode_pos_int(float_pos_neg_type)
105 type_str = encode_pos_int(float_neg_pos_type)
107 type_str = encode_pos_int(float_neg_neg_type)
110 encode_pos_int(mant) + \
113 def encode_attribute(self, name, value):
116 str_value = self.
encode(value)
117 return str_value[0] + str_name + str_value[1:]
119 def encode_map(self, obj):
120 str_list = [encode_pos_int(map_type) + encode_pos_int(len(obj))]
121 for name, value
in list(obj.items()):
123 return string.join(str_list,
"")
125 def encode_list(self, lst):
126 str_list = [encode_pos_int(list_type) + encode_pos_int(len(lst))]
128 str_list.append(self.
encode(item))
129 return string.join(str_list,
"")
136 def __init__(self, data_flag, value, decoder=None):
147 """uses tree that start from root_obj, current route to leave 148 is kept in obj_stack""" 155 def parse_stream(self, msg):
160 __call__=parse_stream
166 if self.
stack[-1].data_flag:
167 self.
stack[-1].decoder(ch)
169 getattr(self, self.
type2init[ch].__name__)()
171 if self.
stack[-2].is_mapping:
174 self.
stack[-1].is_name = 1
177 obj=self.
stack[0].value.pop(0)
180 def start_value(self, initial_value):
184 def value_done(self):
187 self.
stack[-1].name = c.value
189 obj = self.
stack[-1].value
191 if not isinstance(obj, Object):
194 setattr(obj, c.name, c.value)
196 if type(obj)!=ListType:
199 self.
stack[-1].decoder()
201 def decode_int_value(self, ch):
204 c.value = c.value + (ch & ~0x80) * c.multiplier
205 c.multiplier = c.multiplier * 128
208 c.value = int(c.value)
209 except OverflowError:
214 def init_int_pos(self):
216 self.
stack[-1].multiplier = 1
217 def init_int_neg(self):
219 self.
stack[-1].multiplier = -1
220 def decode_int(self, ch):
224 def init_float(self, multiplier1, multiplier2):
226 self.
stack[-1].multiplier = multiplier1
227 self.
stack[-1].multiplier2 = multiplier2
228 def init_float_pos_pos(self):
230 def init_float_pos_neg(self):
232 def init_float_neg_pos(self):
234 def init_float_neg_neg(self):
236 def decode_float_mantissa(self, ch):
239 self.
stack[-1].value = 0
240 self.
stack[-1].multiplier = self.
stack[-1].multiplier2
242 def decode_float_exponent(self, ch):
247 init_length = init_int_pos
249 def decode_string_length(self, ch):
251 self.
stack[-1].remaining = int(self.
stack[-1].value)
252 self.
stack[-1].value =
"" 253 if self.
stack[-1].remaining:
257 def decode_string_value(self, ch):
258 self.
stack[-1].value = self.
stack[-1].value + ch
259 self.
stack[-1].remaining = self.
stack[-1].remaining - 1
260 if not self.
stack[-1].remaining:
264 def init_collection(self, initial_value):
265 self.
stack[-1].remaining = int(self.
stack[-1].value)
266 self.
stack[-1].value = initial_value
267 if self.
stack[-1].remaining:
268 self.
stack[-1].data_flag = 0
272 def decode_collection_value(self):
273 self.
stack[-1].remaining = self.
stack[-1].remaining - 1
274 if not self.
stack[-1].remaining:
278 def decode_list_length(self, ch):
282 def decode_map_length(self, ch):
284 self.
stack[-1].is_mapping = 1
287 type2init = {encode_pos_int(int_pos_type): init_int_pos,
288 encode_pos_int(int_neg_type): init_int_neg,
289 encode_pos_int(float_pos_pos_type): init_float_pos_pos,
290 encode_pos_int(float_pos_neg_type): init_float_pos_neg,
291 encode_pos_int(float_neg_pos_type): init_float_neg_pos,
292 encode_pos_int(float_neg_neg_type): init_float_neg_neg,
293 encode_pos_int(string_type): init_length,
294 encode_pos_int(list_type): init_length,
295 encode_pos_int(map_type): init_length,
296 encode_pos_int(variable_string_type): init_length,
297 encode_pos_int(variable_list_type): init_length,
298 encode_pos_int(variable_map_type): init_length}
300 type2decoder = {encode_pos_int(int_pos_type): decode_int,
301 encode_pos_int(int_neg_type): decode_int,
302 encode_pos_int(float_pos_pos_type): decode_float_mantissa,
303 encode_pos_int(float_pos_neg_type): decode_float_mantissa,
304 encode_pos_int(float_neg_pos_type): decode_float_mantissa,
305 encode_pos_int(float_neg_neg_type): decode_float_mantissa,
306 encode_pos_int(string_type): decode_string_length,
307 encode_pos_int(list_type): decode_list_length,
308 encode_pos_int(map_type): decode_map_length,
309 encode_pos_int(variable_string_type): decode_string_length,
310 encode_pos_int(variable_list_type): decode_list_length,
311 encode_pos_int(variable_map_type): decode_map_length}
315 return binary1_msg_parser
def decode_float_exponent(self, ch)
def decode_string_length(self, ch)
def encode_list(self, lst)
def encode_map(self, obj)
def start_value(self, initial_value)
def decode_collection_value(self)
def encode_name(self, value)
def encode_int(self, value)
def decode_int_value(self, ch)
def init_float(self, multiplier1, multiplier2)
def init_collection(self, initial_value)
def encode_string(self, value)
def encode_float(self, value)
def decode_string_value(self, ch)
def encode_attribute(self, name, value)