/Mac/Modules/menu/menusupport.py

http://unladen-swallow.googlecode.com/ · Python · 108 lines · 82 code · 14 blank · 12 comment · 2 complexity · 36419b0df02422650933b0beea2303d9 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 = 'Menus.h' # The Apple header file
  8. MODNAME = '_Menu' # The name of the module
  9. OBJECTNAME = 'Menu' # The basic name of the objects used here
  10. # The following is *usually* unchanged but may still require tuning
  11. MODPREFIX = 'Menu' # The prefix for module-wide routines
  12. OBJECTTYPE = OBJECTNAME + 'Handle' # The C type used to represent them
  13. OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
  14. INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
  15. EXTRAFILE = string.lower(MODPREFIX) + 'edit.py' # A similar file but hand-made
  16. OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
  17. from macsupport import *
  18. # Create the type objects
  19. MenuHandle = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
  20. MenuRef = MenuHandle
  21. OptMenuRef = OpaqueByValueType(OBJECTTYPE, "Opt" + OBJECTPREFIX)
  22. Handle = OpaqueByValueType("Handle", "ResObj")
  23. MenuBarHandle = OpaqueByValueType("MenuBarHandle", "ResObj")
  24. MenuID = Type("MenuID", "h")
  25. MenuItemIndex = Type("MenuItemIndex", "h")
  26. MenuItemID = Type("MenuItemID", "l")
  27. MenuCommand = Type("MenuCommand", "l")
  28. MenuAttributes = Type("MenuAttributes", "l")
  29. MenuItemAttributes = Type("MenuItemAttributes", "l")
  30. unsigned_char = Type('unsigned char', 'b')
  31. FMFontFamily = Type("FMFontFamily", "h")
  32. FMFontStyle = Type("FMFontStyle", "h")
  33. UniChar = Type("UniChar", "h")
  34. includestuff = includestuff + """
  35. #include <Carbon/Carbon.h>
  36. #ifdef USE_TOOLBOX_OBJECT_GLUE
  37. extern PyObject *_MenuObj_New(MenuHandle);
  38. extern int _MenuObj_Convert(PyObject *, MenuHandle *);
  39. #define MenuObj_New _MenuObj_New
  40. #define MenuObj_Convert _MenuObj_Convert
  41. #endif
  42. #define as_Menu(h) ((MenuHandle)h)
  43. #define as_Resource(h) ((Handle)h)
  44. /* Alternative version of MenuObj_New, which returns None for NULL argument */
  45. PyObject *OptMenuObj_New(MenuRef itself)
  46. {
  47. if (itself == NULL) {
  48. Py_INCREF(Py_None);
  49. return Py_None;
  50. }
  51. return MenuObj_New(itself);
  52. }
  53. /* Alternative version of MenuObj_Convert, which returns NULL for a None argument */
  54. int OptMenuObj_Convert(PyObject *v, MenuRef *p_itself)
  55. {
  56. if ( v == Py_None ) {
  57. *p_itself = NULL;
  58. return 1;
  59. }
  60. return MenuObj_Convert(v, p_itself);
  61. }
  62. """
  63. initstuff = initstuff + """
  64. PyMac_INIT_TOOLBOX_OBJECT_NEW(MenuHandle, MenuObj_New);
  65. PyMac_INIT_TOOLBOX_OBJECT_CONVERT(MenuHandle, MenuObj_Convert);
  66. """
  67. class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
  68. pass
  69. # Create the generator groups and link them
  70. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
  71. object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
  72. module.addobject(object)
  73. # Create the generator classes used to populate the lists
  74. Function = OSErrWeakLinkFunctionGenerator
  75. Method = OSErrWeakLinkMethodGenerator
  76. # Create and populate the lists
  77. functions = []
  78. methods = []
  79. execfile(INPUTFILE)
  80. execfile(EXTRAFILE)
  81. # add the populated lists to the generator groups
  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()