Home | History | Annotate | Download | only in cm
      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 = 'Cm'                        # 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 <Carbon/Carbon.h>
     25 
     26 #ifdef USE_TOOLBOX_OBJECT_GLUE
     27 extern PyObject *_CmpObj_New(Component);
     28 extern int _CmpObj_Convert(PyObject *, Component *);
     29 extern PyObject *_CmpInstObj_New(ComponentInstance);
     30 extern int _CmpInstObj_Convert(PyObject *, ComponentInstance *);
     31 
     32 #define CmpObj_New _CmpObj_New
     33 #define CmpObj_Convert _CmpObj_Convert
     34 #define CmpInstObj_New _CmpInstObj_New
     35 #define CmpInstObj_Convert _CmpInstObj_Convert
     36 #endif
     37 
     38 /*
     39 ** Parse/generate ComponentDescriptor records
     40 */
     41 static PyObject *
     42 CmpDesc_New(ComponentDescription *itself)
     43 {
     44 
     45         return Py_BuildValue("O&O&O&ll",
     46                 PyMac_BuildOSType, itself->componentType,
     47                 PyMac_BuildOSType, itself->componentSubType,
     48                 PyMac_BuildOSType, itself->componentManufacturer,
     49                 itself->componentFlags, itself->componentFlagsMask);
     50 }
     51 
     52 static int
     53 CmpDesc_Convert(PyObject *v, ComponentDescription *p_itself)
     54 {
     55         return PyArg_ParseTuple(v, "O&O&O&ll",
     56                 PyMac_GetOSType, &p_itself->componentType,
     57                 PyMac_GetOSType, &p_itself->componentSubType,
     58                 PyMac_GetOSType, &p_itself->componentManufacturer,
     59                 &p_itself->componentFlags, &p_itself->componentFlagsMask);
     60 }
     61 
     62 """
     63 
     64 initstuff = initstuff + """
     65         PyMac_INIT_TOOLBOX_OBJECT_NEW(Component, CmpObj_New);
     66         PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Component, CmpObj_Convert);
     67         PyMac_INIT_TOOLBOX_OBJECT_NEW(ComponentInstance, CmpInstObj_New);
     68         PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ComponentInstance, CmpInstObj_Convert);
     69 """
     70 
     71 ComponentDescription = OpaqueType('ComponentDescription', 'CmpDesc')
     72 Component = OpaqueByValueType('Component', C_OBJECTPREFIX)
     73 ComponentInstance = OpaqueByValueType('ComponentInstance', CI_OBJECTPREFIX)
     74 ComponentResult = Type("ComponentResult", "l")
     75 
     76 ComponentResourceHandle = OpaqueByValueType("ComponentResourceHandle", "ResObj")
     77 
     78 class MyCIObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
     79     def outputCheckNewArg(self):
     80         Output("""if (itself == NULL) {
     81                                 PyErr_SetString(Cm_Error,"NULL ComponentInstance");
     82                                 return NULL;
     83                         }""")
     84 
     85 class MyCObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
     86     def outputCheckNewArg(self):
     87         Output("""if (itself == NULL) {
     88                                 /* XXXX Or should we return None? */
     89                                 PyErr_SetString(Cm_Error,"No such component");
     90                                 return NULL;
     91                         }""")
     92 
     93     def outputCheckConvertArg(self):
     94         Output("""if ( v == Py_None ) {
     95                                 *p_itself = 0;
     96                                 return 1;
     97         }""")
     98 
     99 # Create the generator groups and link them
    100 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
    101 ci_object = MyCIObjectDefinition('ComponentInstance', CI_OBJECTPREFIX,
    102                 'ComponentInstance')
    103 c_object = MyCObjectDefinition('Component', C_OBJECTPREFIX, 'Component')
    104 module.addobject(ci_object)
    105 module.addobject(c_object)
    106 
    107 # Create the generator classes used to populate the lists
    108 Function = OSErrWeakLinkFunctionGenerator
    109 Method = OSErrWeakLinkMethodGenerator
    110 
    111 # Create and populate the lists
    112 functions = []
    113 c_methods = []
    114 ci_methods = []
    115 execfile(INPUTFILE)
    116 
    117 # add the populated lists to the generator groups
    118 # (in a different wordl the scan program would generate this)
    119 for f in functions: module.add(f)
    120 for f in c_methods: c_object.add(f)
    121 for f in ci_methods: ci_object.add(f)
    122 
    123 # generate output (open the output file as late as possible)
    124 SetOutputFileName(OUTPUTFILE)
    125 module.generate()
    126