1 """
2 Variables and classes that are common for some packetloaders
3 """
4
5 from struct import Struct
6 from lacewing.baseloader import _BaseLoader
7 from lacewing.bytereader import ByteReader
8
9 CONNECT, SET_NAME, JOIN_CHANNEL, LEAVE_CHANNEL, CHANNEL_LIST = xrange(5)
10
11 INT_DATA = Struct('<I')
12
13 DATA_TYPES = [
14 'String',
15 'Number',
16 'Binary '
17 ]
18
20 if isinstance(value, str):
21 return 'String'
22 elif isinstance(value, int):
23 return 'Number'
24 elif isinstance(value, ByteReader):
25 return 'Binary'
26 else:
27 raise Exception('could not detect message type')
28
30 """
31 Mixin for doing various binary read/write
32 operations on a message
33 """
35 """
36 Sets the kind of data this packet contains
37
38 @param typeName: See L{DATA_TYPES} for possible values
39 """
40 self.settings['variant'] = DATA_TYPES.index(typeName)
41
43 """
44 Gets the kind of data this packet contains
45
46 @return: See L{DATA_TYPES} for possible values
47 """
48 return DATA_TYPES[self.settings['variant']]
49
50 import json
51
53 isObject = True
56
58 return json.dumps(self.value)
59
61 isObject = False
70
72 dataType = self.getDataType()
73 if dataType == 'Number':
74 return INT_DATA.pack(self.value)
75 elif dataType in ('Binary', 'String'):
76 return str(self.value)
77