/Mac/Modules/cm/cmsupport.py

http://unladen-swallow.googlecode.com/ · Python · 125 lines · 95 code · 17 blank · 13 comment · 3 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. import string
  6. # Declarations that change for each manager
  7. MACHEADERFILE = 'Components.h' # The Apple header file
  8. MODNAME = '_Cm' # The name of the module
  9. # The following is *usually* unchanged but may still require tuning
  10. MODPREFIX = 'Cm' # The prefix for module-wide routines
  11. C_OBJECTPREFIX = 'CmpObj' # The prefix for object methods
  12. CI_OBJECTPREFIX = 'CmpInstObj'
  13. INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
  14. OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
  15. from macsupport import *
  16. # Create the type objects
  17. includestuff = includestuff + """
  18. #include <Carbon/Carbon.h>
  19. #ifdef USE_TOOLBOX_OBJECT_GLUE
  20. extern PyObject *_CmpObj_New(Component);
  21. extern int _CmpObj_Convert(PyObject *, Component *);
  22. extern PyObject *_CmpInstObj_New(ComponentInstance);
  23. extern int _CmpInstObj_Convert(PyObject *, ComponentInstance *);
  24. #define CmpObj_New _CmpObj_New
  25. #define CmpObj_Convert _CmpObj_Convert
  26. #define CmpInstObj_New _CmpInstObj_New
  27. #define CmpInstObj_Convert _CmpInstObj_Convert
  28. #endif
  29. /*
  30. ** Parse/generate ComponentDescriptor records
  31. */
  32. static PyObject *
  33. CmpDesc_New(ComponentDescription *itself)
  34. {
  35. return Py_BuildValue("O&O&O&ll",
  36. PyMac_BuildOSType, itself->componentType,
  37. PyMac_BuildOSType, itself->componentSubType,
  38. PyMac_BuildOSType, itself->componentManufacturer,
  39. itself->componentFlags, itself->componentFlagsMask);
  40. }
  41. static int
  42. CmpDesc_Convert(PyObject *v, 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. initstuff = initstuff + """
  52. PyMac_INIT_TOOLBOX_OBJECT_NEW(Component, CmpObj_New);
  53. PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Component, CmpObj_Convert);
  54. PyMac_INIT_TOOLBOX_OBJECT_NEW(ComponentInstance, CmpInstObj_New);
  55. PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ComponentInstance, CmpInstObj_Convert);
  56. """
  57. ComponentDescription = OpaqueType('ComponentDescription', 'CmpDesc')
  58. Component = OpaqueByValueType('Component', C_OBJECTPREFIX)
  59. ComponentInstance = OpaqueByValueType('ComponentInstance', CI_OBJECTPREFIX)
  60. ComponentResult = Type("ComponentResult", "l")
  61. ComponentResourceHandle = OpaqueByValueType("ComponentResourceHandle", "ResObj")
  62. class MyCIObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
  63. def outputCheckNewArg(self):
  64. Output("""if (itself == NULL) {
  65. PyErr_SetString(Cm_Error,"NULL ComponentInstance");
  66. return NULL;
  67. }""")
  68. class MyCObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
  69. def outputCheckNewArg(self):
  70. Output("""if (itself == NULL) {
  71. /* XXXX Or should we return None? */
  72. PyErr_SetString(Cm_Error,"No such component");
  73. return NULL;
  74. }""")
  75. def outputCheckConvertArg(self):
  76. Output("""if ( v == Py_None ) {
  77. *p_itself = 0;
  78. return 1;
  79. }""")
  80. # Create the generator groups and link them
  81. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
  82. ci_object = MyCIObjectDefinition('ComponentInstance', CI_OBJECTPREFIX,
  83. 'ComponentInstance')
  84. c_object = MyCObjectDefinition('Component', C_OBJECTPREFIX, 'Component')
  85. module.addobject(ci_object)
  86. module.addobject(c_object)
  87. # Create the generator classes used to populate the lists
  88. Function = OSErrWeakLinkFunctionGenerator
  89. Method = OSErrWeakLinkMethodGenerator
  90. # Create and populate the lists
  91. functions = []
  92. c_methods = []
  93. ci_methods = []
  94. execfile(INPUTFILE)
  95. # add the populated lists to the generator groups
  96. # (in a different wordl the scan program would generate this)
  97. for f in functions: module.add(f)
  98. for f in c_methods: c_object.add(f)
  99. for f in ci_methods: ci_object.add(f)
  100. # generate output (open the output file as late as possible)
  101. SetOutputFileName(OUTPUTFILE)
  102. module.generate()