2 Produces a 3 dictionaries from application aete's
3 to be read by PythonScript
5 v.02 january 31, 1998 added support for inheriting suites from aeut
6 v.03 february 16, 1998 changes to identify
7 v.04 february 26, 1998 simplified decode
8 v.05 23/04/98 simplified _launch
25 app
='CSOm' #'ezVu'# 'nwSP'#MACS'#
27 #Restrict the application suites to the dialect we want to use.
28 LANG
= 0 # 0 = English, 1 = French, 11 = Japanese
29 lang
= {0:'English', 1:'French', 11:'Japanese'}
31 #The following are neaded to open the application aete
32 kASAppleScriptSuite
= 'ascr'
37 class AETE(baetools
.TalkTo
):
43 except MacOS
.Error
, msg
:
47 data
= decode(data
['----'].data
)
48 data
= compileaete(data
)
53 """Decode an aete into a python data structure"""
54 f
= StringIO
.StringIO(data
)
55 aete
= generic(getaete
, f
)
59 """Recursively replace singleton tuples by their constituent item"""
60 if type(item
) is types
.ListType
:
61 return map(simplify
, item
)
62 elif type(item
) == types
.TupleType
and len(item
) == 2:
63 return simplify(item
[1])
68 ## Here follows the aete resource decoder.
69 ## It is presented bottom-up instead of top-down because there are direct
70 ## references to the lower-level part-decoders from the high-level part-decoders.
72 def getflag(f
, *args
):
77 raise EOFError, 'in getflag' + str(args
)
81 def getbyte(f
, *args
):
84 raise EOFError, 'in getbyte' + str(args
)
87 def getword(f
, *args
):
91 raise EOFError, 'in getword' + str(args
)
92 return (ord(s
[0])<<8) |
ord(s
[1])
94 def getlong(f
, *args
):
98 raise EOFError, 'in getlong' + str(args
)
99 return (ord(s
[0])<<24) |
(ord(s
[1])<<16) |
(ord(s
[2])<<8) |
ord(s
[3])
101 def getostype(f
, *args
):
105 raise EOFError, 'in getostype' + str(args
)
108 def getpstr(f
, *args
):
111 raise EOFError, 'in getpstr[1]' + str(args
)
113 if nbytes
== 0: return ''
116 raise EOFError, 'in getpstr[2]' + str(args
)
123 ## print 'align:', `c`
125 def getlist(f
, description
, getitem
):
128 for i
in range(count
):
129 list.append(generic(getitem
, f
))
133 def alt_generic(what
, f
, *args
):
134 print "generic", `what`
, args
135 res
= vageneric(what
, f
, args
)
139 def generic(what
, f
, *args
):
140 if type(what
) == types
.FunctionType
:
141 return apply(what
, (f
,) + args
)
142 if type(what
) == types
.ListType
:
145 item
= apply(generic
, thing
[:1] + (f
,) + thing
[1:])
148 return "BAD GENERIC ARGS: %s" % `what`
152 (getpstr
, "description"),
157 (getostype
, "keyword"),
162 (getpstr
, "description"),
163 (getostype
, "suite code"),
164 (getostype
, "event code"),
165 (getdata
, "returns"),
166 (getdata
, "accepts"),
167 (getlist
, "optional arguments", getargument
)
176 (getlist
, "keyform", getostype
)
180 (getostype
, "class code"),
181 (getpstr
, "description"),
182 (getlist
, "properties", getproperty
),
183 (getlist
, "elements", getelement
)
186 (getpstr
, "operator name"),
187 (getostype
, "operator ID"),
188 (getpstr
, "operator comment"),
191 (getpstr
, "enumerator name"),
192 (getostype
, "enumerator ID"),
193 (getpstr
, "enumerator comment")
196 (getostype
, "enumeration ID"),
197 (getlist
, "enumerator", getenumerator
)
200 (getpstr
, "suite name"),
201 (getpstr
, "suite description"),
202 (getostype
, "suite ID"),
203 (getword
, "suite level"),
204 (getword
, "suite version"),
205 (getlist
, "events", getevent
),
206 (getlist
, "classes", getclass
),
207 (getlist
, "comparisons", getcomparison
),
208 (getlist
, "enumerations", getenumeration
)
211 (getbyte
, "major version in BCD"),
212 (getbyte
, "minor version in BCD"),
213 (getword
, "language code"),
214 (getword
, "script code"),
215 (getlist
, "suites", getsuite
)
218 def compileaete(aete
):
219 """Generate dictionary for a full aete resource."""
220 [major
, minor
, language
, script
, suites
] = aete
223 for gsuite
in gsuites
:
224 if gsuite
[0] == 'AppleScript Suite':
226 suite
= compilesuite(suite
)
227 suitedict
[identify(suite
[0])] = suite
[1:]
232 for gsuite
in gsuites
:
233 if suitecode
== gsuite
[2]:
235 suite
= compilesuite(suite
)
236 suitedict
[identify(suite
[0])] = suite
[1:]
237 suitedict
= combinesuite(suitedict
)
240 def compilesuite(suite
):
241 """Generate dictionary for a single suite"""
242 [name
, desc
, code
, level
, version
, events
, classes
, comps
, enums
] = suite
249 ev
[0] = identify(ev
[:2])
250 eventdict
[identify(event
[:2])] = event
[1:]
251 for klass
in classes
:
254 kl
[0] = identify(kl
[:2])
255 classdict
[identify(klass
[:2])] = klass
[1:]
257 enumdict
[enum
[0]] = enum
[1]
258 return name
, eventdict
, classdict
, enumdict
260 def combinesuite(suite
):
261 """Combines suite dictionaries to seperate event, class, enumeration dictionaries
268 for value
in suite
.values():
269 for key
in value
[0].keys():
272 for key
in value
[1].keys():
274 if key
in classDict
.keys():
275 nval
= classDict
[key
][2]
276 val
[2] = val
[2] + nval
278 for key
in value
[2].keys():
281 return eventDict
, classDict
, enumDict
284 illegal_ids
= [ "for", "in", "from", "and", "or", "not", "print", "class", "return",
285 "def", "name", 'data' ]
288 """Turn any string into an identifier:
298 rv
= string
.replace(str[0], ' ', '_')
299 rv
= string
.replace(rv
, '-', '')
300 rv
= string
.replace(rv
, ',', '')
301 rv
= string
.capitalize(rv
)
306 """open and read the aete of the target application"""
307 arguments
['----'] = LANG
309 _reply
, _arguments
, _attributes
= _aete
.send(kASAppleScriptSuite
, kGetAETE
, arguments
, attributes
)
310 if _arguments
.has_key('errn'):
311 raise baetools
.Error
, baetools
.decodeerror(_arguments
)
315 """Open and read a aeut file.
316 XXXXX This has been temporarily hard coded until a Python aeut is written XXXX"""
319 rf
= OpenRFPerm(fullname
, 0, 1)
323 for i
in range(Count1Resources('aeut')):
324 res
= Get1IndResource('aeut', 1+i
)
325 resources
.append(res
)
326 for res
in resources
:
328 data
= decode(data
)[4]
334 """find the correct Dialect file"""
336 dialect
= lang
[LANG
] + " Dialect"
339 vRefNum
, dirID
= macfs
.FindFolder(kOnSystemDisk
, kScriptingAdditionsFolderType
, 0)
340 fss
= macfs
.FSSpec((vRefNum
, dirID
, ''))
341 fss
= fss
.as_pathname()
344 vRefNum
, dirID
= macfs
.FindFolder(kOnSystemDisk
, kExtensionFolderType
, 0)
345 fss
= macfs
.FSSpec((vRefNum
, dirID
, ''))
346 fss
= fss
.as_pathname()
347 fss
= macpath
.join(fss
, "Scripting Additions")
348 fss
= macpath
.join(fss
, "Dialect")
349 fss
= macpath
.join(fss
, dialect
)
354 # """Open and read the aetes of osaxen in the scripting additions folder"""
358 # vRefNum, dirID = macfs.FindFolder(kOnSystemDisk, kExtensionFolderType, 0)
359 # fss = macfs.FSSpec((vRefNum, dirID, ''))
360 # fss = fss.as_pathname()
361 # osax = macpath.join(fss, "Scripting Additions")
362 # for file in os.listdir(osax):
363 # fullname = macpath.join(osax, file)
365 # rf = OpenRFPerm(fullname, 0, 1)
369 # for i in range(Count1Resources('aete')):
370 # res = Get1IndResource('aete', 1+i)
371 # resources.append(res)
372 # for res in resources:
374 # data = decode(data)[4]
381 #The following should be replaced by direct access to a python 'aeut'
383 def _launch(appfile
):
384 """Open a file thru the finder. Specify file by name or fsspec"""
386 # from PythonScript import PyScript
388 _finder
= AETE('MACS')
390 parameters
['----'] = eval("baetypes.ObjectSpecifier('%s', '%s', %s)" % ('appf', 'ID ', `appfile`
))
391 _reply
, _arguments
, _attributes
= _finder
.send( 'aevt', 'odoc', parameters
, attributes
= {})
392 if _arguments
.has_key('errn'):
393 raise baetools
.Error
, baetools
.decodeerror(_arguments
)
394 # XXXX Optionally decode result
395 if _arguments
.has_key('----'):
396 return _arguments
['----']
400 if __name__
== '__main__':
402 # profile.run('Getaete(app)', 'Getaeteprof')