This is (hopefully) last checkin before releasing 2.1c2 -- get rid of
[python/dscho.git] / Mac / Modules / cm / cmsupport.py
blob598b51de2facce33d5f6bf0fba93db154fc9cbf7
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 #include <%s>""" % MACHEADERFILE + """
27 ** Parse/generate ComponentDescriptor records
29 static PyObject *
30 CmpDesc_New(itself)
31 ComponentDescription *itself;
34 return Py_BuildValue("O&O&O&ll",
35 PyMac_BuildOSType, itself->componentType,
36 PyMac_BuildOSType, itself->componentSubType,
37 PyMac_BuildOSType, itself->componentManufacturer,
38 itself->componentFlags, itself->componentFlagsMask);
41 static int
42 CmpDesc_Convert(v, p_itself)
43 PyObject *v;
44 ComponentDescription *p_itself;
46 return PyArg_ParseTuple(v, "O&O&O&ll",
47 PyMac_GetOSType, &p_itself->componentType,
48 PyMac_GetOSType, &p_itself->componentSubType,
49 PyMac_GetOSType, &p_itself->componentManufacturer,
50 &p_itself->componentFlags, &p_itself->componentFlagsMask);
53 """
55 ComponentDescription = OpaqueType('ComponentDescription', 'CmpDesc')
56 Component = OpaqueByValueType('Component', C_OBJECTPREFIX)
57 ComponentInstance = OpaqueByValueType('ComponentInstance', CI_OBJECTPREFIX)
58 ComponentResult = Type("ComponentResult", "l")
60 ComponentResourceHandle = OpaqueByValueType("ComponentResourceHandle", "ResObj")
62 class MyCIObjectDefinition(GlobalObjectDefinition):
63 def outputCheckNewArg(self):
64 Output("""if (itself == NULL) {
65 PyErr_SetString(Cm_Error,"NULL ComponentInstance");
66 return NULL;
67 }""")
69 class MyCObjectDefinition(GlobalObjectDefinition):
70 def outputCheckNewArg(self):
71 Output("""if (itself == NULL) {
72 /* XXXX Or should we return None? */
73 PyErr_SetString(Cm_Error,"No such component");
74 return NULL;
75 }""")
77 def outputCheckConvertArg(self):
78 Output("""if ( v == Py_None ) {
79 *p_itself = 0;
80 return 1;
81 }""")
83 # Create the generator groups and link them
84 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
85 ci_object = MyCIObjectDefinition('ComponentInstance', CI_OBJECTPREFIX,
86 'ComponentInstance')
87 c_object = MyCObjectDefinition('Component', C_OBJECTPREFIX, 'Component')
88 module.addobject(ci_object)
89 module.addobject(c_object)
91 # Create the generator classes used to populate the lists
92 Function = OSErrFunctionGenerator
93 Method = OSErrMethodGenerator
95 # Create and populate the lists
96 functions = []
97 c_methods = []
98 ci_methods = []
99 execfile(INPUTFILE)
101 # add the populated lists to the generator groups
102 # (in a different wordl the scan program would generate this)
103 for f in functions: module.add(f)
104 for f in c_methods: c_object.add(f)
105 for f in ci_methods: ci_object.add(f)
107 # generate output (open the output file as late as possible)
108 SetOutputFileName(OUTPUTFILE)
109 module.generate()