| 1 | n/a | """ |
|---|
| 2 | n/a | gensuitemodule - Generate an AE suite module from an aete/aeut resource |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | Based on aete.py. |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | Reading and understanding this code is left as an exercise to the reader. |
|---|
| 7 | n/a | """ |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | from warnings import warnpy3k |
|---|
| 10 | n/a | warnpy3k("In 3.x, the gensuitemodule module is removed.", stacklevel=2) |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | import MacOS |
|---|
| 13 | n/a | import EasyDialogs |
|---|
| 14 | n/a | import os |
|---|
| 15 | n/a | import string |
|---|
| 16 | n/a | import sys |
|---|
| 17 | n/a | import types |
|---|
| 18 | n/a | import StringIO |
|---|
| 19 | n/a | import keyword |
|---|
| 20 | n/a | import macresource |
|---|
| 21 | n/a | import aetools |
|---|
| 22 | n/a | import distutils.sysconfig |
|---|
| 23 | n/a | import OSATerminology |
|---|
| 24 | n/a | from Carbon.Res import * |
|---|
| 25 | n/a | import Carbon.Folder |
|---|
| 26 | n/a | import MacOS |
|---|
| 27 | n/a | import getopt |
|---|
| 28 | n/a | import plistlib |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | _MAC_LIB_FOLDER=os.path.dirname(aetools.__file__) |
|---|
| 31 | n/a | DEFAULT_STANDARD_PACKAGEFOLDER=os.path.join(_MAC_LIB_FOLDER, 'lib-scriptpackages') |
|---|
| 32 | n/a | DEFAULT_USER_PACKAGEFOLDER=distutils.sysconfig.get_python_lib() |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | def usage(): |
|---|
| 35 | n/a | sys.stderr.write("Usage: %s [opts] application-or-resource-file\n" % sys.argv[0]) |
|---|
| 36 | n/a | sys.stderr.write("""Options: |
|---|
| 37 | n/a | --output pkgdir Pathname of the output package (short: -o) |
|---|
| 38 | n/a | --resource Parse resource file in stead of launching application (-r) |
|---|
| 39 | n/a | --base package Use another base package in stead of default StdSuites (-b) |
|---|
| 40 | n/a | --edit old=new Edit suite names, use empty new to skip a suite (-e) |
|---|
| 41 | n/a | --creator code Set creator code for package (-c) |
|---|
| 42 | n/a | --dump Dump aete resource to stdout in stead of creating module (-d) |
|---|
| 43 | n/a | --verbose Tell us what happens (-v) |
|---|
| 44 | n/a | """) |
|---|
| 45 | n/a | sys.exit(1) |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | def main(): |
|---|
| 48 | n/a | if len(sys.argv) > 1: |
|---|
| 49 | n/a | SHORTOPTS = "rb:o:e:c:dv" |
|---|
| 50 | n/a | LONGOPTS = ("resource", "base=", "output=", "edit=", "creator=", "dump", "verbose") |
|---|
| 51 | n/a | try: |
|---|
| 52 | n/a | opts, args = getopt.getopt(sys.argv[1:], SHORTOPTS, LONGOPTS) |
|---|
| 53 | n/a | except getopt.GetoptError: |
|---|
| 54 | n/a | usage() |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | process_func = processfile |
|---|
| 57 | n/a | basepkgname = 'StdSuites' |
|---|
| 58 | n/a | output = None |
|---|
| 59 | n/a | edit_modnames = [] |
|---|
| 60 | n/a | creatorsignature = None |
|---|
| 61 | n/a | dump = None |
|---|
| 62 | n/a | verbose = None |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | for o, a in opts: |
|---|
| 65 | n/a | if o in ('-r', '--resource'): |
|---|
| 66 | n/a | process_func = processfile_fromresource |
|---|
| 67 | n/a | if o in ('-b', '--base'): |
|---|
| 68 | n/a | basepkgname = a |
|---|
| 69 | n/a | if o in ('-o', '--output'): |
|---|
| 70 | n/a | output = a |
|---|
| 71 | n/a | if o in ('-e', '--edit'): |
|---|
| 72 | n/a | split = a.split('=') |
|---|
| 73 | n/a | if len(split) != 2: |
|---|
| 74 | n/a | usage() |
|---|
| 75 | n/a | edit_modnames.append(split) |
|---|
| 76 | n/a | if o in ('-c', '--creator'): |
|---|
| 77 | n/a | if len(a) != 4: |
|---|
| 78 | n/a | sys.stderr.write("creator must be 4-char string\n") |
|---|
| 79 | n/a | sys.exit(1) |
|---|
| 80 | n/a | creatorsignature = a |
|---|
| 81 | n/a | if o in ('-d', '--dump'): |
|---|
| 82 | n/a | dump = sys.stdout |
|---|
| 83 | n/a | if o in ('-v', '--verbose'): |
|---|
| 84 | n/a | verbose = sys.stderr |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | if output and len(args) > 1: |
|---|
| 88 | n/a | sys.stderr.write("%s: cannot specify --output with multiple inputs\n" % sys.argv[0]) |
|---|
| 89 | n/a | sys.exit(1) |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | for filename in args: |
|---|
| 92 | n/a | process_func(filename, output=output, basepkgname=basepkgname, |
|---|
| 93 | n/a | edit_modnames=edit_modnames, creatorsignature=creatorsignature, |
|---|
| 94 | n/a | dump=dump, verbose=verbose) |
|---|
| 95 | n/a | else: |
|---|
| 96 | n/a | main_interactive() |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | def main_interactive(interact=0, basepkgname='StdSuites'): |
|---|
| 99 | n/a | if interact: |
|---|
| 100 | n/a | # Ask for save-filename for each module |
|---|
| 101 | n/a | edit_modnames = None |
|---|
| 102 | n/a | else: |
|---|
| 103 | n/a | # Use default filenames for each module |
|---|
| 104 | n/a | edit_modnames = [] |
|---|
| 105 | n/a | appsfolder = Carbon.Folder.FSFindFolder(-32765, 'apps', 0) |
|---|
| 106 | n/a | filename = EasyDialogs.AskFileForOpen( |
|---|
| 107 | n/a | message='Select scriptable application', |
|---|
| 108 | n/a | dialogOptionFlags=0x1056, # allow selection of .app bundles |
|---|
| 109 | n/a | defaultLocation=appsfolder) |
|---|
| 110 | n/a | if not filename: |
|---|
| 111 | n/a | return |
|---|
| 112 | n/a | if not is_scriptable(filename): |
|---|
| 113 | n/a | if EasyDialogs.AskYesNoCancel( |
|---|
| 114 | n/a | "Warning: application does not seem scriptable", |
|---|
| 115 | n/a | yes="Continue", default=2, no="") <= 0: |
|---|
| 116 | n/a | return |
|---|
| 117 | n/a | try: |
|---|
| 118 | n/a | processfile(filename, edit_modnames=edit_modnames, basepkgname=basepkgname, |
|---|
| 119 | n/a | verbose=sys.stderr) |
|---|
| 120 | n/a | except MacOS.Error, arg: |
|---|
| 121 | n/a | print "Error getting terminology:", arg |
|---|
| 122 | n/a | print "Retry, manually parsing resources" |
|---|
| 123 | n/a | processfile_fromresource(filename, edit_modnames=edit_modnames, |
|---|
| 124 | n/a | basepkgname=basepkgname, verbose=sys.stderr) |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | def is_scriptable(application): |
|---|
| 127 | n/a | """Return true if the application is scriptable""" |
|---|
| 128 | n/a | if os.path.isdir(application): |
|---|
| 129 | n/a | plistfile = os.path.join(application, 'Contents', 'Info.plist') |
|---|
| 130 | n/a | if not os.path.exists(plistfile): |
|---|
| 131 | n/a | return False |
|---|
| 132 | n/a | plist = plistlib.Plist.fromFile(plistfile) |
|---|
| 133 | n/a | return plist.get('NSAppleScriptEnabled', False) |
|---|
| 134 | n/a | # If it is a file test for an aete/aeut resource. |
|---|
| 135 | n/a | currf = CurResFile() |
|---|
| 136 | n/a | try: |
|---|
| 137 | n/a | refno = macresource.open_pathname(application) |
|---|
| 138 | n/a | except MacOS.Error: |
|---|
| 139 | n/a | return False |
|---|
| 140 | n/a | UseResFile(refno) |
|---|
| 141 | n/a | n_terminology = Count1Resources('aete') + Count1Resources('aeut') + \ |
|---|
| 142 | n/a | Count1Resources('scsz') + Count1Resources('osiz') |
|---|
| 143 | n/a | CloseResFile(refno) |
|---|
| 144 | n/a | UseResFile(currf) |
|---|
| 145 | n/a | return n_terminology > 0 |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | def processfile_fromresource(fullname, output=None, basepkgname=None, |
|---|
| 148 | n/a | edit_modnames=None, creatorsignature=None, dump=None, verbose=None): |
|---|
| 149 | n/a | """Process all resources in a single file""" |
|---|
| 150 | n/a | if not is_scriptable(fullname) and verbose: |
|---|
| 151 | n/a | print >>verbose, "Warning: app does not seem scriptable: %s" % fullname |
|---|
| 152 | n/a | cur = CurResFile() |
|---|
| 153 | n/a | if verbose: |
|---|
| 154 | n/a | print >>verbose, "Processing", fullname |
|---|
| 155 | n/a | rf = macresource.open_pathname(fullname) |
|---|
| 156 | n/a | try: |
|---|
| 157 | n/a | UseResFile(rf) |
|---|
| 158 | n/a | resources = [] |
|---|
| 159 | n/a | for i in range(Count1Resources('aete')): |
|---|
| 160 | n/a | res = Get1IndResource('aete', 1+i) |
|---|
| 161 | n/a | resources.append(res) |
|---|
| 162 | n/a | for i in range(Count1Resources('aeut')): |
|---|
| 163 | n/a | res = Get1IndResource('aeut', 1+i) |
|---|
| 164 | n/a | resources.append(res) |
|---|
| 165 | n/a | if verbose: |
|---|
| 166 | n/a | print >>verbose, "\nLISTING aete+aeut RESOURCES IN", repr(fullname) |
|---|
| 167 | n/a | aetelist = [] |
|---|
| 168 | n/a | for res in resources: |
|---|
| 169 | n/a | if verbose: |
|---|
| 170 | n/a | print >>verbose, "decoding", res.GetResInfo(), "..." |
|---|
| 171 | n/a | data = res.data |
|---|
| 172 | n/a | aete = decode(data, verbose) |
|---|
| 173 | n/a | aetelist.append((aete, res.GetResInfo())) |
|---|
| 174 | n/a | finally: |
|---|
| 175 | n/a | if rf != cur: |
|---|
| 176 | n/a | CloseResFile(rf) |
|---|
| 177 | n/a | UseResFile(cur) |
|---|
| 178 | n/a | # switch back (needed for dialogs in Python) |
|---|
| 179 | n/a | UseResFile(cur) |
|---|
| 180 | n/a | if dump: |
|---|
| 181 | n/a | dumpaetelist(aetelist, dump) |
|---|
| 182 | n/a | compileaetelist(aetelist, fullname, output=output, |
|---|
| 183 | n/a | basepkgname=basepkgname, edit_modnames=edit_modnames, |
|---|
| 184 | n/a | creatorsignature=creatorsignature, verbose=verbose) |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | def processfile(fullname, output=None, basepkgname=None, |
|---|
| 187 | n/a | edit_modnames=None, creatorsignature=None, dump=None, |
|---|
| 188 | n/a | verbose=None): |
|---|
| 189 | n/a | """Ask an application for its terminology and process that""" |
|---|
| 190 | n/a | if not is_scriptable(fullname) and verbose: |
|---|
| 191 | n/a | print >>verbose, "Warning: app does not seem scriptable: %s" % fullname |
|---|
| 192 | n/a | if verbose: |
|---|
| 193 | n/a | print >>verbose, "\nASKING FOR aete DICTIONARY IN", repr(fullname) |
|---|
| 194 | n/a | try: |
|---|
| 195 | n/a | aedescobj, launched = OSATerminology.GetAppTerminology(fullname) |
|---|
| 196 | n/a | except MacOS.Error, arg: |
|---|
| 197 | n/a | if arg[0] in (-1701, -192): # errAEDescNotFound, resNotFound |
|---|
| 198 | n/a | if verbose: |
|---|
| 199 | n/a | print >>verbose, "GetAppTerminology failed with errAEDescNotFound/resNotFound, trying manually" |
|---|
| 200 | n/a | aedata, sig = getappterminology(fullname, verbose=verbose) |
|---|
| 201 | n/a | if not creatorsignature: |
|---|
| 202 | n/a | creatorsignature = sig |
|---|
| 203 | n/a | else: |
|---|
| 204 | n/a | raise |
|---|
| 205 | n/a | else: |
|---|
| 206 | n/a | if launched: |
|---|
| 207 | n/a | if verbose: |
|---|
| 208 | n/a | print >>verbose, "Launched", fullname |
|---|
| 209 | n/a | raw = aetools.unpack(aedescobj) |
|---|
| 210 | n/a | if not raw: |
|---|
| 211 | n/a | if verbose: |
|---|
| 212 | n/a | print >>verbose, 'Unpack returned empty value:', raw |
|---|
| 213 | n/a | return |
|---|
| 214 | n/a | if not raw[0].data: |
|---|
| 215 | n/a | if verbose: |
|---|
| 216 | n/a | print >>verbose, 'Unpack returned value without data:', raw |
|---|
| 217 | n/a | return |
|---|
| 218 | n/a | aedata = raw[0] |
|---|
| 219 | n/a | aete = decode(aedata.data, verbose) |
|---|
| 220 | n/a | if dump: |
|---|
| 221 | n/a | dumpaetelist([aete], dump) |
|---|
| 222 | n/a | return |
|---|
| 223 | n/a | compileaete(aete, None, fullname, output=output, basepkgname=basepkgname, |
|---|
| 224 | n/a | creatorsignature=creatorsignature, edit_modnames=edit_modnames, |
|---|
| 225 | n/a | verbose=verbose) |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | def getappterminology(fullname, verbose=None): |
|---|
| 228 | n/a | """Get application terminology by sending an AppleEvent""" |
|---|
| 229 | n/a | # First check that we actually can send AppleEvents |
|---|
| 230 | n/a | if not MacOS.WMAvailable(): |
|---|
| 231 | n/a | raise RuntimeError, "Cannot send AppleEvents, no access to window manager" |
|---|
| 232 | n/a | # Next, a workaround for a bug in MacOS 10.2: sending events will hang unless |
|---|
| 233 | n/a | # you have created an event loop first. |
|---|
| 234 | n/a | import Carbon.Evt |
|---|
| 235 | n/a | Carbon.Evt.WaitNextEvent(0,0) |
|---|
| 236 | n/a | if os.path.isdir(fullname): |
|---|
| 237 | n/a | # Now get the signature of the application, hoping it is a bundle |
|---|
| 238 | n/a | pkginfo = os.path.join(fullname, 'Contents', 'PkgInfo') |
|---|
| 239 | n/a | if not os.path.exists(pkginfo): |
|---|
| 240 | n/a | raise RuntimeError, "No PkgInfo file found" |
|---|
| 241 | n/a | tp_cr = open(pkginfo, 'rb').read() |
|---|
| 242 | n/a | cr = tp_cr[4:8] |
|---|
| 243 | n/a | else: |
|---|
| 244 | n/a | # Assume it is a file |
|---|
| 245 | n/a | cr, tp = MacOS.GetCreatorAndType(fullname) |
|---|
| 246 | n/a | # Let's talk to it and ask for its AETE |
|---|
| 247 | n/a | talker = aetools.TalkTo(cr) |
|---|
| 248 | n/a | try: |
|---|
| 249 | n/a | talker._start() |
|---|
| 250 | n/a | except (MacOS.Error, aetools.Error), arg: |
|---|
| 251 | n/a | if verbose: |
|---|
| 252 | n/a | print >>verbose, 'Warning: start() failed, continuing anyway:', arg |
|---|
| 253 | n/a | reply = talker.send("ascr", "gdte") |
|---|
| 254 | n/a | #reply2 = talker.send("ascr", "gdut") |
|---|
| 255 | n/a | # Now pick the bits out of the return that we need. |
|---|
| 256 | n/a | return reply[1]['----'], cr |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | def compileaetelist(aetelist, fullname, output=None, basepkgname=None, |
|---|
| 260 | n/a | edit_modnames=None, creatorsignature=None, verbose=None): |
|---|
| 261 | n/a | for aete, resinfo in aetelist: |
|---|
| 262 | n/a | compileaete(aete, resinfo, fullname, output=output, |
|---|
| 263 | n/a | basepkgname=basepkgname, edit_modnames=edit_modnames, |
|---|
| 264 | n/a | creatorsignature=creatorsignature, verbose=verbose) |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | def dumpaetelist(aetelist, output): |
|---|
| 267 | n/a | import pprint |
|---|
| 268 | n/a | pprint.pprint(aetelist, output) |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | def decode(data, verbose=None): |
|---|
| 271 | n/a | """Decode a resource into a python data structure""" |
|---|
| 272 | n/a | f = StringIO.StringIO(data) |
|---|
| 273 | n/a | aete = generic(getaete, f) |
|---|
| 274 | n/a | aete = simplify(aete) |
|---|
| 275 | n/a | processed = f.tell() |
|---|
| 276 | n/a | unprocessed = len(f.read()) |
|---|
| 277 | n/a | total = f.tell() |
|---|
| 278 | n/a | if unprocessed and verbose: |
|---|
| 279 | n/a | verbose.write("%d processed + %d unprocessed = %d total\n" % |
|---|
| 280 | n/a | (processed, unprocessed, total)) |
|---|
| 281 | n/a | return aete |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | def simplify(item): |
|---|
| 284 | n/a | """Recursively replace singleton tuples by their constituent item""" |
|---|
| 285 | n/a | if type(item) is types.ListType: |
|---|
| 286 | n/a | return map(simplify, item) |
|---|
| 287 | n/a | elif type(item) == types.TupleType and len(item) == 2: |
|---|
| 288 | n/a | return simplify(item[1]) |
|---|
| 289 | n/a | else: |
|---|
| 290 | n/a | return item |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | # Here follows the aete resource decoder. |
|---|
| 294 | n/a | # It is presented bottom-up instead of top-down because there are direct |
|---|
| 295 | n/a | # references to the lower-level part-decoders from the high-level part-decoders. |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | def getbyte(f, *args): |
|---|
| 298 | n/a | c = f.read(1) |
|---|
| 299 | n/a | if not c: |
|---|
| 300 | n/a | raise EOFError, 'in getbyte' + str(args) |
|---|
| 301 | n/a | return ord(c) |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | def getword(f, *args): |
|---|
| 304 | n/a | getalign(f) |
|---|
| 305 | n/a | s = f.read(2) |
|---|
| 306 | n/a | if len(s) < 2: |
|---|
| 307 | n/a | raise EOFError, 'in getword' + str(args) |
|---|
| 308 | n/a | return (ord(s[0])<<8) | ord(s[1]) |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | def getlong(f, *args): |
|---|
| 311 | n/a | getalign(f) |
|---|
| 312 | n/a | s = f.read(4) |
|---|
| 313 | n/a | if len(s) < 4: |
|---|
| 314 | n/a | raise EOFError, 'in getlong' + str(args) |
|---|
| 315 | n/a | return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | def getostype(f, *args): |
|---|
| 318 | n/a | getalign(f) |
|---|
| 319 | n/a | s = f.read(4) |
|---|
| 320 | n/a | if len(s) < 4: |
|---|
| 321 | n/a | raise EOFError, 'in getostype' + str(args) |
|---|
| 322 | n/a | return s |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | def getpstr(f, *args): |
|---|
| 325 | n/a | c = f.read(1) |
|---|
| 326 | n/a | if len(c) < 1: |
|---|
| 327 | n/a | raise EOFError, 'in getpstr[1]' + str(args) |
|---|
| 328 | n/a | nbytes = ord(c) |
|---|
| 329 | n/a | if nbytes == 0: return '' |
|---|
| 330 | n/a | s = f.read(nbytes) |
|---|
| 331 | n/a | if len(s) < nbytes: |
|---|
| 332 | n/a | raise EOFError, 'in getpstr[2]' + str(args) |
|---|
| 333 | n/a | return s |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | def getalign(f): |
|---|
| 336 | n/a | if f.tell() & 1: |
|---|
| 337 | n/a | c = f.read(1) |
|---|
| 338 | n/a | ##if c != '\0': |
|---|
| 339 | n/a | ## print align:', repr(c) |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | def getlist(f, description, getitem): |
|---|
| 342 | n/a | count = getword(f) |
|---|
| 343 | n/a | list = [] |
|---|
| 344 | n/a | for i in range(count): |
|---|
| 345 | n/a | list.append(generic(getitem, f)) |
|---|
| 346 | n/a | getalign(f) |
|---|
| 347 | n/a | return list |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | def alt_generic(what, f, *args): |
|---|
| 350 | n/a | print "generic", repr(what), args |
|---|
| 351 | n/a | res = vageneric(what, f, args) |
|---|
| 352 | n/a | print '->', repr(res) |
|---|
| 353 | n/a | return res |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | def generic(what, f, *args): |
|---|
| 356 | n/a | if type(what) == types.FunctionType: |
|---|
| 357 | n/a | return apply(what, (f,) + args) |
|---|
| 358 | n/a | if type(what) == types.ListType: |
|---|
| 359 | n/a | record = [] |
|---|
| 360 | n/a | for thing in what: |
|---|
| 361 | n/a | item = apply(generic, thing[:1] + (f,) + thing[1:]) |
|---|
| 362 | n/a | record.append((thing[1], item)) |
|---|
| 363 | n/a | return record |
|---|
| 364 | n/a | return "BAD GENERIC ARGS: %r" % (what,) |
|---|
| 365 | n/a | |
|---|
| 366 | n/a | getdata = [ |
|---|
| 367 | n/a | (getostype, "type"), |
|---|
| 368 | n/a | (getpstr, "description"), |
|---|
| 369 | n/a | (getword, "flags") |
|---|
| 370 | n/a | ] |
|---|
| 371 | n/a | getargument = [ |
|---|
| 372 | n/a | (getpstr, "name"), |
|---|
| 373 | n/a | (getostype, "keyword"), |
|---|
| 374 | n/a | (getdata, "what") |
|---|
| 375 | n/a | ] |
|---|
| 376 | n/a | getevent = [ |
|---|
| 377 | n/a | (getpstr, "name"), |
|---|
| 378 | n/a | (getpstr, "description"), |
|---|
| 379 | n/a | (getostype, "suite code"), |
|---|
| 380 | n/a | (getostype, "event code"), |
|---|
| 381 | n/a | (getdata, "returns"), |
|---|
| 382 | n/a | (getdata, "accepts"), |
|---|
| 383 | n/a | (getlist, "optional arguments", getargument) |
|---|
| 384 | n/a | ] |
|---|
| 385 | n/a | getproperty = [ |
|---|
| 386 | n/a | (getpstr, "name"), |
|---|
| 387 | n/a | (getostype, "code"), |
|---|
| 388 | n/a | (getdata, "what") |
|---|
| 389 | n/a | ] |
|---|
| 390 | n/a | getelement = [ |
|---|
| 391 | n/a | (getostype, "type"), |
|---|
| 392 | n/a | (getlist, "keyform", getostype) |
|---|
| 393 | n/a | ] |
|---|
| 394 | n/a | getclass = [ |
|---|
| 395 | n/a | (getpstr, "name"), |
|---|
| 396 | n/a | (getostype, "class code"), |
|---|
| 397 | n/a | (getpstr, "description"), |
|---|
| 398 | n/a | (getlist, "properties", getproperty), |
|---|
| 399 | n/a | (getlist, "elements", getelement) |
|---|
| 400 | n/a | ] |
|---|
| 401 | n/a | getcomparison = [ |
|---|
| 402 | n/a | (getpstr, "operator name"), |
|---|
| 403 | n/a | (getostype, "operator ID"), |
|---|
| 404 | n/a | (getpstr, "operator comment"), |
|---|
| 405 | n/a | ] |
|---|
| 406 | n/a | getenumerator = [ |
|---|
| 407 | n/a | (getpstr, "enumerator name"), |
|---|
| 408 | n/a | (getostype, "enumerator ID"), |
|---|
| 409 | n/a | (getpstr, "enumerator comment") |
|---|
| 410 | n/a | ] |
|---|
| 411 | n/a | getenumeration = [ |
|---|
| 412 | n/a | (getostype, "enumeration ID"), |
|---|
| 413 | n/a | (getlist, "enumerator", getenumerator) |
|---|
| 414 | n/a | ] |
|---|
| 415 | n/a | getsuite = [ |
|---|
| 416 | n/a | (getpstr, "suite name"), |
|---|
| 417 | n/a | (getpstr, "suite description"), |
|---|
| 418 | n/a | (getostype, "suite ID"), |
|---|
| 419 | n/a | (getword, "suite level"), |
|---|
| 420 | n/a | (getword, "suite version"), |
|---|
| 421 | n/a | (getlist, "events", getevent), |
|---|
| 422 | n/a | (getlist, "classes", getclass), |
|---|
| 423 | n/a | (getlist, "comparisons", getcomparison), |
|---|
| 424 | n/a | (getlist, "enumerations", getenumeration) |
|---|
| 425 | n/a | ] |
|---|
| 426 | n/a | getaete = [ |
|---|
| 427 | n/a | (getword, "major/minor version in BCD"), |
|---|
| 428 | n/a | (getword, "language code"), |
|---|
| 429 | n/a | (getword, "script code"), |
|---|
| 430 | n/a | (getlist, "suites", getsuite) |
|---|
| 431 | n/a | ] |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | def compileaete(aete, resinfo, fname, output=None, basepkgname=None, |
|---|
| 434 | n/a | edit_modnames=None, creatorsignature=None, verbose=None): |
|---|
| 435 | n/a | """Generate code for a full aete resource. fname passed for doc purposes""" |
|---|
| 436 | n/a | [version, language, script, suites] = aete |
|---|
| 437 | n/a | major, minor = divmod(version, 256) |
|---|
| 438 | n/a | if not creatorsignature: |
|---|
| 439 | n/a | creatorsignature, dummy = MacOS.GetCreatorAndType(fname) |
|---|
| 440 | n/a | packagename = identify(os.path.splitext(os.path.basename(fname))[0]) |
|---|
| 441 | n/a | if language: |
|---|
| 442 | n/a | packagename = packagename+'_lang%d'%language |
|---|
| 443 | n/a | if script: |
|---|
| 444 | n/a | packagename = packagename+'_script%d'%script |
|---|
| 445 | n/a | if len(packagename) > 27: |
|---|
| 446 | n/a | packagename = packagename[:27] |
|---|
| 447 | n/a | if output: |
|---|
| 448 | n/a | # XXXX Put this in site-packages if it isn't a full pathname? |
|---|
| 449 | n/a | if not os.path.exists(output): |
|---|
| 450 | n/a | os.mkdir(output) |
|---|
| 451 | n/a | pathname = output |
|---|
| 452 | n/a | else: |
|---|
| 453 | n/a | pathname = EasyDialogs.AskFolder(message='Create and select package folder for %s'%packagename, |
|---|
| 454 | n/a | defaultLocation=DEFAULT_USER_PACKAGEFOLDER) |
|---|
| 455 | n/a | output = pathname |
|---|
| 456 | n/a | if not pathname: |
|---|
| 457 | n/a | return |
|---|
| 458 | n/a | packagename = os.path.split(os.path.normpath(pathname))[1] |
|---|
| 459 | n/a | if not basepkgname: |
|---|
| 460 | n/a | basepkgname = EasyDialogs.AskFolder(message='Package folder for base suite (usually StdSuites)', |
|---|
| 461 | n/a | defaultLocation=DEFAULT_STANDARD_PACKAGEFOLDER) |
|---|
| 462 | n/a | if basepkgname: |
|---|
| 463 | n/a | dirname, basepkgname = os.path.split(os.path.normpath(basepkgname)) |
|---|
| 464 | n/a | if dirname and not dirname in sys.path: |
|---|
| 465 | n/a | sys.path.insert(0, dirname) |
|---|
| 466 | n/a | basepackage = __import__(basepkgname) |
|---|
| 467 | n/a | else: |
|---|
| 468 | n/a | basepackage = None |
|---|
| 469 | n/a | suitelist = [] |
|---|
| 470 | n/a | allprecompinfo = [] |
|---|
| 471 | n/a | allsuites = [] |
|---|
| 472 | n/a | for suite in suites: |
|---|
| 473 | n/a | compiler = SuiteCompiler(suite, basepackage, output, edit_modnames, verbose) |
|---|
| 474 | n/a | code, modname, precompinfo = compiler.precompilesuite() |
|---|
| 475 | n/a | if not code: |
|---|
| 476 | n/a | continue |
|---|
| 477 | n/a | allprecompinfo = allprecompinfo + precompinfo |
|---|
| 478 | n/a | suiteinfo = suite, pathname, modname |
|---|
| 479 | n/a | suitelist.append((code, modname)) |
|---|
| 480 | n/a | allsuites.append(compiler) |
|---|
| 481 | n/a | for compiler in allsuites: |
|---|
| 482 | n/a | compiler.compilesuite(major, minor, language, script, fname, allprecompinfo) |
|---|
| 483 | n/a | initfilename = os.path.join(output, '__init__.py') |
|---|
| 484 | n/a | fp = open(initfilename, 'w') |
|---|
| 485 | n/a | MacOS.SetCreatorAndType(initfilename, 'Pyth', 'TEXT') |
|---|
| 486 | n/a | fp.write('"""\n') |
|---|
| 487 | n/a | fp.write("Package generated from %s\n"%ascii(fname)) |
|---|
| 488 | n/a | if resinfo: |
|---|
| 489 | n/a | fp.write("Resource %s resid %d %s\n"%(ascii(resinfo[1]), resinfo[0], ascii(resinfo[2]))) |
|---|
| 490 | n/a | fp.write('"""\n') |
|---|
| 491 | n/a | fp.write('import aetools\n') |
|---|
| 492 | n/a | fp.write('Error = aetools.Error\n') |
|---|
| 493 | n/a | suitelist.sort() |
|---|
| 494 | n/a | for code, modname in suitelist: |
|---|
| 495 | n/a | fp.write("import %s\n" % modname) |
|---|
| 496 | n/a | fp.write("\n\n_code_to_module = {\n") |
|---|
| 497 | n/a | for code, modname in suitelist: |
|---|
| 498 | n/a | fp.write(" '%s' : %s,\n"%(ascii(code), modname)) |
|---|
| 499 | n/a | fp.write("}\n\n") |
|---|
| 500 | n/a | fp.write("\n\n_code_to_fullname = {\n") |
|---|
| 501 | n/a | for code, modname in suitelist: |
|---|
| 502 | n/a | fp.write(" '%s' : ('%s.%s', '%s'),\n"%(ascii(code), packagename, modname, modname)) |
|---|
| 503 | n/a | fp.write("}\n\n") |
|---|
| 504 | n/a | for code, modname in suitelist: |
|---|
| 505 | n/a | fp.write("from %s import *\n"%modname) |
|---|
| 506 | n/a | |
|---|
| 507 | n/a | # Generate property dicts and element dicts for all types declared in this module |
|---|
| 508 | n/a | fp.write("\ndef getbaseclasses(v):\n") |
|---|
| 509 | n/a | fp.write(" if not getattr(v, '_propdict', None):\n") |
|---|
| 510 | n/a | fp.write(" v._propdict = {}\n") |
|---|
| 511 | n/a | fp.write(" v._elemdict = {}\n") |
|---|
| 512 | n/a | fp.write(" for superclassname in getattr(v, '_superclassnames', []):\n") |
|---|
| 513 | n/a | fp.write(" superclass = eval(superclassname)\n") |
|---|
| 514 | n/a | fp.write(" getbaseclasses(superclass)\n") |
|---|
| 515 | n/a | fp.write(" v._propdict.update(getattr(superclass, '_propdict', {}))\n") |
|---|
| 516 | n/a | fp.write(" v._elemdict.update(getattr(superclass, '_elemdict', {}))\n") |
|---|
| 517 | n/a | fp.write(" v._propdict.update(getattr(v, '_privpropdict', {}))\n") |
|---|
| 518 | n/a | fp.write(" v._elemdict.update(getattr(v, '_privelemdict', {}))\n") |
|---|
| 519 | n/a | fp.write("\n") |
|---|
| 520 | n/a | fp.write("import StdSuites\n") |
|---|
| 521 | n/a | allprecompinfo.sort() |
|---|
| 522 | n/a | if allprecompinfo: |
|---|
| 523 | n/a | fp.write("\n#\n# Set property and element dictionaries now that all classes have been defined\n#\n") |
|---|
| 524 | n/a | for codenamemapper in allprecompinfo: |
|---|
| 525 | n/a | for k, v in codenamemapper.getall('class'): |
|---|
| 526 | n/a | fp.write("getbaseclasses(%s)\n" % v) |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | # Generate a code-to-name mapper for all of the types (classes) declared in this module |
|---|
| 529 | n/a | application_class = None |
|---|
| 530 | n/a | if allprecompinfo: |
|---|
| 531 | n/a | fp.write("\n#\n# Indices of types declared in this module\n#\n") |
|---|
| 532 | n/a | fp.write("_classdeclarations = {\n") |
|---|
| 533 | n/a | for codenamemapper in allprecompinfo: |
|---|
| 534 | n/a | for k, v in codenamemapper.getall('class'): |
|---|
| 535 | n/a | fp.write(" %r : %s,\n" % (k, v)) |
|---|
| 536 | n/a | if k == 'capp': |
|---|
| 537 | n/a | application_class = v |
|---|
| 538 | n/a | fp.write("}\n") |
|---|
| 539 | n/a | |
|---|
| 540 | n/a | |
|---|
| 541 | n/a | if suitelist: |
|---|
| 542 | n/a | fp.write("\n\nclass %s(%s_Events"%(packagename, suitelist[0][1])) |
|---|
| 543 | n/a | for code, modname in suitelist[1:]: |
|---|
| 544 | n/a | fp.write(",\n %s_Events"%modname) |
|---|
| 545 | n/a | fp.write(",\n aetools.TalkTo):\n") |
|---|
| 546 | n/a | fp.write(" _signature = %r\n\n"%(creatorsignature,)) |
|---|
| 547 | n/a | fp.write(" _moduleName = '%s'\n\n"%packagename) |
|---|
| 548 | n/a | if application_class: |
|---|
| 549 | n/a | fp.write(" _elemdict = %s._elemdict\n" % application_class) |
|---|
| 550 | n/a | fp.write(" _propdict = %s._propdict\n" % application_class) |
|---|
| 551 | n/a | fp.close() |
|---|
| 552 | n/a | |
|---|
| 553 | n/a | class SuiteCompiler: |
|---|
| 554 | n/a | def __init__(self, suite, basepackage, output, edit_modnames, verbose): |
|---|
| 555 | n/a | self.suite = suite |
|---|
| 556 | n/a | self.basepackage = basepackage |
|---|
| 557 | n/a | self.edit_modnames = edit_modnames |
|---|
| 558 | n/a | self.output = output |
|---|
| 559 | n/a | self.verbose = verbose |
|---|
| 560 | n/a | |
|---|
| 561 | n/a | # Set by precompilesuite |
|---|
| 562 | n/a | self.pathname = None |
|---|
| 563 | n/a | self.modname = None |
|---|
| 564 | n/a | |
|---|
| 565 | n/a | # Set by compilesuite |
|---|
| 566 | n/a | self.fp = None |
|---|
| 567 | n/a | self.basemodule = None |
|---|
| 568 | n/a | self.enumsneeded = {} |
|---|
| 569 | n/a | |
|---|
| 570 | n/a | def precompilesuite(self): |
|---|
| 571 | n/a | """Parse a single suite without generating the output. This step is needed |
|---|
| 572 | n/a | so we can resolve recursive references by suites to enums/comps/etc declared |
|---|
| 573 | n/a | in other suites""" |
|---|
| 574 | n/a | [name, desc, code, level, version, events, classes, comps, enums] = self.suite |
|---|
| 575 | n/a | |
|---|
| 576 | n/a | modname = identify(name) |
|---|
| 577 | n/a | if len(modname) > 28: |
|---|
| 578 | n/a | modname = modname[:27] |
|---|
| 579 | n/a | if self.edit_modnames is None: |
|---|
| 580 | n/a | self.pathname = EasyDialogs.AskFileForSave(message='Python output file', |
|---|
| 581 | n/a | savedFileName=modname+'.py') |
|---|
| 582 | n/a | else: |
|---|
| 583 | n/a | for old, new in self.edit_modnames: |
|---|
| 584 | n/a | if old == modname: |
|---|
| 585 | n/a | modname = new |
|---|
| 586 | n/a | if modname: |
|---|
| 587 | n/a | self.pathname = os.path.join(self.output, modname + '.py') |
|---|
| 588 | n/a | else: |
|---|
| 589 | n/a | self.pathname = None |
|---|
| 590 | n/a | if not self.pathname: |
|---|
| 591 | n/a | return None, None, None |
|---|
| 592 | n/a | |
|---|
| 593 | n/a | self.modname = os.path.splitext(os.path.split(self.pathname)[1])[0] |
|---|
| 594 | n/a | |
|---|
| 595 | n/a | if self.basepackage and code in self.basepackage._code_to_module: |
|---|
| 596 | n/a | # We are an extension of a baseclass (usually an application extending |
|---|
| 597 | n/a | # Standard_Suite or so). Import everything from our base module |
|---|
| 598 | n/a | basemodule = self.basepackage._code_to_module[code] |
|---|
| 599 | n/a | else: |
|---|
| 600 | n/a | # We are not an extension. |
|---|
| 601 | n/a | basemodule = None |
|---|
| 602 | n/a | |
|---|
| 603 | n/a | self.enumsneeded = {} |
|---|
| 604 | n/a | for event in events: |
|---|
| 605 | n/a | self.findenumsinevent(event) |
|---|
| 606 | n/a | |
|---|
| 607 | n/a | objc = ObjectCompiler(None, self.modname, basemodule, interact=(self.edit_modnames is None), |
|---|
| 608 | n/a | verbose=self.verbose) |
|---|
| 609 | n/a | for cls in classes: |
|---|
| 610 | n/a | objc.compileclass(cls) |
|---|
| 611 | n/a | for cls in classes: |
|---|
| 612 | n/a | objc.fillclasspropsandelems(cls) |
|---|
| 613 | n/a | for comp in comps: |
|---|
| 614 | n/a | objc.compilecomparison(comp) |
|---|
| 615 | n/a | for enum in enums: |
|---|
| 616 | n/a | objc.compileenumeration(enum) |
|---|
| 617 | n/a | |
|---|
| 618 | n/a | for enum in self.enumsneeded.keys(): |
|---|
| 619 | n/a | objc.checkforenum(enum) |
|---|
| 620 | n/a | |
|---|
| 621 | n/a | objc.dumpindex() |
|---|
| 622 | n/a | |
|---|
| 623 | n/a | precompinfo = objc.getprecompinfo(self.modname) |
|---|
| 624 | n/a | |
|---|
| 625 | n/a | return code, self.modname, precompinfo |
|---|
| 626 | n/a | |
|---|
| 627 | n/a | def compilesuite(self, major, minor, language, script, fname, precompinfo): |
|---|
| 628 | n/a | """Generate code for a single suite""" |
|---|
| 629 | n/a | [name, desc, code, level, version, events, classes, comps, enums] = self.suite |
|---|
| 630 | n/a | # Sort various lists, so re-generated source is easier compared |
|---|
| 631 | n/a | def class_sorter(k1, k2): |
|---|
| 632 | n/a | """Sort classes by code, and make sure main class sorts before synonyms""" |
|---|
| 633 | n/a | # [name, code, desc, properties, elements] = cls |
|---|
| 634 | n/a | if k1[1] < k2[1]: return -1 |
|---|
| 635 | n/a | if k1[1] > k2[1]: return 1 |
|---|
| 636 | n/a | if not k2[3] or k2[3][0][1] == 'c@#!': |
|---|
| 637 | n/a | # This is a synonym, the other one is better |
|---|
| 638 | n/a | return -1 |
|---|
| 639 | n/a | if not k1[3] or k1[3][0][1] == 'c@#!': |
|---|
| 640 | n/a | # This is a synonym, the other one is better |
|---|
| 641 | n/a | return 1 |
|---|
| 642 | n/a | return 0 |
|---|
| 643 | n/a | |
|---|
| 644 | n/a | events.sort() |
|---|
| 645 | n/a | classes.sort(class_sorter) |
|---|
| 646 | n/a | comps.sort() |
|---|
| 647 | n/a | enums.sort() |
|---|
| 648 | n/a | |
|---|
| 649 | n/a | self.fp = fp = open(self.pathname, 'w') |
|---|
| 650 | n/a | MacOS.SetCreatorAndType(self.pathname, 'Pyth', 'TEXT') |
|---|
| 651 | n/a | |
|---|
| 652 | n/a | fp.write('"""Suite %s: %s\n' % (ascii(name), ascii(desc))) |
|---|
| 653 | n/a | fp.write("Level %d, version %d\n\n" % (level, version)) |
|---|
| 654 | n/a | fp.write("Generated from %s\n"%ascii(fname)) |
|---|
| 655 | n/a | fp.write("AETE/AEUT resource version %d/%d, language %d, script %d\n" % \ |
|---|
| 656 | n/a | (major, minor, language, script)) |
|---|
| 657 | n/a | fp.write('"""\n\n') |
|---|
| 658 | n/a | |
|---|
| 659 | n/a | fp.write('import aetools\n') |
|---|
| 660 | n/a | fp.write('import MacOS\n\n') |
|---|
| 661 | n/a | fp.write("_code = %r\n\n"% (code,)) |
|---|
| 662 | n/a | if self.basepackage and code in self.basepackage._code_to_module: |
|---|
| 663 | n/a | # We are an extension of a baseclass (usually an application extending |
|---|
| 664 | n/a | # Standard_Suite or so). Import everything from our base module |
|---|
| 665 | n/a | fp.write('from %s import *\n'%self.basepackage._code_to_fullname[code][0]) |
|---|
| 666 | n/a | basemodule = self.basepackage._code_to_module[code] |
|---|
| 667 | n/a | elif self.basepackage and code.lower() in self.basepackage._code_to_module: |
|---|
| 668 | n/a | # This is needed by CodeWarrior and some others. |
|---|
| 669 | n/a | fp.write('from %s import *\n'%self.basepackage._code_to_fullname[code.lower()][0]) |
|---|
| 670 | n/a | basemodule = self.basepackage._code_to_module[code.lower()] |
|---|
| 671 | n/a | else: |
|---|
| 672 | n/a | # We are not an extension. |
|---|
| 673 | n/a | basemodule = None |
|---|
| 674 | n/a | self.basemodule = basemodule |
|---|
| 675 | n/a | self.compileclassheader() |
|---|
| 676 | n/a | |
|---|
| 677 | n/a | self.enumsneeded = {} |
|---|
| 678 | n/a | if events: |
|---|
| 679 | n/a | for event in events: |
|---|
| 680 | n/a | self.compileevent(event) |
|---|
| 681 | n/a | else: |
|---|
| 682 | n/a | fp.write(" pass\n\n") |
|---|
| 683 | n/a | |
|---|
| 684 | n/a | objc = ObjectCompiler(fp, self.modname, basemodule, precompinfo, interact=(self.edit_modnames is None), |
|---|
| 685 | n/a | verbose=self.verbose) |
|---|
| 686 | n/a | for cls in classes: |
|---|
| 687 | n/a | objc.compileclass(cls) |
|---|
| 688 | n/a | for cls in classes: |
|---|
| 689 | n/a | objc.fillclasspropsandelems(cls) |
|---|
| 690 | n/a | for comp in comps: |
|---|
| 691 | n/a | objc.compilecomparison(comp) |
|---|
| 692 | n/a | for enum in enums: |
|---|
| 693 | n/a | objc.compileenumeration(enum) |
|---|
| 694 | n/a | |
|---|
| 695 | n/a | for enum in self.enumsneeded.keys(): |
|---|
| 696 | n/a | objc.checkforenum(enum) |
|---|
| 697 | n/a | |
|---|
| 698 | n/a | objc.dumpindex() |
|---|
| 699 | n/a | |
|---|
| 700 | n/a | def compileclassheader(self): |
|---|
| 701 | n/a | """Generate class boilerplate""" |
|---|
| 702 | n/a | classname = '%s_Events'%self.modname |
|---|
| 703 | n/a | if self.basemodule: |
|---|
| 704 | n/a | modshortname = string.split(self.basemodule.__name__, '.')[-1] |
|---|
| 705 | n/a | baseclassname = '%s_Events'%modshortname |
|---|
| 706 | n/a | self.fp.write("class %s(%s):\n\n"%(classname, baseclassname)) |
|---|
| 707 | n/a | else: |
|---|
| 708 | n/a | self.fp.write("class %s:\n\n"%classname) |
|---|
| 709 | n/a | |
|---|
| 710 | n/a | def compileevent(self, event): |
|---|
| 711 | n/a | """Generate code for a single event""" |
|---|
| 712 | n/a | [name, desc, code, subcode, returns, accepts, arguments] = event |
|---|
| 713 | n/a | fp = self.fp |
|---|
| 714 | n/a | funcname = identify(name) |
|---|
| 715 | n/a | # |
|---|
| 716 | n/a | # generate name->keyword map |
|---|
| 717 | n/a | # |
|---|
| 718 | n/a | if arguments: |
|---|
| 719 | n/a | fp.write(" _argmap_%s = {\n"%funcname) |
|---|
| 720 | n/a | for a in arguments: |
|---|
| 721 | n/a | fp.write(" %r : %r,\n"%(identify(a[0]), a[1])) |
|---|
| 722 | n/a | fp.write(" }\n\n") |
|---|
| 723 | n/a | |
|---|
| 724 | n/a | # |
|---|
| 725 | n/a | # Generate function header |
|---|
| 726 | n/a | # |
|---|
| 727 | n/a | has_arg = (not is_null(accepts)) |
|---|
| 728 | n/a | opt_arg = (has_arg and is_optional(accepts)) |
|---|
| 729 | n/a | |
|---|
| 730 | n/a | fp.write(" def %s(self, "%funcname) |
|---|
| 731 | n/a | if has_arg: |
|---|
| 732 | n/a | if not opt_arg: |
|---|
| 733 | n/a | fp.write("_object, ") # Include direct object, if it has one |
|---|
| 734 | n/a | else: |
|---|
| 735 | n/a | fp.write("_object=None, ") # Also include if it is optional |
|---|
| 736 | n/a | else: |
|---|
| 737 | n/a | fp.write("_no_object=None, ") # For argument checking |
|---|
| 738 | n/a | fp.write("_attributes={}, **_arguments):\n") # include attribute dict and args |
|---|
| 739 | n/a | # |
|---|
| 740 | n/a | # Generate doc string (important, since it may be the only |
|---|
| 741 | n/a | # available documentation, due to our name-remaping) |
|---|
| 742 | n/a | # |
|---|
| 743 | n/a | fp.write(' """%s: %s\n'%(ascii(name), ascii(desc))) |
|---|
| 744 | n/a | if has_arg: |
|---|
| 745 | n/a | fp.write(" Required argument: %s\n"%getdatadoc(accepts)) |
|---|
| 746 | n/a | elif opt_arg: |
|---|
| 747 | n/a | fp.write(" Optional argument: %s\n"%getdatadoc(accepts)) |
|---|
| 748 | n/a | for arg in arguments: |
|---|
| 749 | n/a | fp.write(" Keyword argument %s: %s\n"%(identify(arg[0]), |
|---|
| 750 | n/a | getdatadoc(arg[2]))) |
|---|
| 751 | n/a | fp.write(" Keyword argument _attributes: AppleEvent attribute dictionary\n") |
|---|
| 752 | n/a | if not is_null(returns): |
|---|
| 753 | n/a | fp.write(" Returns: %s\n"%getdatadoc(returns)) |
|---|
| 754 | n/a | fp.write(' """\n') |
|---|
| 755 | n/a | # |
|---|
| 756 | n/a | # Fiddle the args so everything ends up in 'arguments' dictionary |
|---|
| 757 | n/a | # |
|---|
| 758 | n/a | fp.write(" _code = %r\n"% (code,)) |
|---|
| 759 | n/a | fp.write(" _subcode = %r\n\n"% (subcode,)) |
|---|
| 760 | n/a | # |
|---|
| 761 | n/a | # Do keyword name substitution |
|---|
| 762 | n/a | # |
|---|
| 763 | n/a | if arguments: |
|---|
| 764 | n/a | fp.write(" aetools.keysubst(_arguments, self._argmap_%s)\n"%funcname) |
|---|
| 765 | n/a | else: |
|---|
| 766 | n/a | fp.write(" if _arguments: raise TypeError, 'No optional args expected'\n") |
|---|
| 767 | n/a | # |
|---|
| 768 | n/a | # Stuff required arg (if there is one) into arguments |
|---|
| 769 | n/a | # |
|---|
| 770 | n/a | if has_arg: |
|---|
| 771 | n/a | fp.write(" _arguments['----'] = _object\n") |
|---|
| 772 | n/a | elif opt_arg: |
|---|
| 773 | n/a | fp.write(" if _object:\n") |
|---|
| 774 | n/a | fp.write(" _arguments['----'] = _object\n") |
|---|
| 775 | n/a | else: |
|---|
| 776 | n/a | fp.write(" if _no_object is not None: raise TypeError, 'No direct arg expected'\n") |
|---|
| 777 | n/a | fp.write("\n") |
|---|
| 778 | n/a | # |
|---|
| 779 | n/a | # Do enum-name substitution |
|---|
| 780 | n/a | # |
|---|
| 781 | n/a | for a in arguments: |
|---|
| 782 | n/a | if is_enum(a[2]): |
|---|
| 783 | n/a | kname = a[1] |
|---|
| 784 | n/a | ename = a[2][0] |
|---|
| 785 | n/a | if ename != '****': |
|---|
| 786 | n/a | fp.write(" aetools.enumsubst(_arguments, %r, _Enum_%s)\n" % |
|---|
| 787 | n/a | (kname, identify(ename))) |
|---|
| 788 | n/a | self.enumsneeded[ename] = 1 |
|---|
| 789 | n/a | fp.write("\n") |
|---|
| 790 | n/a | # |
|---|
| 791 | n/a | # Do the transaction |
|---|
| 792 | n/a | # |
|---|
| 793 | n/a | fp.write(" _reply, _arguments, _attributes = self.send(_code, _subcode,\n") |
|---|
| 794 | n/a | fp.write(" _arguments, _attributes)\n") |
|---|
| 795 | n/a | # |
|---|
| 796 | n/a | # Error handling |
|---|
| 797 | n/a | # |
|---|
| 798 | n/a | fp.write(" if _arguments.get('errn', 0):\n") |
|---|
| 799 | n/a | fp.write(" raise aetools.Error, aetools.decodeerror(_arguments)\n") |
|---|
| 800 | n/a | fp.write(" # XXXX Optionally decode result\n") |
|---|
| 801 | n/a | # |
|---|
| 802 | n/a | # Decode result |
|---|
| 803 | n/a | # |
|---|
| 804 | n/a | fp.write(" if '----' in _arguments:\n") |
|---|
| 805 | n/a | if is_enum(returns): |
|---|
| 806 | n/a | fp.write(" # XXXX Should do enum remapping here...\n") |
|---|
| 807 | n/a | fp.write(" return _arguments['----']\n") |
|---|
| 808 | n/a | fp.write("\n") |
|---|
| 809 | n/a | |
|---|
| 810 | n/a | def findenumsinevent(self, event): |
|---|
| 811 | n/a | """Find all enums for a single event""" |
|---|
| 812 | n/a | [name, desc, code, subcode, returns, accepts, arguments] = event |
|---|
| 813 | n/a | for a in arguments: |
|---|
| 814 | n/a | if is_enum(a[2]): |
|---|
| 815 | n/a | ename = a[2][0] |
|---|
| 816 | n/a | if ename != '****': |
|---|
| 817 | n/a | self.enumsneeded[ename] = 1 |
|---|
| 818 | n/a | |
|---|
| 819 | n/a | # |
|---|
| 820 | n/a | # This class stores the code<->name translations for a single module. It is used |
|---|
| 821 | n/a | # to keep the information while we're compiling the module, but we also keep these objects |
|---|
| 822 | n/a | # around so if one suite refers to, say, an enum in another suite we know where to |
|---|
| 823 | n/a | # find it. Finally, if we really can't find a code, the user can add modules by |
|---|
| 824 | n/a | # hand. |
|---|
| 825 | n/a | # |
|---|
| 826 | n/a | class CodeNameMapper: |
|---|
| 827 | n/a | |
|---|
| 828 | n/a | def __init__(self, interact=1, verbose=None): |
|---|
| 829 | n/a | self.code2name = { |
|---|
| 830 | n/a | "property" : {}, |
|---|
| 831 | n/a | "class" : {}, |
|---|
| 832 | n/a | "enum" : {}, |
|---|
| 833 | n/a | "comparison" : {}, |
|---|
| 834 | n/a | } |
|---|
| 835 | n/a | self.name2code = { |
|---|
| 836 | n/a | "property" : {}, |
|---|
| 837 | n/a | "class" : {}, |
|---|
| 838 | n/a | "enum" : {}, |
|---|
| 839 | n/a | "comparison" : {}, |
|---|
| 840 | n/a | } |
|---|
| 841 | n/a | self.modulename = None |
|---|
| 842 | n/a | self.star_imported = 0 |
|---|
| 843 | n/a | self.can_interact = interact |
|---|
| 844 | n/a | self.verbose = verbose |
|---|
| 845 | n/a | |
|---|
| 846 | n/a | def addnamecode(self, type, name, code): |
|---|
| 847 | n/a | self.name2code[type][name] = code |
|---|
| 848 | n/a | if code not in self.code2name[type]: |
|---|
| 849 | n/a | self.code2name[type][code] = name |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | def hasname(self, name): |
|---|
| 852 | n/a | for dict in self.name2code.values(): |
|---|
| 853 | n/a | if name in dict: |
|---|
| 854 | n/a | return True |
|---|
| 855 | n/a | return False |
|---|
| 856 | n/a | |
|---|
| 857 | n/a | def hascode(self, type, code): |
|---|
| 858 | n/a | return code in self.code2name[type] |
|---|
| 859 | n/a | |
|---|
| 860 | n/a | def findcodename(self, type, code): |
|---|
| 861 | n/a | if not self.hascode(type, code): |
|---|
| 862 | n/a | return None, None, None |
|---|
| 863 | n/a | name = self.code2name[type][code] |
|---|
| 864 | n/a | if self.modulename and not self.star_imported: |
|---|
| 865 | n/a | qualname = '%s.%s'%(self.modulename, name) |
|---|
| 866 | n/a | else: |
|---|
| 867 | n/a | qualname = name |
|---|
| 868 | n/a | return name, qualname, self.modulename |
|---|
| 869 | n/a | |
|---|
| 870 | n/a | def getall(self, type): |
|---|
| 871 | n/a | return self.code2name[type].items() |
|---|
| 872 | n/a | |
|---|
| 873 | n/a | def addmodule(self, module, name, star_imported): |
|---|
| 874 | n/a | self.modulename = name |
|---|
| 875 | n/a | self.star_imported = star_imported |
|---|
| 876 | n/a | for code, name in module._propdeclarations.items(): |
|---|
| 877 | n/a | self.addnamecode('property', name, code) |
|---|
| 878 | n/a | for code, name in module._classdeclarations.items(): |
|---|
| 879 | n/a | self.addnamecode('class', name, code) |
|---|
| 880 | n/a | for code in module._enumdeclarations.keys(): |
|---|
| 881 | n/a | self.addnamecode('enum', '_Enum_'+identify(code), code) |
|---|
| 882 | n/a | for code, name in module._compdeclarations.items(): |
|---|
| 883 | n/a | self.addnamecode('comparison', name, code) |
|---|
| 884 | n/a | |
|---|
| 885 | n/a | def prepareforexport(self, name=None): |
|---|
| 886 | n/a | if not self.modulename: |
|---|
| 887 | n/a | self.modulename = name |
|---|
| 888 | n/a | return self |
|---|
| 889 | n/a | |
|---|
| 890 | n/a | class ObjectCompiler: |
|---|
| 891 | n/a | def __init__(self, fp, modname, basesuite, othernamemappers=None, interact=1, |
|---|
| 892 | n/a | verbose=None): |
|---|
| 893 | n/a | self.fp = fp |
|---|
| 894 | n/a | self.verbose = verbose |
|---|
| 895 | n/a | self.basesuite = basesuite |
|---|
| 896 | n/a | self.can_interact = interact |
|---|
| 897 | n/a | self.modulename = modname |
|---|
| 898 | n/a | self.namemappers = [CodeNameMapper(self.can_interact, self.verbose)] |
|---|
| 899 | n/a | if othernamemappers: |
|---|
| 900 | n/a | self.othernamemappers = othernamemappers[:] |
|---|
| 901 | n/a | else: |
|---|
| 902 | n/a | self.othernamemappers = [] |
|---|
| 903 | n/a | if basesuite: |
|---|
| 904 | n/a | basemapper = CodeNameMapper(self.can_interact, self.verbose) |
|---|
| 905 | n/a | basemapper.addmodule(basesuite, '', 1) |
|---|
| 906 | n/a | self.namemappers.append(basemapper) |
|---|
| 907 | n/a | |
|---|
| 908 | n/a | def getprecompinfo(self, modname): |
|---|
| 909 | n/a | list = [] |
|---|
| 910 | n/a | for mapper in self.namemappers: |
|---|
| 911 | n/a | emapper = mapper.prepareforexport(modname) |
|---|
| 912 | n/a | if emapper: |
|---|
| 913 | n/a | list.append(emapper) |
|---|
| 914 | n/a | return list |
|---|
| 915 | n/a | |
|---|
| 916 | n/a | def findcodename(self, type, code): |
|---|
| 917 | n/a | while 1: |
|---|
| 918 | n/a | # First try: check whether we already know about this code. |
|---|
| 919 | n/a | for mapper in self.namemappers: |
|---|
| 920 | n/a | if mapper.hascode(type, code): |
|---|
| 921 | n/a | return mapper.findcodename(type, code) |
|---|
| 922 | n/a | # Second try: maybe one of the other modules knows about it. |
|---|
| 923 | n/a | for mapper in self.othernamemappers: |
|---|
| 924 | n/a | if mapper.hascode(type, code): |
|---|
| 925 | n/a | self.othernamemappers.remove(mapper) |
|---|
| 926 | n/a | self.namemappers.append(mapper) |
|---|
| 927 | n/a | if self.fp: |
|---|
| 928 | n/a | self.fp.write("import %s\n"%mapper.modulename) |
|---|
| 929 | n/a | break |
|---|
| 930 | n/a | else: |
|---|
| 931 | n/a | # If all this has failed we ask the user for a guess on where it could |
|---|
| 932 | n/a | # be and retry. |
|---|
| 933 | n/a | if self.fp: |
|---|
| 934 | n/a | m = self.askdefinitionmodule(type, code) |
|---|
| 935 | n/a | else: |
|---|
| 936 | n/a | m = None |
|---|
| 937 | n/a | if not m: return None, None, None |
|---|
| 938 | n/a | mapper = CodeNameMapper(self.can_interact, self.verbose) |
|---|
| 939 | n/a | mapper.addmodule(m, m.__name__, 0) |
|---|
| 940 | n/a | self.namemappers.append(mapper) |
|---|
| 941 | n/a | |
|---|
| 942 | n/a | def hasname(self, name): |
|---|
| 943 | n/a | for mapper in self.othernamemappers: |
|---|
| 944 | n/a | if mapper.hasname(name) and mapper.modulename != self.modulename: |
|---|
| 945 | n/a | if self.verbose: |
|---|
| 946 | n/a | print >>self.verbose, "Duplicate Python identifier:", name, self.modulename, mapper.modulename |
|---|
| 947 | n/a | return True |
|---|
| 948 | n/a | return False |
|---|
| 949 | n/a | |
|---|
| 950 | n/a | def askdefinitionmodule(self, type, code): |
|---|
| 951 | n/a | if not self.can_interact: |
|---|
| 952 | n/a | if self.verbose: |
|---|
| 953 | n/a | print >>self.verbose, "** No definition for %s '%s' found" % (type, code) |
|---|
| 954 | n/a | return None |
|---|
| 955 | n/a | path = EasyDialogs.AskFileForSave(message='Where is %s %s declared?'%(type, code)) |
|---|
| 956 | n/a | if not path: return |
|---|
| 957 | n/a | path, file = os.path.split(path) |
|---|
| 958 | n/a | modname = os.path.splitext(file)[0] |
|---|
| 959 | n/a | if not path in sys.path: |
|---|
| 960 | n/a | sys.path.insert(0, path) |
|---|
| 961 | n/a | m = __import__(modname) |
|---|
| 962 | n/a | self.fp.write("import %s\n"%modname) |
|---|
| 963 | n/a | return m |
|---|
| 964 | n/a | |
|---|
| 965 | n/a | def compileclass(self, cls): |
|---|
| 966 | n/a | [name, code, desc, properties, elements] = cls |
|---|
| 967 | n/a | pname = identify(name) |
|---|
| 968 | n/a | if self.namemappers[0].hascode('class', code): |
|---|
| 969 | n/a | # plural forms and such |
|---|
| 970 | n/a | othername, dummy, dummy = self.namemappers[0].findcodename('class', code) |
|---|
| 971 | n/a | if self.fp: |
|---|
| 972 | n/a | self.fp.write("\n%s = %s\n"%(pname, othername)) |
|---|
| 973 | n/a | else: |
|---|
| 974 | n/a | if self.fp: |
|---|
| 975 | n/a | self.fp.write('\nclass %s(aetools.ComponentItem):\n' % pname) |
|---|
| 976 | n/a | self.fp.write(' """%s - %s """\n' % (ascii(name), ascii(desc))) |
|---|
| 977 | n/a | self.fp.write(' want = %r\n' % (code,)) |
|---|
| 978 | n/a | self.namemappers[0].addnamecode('class', pname, code) |
|---|
| 979 | n/a | is_application_class = (code == 'capp') |
|---|
| 980 | n/a | properties.sort() |
|---|
| 981 | n/a | for prop in properties: |
|---|
| 982 | n/a | self.compileproperty(prop, is_application_class) |
|---|
| 983 | n/a | elements.sort() |
|---|
| 984 | n/a | for elem in elements: |
|---|
| 985 | n/a | self.compileelement(elem) |
|---|
| 986 | n/a | |
|---|
| 987 | n/a | def compileproperty(self, prop, is_application_class=False): |
|---|
| 988 | n/a | [name, code, what] = prop |
|---|
| 989 | n/a | if code == 'c@#!': |
|---|
| 990 | n/a | # Something silly with plurals. Skip it. |
|---|
| 991 | n/a | return |
|---|
| 992 | n/a | pname = identify(name) |
|---|
| 993 | n/a | if self.namemappers[0].hascode('property', code): |
|---|
| 994 | n/a | # plural forms and such |
|---|
| 995 | n/a | othername, dummy, dummy = self.namemappers[0].findcodename('property', code) |
|---|
| 996 | n/a | if pname == othername: |
|---|
| 997 | n/a | return |
|---|
| 998 | n/a | if self.fp: |
|---|
| 999 | n/a | self.fp.write("\n_Prop_%s = _Prop_%s\n"%(pname, othername)) |
|---|
| 1000 | n/a | else: |
|---|
| 1001 | n/a | if self.fp: |
|---|
| 1002 | n/a | self.fp.write("class _Prop_%s(aetools.NProperty):\n" % pname) |
|---|
| 1003 | n/a | self.fp.write(' """%s - %s """\n' % (ascii(name), ascii(what[1]))) |
|---|
| 1004 | n/a | self.fp.write(" which = %r\n" % (code,)) |
|---|
| 1005 | n/a | self.fp.write(" want = %r\n" % (what[0],)) |
|---|
| 1006 | n/a | self.namemappers[0].addnamecode('property', pname, code) |
|---|
| 1007 | n/a | if is_application_class and self.fp: |
|---|
| 1008 | n/a | self.fp.write("%s = _Prop_%s()\n" % (pname, pname)) |
|---|
| 1009 | n/a | |
|---|
| 1010 | n/a | def compileelement(self, elem): |
|---|
| 1011 | n/a | [code, keyform] = elem |
|---|
| 1012 | n/a | if self.fp: |
|---|
| 1013 | n/a | self.fp.write("# element %r as %s\n" % (code, keyform)) |
|---|
| 1014 | n/a | |
|---|
| 1015 | n/a | def fillclasspropsandelems(self, cls): |
|---|
| 1016 | n/a | [name, code, desc, properties, elements] = cls |
|---|
| 1017 | n/a | cname = identify(name) |
|---|
| 1018 | n/a | if self.namemappers[0].hascode('class', code) and \ |
|---|
| 1019 | n/a | self.namemappers[0].findcodename('class', code)[0] != cname: |
|---|
| 1020 | n/a | # This is an other name (plural or so) for something else. Skip. |
|---|
| 1021 | n/a | if self.fp and (elements or len(properties) > 1 or (len(properties) == 1 and |
|---|
| 1022 | n/a | properties[0][1] != 'c@#!')): |
|---|
| 1023 | n/a | if self.verbose: |
|---|
| 1024 | n/a | print >>self.verbose, '** Skip multiple %s of %s (code %r)' % (cname, self.namemappers[0].findcodename('class', code)[0], code) |
|---|
| 1025 | n/a | raise RuntimeError, "About to skip non-empty class" |
|---|
| 1026 | n/a | return |
|---|
| 1027 | n/a | plist = [] |
|---|
| 1028 | n/a | elist = [] |
|---|
| 1029 | n/a | superclasses = [] |
|---|
| 1030 | n/a | for prop in properties: |
|---|
| 1031 | n/a | [pname, pcode, what] = prop |
|---|
| 1032 | n/a | if pcode == "c@#^": |
|---|
| 1033 | n/a | superclasses.append(what) |
|---|
| 1034 | n/a | if pcode == 'c@#!': |
|---|
| 1035 | n/a | continue |
|---|
| 1036 | n/a | pname = identify(pname) |
|---|
| 1037 | n/a | plist.append(pname) |
|---|
| 1038 | n/a | |
|---|
| 1039 | n/a | superclassnames = [] |
|---|
| 1040 | n/a | for superclass in superclasses: |
|---|
| 1041 | n/a | superId, superDesc, dummy = superclass |
|---|
| 1042 | n/a | superclassname, fullyqualifiedname, module = self.findcodename("class", superId) |
|---|
| 1043 | n/a | # I don't think this is correct: |
|---|
| 1044 | n/a | if superclassname == cname: |
|---|
| 1045 | n/a | pass # superclassnames.append(fullyqualifiedname) |
|---|
| 1046 | n/a | else: |
|---|
| 1047 | n/a | superclassnames.append(superclassname) |
|---|
| 1048 | n/a | |
|---|
| 1049 | n/a | if self.fp: |
|---|
| 1050 | n/a | self.fp.write("%s._superclassnames = %r\n"%(cname, superclassnames)) |
|---|
| 1051 | n/a | |
|---|
| 1052 | n/a | for elem in elements: |
|---|
| 1053 | n/a | [ecode, keyform] = elem |
|---|
| 1054 | n/a | if ecode == 'c@#!': |
|---|
| 1055 | n/a | continue |
|---|
| 1056 | n/a | name, ename, module = self.findcodename('class', ecode) |
|---|
| 1057 | n/a | if not name: |
|---|
| 1058 | n/a | if self.fp: |
|---|
| 1059 | n/a | self.fp.write("# XXXX %s element %r not found!!\n"%(cname, ecode)) |
|---|
| 1060 | n/a | else: |
|---|
| 1061 | n/a | elist.append((name, ename)) |
|---|
| 1062 | n/a | |
|---|
| 1063 | n/a | plist.sort() |
|---|
| 1064 | n/a | elist.sort() |
|---|
| 1065 | n/a | |
|---|
| 1066 | n/a | if self.fp: |
|---|
| 1067 | n/a | self.fp.write("%s._privpropdict = {\n"%cname) |
|---|
| 1068 | n/a | for n in plist: |
|---|
| 1069 | n/a | self.fp.write(" '%s' : _Prop_%s,\n"%(n, n)) |
|---|
| 1070 | n/a | self.fp.write("}\n") |
|---|
| 1071 | n/a | self.fp.write("%s._privelemdict = {\n"%cname) |
|---|
| 1072 | n/a | for n, fulln in elist: |
|---|
| 1073 | n/a | self.fp.write(" '%s' : %s,\n"%(n, fulln)) |
|---|
| 1074 | n/a | self.fp.write("}\n") |
|---|
| 1075 | n/a | |
|---|
| 1076 | n/a | def compilecomparison(self, comp): |
|---|
| 1077 | n/a | [name, code, comment] = comp |
|---|
| 1078 | n/a | iname = identify(name) |
|---|
| 1079 | n/a | self.namemappers[0].addnamecode('comparison', iname, code) |
|---|
| 1080 | n/a | if self.fp: |
|---|
| 1081 | n/a | self.fp.write("class %s(aetools.NComparison):\n" % iname) |
|---|
| 1082 | n/a | self.fp.write(' """%s - %s """\n' % (ascii(name), ascii(comment))) |
|---|
| 1083 | n/a | |
|---|
| 1084 | n/a | def compileenumeration(self, enum): |
|---|
| 1085 | n/a | [code, items] = enum |
|---|
| 1086 | n/a | name = "_Enum_%s" % identify(code) |
|---|
| 1087 | n/a | if self.fp: |
|---|
| 1088 | n/a | self.fp.write("%s = {\n" % name) |
|---|
| 1089 | n/a | for item in items: |
|---|
| 1090 | n/a | self.compileenumerator(item) |
|---|
| 1091 | n/a | self.fp.write("}\n\n") |
|---|
| 1092 | n/a | self.namemappers[0].addnamecode('enum', name, code) |
|---|
| 1093 | n/a | return code |
|---|
| 1094 | n/a | |
|---|
| 1095 | n/a | def compileenumerator(self, item): |
|---|
| 1096 | n/a | [name, code, desc] = item |
|---|
| 1097 | n/a | self.fp.write(" %r : %r,\t# %s\n" % (identify(name), code, ascii(desc))) |
|---|
| 1098 | n/a | |
|---|
| 1099 | n/a | def checkforenum(self, enum): |
|---|
| 1100 | n/a | """This enum code is used by an event. Make sure it's available""" |
|---|
| 1101 | n/a | name, fullname, module = self.findcodename('enum', enum) |
|---|
| 1102 | n/a | if not name: |
|---|
| 1103 | n/a | if self.fp: |
|---|
| 1104 | n/a | self.fp.write("_Enum_%s = None # XXXX enum %s not found!!\n"%(identify(enum), ascii(enum))) |
|---|
| 1105 | n/a | return |
|---|
| 1106 | n/a | if module: |
|---|
| 1107 | n/a | if self.fp: |
|---|
| 1108 | n/a | self.fp.write("from %s import %s\n"%(module, name)) |
|---|
| 1109 | n/a | |
|---|
| 1110 | n/a | def dumpindex(self): |
|---|
| 1111 | n/a | if not self.fp: |
|---|
| 1112 | n/a | return |
|---|
| 1113 | n/a | self.fp.write("\n#\n# Indices of types declared in this module\n#\n") |
|---|
| 1114 | n/a | |
|---|
| 1115 | n/a | self.fp.write("_classdeclarations = {\n") |
|---|
| 1116 | n/a | classlist = self.namemappers[0].getall('class') |
|---|
| 1117 | n/a | classlist.sort() |
|---|
| 1118 | n/a | for k, v in classlist: |
|---|
| 1119 | n/a | self.fp.write(" %r : %s,\n" % (k, v)) |
|---|
| 1120 | n/a | self.fp.write("}\n") |
|---|
| 1121 | n/a | |
|---|
| 1122 | n/a | self.fp.write("\n_propdeclarations = {\n") |
|---|
| 1123 | n/a | proplist = self.namemappers[0].getall('property') |
|---|
| 1124 | n/a | proplist.sort() |
|---|
| 1125 | n/a | for k, v in proplist: |
|---|
| 1126 | n/a | self.fp.write(" %r : _Prop_%s,\n" % (k, v)) |
|---|
| 1127 | n/a | self.fp.write("}\n") |
|---|
| 1128 | n/a | |
|---|
| 1129 | n/a | self.fp.write("\n_compdeclarations = {\n") |
|---|
| 1130 | n/a | complist = self.namemappers[0].getall('comparison') |
|---|
| 1131 | n/a | complist.sort() |
|---|
| 1132 | n/a | for k, v in complist: |
|---|
| 1133 | n/a | self.fp.write(" %r : %s,\n" % (k, v)) |
|---|
| 1134 | n/a | self.fp.write("}\n") |
|---|
| 1135 | n/a | |
|---|
| 1136 | n/a | self.fp.write("\n_enumdeclarations = {\n") |
|---|
| 1137 | n/a | enumlist = self.namemappers[0].getall('enum') |
|---|
| 1138 | n/a | enumlist.sort() |
|---|
| 1139 | n/a | for k, v in enumlist: |
|---|
| 1140 | n/a | self.fp.write(" %r : %s,\n" % (k, v)) |
|---|
| 1141 | n/a | self.fp.write("}\n") |
|---|
| 1142 | n/a | |
|---|
| 1143 | n/a | def compiledata(data): |
|---|
| 1144 | n/a | [type, description, flags] = data |
|---|
| 1145 | n/a | return "%r -- %r %s" % (type, description, compiledataflags(flags)) |
|---|
| 1146 | n/a | |
|---|
| 1147 | n/a | def is_null(data): |
|---|
| 1148 | n/a | return data[0] == 'null' |
|---|
| 1149 | n/a | |
|---|
| 1150 | n/a | def is_optional(data): |
|---|
| 1151 | n/a | return (data[2] & 0x8000) |
|---|
| 1152 | n/a | |
|---|
| 1153 | n/a | def is_enum(data): |
|---|
| 1154 | n/a | return (data[2] & 0x2000) |
|---|
| 1155 | n/a | |
|---|
| 1156 | n/a | def getdatadoc(data): |
|---|
| 1157 | n/a | [type, descr, flags] = data |
|---|
| 1158 | n/a | if descr: |
|---|
| 1159 | n/a | return ascii(descr) |
|---|
| 1160 | n/a | if type == '****': |
|---|
| 1161 | n/a | return 'anything' |
|---|
| 1162 | n/a | if type == 'obj ': |
|---|
| 1163 | n/a | return 'an AE object reference' |
|---|
| 1164 | n/a | return "undocumented, typecode %r"%(type,) |
|---|
| 1165 | n/a | |
|---|
| 1166 | n/a | dataflagdict = {15: "optional", 14: "list", 13: "enum", 12: "mutable"} |
|---|
| 1167 | n/a | def compiledataflags(flags): |
|---|
| 1168 | n/a | bits = [] |
|---|
| 1169 | n/a | for i in range(16): |
|---|
| 1170 | n/a | if flags & (1<<i): |
|---|
| 1171 | n/a | if i in dataflagdict.keys(): |
|---|
| 1172 | n/a | bits.append(dataflagdict[i]) |
|---|
| 1173 | n/a | else: |
|---|
| 1174 | n/a | bits.append(repr(i)) |
|---|
| 1175 | n/a | return '[%s]' % string.join(bits) |
|---|
| 1176 | n/a | |
|---|
| 1177 | n/a | def ascii(str): |
|---|
| 1178 | n/a | """Return a string with all non-ascii characters hex-encoded""" |
|---|
| 1179 | n/a | if type(str) != type(''): |
|---|
| 1180 | n/a | return map(ascii, str) |
|---|
| 1181 | n/a | rv = '' |
|---|
| 1182 | n/a | for c in str: |
|---|
| 1183 | n/a | if c in ('\t', '\n', '\r') or ' ' <= c < chr(0x7f): |
|---|
| 1184 | n/a | rv = rv + c |
|---|
| 1185 | n/a | else: |
|---|
| 1186 | n/a | rv = rv + '\\' + 'x%02.2x' % ord(c) |
|---|
| 1187 | n/a | return rv |
|---|
| 1188 | n/a | |
|---|
| 1189 | n/a | def identify(str): |
|---|
| 1190 | n/a | """Turn any string into an identifier: |
|---|
| 1191 | n/a | - replace space by _ |
|---|
| 1192 | n/a | - replace other illegal chars by _xx_ (hex code) |
|---|
| 1193 | n/a | - append _ if the result is a python keyword |
|---|
| 1194 | n/a | """ |
|---|
| 1195 | n/a | if not str: |
|---|
| 1196 | n/a | return "empty_ae_name_" |
|---|
| 1197 | n/a | rv = '' |
|---|
| 1198 | n/a | ok = string.ascii_letters + '_' |
|---|
| 1199 | n/a | ok2 = ok + string.digits |
|---|
| 1200 | n/a | for c in str: |
|---|
| 1201 | n/a | if c in ok: |
|---|
| 1202 | n/a | rv = rv + c |
|---|
| 1203 | n/a | elif c == ' ': |
|---|
| 1204 | n/a | rv = rv + '_' |
|---|
| 1205 | n/a | else: |
|---|
| 1206 | n/a | rv = rv + '_%02.2x_'%ord(c) |
|---|
| 1207 | n/a | ok = ok2 |
|---|
| 1208 | n/a | if keyword.iskeyword(rv): |
|---|
| 1209 | n/a | rv = rv + '_' |
|---|
| 1210 | n/a | return rv |
|---|
| 1211 | n/a | |
|---|
| 1212 | n/a | # Call the main program |
|---|
| 1213 | n/a | |
|---|
| 1214 | n/a | if __name__ == '__main__': |
|---|
| 1215 | n/a | main() |
|---|
| 1216 | n/a | sys.exit(1) |
|---|