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).
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
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);
42 CmpDesc_Convert(v, p_itself)
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);
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");
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");
77 def outputCheckConvertArg(self
):
78 Output("""if ( v == Py_None ) {
83 # Create the generator groups and link them
84 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
)
85 ci_object
= MyCIObjectDefinition('ComponentInstance', CI_OBJECTPREFIX
,
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
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
)