2525# These ones seem to be missing from AppleEvents
2626# (they're in AERegistry.h)
2727
28- #typeColorTable = 'clrt'
29- #typeDrawingArea = 'cdrw'
30- #typePixelMap = 'cpix'
31- #typePixelMapMinus = 'tpmm'
32- #typeRotation = 'trot'
33- #typeTextStyles = 'tsty'
34- #typeStyledText = 'STXT'
35- #typeAEText = 'tTXT'
36- #typeEnumeration = 'enum'
37-
28+ #typeColorTable = b'clrt'
29+ #typeDrawingArea = b'cdrw'
30+ #typePixelMap = b'cpix'
31+ #typePixelMapMinus = b'tpmm'
32+ #typeRotation = b'trot'
33+ #typeTextStyles = b'tsty'
34+ #typeStyledText = b'STXT'
35+ #typeAEText = b'tTXT'
36+ #typeEnumeration = b'enum'
37+
38+ def b2i (byte_string ):
39+ result = 0
40+ for byte in byte_string :
41+ result <<= 8
42+ result += byte
43+ return result
3844#
3945# Some AE types are immedeately coerced into something
4046# we like better (and which is equivalent)
4147#
4248unpacker_coercions = {
43- typeComp : typeFloat ,
44- typeColorTable : typeAEList ,
45- typeDrawingArea : typeAERecord ,
46- typeFixed : typeFloat ,
47- typeExtended : typeFloat ,
48- typePixelMap : typeAERecord ,
49- typeRotation : typeAERecord ,
50- typeStyledText : typeAERecord ,
51- typeTextStyles : typeAERecord ,
49+ b2i ( typeComp ) : typeFloat ,
50+ b2i ( typeColorTable ) : typeAEList ,
51+ b2i ( typeDrawingArea ) : typeAERecord ,
52+ b2i ( typeFixed ) : typeFloat ,
53+ b2i ( typeExtended ) : typeFloat ,
54+ b2i ( typePixelMap ) : typeAERecord ,
55+ b2i ( typeRotation ) : typeAERecord ,
56+ b2i ( typeStyledText ) : typeAERecord ,
57+ b2i ( typeTextStyles ) : typeAERecord ,
5258};
5359
5460#
@@ -72,33 +78,35 @@ def pack(x, forcetype = None):
7278 """Pack a python object into an AE descriptor"""
7379
7480 if forcetype :
75- if isinstance (x , str ):
81+ if isinstance (x , bytes ):
7682 return AE .AECreateDesc (forcetype , x )
7783 else :
7884 return pack (x ).AECoerceDesc (forcetype )
7985
8086 if x == None :
81- return AE .AECreateDesc ('null' , '' )
87+ return AE .AECreateDesc (b 'null' , '' )
8288
8389 if isinstance (x , AEDescType ):
8490 return x
8591 if isinstance (x , FSSType ):
86- return AE .AECreateDesc ('fss ' , x .data )
92+ return AE .AECreateDesc (b 'fss ' , x .data )
8793 if isinstance (x , FSRefType ):
88- return AE .AECreateDesc ('fsrf' , x .data )
94+ return AE .AECreateDesc (b 'fsrf' , x .data )
8995 if isinstance (x , AliasType ):
90- return AE .AECreateDesc ('alis' , x .data )
96+ return AE .AECreateDesc (b 'alis' , x .data )
9197 if isinstance (x , int ):
92- return AE .AECreateDesc ('long' , struct .pack ('l' , x ))
98+ return AE .AECreateDesc (b 'long' , struct .pack ('l' , x ))
9399 if isinstance (x , float ):
94- return AE .AECreateDesc ('doub' , struct .pack ('d' , x ))
100+ return AE .AECreateDesc (b'doub' , struct .pack ('d' , x ))
101+ if isinstance (x , (bytes , str8 )):
102+ return AE .AECreateDesc (b'TEXT' , x )
95103 if isinstance (x , str ):
96- return AE . AECreateDesc ( 'TEXT' , x )
97- if isinstance ( x , unicode ):
104+ # See http://developer.apple.com/documentation/Carbon/Reference/Apple_Event_Manager/Reference/reference.html#//apple_ref/doc/constant_group/typeUnicodeText
105+ # for the possible encodings.
98106 data = x .encode ('utf16' )
99107 if data [:2 ] == '\xfe \xff ' :
100108 data = data [2 :]
101- return AE .AECreateDesc ('utxt' , data )
109+ return AE .AECreateDesc (b 'utxt' , data )
102110 if isinstance (x , list ):
103111 lst = AE .AECreateList ('' , 0 )
104112 for item in x :
@@ -112,37 +120,37 @@ def pack(x, forcetype = None):
112120 return record
113121 if isinstance (x , type ) and issubclass (x , ObjectSpecifier ):
114122 # Note: we are getting a class object here, not an instance
115- return AE .AECreateDesc ('type' , x .want )
123+ return AE .AECreateDesc (b 'type' , x .want )
116124 if hasattr (x , '__aepack__' ):
117125 return x .__aepack__ ()
118126 if hasattr (x , 'which' ):
119- return AE .AECreateDesc ('TEXT' , x .which )
127+ return AE .AECreateDesc (b 'TEXT' , x .which )
120128 if hasattr (x , 'want' ):
121- return AE .AECreateDesc ('TEXT' , x .want )
122- return AE .AECreateDesc ('TEXT' , repr (x )) # Copout
129+ return AE .AECreateDesc (b 'TEXT' , x .want )
130+ return AE .AECreateDesc (b 'TEXT' , repr (x )) # Copout
123131
124132def unpack (desc , formodulename = "" ):
125133 """Unpack an AE descriptor to a python object"""
126134 t = desc .type
127135
128- if t in unpacker_coercions :
129- desc = desc .AECoerceDesc (unpacker_coercions [t ])
136+ if b2i ( t ) in unpacker_coercions :
137+ desc = desc .AECoerceDesc (unpacker_coercions [b2i ( t ) ])
130138 t = desc .type # This is a guess by Jack....
131139
132140 if t == typeAEList :
133141 l = []
134142 for i in range (desc .AECountItems ()):
135- keyword , item = desc .AEGetNthDesc (i + 1 , '****' )
143+ keyword , item = desc .AEGetNthDesc (i + 1 , b '****' )
136144 l .append (unpack (item , formodulename ))
137145 return l
138146 if t == typeAERecord :
139147 d = {}
140148 for i in range (desc .AECountItems ()):
141- keyword , item = desc .AEGetNthDesc (i + 1 , '****' )
142- d [keyword ] = unpack (item , formodulename )
149+ keyword , item = desc .AEGetNthDesc (i + 1 , b '****' )
150+ d [b2i ( keyword ) ] = unpack (item , formodulename )
143151 return d
144152 if t == typeAEText :
145- record = desc .AECoerceDesc ('reco' )
153+ record = desc .AECoerceDesc (b 'reco' )
146154 return mkaetext (unpack (record , formodulename ))
147155 if t == typeAlias :
148156 return Carbon .File .Alias (rawdata = desc .data )
@@ -170,7 +178,7 @@ def unpack(desc, formodulename=""):
170178 if t == typeFSRef :
171179 return Carbon .File .FSRef (rawdata = desc .data )
172180 if t == typeInsertionLoc :
173- record = desc .AECoerceDesc ('reco' )
181+ record = desc .AECoerceDesc (b 'reco' )
174182 return mkinsertionloc (unpack (record , formodulename ))
175183 # typeInteger equal to typeLongInteger
176184 if t == typeIntlText :
@@ -194,7 +202,7 @@ def unpack(desc, formodulename=""):
194202 v = 0x100000000 + v
195203 return v
196204 if t == typeObjectSpecifier :
197- record = desc .AECoerceDesc ('reco' )
205+ record = desc .AECoerceDesc (b 'reco' )
198206 # If we have been told the name of the module we are unpacking aedescs for,
199207 # we can attempt to create the right type of python object from that module.
200208 if formodulename :
@@ -234,14 +242,14 @@ def unpack(desc, formodulename=""):
234242 #
235243 # The following are special
236244 #
237- if t == 'rang' :
238- record = desc .AECoerceDesc ('reco' )
245+ if t == b 'rang' :
246+ record = desc .AECoerceDesc (b 'reco' )
239247 return mkrange (unpack (record , formodulename ))
240- if t == 'cmpd' :
241- record = desc .AECoerceDesc ('reco' )
248+ if t == b 'cmpd' :
249+ record = desc .AECoerceDesc (b 'reco' )
242250 return mkcomparison (unpack (record , formodulename ))
243- if t == 'logi' :
244- record = desc .AECoerceDesc ('reco' )
251+ if t == b 'logi' :
252+ record = desc .AECoerceDesc (b 'reco' )
245253 return mklogical (unpack (record , formodulename ))
246254 return mkunknown (desc .type , desc .data )
247255
@@ -297,39 +305,44 @@ def mkkeyword(keyword):
297305 return aetypes .Keyword (keyword )
298306
299307def mkrange (dict ):
300- return aetypes .Range (dict ['star' ], dict ['stop' ])
308+ return aetypes .Range (dict [b2i ( b 'star') ], dict [b2i ( b 'stop') ])
301309
302310def mkcomparison (dict ):
303- return aetypes .Comparison (dict ['obj1' ], dict ['relo' ].enum , dict ['obj2' ])
311+ return aetypes .Comparison (dict [b2i (b'obj1' )],
312+ dict [b2i (b'relo' )].enum ,
313+ dict [b2i (b'obj2' )])
304314
305315def mklogical (dict ):
306- return aetypes .Logical (dict ['logc' ], dict ['term' ])
316+ return aetypes .Logical (dict [b2i ( b 'logc') ], dict [b2i ( b 'term') ])
307317
308318def mkstyledtext (dict ):
309- return aetypes .StyledText (dict ['ksty' ], dict ['ktxt' ])
319+ return aetypes .StyledText (dict [b2i ( b 'ksty') ], dict [b2i ( b 'ktxt') ])
310320
311321def mkaetext (dict ):
312- return aetypes .AEText (dict [keyAEScriptTag ], dict [keyAEStyles ], dict [keyAEText ])
322+ return aetypes .AEText (dict [b2i (keyAEScriptTag )],
323+ dict [b2i (keyAEStyles )],
324+ dict [b2i (keyAEText )])
313325
314326def mkinsertionloc (dict ):
315- return aetypes .InsertionLoc (dict [keyAEObject ], dict [keyAEPosition ])
327+ return aetypes .InsertionLoc (dict [b2i (keyAEObject )],
328+ dict [b2i (keyAEPosition )])
316329
317330def mkobject (dict ):
318- want = dict ['want' ].type
319- form = dict ['form' ].enum
320- seld = dict ['seld' ]
321- fr = dict ['from' ]
322- if form in ('name' , 'indx' , 'rang' , 'test' ):
323- if want == 'text' : return aetypes .Text (seld , fr )
324- if want == 'cha ' : return aetypes .Character (seld , fr )
325- if want == 'cwor' : return aetypes .Word (seld , fr )
326- if want == 'clin' : return aetypes .Line (seld , fr )
327- if want == 'cpar' : return aetypes .Paragraph (seld , fr )
328- if want == 'cwin' : return aetypes .Window (seld , fr )
329- if want == 'docu' : return aetypes .Document (seld , fr )
330- if want == 'file' : return aetypes .File (seld , fr )
331- if want == 'cins' : return aetypes .InsertionPoint (seld , fr )
332- if want == 'prop' and form == 'prop' and aetypes .IsType (seld ):
331+ want = dict [b2i ( b 'want') ].type
332+ form = dict [b2i ( b 'form') ].enum
333+ seld = dict [b2i ( b 'seld') ]
334+ fr = dict [b2i ( b 'from') ]
335+ if form in (b 'name' , b 'indx' , b 'rang' , b 'test' ):
336+ if want == b 'text' : return aetypes .Text (seld , fr )
337+ if want == b 'cha ' : return aetypes .Character (seld , fr )
338+ if want == b 'cwor' : return aetypes .Word (seld , fr )
339+ if want == b 'clin' : return aetypes .Line (seld , fr )
340+ if want == b 'cpar' : return aetypes .Paragraph (seld , fr )
341+ if want == b 'cwin' : return aetypes .Window (seld , fr )
342+ if want == b 'docu' : return aetypes .Document (seld , fr )
343+ if want == b 'file' : return aetypes .File (seld , fr )
344+ if want == b 'cins' : return aetypes .InsertionPoint (seld , fr )
345+ if want == b 'prop' and form == b 'prop' and aetypes .IsType (seld ):
333346 return aetypes .Property (seld .type , fr )
334347 return aetypes .ObjectSpecifier (want , form , seld , fr )
335348
@@ -338,14 +351,15 @@ def mkobject(dict):
338351# to __class__ is safe. Moreover, shouldn't there be a better
339352# initializer for the classes in the suites?
340353def mkobjectfrommodule (dict , modulename ):
341- if isinstance (dict ['want' ], type ) and issubclass (dict ['want' ], ObjectSpecifier ):
354+ if (isinstance (dict [b2i (b'want' )], type ) and
355+ issubclass (dict [b2i (b'want' )], ObjectSpecifier )):
342356 # The type has already been converted to Python. Convert back:-(
343- classtype = dict ['want' ]
344- dict ['want' ] = aetypes .mktype (classtype .want )
345- want = dict ['want' ].type
357+ classtype = dict [b2i ( b 'want') ]
358+ dict [b2i ( b 'want') ] = aetypes .mktype (classtype .want )
359+ want = dict [b2i ( b 'want') ].type
346360 module = __import__ (modulename )
347361 codenamemapper = module ._classdeclarations
348- classtype = codenamemapper .get (want , None )
362+ classtype = codenamemapper .get (b2i ( want ) , None )
349363 newobj = mkobject (dict )
350364 if classtype :
351365 assert issubclass (classtype , ObjectSpecifier )
@@ -356,7 +370,7 @@ def mktype(typecode, modulename=None):
356370 if modulename :
357371 module = __import__ (modulename )
358372 codenamemapper = module ._classdeclarations
359- classtype = codenamemapper .get (typecode , None )
373+ classtype = codenamemapper .get (b2i ( typecode ) , None )
360374 if classtype :
361375 return classtype
362376 return aetypes .mktype (typecode )
0 commit comments