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 = 'QDOffscreen.h' # The Apple header file 10MODNAME = '_Qdoffs' # The name of the module 11OBJECTNAME = 'GWorld' # The basic name of the objects used here 12 13# The following is *usually* unchanged but may still require tuning 14MODPREFIX = 'Qdoffs' # The prefix for module-wide routines 15OBJECTTYPE = OBJECTNAME + 'Ptr' # The C type used to represent them 16OBJECTPREFIX = OBJECTNAME + 'Obj' # The prefix for object methods 17INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner 18#EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions 19OUTPUTFILE = MODNAME + "module.c" # The file generated by this program 20 21from macsupport import * 22 23# Create the type objects 24 25GWorldPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) 26GWorldFlags = Type("GWorldFlags", "l") 27GDHandle = OpaqueByValueType("GDHandle", "ResObj") 28OptGDHandle = OpaqueByValueType("GDHandle", "OptResObj") 29CTabHandle = OpaqueByValueType("CTabHandle", "OptResObj") 30PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj") 31PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj") 32CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj") 33GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj") 34QDErr = OSErrType("QDErr", 'h') 35 36includestuff = includestuff + """ 37#include <Carbon/Carbon.h> 38 39#ifdef USE_TOOLBOX_OBJECT_GLUE 40extern PyObject *_GWorldObj_New(GWorldPtr); 41extern int _GWorldObj_Convert(PyObject *, GWorldPtr *); 42 43#define GWorldObj_New _GWorldObj_New 44#define GWorldObj_Convert _GWorldObj_Convert 45#endif 46 47#define as_GrafPtr(gworld) ((GrafPtr)(gworld)) 48 49""" 50 51initstuff = initstuff + """ 52 PyMac_INIT_TOOLBOX_OBJECT_NEW(GWorldPtr, GWorldObj_New); 53 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(GWorldPtr, GWorldObj_Convert); 54""" 55 56class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): 57 # XXXX Should inherit from GrafPtr? 58 def outputCheckNewArg(self): 59 Output("if (itself == NULL) return PyMac_Error(resNotFound);") 60## def outputInitStructMembers(self): 61## GlobalObjectDefinition.outputInitStructMembers(self) 62## Output("SetWRefCon(itself, (long)it);") 63## def outputCheckConvertArg(self): 64## OutLbrace("if (DlgObj_Check(v))") 65## Output("*p_itself = ((WindowObject *)v)->ob_itself;") 66## Output("return 1;") 67## OutRbrace() 68## Out(""" 69## if (v == Py_None) { *p_itself = NULL; return 1; } 70## if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; } 71## """) 72 def outputFreeIt(self, itselfname): 73 Output("DisposeGWorld(%s);", itselfname) 74# From here on it's basically all boiler plate... 75 76# Create the generator groups and link them 77module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) 78object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) 79module.addobject(object) 80 81 82# Create the generator classes used to populate the lists 83Function = OSErrWeakLinkFunctionGenerator 84Method = OSErrWeakLinkMethodGenerator 85 86# Create and populate the lists 87functions = [] 88methods = [] 89execfile(INPUTFILE) 90 91# A method to convert a GWorldPtr to a GrafPtr 92f = Method(GrafPtr, 'as_GrafPtr', (GWorldPtr, 'p', InMode)) 93methods.append(f) 94 95# 96# Manual generator: get data out of a pixmap 97pixmapgetbytes_body = """ 98PixMapHandle pm; 99int from, length; 100char *cp; 101 102if ( !PyArg_ParseTuple(_args, "O&ii", ResObj_Convert, &pm, &from, &length) ) 103 return NULL; 104cp = GetPixBaseAddr(pm)+from; 105_res = PyString_FromStringAndSize(cp, length); 106return _res; 107""" 108f = ManualGenerator("GetPixMapBytes", pixmapgetbytes_body) 109f.docstring = lambda: """(pixmap, int start, int size) -> string. Return bytes from the pixmap""" 110functions.append(f) 111 112# Manual generator: store data in a pixmap 113pixmapputbytes_body = """ 114PixMapHandle pm; 115int from, length; 116char *cp, *icp; 117 118if ( !PyArg_ParseTuple(_args, "O&is#", ResObj_Convert, &pm, &from, &icp, &length) ) 119 return NULL; 120cp = GetPixBaseAddr(pm)+from; 121memcpy(cp, icp, length); 122Py_INCREF(Py_None); 123_res = Py_None; 124return _res; 125""" 126f = ManualGenerator("PutPixMapBytes", pixmapputbytes_body) 127f.docstring = lambda: """(pixmap, int start, string data). Store bytes into the pixmap""" 128functions.append(f) 129 130# add the populated lists to the generator groups 131# (in a different wordl the scan program would generate this) 132for f in functions: module.add(f) 133for f in methods: object.add(f) 134 135 136 137# generate output (open the output file as late as possible) 138SetOutputFileName(OUTPUTFILE) 139module.generate() 140