/Mac/Modules/app/appsupport.py

http://unladen-swallow.googlecode.com/ · Python · 133 lines · 75 code · 23 blank · 35 comment · 2 complexity · 734aa6b33c2a0975882f221e3aeed6f9 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 = 'Appearance.h' # The Apple header file
  8. MODNAME = '_App' # The name of the module
  9. OBJECTNAME = 'ThemeDrawingState' # The basic name of the objects used here
  10. KIND = '' # Usually 'Ptr' or 'Handle'
  11. # The following is *usually* unchanged but may still require tuning
  12. MODPREFIX = 'App' # The prefix for module-wide routines
  13. OBJECTTYPE = OBJECTNAME + KIND # The C type used to represent them
  14. OBJECTPREFIX = OBJECTNAME + 'Obj' # The prefix for object methods
  15. INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
  16. OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
  17. from macsupport import *
  18. # Create the type objects
  19. #MenuRef = OpaqueByValueType("MenuRef", "MenuObj")
  20. #WindowPeek = OpaqueByValueType("WindowPeek", OBJECTPREFIX)
  21. RgnHandle = FakeType("(RgnHandle)0")
  22. NULL = FakeType("NULL")
  23. # XXXX Should be next, but this will break a lot of code...
  24. # RgnHandle = OpaqueByValueType("RgnHandle", "OptResObj")
  25. #KeyMap = ArrayOutputBufferType("KeyMap")
  26. #MacOSEventKind = Type("MacOSEventKind", "h") # Old-style
  27. #MacOSEventMask = Type("MacOSEventMask", "h") # Old-style
  28. #EventMask = Type("EventMask", "h")
  29. #EventKind = Type("EventKind", "h")
  30. ThemeBrush = Type("ThemeBrush", "h")
  31. ThemeColor = Type("ThemeColor", "h")
  32. ThemeTextColor = Type("ThemeTextColor", "h")
  33. ThemeMenuBarState = Type("ThemeMenuBarState", "H")
  34. ThemeMenuState = Type("ThemeMenuState", "H")
  35. ThemeMenuType = Type("ThemeMenuType", "H")
  36. ThemeMenuItemType = Type("ThemeMenuItemType", "H")
  37. ThemeFontID = Type("ThemeFontID", "H")
  38. ThemeTabStyle = Type("ThemeTabStyle", "H")
  39. ThemeTabDirection = Type("ThemeTabDirection", "H")
  40. ThemeDrawState = Type("ThemeDrawState", "l")
  41. ThemeCursor = Type("ThemeCursor", "l")
  42. ThemeCheckBoxStyle = Type("ThemeCheckBoxStyle", "H")
  43. ThemeScrollBarArrowStyle = Type("ThemeScrollBarArrowStyle", "H")
  44. ThemeScrollBarThumbStyle = Type("ThemeScrollBarThumbStyle", "H")
  45. CTabHandle = OpaqueByValueType("CTabHandle", "ResObj")
  46. ThemeTrackEnableState = Type("ThemeTrackEnableState", "b")
  47. ThemeTrackPressState = Type("ThemeTrackPressState", "b")
  48. ThemeThumbDirection = Type("ThemeThumbDirection", "b")
  49. ThemeTrackAttributes = Type("ThemeTrackAttributes", "H")
  50. ControlPartCode = Type("ControlPartCode", "h")
  51. ThemeWindowAttributes = Type("ThemeWindowAttributes", "l")
  52. ThemeWindowType = Type("ThemeWindowType", "H")
  53. ThemeTitleBarWidget = Type("ThemeTitleBarWidget", "H")
  54. ThemeArrowOrientation = Type("ThemeArrowOrientation", "H")
  55. ThemePopupArrowSize = Type("ThemePopupArrowSize", "H")
  56. ThemeGrowDirection = Type("ThemeGrowDirection", "H")
  57. ThemeSoundKind = OSTypeType("ThemeSoundKind")
  58. ThemeDragSoundKind = OSTypeType("ThemeDragSoundKind")
  59. ThemeBackgroundKind = Type("ThemeBackgroundKind", "l")
  60. ThemeMetric = Type("ThemeMetric", "l")
  61. RGBColor = OpaqueType("RGBColor", "QdRGB")
  62. TruncCode = Type("TruncCode", "h")
  63. ThemeButtonKind = UInt16
  64. ThemeButtonDrawInfo_ptr = OpaqueType("ThemeButtonDrawInfo", "ThemeButtonDrawInfo")
  65. ThemeEraseUPP = FakeType("NULL")
  66. ThemeButtonDrawUPP = FakeType("NULL")
  67. includestuff = includestuff + """
  68. #include <Carbon/Carbon.h>
  69. int ThemeButtonDrawInfo_Convert(PyObject *v, ThemeButtonDrawInfo *p_itself)
  70. {
  71. return PyArg_Parse(v, "(iHH)", &p_itself->state, &p_itself->value, &p_itself->adornment);
  72. }
  73. """
  74. class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
  75. pass
  76. ## def outputCheckNewArg(self):
  77. ## Output("if (itself == NULL) return PyMac_Error(resNotFound);")
  78. ## def outputCheckConvertArg(self):
  79. ## OutLbrace("if (DlgObj_Check(v))")
  80. ## Output("*p_itself = ((WindowObject *)v)->ob_itself;")
  81. ## Output("return 1;")
  82. ## OutRbrace()
  83. ## Out("""
  84. ## if (v == Py_None) { *p_itself = NULL; return 1; }
  85. ## if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
  86. ## """)
  87. # From here on it's basically all boiler plate...
  88. # Create the generator groups and link them
  89. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
  90. object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
  91. module.addobject(object)
  92. ThemeDrawingState = OpaqueByValueType("ThemeDrawingState", "ThemeDrawingStateObj")
  93. Method = WeakLinkMethodGenerator
  94. # Create the generator classes used to populate the lists
  95. Function = OSErrWeakLinkFunctionGenerator
  96. ##Method = OSErrWeakLinkMethodGenerator
  97. # Create and populate the lists
  98. functions = []
  99. methods = []
  100. execfile(INPUTFILE)
  101. # add the populated lists to the generator groups
  102. # (in a different wordl the scan program would generate this)
  103. for f in functions: module.add(f)
  104. for f in methods: object.add(f)
  105. # generate output (open the output file as late as possible)
  106. SetOutputFileName(OUTPUTFILE)
  107. module.generate()