py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Mac / Modules / cm / cmsupport.py
blobcb9112b958199c1a7639f1cf44a4767b5dbfeb6c
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).
6 import string
8 # Declarations that change for each manager
9 MACHEADERFILE = 'Components.h' # The Apple header file
10 MODNAME = 'Cm' # The name of the module
12 # The following is *usually* unchanged but may still require tuning
13 MODPREFIX = MODNAME # The prefix for module-wide routines
14 C_OBJECTPREFIX = 'CmpObj' # The prefix for object methods
15 CI_OBJECTPREFIX = 'CmpInstObj'
16 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
17 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
19 from macsupport import *
21 # Create the type objects
23 includestuff = includestuff + """
24 #ifdef WITHOUT_FRAMEWORKS
25 #include <Components.h>
26 #else
27 #include <Carbon/Carbon.h>
28 #endif
30 #ifdef USE_TOOLBOX_OBJECT_GLUE
31 extern PyObject *_CmpObj_New(Component);
32 extern int _CmpObj_Convert(PyObject *, Component *);
33 extern PyObject *_CmpInstObj_New(ComponentInstance);
34 extern int _CmpInstObj_Convert(PyObject *, ComponentInstance *);
36 #define CmpObj_New _CmpObj_New
37 #define CmpObj_Convert _CmpObj_Convert
38 #define CmpInstObj_New _CmpInstObj_New
39 #define CmpInstObj_Convert _CmpInstObj_Convert
40 #endif
43 ** Parse/generate ComponentDescriptor records
45 static PyObject *
46 CmpDesc_New(ComponentDescription *itself)
49 return Py_BuildValue("O&O&O&ll",
50 PyMac_BuildOSType, itself->componentType,
51 PyMac_BuildOSType, itself->componentSubType,
52 PyMac_BuildOSType, itself->componentManufacturer,
53 itself->componentFlags, itself->componentFlagsMask);
56 static int
57 CmpDesc_Convert(PyObject *v, ComponentDescription *p_itself)
59 return PyArg_ParseTuple(v, "O&O&O&ll",
60 PyMac_GetOSType, &p_itself->componentType,
61 PyMac_GetOSType, &p_itself->componentSubType,
62 PyMac_GetOSType, &p_itself->componentManufacturer,
63 &p_itself->componentFlags, &p_itself->componentFlagsMask);
66 """
68 initstuff = initstuff + """
69 PyMac_INIT_TOOLBOX_OBJECT_NEW(Component, CmpObj_New);
70 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Component, CmpObj_Convert);
71 PyMac_INIT_TOOLBOX_OBJECT_NEW(ComponentInstance, CmpInstObj_New);
72 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ComponentInstance, CmpInstObj_Convert);
73 """
75 ComponentDescription = OpaqueType('ComponentDescription', 'CmpDesc')
76 Component = OpaqueByValueType('Component', C_OBJECTPREFIX)
77 ComponentInstance = OpaqueByValueType('ComponentInstance', CI_OBJECTPREFIX)
78 ComponentResult = Type("ComponentResult", "l")
80 ComponentResourceHandle = OpaqueByValueType("ComponentResourceHandle", "ResObj")
82 class MyCIObjectDefinition(GlobalObjectDefinition):
83 def outputCheckNewArg(self):
84 Output("""if (itself == NULL) {
85 PyErr_SetString(Cm_Error,"NULL ComponentInstance");
86 return NULL;
87 }""")
89 class MyCObjectDefinition(GlobalObjectDefinition):
90 def outputCheckNewArg(self):
91 Output("""if (itself == NULL) {
92 /* XXXX Or should we return None? */
93 PyErr_SetString(Cm_Error,"No such component");
94 return NULL;
95 }""")
97 def outputCheckConvertArg(self):
98 Output("""if ( v == Py_None ) {
99 *p_itself = 0;
100 return 1;
101 }""")
103 # Create the generator groups and link them
104 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
105 ci_object = MyCIObjectDefinition('ComponentInstance', CI_OBJECTPREFIX,
106 'ComponentInstance')
107 c_object = MyCObjectDefinition('Component', C_OBJECTPREFIX, 'Component')
108 module.addobject(ci_object)
109 module.addobject(c_object)
111 # Create the generator classes used to populate the lists
112 Function = OSErrFunctionGenerator
113 Method = OSErrMethodGenerator
115 # Create and populate the lists
116 functions = []
117 c_methods = []
118 ci_methods = []
119 execfile(INPUTFILE)
121 # add the populated lists to the generator groups
122 # (in a different wordl the scan program would generate this)
123 for f in functions: module.add(f)
124 for f in c_methods: c_object.add(f)
125 for f in ci_methods: ci_object.add(f)
127 # generate output (open the output file as late as possible)
128 SetOutputFileName(OUTPUTFILE)
129 module.generate()