| 1 | n/a | """Tools for use in AppleEvent clients and servers: |
|---|
| 2 | n/a | conversion between AE types and python types |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | pack(x) converts a Python object to an AEDesc object |
|---|
| 5 | n/a | unpack(desc) does the reverse |
|---|
| 6 | n/a | coerce(x, wanted_sample) coerces a python object to another python object |
|---|
| 7 | n/a | """ |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | # |
|---|
| 10 | n/a | # This code was originally written by Guido, and modified/extended by Jack |
|---|
| 11 | n/a | # to include the various types that were missing. The reference used is |
|---|
| 12 | n/a | # Apple Event Registry, chapter 9. |
|---|
| 13 | n/a | # |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | from warnings import warnpy3k |
|---|
| 16 | n/a | warnpy3k("In 3.x, the aepack module is removed.", stacklevel=2) |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | import struct |
|---|
| 19 | n/a | import types |
|---|
| 20 | n/a | from types import * |
|---|
| 21 | n/a | from Carbon import AE |
|---|
| 22 | n/a | from Carbon.AppleEvents import * |
|---|
| 23 | n/a | import MacOS |
|---|
| 24 | n/a | import Carbon.File |
|---|
| 25 | n/a | import aetypes |
|---|
| 26 | n/a | from aetypes import mkenum, ObjectSpecifier |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | # These ones seem to be missing from AppleEvents |
|---|
| 29 | n/a | # (they're in AERegistry.h) |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | #typeColorTable = 'clrt' |
|---|
| 32 | n/a | #typeDrawingArea = 'cdrw' |
|---|
| 33 | n/a | #typePixelMap = 'cpix' |
|---|
| 34 | n/a | #typePixelMapMinus = 'tpmm' |
|---|
| 35 | n/a | #typeRotation = 'trot' |
|---|
| 36 | n/a | #typeTextStyles = 'tsty' |
|---|
| 37 | n/a | #typeStyledText = 'STXT' |
|---|
| 38 | n/a | #typeAEText = 'tTXT' |
|---|
| 39 | n/a | #typeEnumeration = 'enum' |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | # |
|---|
| 42 | n/a | # Some AE types are immedeately coerced into something |
|---|
| 43 | n/a | # we like better (and which is equivalent) |
|---|
| 44 | n/a | # |
|---|
| 45 | n/a | unpacker_coercions = { |
|---|
| 46 | n/a | typeComp : typeFloat, |
|---|
| 47 | n/a | typeColorTable : typeAEList, |
|---|
| 48 | n/a | typeDrawingArea : typeAERecord, |
|---|
| 49 | n/a | typeFixed : typeFloat, |
|---|
| 50 | n/a | typeExtended : typeFloat, |
|---|
| 51 | n/a | typePixelMap : typeAERecord, |
|---|
| 52 | n/a | typeRotation : typeAERecord, |
|---|
| 53 | n/a | typeStyledText : typeAERecord, |
|---|
| 54 | n/a | typeTextStyles : typeAERecord, |
|---|
| 55 | n/a | }; |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | # |
|---|
| 58 | n/a | # Some python types we need in the packer: |
|---|
| 59 | n/a | # |
|---|
| 60 | n/a | AEDescType = AE.AEDescType |
|---|
| 61 | n/a | try: |
|---|
| 62 | n/a | FSSType = Carbon.File.FSSpecType |
|---|
| 63 | n/a | except AttributeError: |
|---|
| 64 | n/a | class FSSType: |
|---|
| 65 | n/a | pass |
|---|
| 66 | n/a | FSRefType = Carbon.File.FSRefType |
|---|
| 67 | n/a | AliasType = Carbon.File.AliasType |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | def packkey(ae, key, value): |
|---|
| 70 | n/a | if hasattr(key, 'which'): |
|---|
| 71 | n/a | keystr = key.which |
|---|
| 72 | n/a | elif hasattr(key, 'want'): |
|---|
| 73 | n/a | keystr = key.want |
|---|
| 74 | n/a | else: |
|---|
| 75 | n/a | keystr = key |
|---|
| 76 | n/a | ae.AEPutParamDesc(keystr, pack(value)) |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | def pack(x, forcetype = None): |
|---|
| 79 | n/a | """Pack a python object into an AE descriptor""" |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | if forcetype: |
|---|
| 82 | n/a | if type(x) is StringType: |
|---|
| 83 | n/a | return AE.AECreateDesc(forcetype, x) |
|---|
| 84 | n/a | else: |
|---|
| 85 | n/a | return pack(x).AECoerceDesc(forcetype) |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | if x is None: |
|---|
| 88 | n/a | return AE.AECreateDesc('null', '') |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | if isinstance(x, AEDescType): |
|---|
| 91 | n/a | return x |
|---|
| 92 | n/a | if isinstance(x, FSSType): |
|---|
| 93 | n/a | return AE.AECreateDesc('fss ', x.data) |
|---|
| 94 | n/a | if isinstance(x, FSRefType): |
|---|
| 95 | n/a | return AE.AECreateDesc('fsrf', x.data) |
|---|
| 96 | n/a | if isinstance(x, AliasType): |
|---|
| 97 | n/a | return AE.AECreateDesc('alis', x.data) |
|---|
| 98 | n/a | if isinstance(x, IntType): |
|---|
| 99 | n/a | return AE.AECreateDesc('long', struct.pack('l', x)) |
|---|
| 100 | n/a | if isinstance(x, FloatType): |
|---|
| 101 | n/a | return AE.AECreateDesc('doub', struct.pack('d', x)) |
|---|
| 102 | n/a | if isinstance(x, StringType): |
|---|
| 103 | n/a | return AE.AECreateDesc('TEXT', x) |
|---|
| 104 | n/a | if isinstance(x, UnicodeType): |
|---|
| 105 | n/a | data = x.encode('utf16') |
|---|
| 106 | n/a | if data[:2] == '\xfe\xff': |
|---|
| 107 | n/a | data = data[2:] |
|---|
| 108 | n/a | return AE.AECreateDesc('utxt', data) |
|---|
| 109 | n/a | if isinstance(x, ListType): |
|---|
| 110 | n/a | list = AE.AECreateList('', 0) |
|---|
| 111 | n/a | for item in x: |
|---|
| 112 | n/a | list.AEPutDesc(0, pack(item)) |
|---|
| 113 | n/a | return list |
|---|
| 114 | n/a | if isinstance(x, DictionaryType): |
|---|
| 115 | n/a | record = AE.AECreateList('', 1) |
|---|
| 116 | n/a | for key, value in x.items(): |
|---|
| 117 | n/a | packkey(record, key, value) |
|---|
| 118 | n/a | #record.AEPutParamDesc(key, pack(value)) |
|---|
| 119 | n/a | return record |
|---|
| 120 | n/a | if type(x) == types.ClassType and issubclass(x, ObjectSpecifier): |
|---|
| 121 | n/a | # Note: we are getting a class object here, not an instance |
|---|
| 122 | n/a | return AE.AECreateDesc('type', x.want) |
|---|
| 123 | n/a | if hasattr(x, '__aepack__'): |
|---|
| 124 | n/a | return x.__aepack__() |
|---|
| 125 | n/a | if hasattr(x, 'which'): |
|---|
| 126 | n/a | return AE.AECreateDesc('TEXT', x.which) |
|---|
| 127 | n/a | if hasattr(x, 'want'): |
|---|
| 128 | n/a | return AE.AECreateDesc('TEXT', x.want) |
|---|
| 129 | n/a | return AE.AECreateDesc('TEXT', repr(x)) # Copout |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def unpack(desc, formodulename=""): |
|---|
| 132 | n/a | """Unpack an AE descriptor to a python object""" |
|---|
| 133 | n/a | t = desc.type |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | if t in unpacker_coercions: |
|---|
| 136 | n/a | desc = desc.AECoerceDesc(unpacker_coercions[t]) |
|---|
| 137 | n/a | t = desc.type # This is a guess by Jack.... |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | if t == typeAEList: |
|---|
| 140 | n/a | l = [] |
|---|
| 141 | n/a | for i in range(desc.AECountItems()): |
|---|
| 142 | n/a | keyword, item = desc.AEGetNthDesc(i+1, '****') |
|---|
| 143 | n/a | l.append(unpack(item, formodulename)) |
|---|
| 144 | n/a | return l |
|---|
| 145 | n/a | if t == typeAERecord: |
|---|
| 146 | n/a | d = {} |
|---|
| 147 | n/a | for i in range(desc.AECountItems()): |
|---|
| 148 | n/a | keyword, item = desc.AEGetNthDesc(i+1, '****') |
|---|
| 149 | n/a | d[keyword] = unpack(item, formodulename) |
|---|
| 150 | n/a | return d |
|---|
| 151 | n/a | if t == typeAEText: |
|---|
| 152 | n/a | record = desc.AECoerceDesc('reco') |
|---|
| 153 | n/a | return mkaetext(unpack(record, formodulename)) |
|---|
| 154 | n/a | if t == typeAlias: |
|---|
| 155 | n/a | return Carbon.File.Alias(rawdata=desc.data) |
|---|
| 156 | n/a | # typeAppleEvent returned as unknown |
|---|
| 157 | n/a | if t == typeBoolean: |
|---|
| 158 | n/a | return struct.unpack('b', desc.data)[0] |
|---|
| 159 | n/a | if t == typeChar: |
|---|
| 160 | n/a | return desc.data |
|---|
| 161 | n/a | if t == typeUnicodeText: |
|---|
| 162 | n/a | return unicode(desc.data, 'utf16') |
|---|
| 163 | n/a | # typeColorTable coerced to typeAEList |
|---|
| 164 | n/a | # typeComp coerced to extended |
|---|
| 165 | n/a | # typeData returned as unknown |
|---|
| 166 | n/a | # typeDrawingArea coerced to typeAERecord |
|---|
| 167 | n/a | if t == typeEnumeration: |
|---|
| 168 | n/a | return mkenum(desc.data) |
|---|
| 169 | n/a | # typeEPS returned as unknown |
|---|
| 170 | n/a | if t == typeFalse: |
|---|
| 171 | n/a | return 0 |
|---|
| 172 | n/a | if t == typeFloat: |
|---|
| 173 | n/a | data = desc.data |
|---|
| 174 | n/a | return struct.unpack('d', data)[0] |
|---|
| 175 | n/a | if t == typeFSS: |
|---|
| 176 | n/a | return Carbon.File.FSSpec(rawdata=desc.data) |
|---|
| 177 | n/a | if t == typeFSRef: |
|---|
| 178 | n/a | return Carbon.File.FSRef(rawdata=desc.data) |
|---|
| 179 | n/a | if t == typeInsertionLoc: |
|---|
| 180 | n/a | record = desc.AECoerceDesc('reco') |
|---|
| 181 | n/a | return mkinsertionloc(unpack(record, formodulename)) |
|---|
| 182 | n/a | # typeInteger equal to typeLongInteger |
|---|
| 183 | n/a | if t == typeIntlText: |
|---|
| 184 | n/a | script, language = struct.unpack('hh', desc.data[:4]) |
|---|
| 185 | n/a | return aetypes.IntlText(script, language, desc.data[4:]) |
|---|
| 186 | n/a | if t == typeIntlWritingCode: |
|---|
| 187 | n/a | script, language = struct.unpack('hh', desc.data) |
|---|
| 188 | n/a | return aetypes.IntlWritingCode(script, language) |
|---|
| 189 | n/a | if t == typeKeyword: |
|---|
| 190 | n/a | return mkkeyword(desc.data) |
|---|
| 191 | n/a | if t == typeLongInteger: |
|---|
| 192 | n/a | return struct.unpack('l', desc.data)[0] |
|---|
| 193 | n/a | if t == typeLongDateTime: |
|---|
| 194 | n/a | a, b = struct.unpack('lL', desc.data) |
|---|
| 195 | n/a | return (long(a) << 32) + b |
|---|
| 196 | n/a | if t == typeNull: |
|---|
| 197 | n/a | return None |
|---|
| 198 | n/a | if t == typeMagnitude: |
|---|
| 199 | n/a | v = struct.unpack('l', desc.data) |
|---|
| 200 | n/a | if v < 0: |
|---|
| 201 | n/a | v = 0x100000000L + v |
|---|
| 202 | n/a | return v |
|---|
| 203 | n/a | if t == typeObjectSpecifier: |
|---|
| 204 | n/a | record = desc.AECoerceDesc('reco') |
|---|
| 205 | n/a | # If we have been told the name of the module we are unpacking aedescs for, |
|---|
| 206 | n/a | # we can attempt to create the right type of python object from that module. |
|---|
| 207 | n/a | if formodulename: |
|---|
| 208 | n/a | return mkobjectfrommodule(unpack(record, formodulename), formodulename) |
|---|
| 209 | n/a | return mkobject(unpack(record, formodulename)) |
|---|
| 210 | n/a | # typePict returned as unknown |
|---|
| 211 | n/a | # typePixelMap coerced to typeAERecord |
|---|
| 212 | n/a | # typePixelMapMinus returned as unknown |
|---|
| 213 | n/a | # typeProcessSerialNumber returned as unknown |
|---|
| 214 | n/a | if t == typeQDPoint: |
|---|
| 215 | n/a | v, h = struct.unpack('hh', desc.data) |
|---|
| 216 | n/a | return aetypes.QDPoint(v, h) |
|---|
| 217 | n/a | if t == typeQDRectangle: |
|---|
| 218 | n/a | v0, h0, v1, h1 = struct.unpack('hhhh', desc.data) |
|---|
| 219 | n/a | return aetypes.QDRectangle(v0, h0, v1, h1) |
|---|
| 220 | n/a | if t == typeRGBColor: |
|---|
| 221 | n/a | r, g, b = struct.unpack('hhh', desc.data) |
|---|
| 222 | n/a | return aetypes.RGBColor(r, g, b) |
|---|
| 223 | n/a | # typeRotation coerced to typeAERecord |
|---|
| 224 | n/a | # typeScrapStyles returned as unknown |
|---|
| 225 | n/a | # typeSessionID returned as unknown |
|---|
| 226 | n/a | if t == typeShortFloat: |
|---|
| 227 | n/a | return struct.unpack('f', desc.data)[0] |
|---|
| 228 | n/a | if t == typeShortInteger: |
|---|
| 229 | n/a | return struct.unpack('h', desc.data)[0] |
|---|
| 230 | n/a | # typeSMFloat identical to typeShortFloat |
|---|
| 231 | n/a | # typeSMInt indetical to typeShortInt |
|---|
| 232 | n/a | # typeStyledText coerced to typeAERecord |
|---|
| 233 | n/a | if t == typeTargetID: |
|---|
| 234 | n/a | return mktargetid(desc.data) |
|---|
| 235 | n/a | # typeTextStyles coerced to typeAERecord |
|---|
| 236 | n/a | # typeTIFF returned as unknown |
|---|
| 237 | n/a | if t == typeTrue: |
|---|
| 238 | n/a | return 1 |
|---|
| 239 | n/a | if t == typeType: |
|---|
| 240 | n/a | return mktype(desc.data, formodulename) |
|---|
| 241 | n/a | # |
|---|
| 242 | n/a | # The following are special |
|---|
| 243 | n/a | # |
|---|
| 244 | n/a | if t == 'rang': |
|---|
| 245 | n/a | record = desc.AECoerceDesc('reco') |
|---|
| 246 | n/a | return mkrange(unpack(record, formodulename)) |
|---|
| 247 | n/a | if t == 'cmpd': |
|---|
| 248 | n/a | record = desc.AECoerceDesc('reco') |
|---|
| 249 | n/a | return mkcomparison(unpack(record, formodulename)) |
|---|
| 250 | n/a | if t == 'logi': |
|---|
| 251 | n/a | record = desc.AECoerceDesc('reco') |
|---|
| 252 | n/a | return mklogical(unpack(record, formodulename)) |
|---|
| 253 | n/a | return mkunknown(desc.type, desc.data) |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | def coerce(data, egdata): |
|---|
| 256 | n/a | """Coerce a python object to another type using the AE coercers""" |
|---|
| 257 | n/a | pdata = pack(data) |
|---|
| 258 | n/a | pegdata = pack(egdata) |
|---|
| 259 | n/a | pdata = pdata.AECoerceDesc(pegdata.type) |
|---|
| 260 | n/a | return unpack(pdata) |
|---|
| 261 | n/a | |
|---|
| 262 | n/a | # |
|---|
| 263 | n/a | # Helper routines for unpack |
|---|
| 264 | n/a | # |
|---|
| 265 | n/a | def mktargetid(data): |
|---|
| 266 | n/a | sessionID = getlong(data[:4]) |
|---|
| 267 | n/a | name = mkppcportrec(data[4:4+72]) |
|---|
| 268 | n/a | location = mklocationnamerec(data[76:76+36]) |
|---|
| 269 | n/a | rcvrName = mkppcportrec(data[112:112+72]) |
|---|
| 270 | n/a | return sessionID, name, location, rcvrName |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | def mkppcportrec(rec): |
|---|
| 273 | n/a | namescript = getword(rec[:2]) |
|---|
| 274 | n/a | name = getpstr(rec[2:2+33]) |
|---|
| 275 | n/a | portkind = getword(rec[36:38]) |
|---|
| 276 | n/a | if portkind == 1: |
|---|
| 277 | n/a | ctor = rec[38:42] |
|---|
| 278 | n/a | type = rec[42:46] |
|---|
| 279 | n/a | identity = (ctor, type) |
|---|
| 280 | n/a | else: |
|---|
| 281 | n/a | identity = getpstr(rec[38:38+33]) |
|---|
| 282 | n/a | return namescript, name, portkind, identity |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | def mklocationnamerec(rec): |
|---|
| 285 | n/a | kind = getword(rec[:2]) |
|---|
| 286 | n/a | stuff = rec[2:] |
|---|
| 287 | n/a | if kind == 0: stuff = None |
|---|
| 288 | n/a | if kind == 2: stuff = getpstr(stuff) |
|---|
| 289 | n/a | return kind, stuff |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | def mkunknown(type, data): |
|---|
| 292 | n/a | return aetypes.Unknown(type, data) |
|---|
| 293 | n/a | |
|---|
| 294 | n/a | def getpstr(s): |
|---|
| 295 | n/a | return s[1:1+ord(s[0])] |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | def getlong(s): |
|---|
| 298 | n/a | return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | def getword(s): |
|---|
| 301 | n/a | return (ord(s[0])<<8) | (ord(s[1])<<0) |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | def mkkeyword(keyword): |
|---|
| 304 | n/a | return aetypes.Keyword(keyword) |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | def mkrange(dict): |
|---|
| 307 | n/a | return aetypes.Range(dict['star'], dict['stop']) |
|---|
| 308 | n/a | |
|---|
| 309 | n/a | def mkcomparison(dict): |
|---|
| 310 | n/a | return aetypes.Comparison(dict['obj1'], dict['relo'].enum, dict['obj2']) |
|---|
| 311 | n/a | |
|---|
| 312 | n/a | def mklogical(dict): |
|---|
| 313 | n/a | return aetypes.Logical(dict['logc'], dict['term']) |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | def mkstyledtext(dict): |
|---|
| 316 | n/a | return aetypes.StyledText(dict['ksty'], dict['ktxt']) |
|---|
| 317 | n/a | |
|---|
| 318 | n/a | def mkaetext(dict): |
|---|
| 319 | n/a | return aetypes.AEText(dict[keyAEScriptTag], dict[keyAEStyles], dict[keyAEText]) |
|---|
| 320 | n/a | |
|---|
| 321 | n/a | def mkinsertionloc(dict): |
|---|
| 322 | n/a | return aetypes.InsertionLoc(dict[keyAEObject], dict[keyAEPosition]) |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | def mkobject(dict): |
|---|
| 325 | n/a | want = dict['want'].type |
|---|
| 326 | n/a | form = dict['form'].enum |
|---|
| 327 | n/a | seld = dict['seld'] |
|---|
| 328 | n/a | fr = dict['from'] |
|---|
| 329 | n/a | if form in ('name', 'indx', 'rang', 'test'): |
|---|
| 330 | n/a | if want == 'text': return aetypes.Text(seld, fr) |
|---|
| 331 | n/a | if want == 'cha ': return aetypes.Character(seld, fr) |
|---|
| 332 | n/a | if want == 'cwor': return aetypes.Word(seld, fr) |
|---|
| 333 | n/a | if want == 'clin': return aetypes.Line(seld, fr) |
|---|
| 334 | n/a | if want == 'cpar': return aetypes.Paragraph(seld, fr) |
|---|
| 335 | n/a | if want == 'cwin': return aetypes.Window(seld, fr) |
|---|
| 336 | n/a | if want == 'docu': return aetypes.Document(seld, fr) |
|---|
| 337 | n/a | if want == 'file': return aetypes.File(seld, fr) |
|---|
| 338 | n/a | if want == 'cins': return aetypes.InsertionPoint(seld, fr) |
|---|
| 339 | n/a | if want == 'prop' and form == 'prop' and aetypes.IsType(seld): |
|---|
| 340 | n/a | return aetypes.Property(seld.type, fr) |
|---|
| 341 | n/a | return aetypes.ObjectSpecifier(want, form, seld, fr) |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | # Note by Jack: I'm not 100% sure of the following code. This was |
|---|
| 344 | n/a | # provided by Donovan Preston, but I wonder whether the assignment |
|---|
| 345 | n/a | # to __class__ is safe. Moreover, shouldn't there be a better |
|---|
| 346 | n/a | # initializer for the classes in the suites? |
|---|
| 347 | n/a | def mkobjectfrommodule(dict, modulename): |
|---|
| 348 | n/a | if type(dict['want']) == types.ClassType and issubclass(dict['want'], ObjectSpecifier): |
|---|
| 349 | n/a | # The type has already been converted to Python. Convert back:-( |
|---|
| 350 | n/a | classtype = dict['want'] |
|---|
| 351 | n/a | dict['want'] = aetypes.mktype(classtype.want) |
|---|
| 352 | n/a | want = dict['want'].type |
|---|
| 353 | n/a | module = __import__(modulename) |
|---|
| 354 | n/a | codenamemapper = module._classdeclarations |
|---|
| 355 | n/a | classtype = codenamemapper.get(want, None) |
|---|
| 356 | n/a | newobj = mkobject(dict) |
|---|
| 357 | n/a | if classtype: |
|---|
| 358 | n/a | assert issubclass(classtype, ObjectSpecifier) |
|---|
| 359 | n/a | newobj.__class__ = classtype |
|---|
| 360 | n/a | return newobj |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | def mktype(typecode, modulename=None): |
|---|
| 363 | n/a | if modulename: |
|---|
| 364 | n/a | module = __import__(modulename) |
|---|
| 365 | n/a | codenamemapper = module._classdeclarations |
|---|
| 366 | n/a | classtype = codenamemapper.get(typecode, None) |
|---|
| 367 | n/a | if classtype: |
|---|
| 368 | n/a | return classtype |
|---|
| 369 | n/a | return aetypes.mktype(typecode) |
|---|