Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Mac / Modules / cm / cmsupport.py
blob0a1915f3cf99618436788d3c0437c8144e0681ac
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 PyObject *CmpDesc_New(itself)
30 ComponentDescription *itself;
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);
40 CmpDesc_Convert(v, p_itself)
41 PyObject *v;
42 ComponentDescription *p_itself;
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);
51 """
53 ComponentDescription = OpaqueType('ComponentDescription', 'CmpDesc')
54 Component = OpaqueByValueType('Component', C_OBJECTPREFIX)
55 ComponentInstance = OpaqueByValueType('ComponentInstance', CI_OBJECTPREFIX)
56 ComponentResult = Type("ComponentResult", "l")
58 ComponentResourceHandle = OpaqueByValueType("ComponentResourceHandle", "ResObj")
60 class MyCIObjectDefinition(GlobalObjectDefinition):
61 def outputCheckNewArg(self):
62 Output("""if (itself == NULL) {
63 PyErr_SetString(Cm_Error,"NULL ComponentInstance");
64 return NULL;
65 }""")
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 }""")
75 def outputCheckConvertArg(self):
76 Output("""if ( v == Py_None ) {
77 *p_itself = 0;
78 return 1;
79 }""")
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)
89 # Create the generator classes used to populate the lists
90 Function = OSErrFunctionGenerator
91 Method = OSErrMethodGenerator
93 # Create and populate the lists
94 functions = []
95 c_methods = []
96 ci_methods = []
97 execfile(INPUTFILE)
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)
105 # generate output (open the output file as late as possible)
106 SetOutputFileName(OUTPUTFILE)
107 module.generate()