Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 1 | """ |
| 2 | gensuitemodule - Generate an AE suite module from an aete/aeut resource |
| 3 | |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 4 | Based on aete.py. |
| 5 | |
| 6 | Reading and understanding this code is left as an exercise to the reader. |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
| 9 | import MacOS |
| 10 | import os |
| 11 | import string |
| 12 | import sys |
| 13 | import types |
| 14 | import StringIO |
| 15 | import macfs |
| 16 | |
| 17 | from Res import * |
| 18 | |
| 19 | def main(): |
Jack Jansen | 9d19a91 | 1995-08-14 12:14:55 +0000 | [diff] [blame] | 20 | fss, ok = macfs.PromptGetFile('Select file with aeut/aete resource:') |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 21 | if not ok: |
| 22 | sys.exit(0) |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 23 | processfile(fss.as_pathname()) |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 24 | |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 25 | def processfile(fullname): |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 26 | """Process all resources in a single file""" |
| 27 | cur = CurResFile() |
| 28 | print fullname |
| 29 | rf = OpenRFPerm(fullname, 0, 1) |
| 30 | try: |
| 31 | UseResFile(rf) |
| 32 | resources = [] |
| 33 | for i in range(Count1Resources('aete')): |
| 34 | res = Get1IndResource('aete', 1+i) |
| 35 | resources.append(res) |
| 36 | for i in range(Count1Resources('aeut')): |
| 37 | res = Get1IndResource('aeut', 1+i) |
| 38 | resources.append(res) |
| 39 | print "\nLISTING aete+aeut RESOURCES IN", `fullname` |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 40 | aetelist = [] |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 41 | for res in resources: |
| 42 | print "decoding", res.GetResInfo(), "..." |
| 43 | data = res.data |
| 44 | aete = decode(data) |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 45 | aetelist.append((aete, res.GetResInfo())) |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 46 | finally: |
| 47 | if rf <> cur: |
| 48 | CloseResFile(rf) |
| 49 | UseResFile(cur) |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 50 | # switch back (needed for dialogs in Python) |
| 51 | UseResFile(cur) |
| 52 | compileaetelist(aetelist, fullname) |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 53 | |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 54 | def compileaetelist(aetelist, fullname): |
| 55 | for aete, resinfo in aetelist: |
| 56 | compileaete(aete, resinfo, fullname) |
| 57 | |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 58 | def decode(data): |
| 59 | """Decode a resource into a python data structure""" |
| 60 | f = StringIO.StringIO(data) |
| 61 | aete = generic(getaete, f) |
| 62 | aete = simplify(aete) |
| 63 | processed = f.tell() |
| 64 | unprocessed = len(f.read()) |
| 65 | total = f.tell() |
| 66 | if unprocessed: |
| 67 | sys.stderr.write("%d processed + %d unprocessed = %d total\n" % |
| 68 | (processed, unprocessed, total)) |
| 69 | return aete |
| 70 | |
| 71 | def simplify(item): |
| 72 | """Recursively replace singleton tuples by their constituent item""" |
| 73 | if type(item) is types.ListType: |
| 74 | return map(simplify, item) |
| 75 | elif type(item) == types.TupleType and len(item) == 2: |
| 76 | return simplify(item[1]) |
| 77 | else: |
| 78 | return item |
| 79 | |
| 80 | |
| 81 | # Here follows the aete resource decoder. |
| 82 | # It is presented bottom-up instead of top-down because there are direct |
| 83 | # references to the lower-level part-decoders from the high-level part-decoders. |
| 84 | |
| 85 | def getbyte(f, *args): |
| 86 | c = f.read(1) |
| 87 | if not c: |
| 88 | raise EOFError, 'in getbyte' + str(args) |
| 89 | return ord(c) |
| 90 | |
| 91 | def getword(f, *args): |
| 92 | getalign(f) |
| 93 | s = f.read(2) |
| 94 | if len(s) < 2: |
| 95 | raise EOFError, 'in getword' + str(args) |
| 96 | return (ord(s[0])<<8) | ord(s[1]) |
| 97 | |
| 98 | def getlong(f, *args): |
| 99 | getalign(f) |
| 100 | s = f.read(4) |
| 101 | if len(s) < 4: |
| 102 | raise EOFError, 'in getlong' + str(args) |
| 103 | return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) |
| 104 | |
| 105 | def getostype(f, *args): |
| 106 | getalign(f) |
| 107 | s = f.read(4) |
| 108 | if len(s) < 4: |
| 109 | raise EOFError, 'in getostype' + str(args) |
| 110 | return s |
| 111 | |
| 112 | def getpstr(f, *args): |
| 113 | c = f.read(1) |
| 114 | if len(c) < 1: |
| 115 | raise EOFError, 'in getpstr[1]' + str(args) |
| 116 | nbytes = ord(c) |
| 117 | if nbytes == 0: return '' |
| 118 | s = f.read(nbytes) |
| 119 | if len(s) < nbytes: |
| 120 | raise EOFError, 'in getpstr[2]' + str(args) |
| 121 | return s |
| 122 | |
| 123 | def getalign(f): |
| 124 | if f.tell() & 1: |
| 125 | c = f.read(1) |
| 126 | ##if c <> '\0': |
| 127 | ## print 'align:', `c` |
| 128 | |
| 129 | def getlist(f, description, getitem): |
| 130 | count = getword(f) |
| 131 | list = [] |
| 132 | for i in range(count): |
| 133 | list.append(generic(getitem, f)) |
| 134 | getalign(f) |
| 135 | return list |
| 136 | |
| 137 | def alt_generic(what, f, *args): |
| 138 | print "generic", `what`, args |
| 139 | res = vageneric(what, f, args) |
| 140 | print '->', `res` |
| 141 | return res |
| 142 | |
| 143 | def generic(what, f, *args): |
| 144 | if type(what) == types.FunctionType: |
| 145 | return apply(what, (f,) + args) |
| 146 | if type(what) == types.ListType: |
| 147 | record = [] |
| 148 | for thing in what: |
| 149 | item = apply(generic, thing[:1] + (f,) + thing[1:]) |
| 150 | record.append((thing[1], item)) |
| 151 | return record |
| 152 | return "BAD GENERIC ARGS: %s" % `what` |
| 153 | |
| 154 | getdata = [ |
| 155 | (getostype, "type"), |
| 156 | (getpstr, "description"), |
| 157 | (getword, "flags") |
| 158 | ] |
| 159 | getargument = [ |
| 160 | (getpstr, "name"), |
| 161 | (getostype, "keyword"), |
| 162 | (getdata, "what") |
| 163 | ] |
| 164 | getevent = [ |
| 165 | (getpstr, "name"), |
| 166 | (getpstr, "description"), |
| 167 | (getostype, "suite code"), |
| 168 | (getostype, "event code"), |
| 169 | (getdata, "returns"), |
| 170 | (getdata, "accepts"), |
| 171 | (getlist, "optional arguments", getargument) |
| 172 | ] |
| 173 | getproperty = [ |
| 174 | (getpstr, "name"), |
| 175 | (getostype, "code"), |
| 176 | (getdata, "what") |
| 177 | ] |
| 178 | getelement = [ |
| 179 | (getostype, "type"), |
| 180 | (getlist, "keyform", getostype) |
| 181 | ] |
| 182 | getclass = [ |
| 183 | (getpstr, "name"), |
| 184 | (getostype, "class code"), |
| 185 | (getpstr, "description"), |
| 186 | (getlist, "properties", getproperty), |
| 187 | (getlist, "elements", getelement) |
| 188 | ] |
| 189 | getcomparison = [ |
| 190 | (getpstr, "operator name"), |
| 191 | (getostype, "operator ID"), |
| 192 | (getpstr, "operator comment"), |
| 193 | ] |
| 194 | getenumerator = [ |
| 195 | (getpstr, "enumerator name"), |
| 196 | (getostype, "enumerator ID"), |
| 197 | (getpstr, "enumerator comment") |
| 198 | ] |
| 199 | getenumeration = [ |
| 200 | (getostype, "enumeration ID"), |
| 201 | (getlist, "enumerator", getenumerator) |
| 202 | ] |
| 203 | getsuite = [ |
| 204 | (getpstr, "suite name"), |
| 205 | (getpstr, "suite description"), |
| 206 | (getostype, "suite ID"), |
| 207 | (getword, "suite level"), |
| 208 | (getword, "suite version"), |
| 209 | (getlist, "events", getevent), |
| 210 | (getlist, "classes", getclass), |
| 211 | (getlist, "comparisons", getcomparison), |
| 212 | (getlist, "enumerations", getenumeration) |
| 213 | ] |
| 214 | getaete = [ |
| 215 | (getword, "major/minor version in BCD"), |
| 216 | (getword, "language code"), |
| 217 | (getword, "script code"), |
| 218 | (getlist, "suites", getsuite) |
| 219 | ] |
| 220 | |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 221 | def compileaete(aete, resinfo, fname): |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 222 | """Generate code for a full aete resource. fname passed for doc purposes""" |
| 223 | [version, language, script, suites] = aete |
| 224 | major, minor = divmod(version, 256) |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 225 | fss = macfs.FSSpec(fname) |
| 226 | creatorsignature, dummy = fss.GetCreatorType() |
| 227 | packagename = identify(os.path.basename(fname)) |
| 228 | if language: |
| 229 | packagename = packagename+'_lang%d'%language |
| 230 | if script: |
| 231 | packagename = packagename+'_script%d'%script |
| 232 | if len(packagename) > 27: |
| 233 | packagename = packagename[:27] |
| 234 | macfs.SetFolder(os.path.join(sys.prefix, ':Mac:Lib:lib-scriptpackages')) |
| 235 | fss, ok = macfs.GetDirectory('Package folder for %s'%packagename) |
| 236 | if not ok: |
| 237 | return |
| 238 | pathname = fss.as_pathname() |
| 239 | packagename = os.path.split(os.path.normpath(pathname))[1] |
| 240 | fss, ok = macfs.GetDirectory('Package folder for base suite (usually StdSuites)') |
| 241 | if ok: |
| 242 | dirname, basepkgname = os.path.split(os.path.normpath(fss.as_pathname())) |
| 243 | if not dirname in sys.path: |
| 244 | sys.path.insert(0, dirname) |
| 245 | basepackage = __import__(basepkgname) |
| 246 | else: |
| 247 | basepackage = None |
| 248 | macfs.SetFolder(pathname) |
| 249 | suitelist = [] |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 250 | allprecompinfo = [] |
| 251 | allsuites = [] |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 252 | for suite in suites: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 253 | code, suite, fss, modname, precompinfo = precompilesuite(suite, basepackage) |
| 254 | if not code: |
| 255 | continue |
| 256 | allprecompinfo = allprecompinfo + precompinfo |
| 257 | suiteinfo = suite, fss, modname |
| 258 | suitelist.append((code, modname)) |
| 259 | allsuites.append(suiteinfo) |
| 260 | for suiteinfo in allsuites: |
| 261 | compilesuite(suiteinfo, major, minor, language, script, fname, basepackage, allprecompinfo) |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 262 | fss, ok = macfs.StandardPutFile('Package module', '__init__.py') |
| 263 | if not ok: |
| 264 | return |
| 265 | fp = open(fss.as_pathname(), 'w') |
| 266 | fss.SetCreatorType('Pyth', 'TEXT') |
| 267 | fp.write('"""\n') |
| 268 | fp.write("Package generated from %s\n"%fname) |
| 269 | fp.write("Resource %s resid %d %s\n"%(resinfo[1], resinfo[0], resinfo[2])) |
| 270 | fp.write('"""\n') |
| 271 | fp.write('import aetools\n') |
| 272 | for code, modname in suitelist: |
| 273 | fp.write("import %s\n" % modname) |
| 274 | fp.write("\n\n_code_to_module = {\n") |
| 275 | for code, modname in suitelist: |
| 276 | fp.write("\t'%s' : %s,\n"%(code, modname)) |
| 277 | fp.write("}\n\n") |
| 278 | fp.write("\n\n_code_to_fullname = {\n") |
| 279 | for code, modname in suitelist: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 280 | fp.write("\t'%s' : ('%s.%s', '%s'),\n"%(code, packagename, modname, modname)) |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 281 | fp.write("}\n\n") |
| 282 | for code, modname in suitelist: |
| 283 | fp.write("from %s import *\n"%modname) |
| 284 | if suitelist: |
| 285 | fp.write("\n\nclass %s(%s_Events"%(packagename, suitelist[0][1])) |
| 286 | for code, modname in suitelist[1:]: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 287 | fp.write(",\n\t\t%s_Events"%modname) |
| 288 | fp.write(",\n\t\taetools.TalkTo):\n") |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 289 | fp.write("\t_signature = '%s'\n\n"%creatorsignature) |
| 290 | fp.close() |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 291 | |
| 292 | def precompilesuite(suite, basepackage=None): |
| 293 | """Parse a single suite without generating the output. This step is needed |
| 294 | so we can resolve recursive references by suites to enums/comps/etc declared |
| 295 | in other suites""" |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 296 | [name, desc, code, level, version, events, classes, comps, enums] = suite |
| 297 | |
| 298 | modname = identify(name) |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 299 | if len(modname) > 28: |
| 300 | modname = modname[:27] |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 301 | fss, ok = macfs.StandardPutFile('Python output file', modname+'.py') |
| 302 | if not ok: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 303 | return None, None, None, None, None |
| 304 | |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 305 | pathname = fss.as_pathname() |
| 306 | modname = os.path.splitext(os.path.split(pathname)[1])[0] |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 307 | |
| 308 | if basepackage and basepackage._code_to_module.has_key(code): |
| 309 | # We are an extension of a baseclass (usually an application extending |
| 310 | # Standard_Suite or so). Import everything from our base module |
| 311 | basemodule = basepackage._code_to_module[code] |
| 312 | else: |
| 313 | # We are not an extension. |
| 314 | basemodule = None |
| 315 | |
| 316 | enumsneeded = {} |
| 317 | for event in events: |
| 318 | findenumsinevent(event, enumsneeded) |
| 319 | |
| 320 | objc = ObjectCompiler(None, basemodule) |
| 321 | for cls in classes: |
| 322 | objc.compileclass(cls) |
| 323 | for cls in classes: |
| 324 | objc.fillclasspropsandelems(cls) |
| 325 | for comp in comps: |
| 326 | objc.compilecomparison(comp) |
| 327 | for enum in enums: |
| 328 | objc.compileenumeration(enum) |
| 329 | |
| 330 | for enum in enumsneeded.keys(): |
| 331 | objc.checkforenum(enum) |
| 332 | |
| 333 | objc.dumpindex() |
| 334 | |
| 335 | precompinfo = objc.getprecompinfo(modname) |
| 336 | |
| 337 | return code, suite, fss, modname, precompinfo |
| 338 | |
| 339 | def compilesuite((suite, fss, modname), major, minor, language, script, fname, basepackage, precompinfo): |
| 340 | """Generate code for a single suite""" |
| 341 | [name, desc, code, level, version, events, classes, comps, enums] = suite |
| 342 | |
| 343 | pathname = fss.as_pathname() |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 344 | fp = open(fss.as_pathname(), 'w') |
Jack Jansen | 18a99f5 | 1996-03-18 13:35:00 +0000 | [diff] [blame] | 345 | fss.SetCreatorType('Pyth', 'TEXT') |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 346 | |
| 347 | fp.write('"""Suite %s: %s\n' % (name, desc)) |
| 348 | fp.write("Level %d, version %d\n\n" % (level, version)) |
| 349 | fp.write("Generated from %s\n"%fname) |
| 350 | fp.write("AETE/AEUT resource version %d/%d, language %d, script %d\n" % \ |
| 351 | (major, minor, language, script)) |
| 352 | fp.write('"""\n\n') |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 353 | |
| 354 | fp.write('import aetools\n') |
| 355 | fp.write('import MacOS\n\n') |
| 356 | fp.write("_code = %s\n\n"% `code`) |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 357 | if basepackage and basepackage._code_to_module.has_key(code): |
| 358 | # We are an extension of a baseclass (usually an application extending |
| 359 | # Standard_Suite or so). Import everything from our base module |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 360 | fp.write('from %s import *\n'%basepackage._code_to_fullname[code][0]) |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 361 | basemodule = basepackage._code_to_module[code] |
| 362 | else: |
| 363 | # We are not an extension. |
| 364 | basemodule = None |
| 365 | compileclassheader(fp, modname, basemodule) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 366 | |
| 367 | enumsneeded = {} |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 368 | if events: |
| 369 | for event in events: |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 370 | compileevent(fp, event, enumsneeded) |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 371 | else: |
| 372 | fp.write("\tpass\n\n") |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 373 | |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 374 | objc = ObjectCompiler(fp, basemodule, precompinfo) |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 375 | for cls in classes: |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 376 | objc.compileclass(cls) |
| 377 | for cls in classes: |
| 378 | objc.fillclasspropsandelems(cls) |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 379 | for comp in comps: |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 380 | objc.compilecomparison(comp) |
| 381 | for enum in enums: |
| 382 | objc.compileenumeration(enum) |
| 383 | |
| 384 | for enum in enumsneeded.keys(): |
| 385 | objc.checkforenum(enum) |
| 386 | |
| 387 | objc.dumpindex() |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 388 | |
| 389 | return code, modname |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 390 | |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 391 | def compileclassheader(fp, name, module=None): |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 392 | """Generate class boilerplate""" |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 393 | classname = '%s_Events'%name |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 394 | if module: |
| 395 | modshortname = string.split(module.__name__, '.')[-1] |
| 396 | baseclassname = '%s_Events'%modshortname |
| 397 | fp.write("class %s(%s):\n\n"%(classname, baseclassname)) |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 398 | else: |
| 399 | fp.write("class %s:\n\n"%classname) |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 400 | |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 401 | def compileevent(fp, event, enumsneeded): |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 402 | """Generate code for a single event""" |
| 403 | [name, desc, code, subcode, returns, accepts, arguments] = event |
| 404 | funcname = identify(name) |
| 405 | # |
| 406 | # generate name->keyword map |
| 407 | # |
| 408 | if arguments: |
| 409 | fp.write("\t_argmap_%s = {\n"%funcname) |
| 410 | for a in arguments: |
| 411 | fp.write("\t\t%s : %s,\n"%(`identify(a[0])`, `a[1]`)) |
| 412 | fp.write("\t}\n\n") |
| 413 | |
| 414 | # |
| 415 | # Generate function header |
| 416 | # |
| 417 | has_arg = (not is_null(accepts)) |
| 418 | opt_arg = (has_arg and is_optional(accepts)) |
| 419 | |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 420 | fp.write("\tdef %s(self, "%funcname) |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 421 | if has_arg: |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 422 | if not opt_arg: |
| 423 | fp.write("_object, ") # Include direct object, if it has one |
| 424 | else: |
| 425 | fp.write("_object=None, ") # Also include if it is optional |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 426 | else: |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 427 | fp.write("_no_object=None, ") # For argument checking |
| 428 | fp.write("_attributes={}, **_arguments):\n") # include attribute dict and args |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 429 | # |
| 430 | # Generate doc string (important, since it may be the only |
| 431 | # available documentation, due to our name-remaping) |
| 432 | # |
| 433 | fp.write('\t\t"""%s: %s\n'%(name, desc)) |
| 434 | if has_arg: |
| 435 | fp.write("\t\tRequired argument: %s\n"%getdatadoc(accepts)) |
| 436 | elif opt_arg: |
| 437 | fp.write("\t\tOptional argument: %s\n"%getdatadoc(accepts)) |
| 438 | for arg in arguments: |
| 439 | fp.write("\t\tKeyword argument %s: %s\n"%(identify(arg[0]), |
| 440 | getdatadoc(arg[2]))) |
| 441 | fp.write("\t\tKeyword argument _attributes: AppleEvent attribute dictionary\n") |
| 442 | if not is_null(returns): |
| 443 | fp.write("\t\tReturns: %s\n"%getdatadoc(returns)) |
| 444 | fp.write('\t\t"""\n') |
| 445 | # |
| 446 | # Fiddle the args so everything ends up in 'arguments' dictionary |
| 447 | # |
| 448 | fp.write("\t\t_code = %s\n"% `code`) |
| 449 | fp.write("\t\t_subcode = %s\n\n"% `subcode`) |
Jack Jansen | 7321514 | 1995-10-09 23:09:23 +0000 | [diff] [blame] | 450 | # |
| 451 | # Do keyword name substitution |
| 452 | # |
| 453 | if arguments: |
| 454 | fp.write("\t\taetools.keysubst(_arguments, self._argmap_%s)\n"%funcname) |
| 455 | else: |
| 456 | fp.write("\t\tif _arguments: raise TypeError, 'No optional args expected'\n") |
| 457 | # |
| 458 | # Stuff required arg (if there is one) into arguments |
| 459 | # |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 460 | if has_arg: |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 461 | fp.write("\t\t_arguments['----'] = _object\n") |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 462 | elif opt_arg: |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 463 | fp.write("\t\tif _object:\n") |
| 464 | fp.write("\t\t\t_arguments['----'] = _object\n") |
| 465 | else: |
| 466 | fp.write("\t\tif _no_object != None: raise TypeError, 'No direct arg expected'\n") |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 467 | fp.write("\n") |
| 468 | # |
Jack Jansen | 7321514 | 1995-10-09 23:09:23 +0000 | [diff] [blame] | 469 | # Do enum-name substitution |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 470 | # |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 471 | for a in arguments: |
| 472 | if is_enum(a[2]): |
| 473 | kname = a[1] |
| 474 | ename = a[2][0] |
Jack Jansen | a2408e9 | 1996-04-16 14:36:46 +0000 | [diff] [blame] | 475 | if ename <> '****': |
| 476 | fp.write("\t\taetools.enumsubst(_arguments, %s, _Enum_%s)\n" % |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 477 | (`kname`, ename)) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 478 | enumsneeded[ename] = 1 |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 479 | fp.write("\n") |
| 480 | # |
| 481 | # Do the transaction |
| 482 | # |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 483 | fp.write("\t\t_reply, _arguments, _attributes = self.send(_code, _subcode,\n") |
| 484 | fp.write("\t\t\t\t_arguments, _attributes)\n") |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 485 | # |
| 486 | # Error handling |
| 487 | # |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 488 | fp.write("\t\tif _arguments.has_key('errn'):\n") |
Jack Jansen | 977fbf2 | 1996-09-20 15:29:59 +0000 | [diff] [blame] | 489 | fp.write("\t\t\traise aetools.Error, aetools.decodeerror(_arguments)\n") |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 490 | fp.write("\t\t# XXXX Optionally decode result\n") |
| 491 | # |
| 492 | # Decode result |
| 493 | # |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 494 | fp.write("\t\tif _arguments.has_key('----'):\n") |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 495 | if is_enum(returns): |
| 496 | fp.write("\t\t\t# XXXX Should do enum remapping here...\n") |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 497 | fp.write("\t\t\treturn _arguments['----']\n") |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 498 | fp.write("\n") |
| 499 | |
| 500 | # print "\n# Command %s -- %s (%s, %s)" % (`name`, `desc`, `code`, `subcode`) |
| 501 | # print "# returns", compiledata(returns) |
| 502 | # print "# accepts", compiledata(accepts) |
| 503 | # for arg in arguments: |
| 504 | # compileargument(arg) |
| 505 | |
| 506 | def compileargument(arg): |
| 507 | [name, keyword, what] = arg |
| 508 | print "# %s (%s)" % (name, `keyword`), compiledata(what) |
| 509 | |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 510 | def findenumsinevent(event, enumsneeded): |
| 511 | """Find all enums for a single event""" |
| 512 | [name, desc, code, subcode, returns, accepts, arguments] = event |
| 513 | for a in arguments: |
| 514 | if is_enum(a[2]): |
| 515 | ename = a[2][0] |
| 516 | if ename <> '****': |
| 517 | enumsneeded[ename] = 1 |
| 518 | |
| 519 | # |
| 520 | # This class stores the code<->name translations for a single module. It is used |
| 521 | # to keep the information while we're compiling the module, but we also keep these objects |
| 522 | # around so if one suite refers to, say, an enum in another suite we know where to |
| 523 | # find it. Finally, if we really can't find a code, the user can add modules by |
| 524 | # hand. |
| 525 | # |
| 526 | class CodeNameMapper: |
| 527 | |
| 528 | def __init__(self): |
| 529 | self.code2name = { |
| 530 | "property" : {}, |
| 531 | "class" : {}, |
| 532 | "enum" : {}, |
| 533 | "comparison" : {}, |
| 534 | } |
| 535 | self.name2code = { |
| 536 | "property" : {}, |
| 537 | "class" : {}, |
| 538 | "enum" : {}, |
| 539 | "comparison" : {}, |
| 540 | } |
| 541 | self.modulename = None |
| 542 | self.star_imported = 0 |
| 543 | |
| 544 | def addnamecode(self, type, name, code): |
| 545 | self.name2code[type][name] = code |
| 546 | if not self.code2name[type].has_key(code): |
| 547 | self.code2name[type][code] = name |
| 548 | |
| 549 | def hasname(self, type, name): |
| 550 | return self.name2code[type].has_key(name) |
| 551 | |
| 552 | def hascode(self, type, code): |
| 553 | return self.code2name[type].has_key(code) |
| 554 | |
| 555 | def findcodename(self, type, code): |
| 556 | if not self.hascode(type, code): |
| 557 | return None, None, None |
| 558 | name = self.code2name[type][code] |
| 559 | if self.modulename and not self.star_imported: |
| 560 | qualname = '%s.%s'%(self.modulename, name) |
| 561 | else: |
| 562 | qualname = name |
| 563 | return name, qualname, self.modulename |
| 564 | |
| 565 | def getall(self, type): |
| 566 | return self.code2name[type].items() |
| 567 | |
| 568 | def addmodule(self, module, name, star_imported): |
| 569 | self.modulename = name |
| 570 | self.star_imported = star_imported |
| 571 | for code, name in module._propdeclarations.items(): |
| 572 | self.addnamecode('property', name, code) |
| 573 | for code, name in module._classdeclarations.items(): |
| 574 | self.addnamecode('class', name, code) |
| 575 | for code in module._enumdeclarations.keys(): |
| 576 | self.addnamecode('enum', '_Enum_'+identify(code), code) |
| 577 | for code, name in module._compdeclarations.items(): |
| 578 | self.addnamecode('comparison', name, code) |
| 579 | |
| 580 | def prepareforexport(self, name=None): |
| 581 | if not self.modulename: |
| 582 | self.modulename = name |
| 583 | return self |
| 584 | |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 585 | class ObjectCompiler: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 586 | def __init__(self, fp, basesuite=None, othernamemappers=None): |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 587 | self.fp = fp |
Jack Jansen | 60762cb | 2000-08-17 22:11:45 +0000 | [diff] [blame] | 588 | self.basesuite = basesuite |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 589 | self.namemappers = [CodeNameMapper()] |
| 590 | if othernamemappers: |
| 591 | self.othernamemappers = othernamemappers[:] |
| 592 | else: |
| 593 | self.othernamemappers = [] |
| 594 | if basesuite: |
| 595 | basemapper = CodeNameMapper() |
| 596 | basemapper.addmodule(basesuite, '', 1) |
| 597 | self.namemappers.append(basemapper) |
| 598 | |
| 599 | def getprecompinfo(self, modname): |
| 600 | list = [] |
| 601 | for mapper in self.namemappers: |
| 602 | emapper = mapper.prepareforexport(modname) |
| 603 | if emapper: |
| 604 | list.append(emapper) |
| 605 | return list |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 606 | |
| 607 | def findcodename(self, type, code): |
| 608 | while 1: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 609 | # First try: check whether we already know about this code. |
| 610 | for mapper in self.namemappers: |
| 611 | if mapper.hascode(type, code): |
| 612 | return mapper.findcodename(type, code) |
| 613 | # Second try: maybe one of the other modules knows about it. |
| 614 | for mapper in self.othernamemappers: |
| 615 | if mapper.hascode(type, code): |
| 616 | self.othernamemappers.remove(mapper) |
| 617 | self.namemappers.append(mapper) |
| 618 | if self.fp: |
| 619 | self.fp.write("import %s\n"%mapper.modulename) |
| 620 | break |
| 621 | else: |
| 622 | # If all this has failed we ask the user for a guess on where it could |
| 623 | # be and retry. |
| 624 | if self.fp: |
| 625 | m = self.askdefinitionmodule(type, code) |
| 626 | else: |
| 627 | m = None |
| 628 | if not m: return None, None, None |
| 629 | mapper = CodeNameMapper() |
| 630 | mapper.addmodule(m, m.__name__, 0) |
| 631 | self.namemappers.append(mapper) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 632 | |
| 633 | def askdefinitionmodule(self, type, code): |
| 634 | fss, ok = macfs.PromptGetFile('Where is %s %s declared?'%(type, code)) |
| 635 | if not ok: return |
| 636 | path, file = os.path.split(fss.as_pathname()) |
| 637 | modname = os.path.splitext(file)[0] |
| 638 | if not path in sys.path: |
| 639 | sys.path.insert(0, path) |
| 640 | m = __import__(modname) |
| 641 | self.fp.write("import %s\n"%modname) |
| 642 | return m |
| 643 | |
| 644 | def compileclass(self, cls): |
| 645 | [name, code, desc, properties, elements] = cls |
| 646 | pname = identify(name) |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 647 | if self.namemappers[0].hascode('class', code): |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 648 | # plural forms and such |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 649 | othername, dummy, dummy = self.namemappers[0].findcodename('class', code) |
| 650 | if self.fp: |
| 651 | self.fp.write("\n%s = %s\n"%(pname, othername)) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 652 | else: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 653 | if self.fp: |
| 654 | self.fp.write('\nclass %s(aetools.ComponentItem):\n' % pname) |
| 655 | self.fp.write('\t"""%s - %s """\n' % (name, desc)) |
| 656 | self.fp.write('\twant = %s\n' % `code`) |
| 657 | self.namemappers[0].addnamecode('class', pname, code) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 658 | for prop in properties: |
| 659 | self.compileproperty(prop) |
| 660 | for elem in elements: |
| 661 | self.compileelement(elem) |
| 662 | |
| 663 | def compileproperty(self, prop): |
| 664 | [name, code, what] = prop |
| 665 | if code == 'c@#!': |
| 666 | # Something silly with plurals. Skip it. |
| 667 | return |
| 668 | pname = identify(name) |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 669 | if self.namemappers[0].hascode('property', code): |
| 670 | # XXXX Why don't we handle these the same as classes?? |
| 671 | if self.fp: |
| 672 | self.fp.write("# repeated property %s %s\n"%(pname, what[1])) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 673 | else: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 674 | if self.fp: |
| 675 | self.fp.write("class %s(aetools.NProperty):\n" % pname) |
| 676 | self.fp.write('\t"""%s - %s """\n' % (name, what[1])) |
| 677 | self.fp.write("\twhich = %s\n" % `code`) |
| 678 | self.fp.write("\twant = %s\n" % `what[0]`) |
| 679 | self.namemappers[0].addnamecode('property', pname, code) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 680 | |
| 681 | def compileelement(self, elem): |
| 682 | [code, keyform] = elem |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 683 | if self.fp: |
| 684 | self.fp.write("# element %s as %s\n" % (`code`, keyform)) |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 685 | |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 686 | def fillclasspropsandelems(self, cls): |
| 687 | [name, code, desc, properties, elements] = cls |
| 688 | cname = identify(name) |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 689 | if self.namemappers[0].hascode('class', code) and \ |
| 690 | self.namemappers[0].findcodename('class', code)[0] != cname: |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 691 | # This is an other name (plural or so) for something else. Skip. |
| 692 | return |
| 693 | plist = [] |
| 694 | elist = [] |
| 695 | for prop in properties: |
| 696 | [pname, pcode, what] = prop |
| 697 | if pcode == 'c@#!': |
| 698 | continue |
| 699 | pname = identify(pname) |
| 700 | plist.append(pname) |
| 701 | for elem in elements: |
| 702 | [ecode, keyform] = elem |
| 703 | if ecode == 'c@#!': |
| 704 | continue |
| 705 | name, ename, module = self.findcodename('class', ecode) |
| 706 | if not name: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 707 | if self.fp: |
| 708 | self.fp.write("# XXXX %s element %s not found!!\n"%(cname, `ecode`)) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 709 | else: |
Jack Jansen | 34d11f0 | 2000-03-07 23:40:13 +0000 | [diff] [blame] | 710 | elist.append((name, ename)) |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 711 | |
| 712 | if self.fp: |
| 713 | self.fp.write("%s._propdict = {\n"%cname) |
| 714 | for n in plist: |
| 715 | self.fp.write("\t'%s' : %s,\n"%(n, n)) |
| 716 | self.fp.write("}\n") |
| 717 | self.fp.write("%s._elemdict = {\n"%cname) |
| 718 | for n, fulln in elist: |
| 719 | self.fp.write("\t'%s' : %s,\n"%(n, fulln)) |
| 720 | self.fp.write("}\n") |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 721 | |
| 722 | def compilecomparison(self, comp): |
| 723 | [name, code, comment] = comp |
| 724 | iname = identify(name) |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 725 | self.namemappers[0].addnamecode('comparison', iname, code) |
| 726 | if self.fp: |
| 727 | self.fp.write("class %s(aetools.NComparison):\n" % iname) |
| 728 | self.fp.write('\t"""%s - %s """\n' % (name, comment)) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 729 | |
| 730 | def compileenumeration(self, enum): |
| 731 | [code, items] = enum |
| 732 | name = "_Enum_%s" % identify(code) |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 733 | if self.fp: |
| 734 | self.fp.write("%s = {\n" % name) |
| 735 | for item in items: |
| 736 | self.compileenumerator(item) |
| 737 | self.fp.write("}\n\n") |
| 738 | self.namemappers[0].addnamecode('enum', name, code) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 739 | return code |
| 740 | |
| 741 | def compileenumerator(self, item): |
| 742 | [name, code, desc] = item |
| 743 | self.fp.write("\t%s : %s,\t# %s\n" % (`identify(name)`, `code`, desc)) |
| 744 | |
| 745 | def checkforenum(self, enum): |
| 746 | """This enum code is used by an event. Make sure it's available""" |
| 747 | name, fullname, module = self.findcodename('enum', enum) |
| 748 | if not name: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 749 | if self.fp: |
| 750 | self.fp.write("# XXXX enum %s not found!!\n"%(enum)) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 751 | return |
| 752 | if module: |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 753 | if self.fp: |
| 754 | self.fp.write("from %s import %s\n"%(module, name)) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 755 | |
| 756 | def dumpindex(self): |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 757 | if not self.fp: |
| 758 | return |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 759 | self.fp.write("\n#\n# Indices of types declared in this module\n#\n") |
| 760 | self.fp.write("_classdeclarations = {\n") |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 761 | for k, v in self.namemappers[0].getall('class'): |
| 762 | self.fp.write("\t%s : %s,\n" % (`k`, v)) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 763 | self.fp.write("}\n") |
| 764 | self.fp.write("\n_propdeclarations = {\n") |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 765 | for k, v in self.namemappers[0].getall('property'): |
| 766 | self.fp.write("\t%s : %s,\n" % (`k`, v)) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 767 | self.fp.write("}\n") |
| 768 | self.fp.write("\n_compdeclarations = {\n") |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 769 | for k, v in self.namemappers[0].getall('comparison'): |
| 770 | self.fp.write("\t%s : %s,\n" % (`k`, v)) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 771 | self.fp.write("}\n") |
| 772 | self.fp.write("\n_enumdeclarations = {\n") |
Jack Jansen | 12b2b76 | 2000-08-20 19:30:56 +0000 | [diff] [blame^] | 773 | for k, v in self.namemappers[0].getall('enum'): |
| 774 | self.fp.write("\t%s : %s,\n" % (`k`, v)) |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 775 | self.fp.write("}\n") |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 776 | |
| 777 | def compiledata(data): |
| 778 | [type, description, flags] = data |
| 779 | return "%s -- %s %s" % (`type`, `description`, compiledataflags(flags)) |
| 780 | |
| 781 | def is_null(data): |
| 782 | return data[0] == 'null' |
| 783 | |
| 784 | def is_optional(data): |
| 785 | return (data[2] & 0x8000) |
| 786 | |
| 787 | def is_enum(data): |
| 788 | return (data[2] & 0x2000) |
| 789 | |
| 790 | def getdatadoc(data): |
| 791 | [type, descr, flags] = data |
| 792 | if descr: |
| 793 | return descr |
| 794 | if type == '****': |
| 795 | return 'anything' |
| 796 | if type == 'obj ': |
| 797 | return 'an AE object reference' |
| 798 | return "undocumented, typecode %s"%`type` |
| 799 | |
| 800 | dataflagdict = {15: "optional", 14: "list", 13: "enum", 12: "mutable"} |
| 801 | def compiledataflags(flags): |
| 802 | bits = [] |
| 803 | for i in range(16): |
| 804 | if flags & (1<<i): |
| 805 | if i in dataflagdict.keys(): |
| 806 | bits.append(dataflagdict[i]) |
| 807 | else: |
| 808 | bits.append(`i`) |
| 809 | return '[%s]' % string.join(bits) |
| 810 | |
| 811 | # XXXX Do we have a set of python keywords somewhere? |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 812 | illegal_ids = [ "for", "in", "from", "and", "or", "not", "print", "class", "return", |
| 813 | "def" ] |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 814 | |
| 815 | def identify(str): |
| 816 | """Turn any string into an identifier: |
| 817 | - replace space by _ |
| 818 | - replace other illegal chars by _xx_ (hex code) |
| 819 | - prepend _ if the result is a python keyword |
| 820 | """ |
Jack Jansen | 6654422 | 1997-08-08 14:49:02 +0000 | [diff] [blame] | 821 | if not str: |
| 822 | return "_empty_ae_name" |
Jack Jansen | 5ccd826 | 1995-07-17 11:43:20 +0000 | [diff] [blame] | 823 | rv = '' |
| 824 | ok = string.letters + '_' |
| 825 | ok2 = ok + string.digits |
| 826 | for c in str: |
| 827 | if c in ok: |
| 828 | rv = rv + c |
| 829 | elif c == ' ': |
| 830 | rv = rv + '_' |
| 831 | else: |
| 832 | rv = rv + '_%02.2x_'%ord(c) |
| 833 | ok = ok2 |
| 834 | if rv in illegal_ids: |
| 835 | rv = '_' + rv |
| 836 | return rv |
| 837 | |
| 838 | # Call the main program |
| 839 | |
| 840 | if __name__ == '__main__': |
| 841 | main() |
| 842 | sys.exit(1) |