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 6# NOTE: the scrap include file is so bad that the bgen output has to be 7# massaged by hand. 8 9import string 10 11# Declarations that change for each manager 12MACHEADERFILE = 'Scrap.h' # The Apple header file 13MODNAME = '_Scrap' # The name of the module 14OBJECTNAME = 'Scrap' # The basic name of the objects used here 15 16# The following is *usually* unchanged but may still require tuning 17MODPREFIX = 'Scrap' # The prefix for module-wide routines 18OBJECTTYPE = OBJECTNAME + 'Ref' # The C type used to represent them 19OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods 20INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner 21OUTPUTFILE = '@' + MODNAME + "module.c" # The file generated by this program 22 23from macsupport import * 24 25# Create the type objects 26ScrapRef = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) 27 28includestuff = includestuff + """ 29#include <Carbon/Carbon.h> 30 31/* 32** Generate ScrapInfo records 33*/ 34static PyObject * 35SCRRec_New(itself) 36 ScrapStuff *itself; 37{ 38 39 return Py_BuildValue("lO&hhO&", itself->scrapSize, 40 ResObj_New, itself->scrapHandle, itself->scrapCount, itself->scrapState, 41 PyMac_BuildStr255, itself->scrapName); 42} 43""" 44 45ScrapStuffPtr = OpaqueByValueType('ScrapStuffPtr', 'SCRRec') 46ScrapFlavorType = OSTypeType('ScrapFlavorType') 47ScrapFlavorFlags = Type('ScrapFlavorFlags', 'l') 48#ScrapFlavorInfo = OpaqueType('ScrapFlavorInfo', 'ScrapFlavorInfo') 49putscrapbuffer = FixedInputBufferType('void *') 50 51class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): 52 pass 53 54# Create the generator groups and link them 55module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) 56object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) 57module.addobject(object) 58 59# Create the generator classes used to populate the lists 60Function = OSErrFunctionGenerator 61Method = OSErrMethodGenerator 62 63# Create and populate the lists 64functions = [] 65methods = [] 66execfile(INPUTFILE) 67 68# add the populated lists to the generator groups 69# (in a different wordl the scan program would generate this) 70for f in functions: module.add(f) 71for f in methods: object.add(f) 72 73# generate output (open the output file as late as possible) 74SetOutputFileName(OUTPUTFILE) 75module.generate() 76