PageRenderTime 263ms CodeModel.GetById 141ms app.highlight 6ms RepoModel.GetById 114ms app.codeStats 0ms

/Mac/Modules/cm/cmsupport.py

http://unladen-swallow.googlecode.com/
Python | 125 lines | 112 code | 6 blank | 7 comment | 0 complexity | 0c12d6d43de291c559be1b64e15368cc MD5 | raw file
  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
  6import string
  7
  8# Declarations that change for each manager
  9MACHEADERFILE = 'Components.h'          # The Apple header file
 10MODNAME = '_Cm'                         # The name of the module
 11
 12# The following is *usually* unchanged but may still require tuning
 13MODPREFIX = 'Cm'                        # The prefix for module-wide routines
 14C_OBJECTPREFIX = 'CmpObj'       # The prefix for object methods
 15CI_OBJECTPREFIX = 'CmpInstObj'
 16INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
 17OUTPUTFILE = MODNAME + "module.c"       # The file generated by this program
 18
 19from macsupport import *
 20
 21# Create the type objects
 22
 23includestuff = includestuff + """
 24#include <Carbon/Carbon.h>
 25
 26#ifdef USE_TOOLBOX_OBJECT_GLUE
 27extern PyObject *_CmpObj_New(Component);
 28extern int _CmpObj_Convert(PyObject *, Component *);
 29extern PyObject *_CmpInstObj_New(ComponentInstance);
 30extern 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*/
 41static PyObject *
 42CmpDesc_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
 52static int
 53CmpDesc_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
 64initstuff = 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
 71ComponentDescription = OpaqueType('ComponentDescription', 'CmpDesc')
 72Component = OpaqueByValueType('Component', C_OBJECTPREFIX)
 73ComponentInstance = OpaqueByValueType('ComponentInstance', CI_OBJECTPREFIX)
 74ComponentResult = Type("ComponentResult", "l")
 75
 76ComponentResourceHandle = OpaqueByValueType("ComponentResourceHandle", "ResObj")
 77
 78class MyCIObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
 79    def outputCheckNewArg(self):
 80        Output("""if (itself == NULL) {
 81                                PyErr_SetString(Cm_Error,"NULL ComponentInstance");
 82                                return NULL;
 83                        }""")
 84
 85class 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
100module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
101ci_object = MyCIObjectDefinition('ComponentInstance', CI_OBJECTPREFIX,
102                'ComponentInstance')
103c_object = MyCObjectDefinition('Component', C_OBJECTPREFIX, 'Component')
104module.addobject(ci_object)
105module.addobject(c_object)
106
107# Create the generator classes used to populate the lists
108Function = OSErrWeakLinkFunctionGenerator
109Method = OSErrWeakLinkMethodGenerator
110
111# Create and populate the lists
112functions = []
113c_methods = []
114ci_methods = []
115execfile(INPUTFILE)
116
117# add the populated lists to the generator groups
118# (in a different wordl the scan program would generate this)
119for f in functions: module.add(f)
120for f in c_methods: c_object.add(f)
121for f in ci_methods: ci_object.add(f)
122
123# generate output (open the output file as late as possible)
124SetOutputFileName(OUTPUTFILE)
125module.generate()