1# This script generates a Python interface for an Apple Macintosh Manager. 2# It uses the "bgen" package to generate C code. 3# The function specifications are generated by scanning the mamager's header file, 4# using the "scantools" package (customized for this particular manager). 5 6import string 7 8# Declarations that change for each manager 9MACHEADERFILE = 'Events.h' # The Apple header file 10MODNAME = '_Evt' # The name of the module 11OBJECTNAME = 'Event' # The basic name of the objects used here 12KIND = 'Record' # Usually 'Ptr' or 'Handle' 13 14# The following is *usually* unchanged but may still require tuning 15MODPREFIX = 'Evt' # The prefix for module-wide routines 16OBJECTTYPE = OBJECTNAME + KIND # The C type used to represent them 17OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods 18INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner 19OUTPUTFILE = MODNAME + "module.c" # The file generated by this program 20 21from macsupport import * 22 23# Create the type objects 24 25#WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX) 26 27RgnHandle = FakeType("(RgnHandle)0") 28# XXXX Should be next, but this will break a lot of code... 29# RgnHandle = OpaqueByValueType("RgnHandle", "OptResObj") 30 31KeyMap = ArrayOutputBufferType("KeyMap") 32##MacOSEventKind = Type("MacOSEventKind", "h") # Old-style 33##MacOSEventMask = Type("MacOSEventMask", "h") # Old-style 34EventMask = Type("EventMask", "H") 35EventKind = Type("EventKind", "H") 36 37includestuff = includestuff + """ 38#include <Carbon/Carbon.h> 39 40""" 41 42# From here on it's basically all boiler plate... 43 44# Create the generator groups and link them 45module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) 46 47# Create the generator classes used to populate the lists 48Function = OSErrWeakLinkFunctionGenerator 49##Method = OSErrWeakLinkMethodGenerator 50 51# Create and populate the lists 52functions = [] 53execfile(INPUTFILE) 54 55# Move TickCount here, for convenience 56f = Function(UInt32, 'TickCount', 57) 58functions.append(f) 59 60# add the populated lists to the generator groups 61# (in a different wordl the scan program would generate this) 62for f in functions: module.add(f) 63 64WaitNextEvent_body = """ 65Boolean _rv; 66EventMask eventMask; 67EventRecord theEvent; 68UInt32 sleep; 69Handle mouseregion = (Handle)0; 70 71if (!PyArg_ParseTuple(_args, "Hl|O&", 72 &eventMask, 73 &sleep, 74 OptResObj_Convert, &mouseregion)) 75 return NULL; 76_rv = WaitNextEvent(eventMask, 77 &theEvent, 78 sleep, 79 (RgnHandle)mouseregion); 80_res = Py_BuildValue("bO&", 81 _rv, 82 PyMac_BuildEventRecord, &theEvent); 83return _res; 84""" 85f = ManualGenerator("WaitNextEvent", WaitNextEvent_body); 86f.docstring = lambda: "(EventMask eventMask, UInt32 sleep [,RegionHandle]) -> (Boolean _rv, EventRecord theEvent)" 87module.add(f) 88 89 90# generate output (open the output file as late as possible) 91SetOutputFileName(OUTPUTFILE) 92module.generate() 93