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