| 1 | n/a | # This script generates a Python interface for an Apple Macintosh Manager. |
|---|
| 2 | n/a | # It uses the "bgen" package to generate C code. |
|---|
| 3 | n/a | # The function specifications are generated by scanning the mamager's header file, |
|---|
| 4 | n/a | # using the "scantools" package (customized for this particular manager). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | import string |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | # Declarations that change for each manager |
|---|
| 9 | n/a | MACHEADERFILE = 'Components.h' # The Apple header file |
|---|
| 10 | n/a | MODNAME = '_Cm' # The name of the module |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | # The following is *usually* unchanged but may still require tuning |
|---|
| 13 | n/a | MODPREFIX = 'Cm' # The prefix for module-wide routines |
|---|
| 14 | n/a | C_OBJECTPREFIX = 'CmpObj' # The prefix for object methods |
|---|
| 15 | n/a | CI_OBJECTPREFIX = 'CmpInstObj' |
|---|
| 16 | n/a | INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner |
|---|
| 17 | n/a | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | from macsupport import * |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | # Create the type objects |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | includestuff = includestuff + """ |
|---|
| 24 | n/a | #include <Carbon/Carbon.h> |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | #ifdef USE_TOOLBOX_OBJECT_GLUE |
|---|
| 27 | n/a | extern PyObject *_CmpObj_New(Component); |
|---|
| 28 | n/a | extern int _CmpObj_Convert(PyObject *, Component *); |
|---|
| 29 | n/a | extern PyObject *_CmpInstObj_New(ComponentInstance); |
|---|
| 30 | n/a | extern int _CmpInstObj_Convert(PyObject *, ComponentInstance *); |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | #define CmpObj_New _CmpObj_New |
|---|
| 33 | n/a | #define CmpObj_Convert _CmpObj_Convert |
|---|
| 34 | n/a | #define CmpInstObj_New _CmpInstObj_New |
|---|
| 35 | n/a | #define CmpInstObj_Convert _CmpInstObj_Convert |
|---|
| 36 | n/a | #endif |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | /* |
|---|
| 39 | n/a | ** Parse/generate ComponentDescriptor records |
|---|
| 40 | n/a | */ |
|---|
| 41 | n/a | static PyObject * |
|---|
| 42 | n/a | CmpDesc_New(ComponentDescription *itself) |
|---|
| 43 | n/a | { |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | return Py_BuildValue("O&O&O&ll", |
|---|
| 46 | n/a | PyMac_BuildOSType, itself->componentType, |
|---|
| 47 | n/a | PyMac_BuildOSType, itself->componentSubType, |
|---|
| 48 | n/a | PyMac_BuildOSType, itself->componentManufacturer, |
|---|
| 49 | n/a | itself->componentFlags, itself->componentFlagsMask); |
|---|
| 50 | n/a | } |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | static int |
|---|
| 53 | n/a | CmpDesc_Convert(PyObject *v, ComponentDescription *p_itself) |
|---|
| 54 | n/a | { |
|---|
| 55 | n/a | return PyArg_ParseTuple(v, "O&O&O&ll", |
|---|
| 56 | n/a | PyMac_GetOSType, &p_itself->componentType, |
|---|
| 57 | n/a | PyMac_GetOSType, &p_itself->componentSubType, |
|---|
| 58 | n/a | PyMac_GetOSType, &p_itself->componentManufacturer, |
|---|
| 59 | n/a | &p_itself->componentFlags, &p_itself->componentFlagsMask); |
|---|
| 60 | n/a | } |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | """ |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | initstuff = initstuff + """ |
|---|
| 65 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(Component, CmpObj_New); |
|---|
| 66 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Component, CmpObj_Convert); |
|---|
| 67 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(ComponentInstance, CmpInstObj_New); |
|---|
| 68 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ComponentInstance, CmpInstObj_Convert); |
|---|
| 69 | n/a | """ |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | ComponentDescription = OpaqueType('ComponentDescription', 'CmpDesc') |
|---|
| 72 | n/a | Component = OpaqueByValueType('Component', C_OBJECTPREFIX) |
|---|
| 73 | n/a | ComponentInstance = OpaqueByValueType('ComponentInstance', CI_OBJECTPREFIX) |
|---|
| 74 | n/a | ComponentResult = Type("ComponentResult", "l") |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | ComponentResourceHandle = OpaqueByValueType("ComponentResourceHandle", "ResObj") |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | class MyCIObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 79 | n/a | def outputCheckNewArg(self): |
|---|
| 80 | n/a | Output("""if (itself == NULL) { |
|---|
| 81 | n/a | PyErr_SetString(Cm_Error,"NULL ComponentInstance"); |
|---|
| 82 | n/a | return NULL; |
|---|
| 83 | n/a | }""") |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | class MyCObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 86 | n/a | def outputCheckNewArg(self): |
|---|
| 87 | n/a | Output("""if (itself == NULL) { |
|---|
| 88 | n/a | /* XXXX Or should we return None? */ |
|---|
| 89 | n/a | PyErr_SetString(Cm_Error,"No such component"); |
|---|
| 90 | n/a | return NULL; |
|---|
| 91 | n/a | }""") |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | def outputCheckConvertArg(self): |
|---|
| 94 | n/a | Output("""if ( v == Py_None ) { |
|---|
| 95 | n/a | *p_itself = 0; |
|---|
| 96 | n/a | return 1; |
|---|
| 97 | n/a | }""") |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | # Create the generator groups and link them |
|---|
| 100 | n/a | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
|---|
| 101 | n/a | ci_object = MyCIObjectDefinition('ComponentInstance', CI_OBJECTPREFIX, |
|---|
| 102 | n/a | 'ComponentInstance') |
|---|
| 103 | n/a | c_object = MyCObjectDefinition('Component', C_OBJECTPREFIX, 'Component') |
|---|
| 104 | n/a | module.addobject(ci_object) |
|---|
| 105 | n/a | module.addobject(c_object) |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | # Create the generator classes used to populate the lists |
|---|
| 108 | n/a | Function = OSErrWeakLinkFunctionGenerator |
|---|
| 109 | n/a | Method = OSErrWeakLinkMethodGenerator |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | # Create and populate the lists |
|---|
| 112 | n/a | functions = [] |
|---|
| 113 | n/a | c_methods = [] |
|---|
| 114 | n/a | ci_methods = [] |
|---|
| 115 | n/a | execfile(INPUTFILE) |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | # add the populated lists to the generator groups |
|---|
| 118 | n/a | # (in a different wordl the scan program would generate this) |
|---|
| 119 | n/a | for f in functions: module.add(f) |
|---|
| 120 | n/a | for f in c_methods: c_object.add(f) |
|---|
| 121 | n/a | for f in ci_methods: ci_object.add(f) |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | # generate output (open the output file as late as possible) |
|---|
| 124 | n/a | SetOutputFileName(OUTPUTFILE) |
|---|
| 125 | n/a | module.generate() |
|---|