/Mac/Modules/osa/osasupport.py

http://unladen-swallow.googlecode.com/ · Python · 105 lines · 71 code · 17 blank · 17 comment · 2 complexity · e848852548ef284473dde05f63646540 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 = 'OSA.h' # The Apple header file
  8. MODNAME = '_OSA' # The name of the module
  9. # The following is *usually* unchanged but may still require tuning
  10. MODPREFIX = 'OSA' # The prefix for module-wide routines
  11. OBJECTPREFIX = 'OSAObj' # The prefix for object methods
  12. INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
  13. OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
  14. from macsupport import *
  15. # Create the type objects
  16. includestuff = includestuff + """
  17. #if PY_VERSION_HEX < 0x02040000
  18. PyObject *PyMac_GetOSErrException(void);
  19. #endif
  20. #include <Carbon/Carbon.h>
  21. #ifdef USE_TOOLBOX_OBJECT_GLUE
  22. extern PyObject *_OSAObj_New(ComponentInstance);
  23. extern int _OSAObj_Convert(PyObject *, ComponentInstance *);
  24. #define OSAObj_New _OSAObj_New
  25. #define OSAObj_Convert _OSAObj_Convert
  26. #endif
  27. """
  28. initstuff = initstuff + """
  29. /*
  30. PyMac_INIT_TOOLBOX_OBJECT_NEW(ComponentInstance, OSAObj_New);
  31. PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ComponentInstance, OSAObj_Convert);
  32. */
  33. """
  34. ComponentInstance = OpaqueByValueType('ComponentInstance', OBJECTPREFIX)
  35. OSAError = OSErrType("OSAError", "l")
  36. # OSALocalOrGlobal = Type("OSALocalOrGlobal", "l")
  37. OSAID = Type("OSAID", "l")
  38. OSADebugCallFrameRef = Type("OSADebugCallFrameRef", "l")
  39. OSADebugSessionRef = Type("OSADebugSessionRef", "l")
  40. OSADebugStepKind = Type("OSADebugStepKind", "l")
  41. DescType = OSTypeType("DescType")
  42. AEDesc = OpaqueType('AEDesc')
  43. AEDesc_ptr = OpaqueType('AEDesc')
  44. AEAddressDesc = OpaqueType('AEAddressDesc', 'AEDesc')
  45. AEAddressDesc_ptr = OpaqueType('AEAddressDesc', 'AEDesc')
  46. AEDescList = OpaqueType('AEDescList', 'AEDesc')
  47. AEDescList_ptr = OpaqueType('AEDescList', 'AEDesc')
  48. AERecord = OpaqueType('AERecord', 'AEDesc')
  49. AERecord_ptr = OpaqueType('AERecord', 'AEDesc')
  50. AppleEvent = OpaqueType('AppleEvent', 'AEDesc')
  51. AppleEvent_ptr = OpaqueType('AppleEvent', 'AEDesc')
  52. # NOTE: at the moment OSA.ComponentInstance is not a subclass
  53. # of Cm.ComponentInstance. If this is a problem it can be fixed.
  54. class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
  55. def outputCheckNewArg(self):
  56. Output("""if (itself == NULL) {
  57. PyErr_SetString(OSA_Error,"NULL ComponentInstance");
  58. return NULL;
  59. }""")
  60. def outputCheckConvertArg(self):
  61. Output("""
  62. if (CmpInstObj_Convert(v, p_itself))
  63. return 1;
  64. PyErr_Clear();
  65. """)
  66. # Create the generator groups and link them
  67. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
  68. object = MyObjectDefinition('OSAComponentInstance', OBJECTPREFIX,
  69. 'ComponentInstance')
  70. module.addobject(object)
  71. # Create the generator classes used to populate the lists
  72. Function = OSErrWeakLinkFunctionGenerator
  73. Method = OSErrWeakLinkMethodGenerator
  74. # Test which types we are still missing.
  75. execfile(string.lower(MODPREFIX) + 'typetest.py')
  76. # Create and populate the lists
  77. functions = []
  78. methods = []
  79. execfile(INPUTFILE)
  80. # add the populated lists to the generator groups
  81. # (in a different wordl the scan program would generate this)
  82. for f in functions: module.add(f)
  83. for f in methods: object.add(f)
  84. # generate output (open the output file as late as possible)
  85. SetOutputFileName(OUTPUTFILE)
  86. module.generate()