/Mac/Modules/qdoffs/qdoffssupport.py

http://unladen-swallow.googlecode.com/ · Python · 139 lines · 87 code · 20 blank · 32 comment · 2 complexity · 4ea0f946a27c6835b346f2b8ba14042b 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 = 'QDOffscreen.h' # The Apple header file
  8. MODNAME = '_Qdoffs' # The name of the module
  9. OBJECTNAME = 'GWorld' # The basic name of the objects used here
  10. # The following is *usually* unchanged but may still require tuning
  11. MODPREFIX = 'Qdoffs' # The prefix for module-wide routines
  12. OBJECTTYPE = OBJECTNAME + 'Ptr' # The C type used to represent them
  13. OBJECTPREFIX = OBJECTNAME + 'Obj' # The prefix for object methods
  14. INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
  15. #EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions
  16. OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
  17. from macsupport import *
  18. # Create the type objects
  19. GWorldPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
  20. GWorldFlags = Type("GWorldFlags", "l")
  21. GDHandle = OpaqueByValueType("GDHandle", "ResObj")
  22. OptGDHandle = OpaqueByValueType("GDHandle", "OptResObj")
  23. CTabHandle = OpaqueByValueType("CTabHandle", "OptResObj")
  24. PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj")
  25. PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj")
  26. CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
  27. GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj")
  28. QDErr = OSErrType("QDErr", 'h')
  29. includestuff = includestuff + """
  30. #include <Carbon/Carbon.h>
  31. #ifdef USE_TOOLBOX_OBJECT_GLUE
  32. extern PyObject *_GWorldObj_New(GWorldPtr);
  33. extern int _GWorldObj_Convert(PyObject *, GWorldPtr *);
  34. #define GWorldObj_New _GWorldObj_New
  35. #define GWorldObj_Convert _GWorldObj_Convert
  36. #endif
  37. #define as_GrafPtr(gworld) ((GrafPtr)(gworld))
  38. """
  39. initstuff = initstuff + """
  40. PyMac_INIT_TOOLBOX_OBJECT_NEW(GWorldPtr, GWorldObj_New);
  41. PyMac_INIT_TOOLBOX_OBJECT_CONVERT(GWorldPtr, GWorldObj_Convert);
  42. """
  43. class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
  44. # XXXX Should inherit from GrafPtr?
  45. def outputCheckNewArg(self):
  46. Output("if (itself == NULL) return PyMac_Error(resNotFound);")
  47. ## def outputInitStructMembers(self):
  48. ## GlobalObjectDefinition.outputInitStructMembers(self)
  49. ## Output("SetWRefCon(itself, (long)it);")
  50. ## def outputCheckConvertArg(self):
  51. ## OutLbrace("if (DlgObj_Check(v))")
  52. ## Output("*p_itself = ((WindowObject *)v)->ob_itself;")
  53. ## Output("return 1;")
  54. ## OutRbrace()
  55. ## Out("""
  56. ## if (v == Py_None) { *p_itself = NULL; return 1; }
  57. ## if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
  58. ## """)
  59. def outputFreeIt(self, itselfname):
  60. Output("DisposeGWorld(%s);", itselfname)
  61. # From here on it's basically all boiler plate...
  62. # Create the generator groups and link them
  63. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
  64. object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
  65. module.addobject(object)
  66. # Create the generator classes used to populate the lists
  67. Function = OSErrWeakLinkFunctionGenerator
  68. Method = OSErrWeakLinkMethodGenerator
  69. # Create and populate the lists
  70. functions = []
  71. methods = []
  72. execfile(INPUTFILE)
  73. # A method to convert a GWorldPtr to a GrafPtr
  74. f = Method(GrafPtr, 'as_GrafPtr', (GWorldPtr, 'p', InMode))
  75. methods.append(f)
  76. #
  77. # Manual generator: get data out of a pixmap
  78. pixmapgetbytes_body = """
  79. PixMapHandle pm;
  80. int from, length;
  81. char *cp;
  82. if ( !PyArg_ParseTuple(_args, "O&ii", ResObj_Convert, &pm, &from, &length) )
  83. return NULL;
  84. cp = GetPixBaseAddr(pm)+from;
  85. _res = PyString_FromStringAndSize(cp, length);
  86. return _res;
  87. """
  88. f = ManualGenerator("GetPixMapBytes", pixmapgetbytes_body)
  89. f.docstring = lambda: """(pixmap, int start, int size) -> string. Return bytes from the pixmap"""
  90. functions.append(f)
  91. # Manual generator: store data in a pixmap
  92. pixmapputbytes_body = """
  93. PixMapHandle pm;
  94. int from, length;
  95. char *cp, *icp;
  96. if ( !PyArg_ParseTuple(_args, "O&is#", ResObj_Convert, &pm, &from, &icp, &length) )
  97. return NULL;
  98. cp = GetPixBaseAddr(pm)+from;
  99. memcpy(cp, icp, length);
  100. Py_INCREF(Py_None);
  101. _res = Py_None;
  102. return _res;
  103. """
  104. f = ManualGenerator("PutPixMapBytes", pixmapputbytes_body)
  105. f.docstring = lambda: """(pixmap, int start, string data). Store bytes into the pixmap"""
  106. functions.append(f)
  107. # add the populated lists to the generator groups
  108. # (in a different wordl the scan program would generate this)
  109. for f in functions: module.add(f)
  110. for f in methods: object.add(f)
  111. # generate output (open the output file as late as possible)
  112. SetOutputFileName(OUTPUTFILE)
  113. module.generate()