|
3282
|
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
|
|
|
6 import string
|
|
|
7
|
|
|
8 # Declarations that change for each manager
|
|
|
9 MACHEADERFILE = 'Components.h' # The Apple header file
|
|
|
10 MODNAME = 'Cm' # The name of the module
|
|
|
11
|
|
|
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
|
|
|
18
|
|
|
19 from macsupport import *
|
|
|
20
|
|
|
21 # Create the type objects
|
|
|
22
|
|
|
23 includestuff = includestuff + """
|
|
|
24 #include <%s>""" % MACHEADERFILE + """
|
|
|
25
|
|
|
26 /*
|
|
|
27 ** Parse/generate ComponentDescriptor records
|
|
|
28 */
|
|
|
29 PyObject *CmpDesc_New(itself)
|
|
|
30 ComponentDescription *itself;
|
|
|
31 {
|
|
|
32
|
|
|
33 return Py_BuildValue("O&O&O&ll",
|
|
|
34 PyMac_BuildOSType, itself->componentType,
|
|
|
35 PyMac_BuildOSType, itself->componentSubType,
|
|
|
36 PyMac_BuildOSType, itself->componentManufacturer,
|
|
|
37 itself->componentFlags, itself->componentFlagsMask);
|
|
|
38 }
|
|
|
39
|
|
|
40 CmpDesc_Convert(v, p_itself)
|
|
|
41 PyObject *v;
|
|
|
42 ComponentDescription *p_itself;
|
|
|
43 {
|
|
|
44 return PyArg_ParseTuple(v, "O&O&O&ll",
|
|
|
45 PyMac_GetOSType, &p_itself->componentType,
|
|
|
46 PyMac_GetOSType, &p_itself->componentSubType,
|
|
|
47 PyMac_GetOSType, &p_itself->componentManufacturer,
|
|
|
48 &p_itself->componentFlags, &p_itself->componentFlagsMask);
|
|
|
49 }
|
|
|
50
|
|
|
51 """
|
|
|
52
|
|
|
53 ComponentDescription = OpaqueType('ComponentDescription', 'CmpDesc')
|
|
|
54 Component = OpaqueByValueType('Component', C_OBJECTPREFIX)
|
|
|
55 ComponentInstance = OpaqueByValueType('ComponentInstance', CI_OBJECTPREFIX)
|
|
|
56 ComponentResult = Type("ComponentResult", "l")
|
|
|
57
|
|
|
58 ComponentResourceHandle = OpaqueByValueType("ComponentResourceHandle", "ResObj")
|
|
|
59
|
|
|
60 class MyCIObjectDefinition(GlobalObjectDefinition):
|
|
|
61 def outputCheckNewArg(self):
|
|
|
62 Output("""if (itself == NULL) {
|
|
|
63 PyErr_SetString(Cm_Error,"NULL ComponentInstance");
|
|
|
64 return NULL;
|
|
|
65 }""")
|
|
|
66
|
|
|
67 class MyCObjectDefinition(GlobalObjectDefinition):
|
|
|
68 def outputCheckNewArg(self):
|
|
|
69 Output("""if (itself == NULL) {
|
|
|
70 /* XXXX Or should we return None? */
|
|
|
71 PyErr_SetString(Cm_Error,"No such component");
|
|
|
72 return NULL;
|
|
|
73 }""")
|
|
|
74
|
|
|
75 def outputCheckConvertArg(self):
|
|
|
76 Output("""if ( v == Py_None ) {
|
|
|
77 *p_itself = 0;
|
|
|
78 return 1;
|
|
|
79 }""")
|
|
|
80
|
|
|
81 # Create the generator groups and link them
|
|
|
82 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
|
|
|
83 ci_object = MyCIObjectDefinition('ComponentInstance', CI_OBJECTPREFIX,
|
|
|
84 'ComponentInstance')
|
|
|
85 c_object = MyCObjectDefinition('Component', C_OBJECTPREFIX, 'Component')
|
|
|
86 module.addobject(ci_object)
|
|
|
87 module.addobject(c_object)
|
|
|
88
|
|
|
89 # Create the generator classes used to populate the lists
|
|
|
90 Function = OSErrFunctionGenerator
|
|
|
91 Method = OSErrMethodGenerator
|
|
|
92
|
|
|
93 # Create and populate the lists
|
|
|
94 functions = []
|
|
|
95 c_methods = []
|
|
|
96 ci_methods = []
|
|
|
97 execfile(INPUTFILE)
|
|
|
98
|
|
|
99 # add the populated lists to the generator groups
|
|
|
100 # (in a different wordl the scan program would generate this)
|
|
|
101 for f in functions: module.add(f)
|
|
|
102 for f in c_methods: c_object.add(f)
|
|
|
103 for f in ci_methods: ci_object.add(f)
|
|
|
104
|
|
|
105 # generate output (open the output file as late as possible)
|
|
|
106 SetOutputFileName(OUTPUTFILE)
|
|
|
107 module.generate()
|
|
|
108
|