summaryrefslogtreecommitdiffstats
path: root/Mac/Lib/toolbox/aetools.py
blob: 745bce78abbf25ab7e51f44970396d6277db8ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import struct
import types
import AE
import MacOS
import StringIO

AEDescType = type(AE.AECreateDesc('TEXT', ''))

def pack(x):
	if x == None:
		return AE.AECreateDesc('null', '')
	t = type(x)
	if t == AEDescType:
		return x
	if t == types.IntType:
		return AE.AECreateDesc('long', struct.pack('l', x))
	if t == types.FloatType:
		return AE.AECreateDesc('exte', struct.pack('d', x)[2:])
	if t == types.StringType:
		return AE.AECreateDesc('TEXT', x)
	if t == types.ListType:
		list = AE.AECreateList('', 0)
		for item in x:
			list.AEPutDesc(0, pack(item))
		return list
	if t == types.TupleType:
		t, d = x
		return AE.AECreateDesc(t, d)
	if t == types.DictionaryType:
		record = AE.AECreateList('', 1)
		for key, value in x.items():
			record.AEPutKeyDesc(key, pack(value))
	if t == types.InstanceType and hasattr(x, '__aepack__'):
		return x.__aepack__()
	return AE.AECreateDesc('TEXT', repr(x)) # Copout

def unpack(desc):
	t = desc.type
	if t == 'TEXT':
		return desc.data
	if t == 'fals':
		return 0
	if t == 'true':
		return 1
	if t == 'long':
		return struct.unpack('l', desc.data)[0]
	if t == 'shor':
		return struct.unpack('h', desc.data)[0]
	if t == 'sing':
		return struct.unpack('f', desc.data)[0]
	if t == 'exte':
		data = desc.data
		return struct.unpack('d', data[:2] + data)[0]
	if t in ('doub', 'comp', 'magn'):
		return unpack(desc.AECoerceDesc('exte'))
	if t == 'enum':
		return ('enum', desc.data)
	if t == 'null':
		return None
	if t == 'list':
		l = []
		for i in range(desc.AECountItems()):
			keyword, item = desc.AEGetNthDesc(i+1, '****')
			l.append(unpack(item))
		return l
	if t == 'reco':
		d = {}
		for i in range(desc.AECountItems()):
			keyword, item = desc.AEGetNthDesc(i+1, '****')
			d[keyword] = unpack(item)
		return d
	if t == 'obj ':
		return unpackobject(desc.data)
	return desc.type, desc.data # Copout

class Object:
	def __init__(self, dict = {}):
		self.dict = dict
		for key, value in dict.items():
			self.dict[key] = value
	def __repr__(self):
		return "Object(%s)" % `self.dict`
	def __str__(self):
		want = self.dict['want']
		form = self.dict['form']
		seld = self.dict['seld']
		s = "%s %s %s" % (nicewant(want), niceform(form), niceseld(seld))
		fr   = self.dict['from']
		if fr:
			s = s + " of " + str(fr)
		return s
	def __aepack__(self):
		f = StringIO.StringIO()
		putlong(f, len(self.dict))
		putlong(f, 0)
		for key, value in self.dict.items():
			putcode(f, key)
			desc = pack(value)
			putcode(f, desc.type)
			data = desc.data
			putlong(f, len(data))
			f.write(data)
		return AE.AECreateDesc('obj ', f.getvalue())

def nicewant(want):
	if type(want) == types.TupleType and len(want) == 2:
		return reallynicewant(want)
	else:
		return `want`

def reallynicewant((t, w)):
	if t != 'type': return `t, w`
	# These should be taken from the "elements" of the 'aete' resource
	if w == 'cins': return 'insertion point'
	if w == 'cha ': return 'character'
	if w == 'word': return 'word'
	if w == 'para': return 'paragraph'
	if w == 'ccel': return 'cell'
	if w == 'ccol': return 'column'
	if w == 'crow': return 'row'
	if w == 'crng': return 'range'
	if w == 'wind': return 'window'
	if w == 'docu': return 'document'
	return `w`

def niceform(form):
	if type(form) == types.TupleType and len(form) == 2:
		return reallyniceform(form)
	else:
		return `form`

def reallyniceform((t, f)):
	if t <> 'enum': return `t, f`
	if f == 'indx': return ''
	if f == 'name': return ''
	if f == 'rele': return ''
	return `f`

def niceseld(seld):
	if type(seld) == types.TupleType and len(seld) == 2:
		return reallyniceseld(seld)
	else:
		return `seld`

def reallyniceseld((t, s)):
	if t == 'long': return `s`
	if t == 'TEXT': return `s`
	if t == 'enum':
		if s == 'next': return 'after'
		if s == 'prev': return 'before'
	return `t, s`

def unpackobject(data):
	f = StringIO.StringIO(data)
	nkey = getlong(f)
	dumm = getlong(f)
	dict = {}
	for i in range(nkey):
		keyw = getcode(f)
		type = getcode(f)
		size = getlong(f)
		if size:
			data = f.read(size)
		else:
			data = ''
		desc = AE.AECreateDesc(type, data)
		dict[keyw] = unpack(desc)
	return Object(dict)


# --- get various data types from a "file"

def getword(f, *args):
	getalgn(f)
	s = f.read(2)
	if len(s) < 2:
		raise EOFError, 'in getword' + str(args)
	return (ord(s[0])<<8) | ord(s[1])

def getlong(f, *args):
	getalgn(f)
	s = f.read(4)
	if len(s) < 4:
		raise EOFError, 'in getlong' + str(args)
	return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])

def getcode(f, *args):
	getalgn(f)
	s = f.read(4)
	if len(s) < 4:
		raise EOFError, 'in getcode' + str(args)
	return s

def getpstr(f, *args):
	c = f.read(1)
	if len(c) < 1:
		raise EOFError, 'in getpstr[1]' + str(args)
	nbytes = ord(c)
	if nbytes == 0: return ''
	s = f.read(nbytes)
	if len(s) < nbytes:
		raise EOFError, 'in getpstr[2]' + str(args)
	return s

def getalgn(f):
	if f.tell() & 1:
		c = f.read(1)
		##if c <> '\0':
		##	print 'align:', `c`

# ---- end get routines


# ---- put various data types to a "file"

def putlong(f, value):
	putalgn(f)
	f.write(chr((value>>24)&0xff))
	f.write(chr((value>>16)&0xff))
	f.write(chr((value>>8)&0xff))
	f.write(chr(value&0xff))

def putword(f, value):
	putalgn(f)
	f.write(chr((value>>8)&0xff))
	f.write(chr(value&0xff))

def putcode(f, value):
	if type(value) != types.StringType or len(value) != 4:
		raise TypeError, "ostype must be 4-char string"
	putalgn(f)
	f.write(value)

def putpstr(f, value):
	if type(value) != types.StringType or len(value) > 255:
		raise TypeError, "pstr must be string <= 255 chars"
	f.write(chr(len(value)) + value)

def putalgn(f):
	if f.tell() & 1:
		f.write('\0')

# ---- end put routines


aekeywords = [
	'tran',
	'rtid',
	'evcl',
	'evid',
	'addr',
	'optk',
	'timo',
	'inte',	# this attribute is read only - will be set in AESend
	'esrc',	# this attribute is read only
	'miss',	# this attribute is read only
	'from'	# new in 1.0.1
]

def missed(ae):
	try:
		desc = ae.AEGetAttributeDesc('miss', 'keyw')
	except AE.Error, msg:
		return None
	return desc.data

def unpackevent(ae):
	parameters = {}
	while 1:
		key = missed(ae)
		if not key: break
		parameters[key] = unpack(ae.AEGetParamDesc(key, '****'))
	attributes = {}
	for key in aekeywords:
		try:
			desc = ae.AEGetAttributeDesc(key, '****')
		except (AE.Error, MacOS.Error), msg:
			if msg[0] != -1701:
				raise sys.exc_type, sys.exc_value
			continue
		attributes[key] = unpack(desc)
	return parameters, attributes

def packevent(ae, parameters = {}, attributes = {}):
	for key, value in parameters.items():
		ae.AEPutParamDesc(key, pack(value))
	for key, value in attributes.items():
		ae.AEPutAttributeDesc(key, pack(value))

def test():
	target = AE.AECreateDesc('sign', 'KAHL')
	ae = AE.AECreateAppleEvent('aevt', 'oapp', target, -1, 0)
	print unpackevent(ae)

if __name__ == '__main__':
	test()