home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Mac / Modules / waste / wastesupport.py < prev    next >
Encoding:
Text File  |  1996-10-23  |  11.5 KB  |  412 lines  |  [TEXT/Pyth]

  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.  
  6. import string
  7.  
  8. # Declarations that change for each manager
  9. MACHEADERFILE = 'WASTE.h'        # The Apple header file
  10. MODNAME = 'waste'                # The name of the module
  11. OBJECTNAME = 'waste'            # The basic name of the objects used here
  12. KIND = 'Ptr'                # Usually 'Ptr' or 'Handle'
  13.  
  14. # The following is *usually* unchanged but may still require tuning
  15. MODPREFIX = MODNAME            # The prefix for module-wide routines
  16. OBJECTTYPE = "WEReference"        # The C type used to represent them
  17. OBJECTPREFIX = MODPREFIX + 'Obj'    # The prefix for object methods
  18. INPUTFILE = 'wastegen.py' # The file generated by the scanner
  19. TYPETESTFILE = 'wastetypetest.py'    # Another file generated by the scanner
  20. OUTPUTFILE = "wastemodule.c"    # The file generated by this program
  21.  
  22. from macsupport import *
  23.  
  24. # Create the type objects
  25. WEReference = OpaqueByValueType("WEReference", "wasteObj")
  26. ExistingWEReference = OpaqueByValueType("WEReference", "ExistingwasteObj")
  27. WEObjectReference = OpaqueByValueType("WEObjectReference", "WEOObj")
  28. StScrpHandle = OpaqueByValueType("StScrpHandle", "ResObj")
  29. RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
  30. EventModifiers = Type("EventModifiers", "h")
  31. FlavorType = OSTypeType("FlavorType")
  32. WESelector = OSTypeType("WESelector")
  33.  
  34. OptHandle = OpaqueByValueType("Handle", "OptResObj")
  35. OptSoupHandle = OpaqueByValueType("WESoupHandle", "OptResObj")
  36. OptStScrpHandle = OpaqueByValueType("StScrpHandle", "OptResObj")
  37.  
  38. WEStyleMode = Type("WEStyleMode", "h")
  39. WEActionKind = Type("WEActionKind", "h")
  40. WEAlignment = Type("WEAlignment", "b")
  41. WEEdge = Type("WEEdge", "b")
  42. WESoupHandle = OpaqueByValueType("WESoupHandle", "ResObj")
  43. WERunInfo = OpaqueType("WERunInfo", "RunInfo")
  44.  
  45. TextStyle = OpaqueType("TextStyle", "TextStyle")
  46. TextStyle_ptr = TextStyle
  47. LongPt = OpaqueType("LongPt", "LongPt")
  48. LongPt_ptr = LongPt
  49. LongRect = OpaqueType("LongRect", "LongRect")
  50. LongRect_ptr = LongRect
  51.  
  52. includestuff = includestuff + """
  53. #include <%s>""" % MACHEADERFILE + """
  54. #include <WEObjectHandlers.h>
  55. #include <WETabs.h>
  56.  
  57. /* Exported by Qdmodule.c: */
  58. extern PyObject *QdRGB_New(RGBColor *);
  59. extern int QdRGB_Convert(PyObject *, RGBColor *);
  60.  
  61. /* Forward declaration */
  62. staticforward PyObject *WEOObj_New(WEObjectReference);
  63. staticforward PyObject *ExistingwasteObj_New(WEReference);
  64.  
  65. /*
  66. ** Parse/generate TextStyle records
  67. */
  68. static
  69. PyObject *TextStyle_New(itself)
  70.     TextStylePtr itself;
  71. {
  72.  
  73.     return Py_BuildValue("lllO&", (long)itself->tsFont, (long)itself->tsFace, (long)itself->tsSize, QdRGB_New,
  74.                 &itself->tsColor);
  75. }
  76.  
  77. static
  78. TextStyle_Convert(v, p_itself)
  79.     PyObject *v;
  80.     TextStylePtr p_itself;
  81. {
  82.     long font, face, size;
  83.     
  84.     if( !PyArg_ParseTuple(v, "lllO&", &font, &face, &size, QdRGB_Convert, &p_itself->tsColor) )
  85.         return 0;
  86.     p_itself->tsFont = (short)font;
  87.     p_itself->tsFace = (Style)face;
  88.     p_itself->tsSize = (short)size;
  89.     return 1;
  90. }
  91.  
  92. /*
  93. ** Parse/generate RunInfo records
  94. */
  95. static
  96. PyObject *RunInfo_New(itself)
  97.     WERunInfo *itself;
  98. {
  99.  
  100.     return Py_BuildValue("llhhO&O&", itself->runStart, itself->runEnd, itself->runHeight,
  101.         itself->runAscent, TextStyle_New, &itself->runStyle, WEOObj_New, itself->runObject);
  102. }
  103.  
  104. /* Conversion of long points and rects */
  105. int
  106. LongRect_Convert(PyObject *v, LongRect *r)
  107. {
  108.     return PyArg_Parse(v, "(llll)", &r->left, &r->top, &r->right, &r->bottom);
  109. }
  110.  
  111. PyObject *
  112. LongRect_New(LongRect *r)
  113. {
  114.     return Py_BuildValue("(llll)", r->left, r->top, r->right, r->bottom);
  115. }
  116.  
  117.  
  118. LongPt_Convert(PyObject *v, LongPt *p)
  119. {
  120.     return PyArg_Parse(v, "(ll)", &p->h, &p->v);
  121. }
  122.  
  123. PyObject *
  124. LongPt_New(LongPt *p)
  125. {
  126.     return Py_BuildValue("(ll)", p->h, p->v);
  127. }
  128.  
  129. /* Stuff for the callbacks: */
  130. static PyObject *callbackdict;
  131. WENewObjectUPP upp_new_handler;
  132. WEDisposeObjectUPP upp_dispose_handler;
  133. WEDrawObjectUPP upp_draw_handler;
  134. WEClickObjectUPP upp_click_handler;
  135.  
  136. static OSErr
  137. any_handler(WESelector what, WEObjectReference who, PyObject *args, PyObject **rv)
  138. {
  139.     FlavorType tp;
  140.     PyObject *key, *func;
  141.     
  142.     if ( args == NULL ) return errAECorruptData;
  143.     
  144.     tp = WEGetObjectType(who);
  145.     
  146.     if( (key=Py_BuildValue("O&O&", PyMac_BuildOSType, tp, PyMac_BuildOSType, what)) == NULL)
  147.         return errAECorruptData;
  148.     if( (func = PyDict_GetItem(callbackdict, key)) == NULL ) {
  149.         Py_DECREF(key);
  150.         return errAEHandlerNotFound;
  151.     }
  152.     Py_INCREF(func);
  153.     *rv = PyEval_CallObject(func, args);
  154.     Py_DECREF(func);
  155.     Py_DECREF(key);
  156.     if ( *rv == NULL ) {
  157.         fprintf(stderr, "--Exception in callback: ");
  158.         PyErr_Print();
  159.         return errAEReplyNotArrived;
  160.     }
  161.     return 0;
  162. }
  163.  
  164. static pascal OSErr
  165. my_new_handler(Point *objectSize, WEObjectReference objref)
  166. {
  167.     PyObject *args=NULL, *rv=NULL;
  168.     OSErr err;
  169.     
  170.     args=Py_BuildValue("(O&)", WEOObj_New, objref);
  171.     err = any_handler(weNewHandler, objref, args, &rv);
  172.     if (!err) {
  173.         if (!PyMac_GetPoint(rv, objectSize) )
  174.             err = errAECoercionFail;
  175.     }
  176.     if ( args ) Py_DECREF(args);
  177.     if ( rv ) Py_DECREF(rv);
  178.     return err;
  179. }
  180.  
  181. static pascal OSErr
  182. my_dispose_handler(WEObjectReference objref)
  183. {
  184.     PyObject *args=NULL, *rv=NULL;
  185.     OSErr err;
  186.     
  187.     args=Py_BuildValue("(O&)", WEOObj_New, objref);
  188.     err = any_handler(weDisposeHandler, objref, args, &rv);
  189.     if ( args ) Py_DECREF(args);
  190.     if ( rv ) Py_DECREF(rv);
  191.     return err;
  192. }
  193.  
  194. static pascal OSErr
  195. my_draw_handler(Rect *destRect, WEObjectReference objref)
  196. {
  197.     PyObject *args=NULL, *rv=NULL;
  198.     OSErr err;
  199.     
  200.     args=Py_BuildValue("O&O&", PyMac_BuildRect, destRect, WEOObj_New, objref);
  201.     err = any_handler(weDrawHandler, objref, args, &rv);
  202.     if ( args ) Py_DECREF(args);
  203.     if ( rv ) Py_DECREF(rv);
  204.     return err;
  205. }
  206.  
  207. static pascal Boolean
  208. my_click_handler(Point hitPt, EventModifiers modifiers,
  209.         unsigned long clickTime, WEObjectReference objref)
  210. {
  211.     PyObject *args=NULL, *rv=NULL;
  212.     int retvalue;
  213.     OSErr err;
  214.     
  215.     args=Py_BuildValue("O&llO&", PyMac_BuildPoint, hitPt,
  216.             (long)modifiers, (long)clickTime, WEOObj_New, objref);
  217.     err = any_handler(weClickHandler, objref, args, &rv);
  218.     if (!err)
  219.         retvalue = PyInt_AsLong(rv);
  220.     else
  221.         retvalue = 0;
  222.     if ( args ) Py_DECREF(args);
  223.     if ( rv ) Py_DECREF(rv);
  224.     return retvalue;
  225. }
  226.         
  227.  
  228. """
  229. finalstuff = finalstuff + """
  230. /* Return the object corresponding to the window, or NULL */
  231.  
  232. PyObject *
  233. ExistingwasteObj_New(w)
  234.     WEReference w;
  235. {
  236.     PyObject *it = NULL;
  237.     
  238.     if (w == NULL)
  239.         it = NULL;
  240.     else
  241.         WEGetInfo(weRefCon, (void *)&it, w);
  242.     if (it == NULL || ((wasteObject *)it)->ob_itself != w)
  243.         it = Py_None;
  244.     Py_INCREF(it);
  245.     return it;
  246. }
  247. """
  248.  
  249. class WEMethodGenerator(OSErrMethodGenerator):
  250.     """Similar to MethodGenerator, but has self as last argument"""
  251.  
  252.     def parseArgumentList(self, args):
  253.         args, a0 = args[:-1], args[-1]
  254.         t0, n0, m0 = a0
  255.         if m0 != InMode:
  256.             raise ValueError, "method's 'self' must be 'InMode'"
  257.         self.itself = Variable(t0, "_self->ob_itself", SelfMode)
  258.         FunctionGenerator.parseArgumentList(self, args)
  259.         self.argumentList.append(self.itself)
  260.  
  261.  
  262.  
  263. class WEObjectDefinition(GlobalObjectDefinition):
  264.     def outputCheckNewArg(self):
  265.         Output("""if (itself == NULL) {
  266.                     PyErr_SetString(waste_Error,"Cannot create null WE");
  267.                     return NULL;
  268.                 }""")
  269.     def outputInitStructMembers(self):
  270.         GlobalObjectDefinition.outputInitStructMembers(self)
  271.         Output("WESetInfo(weRefCon, (void *)&it, itself);")
  272.     def outputFreeIt(self, itselfname):
  273.         Output("WEDispose(%s);", itselfname)
  274.         
  275. class WEOObjectDefinition(GlobalObjectDefinition):
  276.     def outputCheckNewArg(self):
  277.         Output("""if (itself == NULL) {
  278.                     Py_INCREF(Py_None);
  279.                     return Py_None;
  280.                 }""")
  281.                 
  282. variablestuff = """
  283.     callbackdict = PyDict_New();
  284.     if (callbackdict == NULL || PyDict_SetItemString(d, "callbacks", callbackdict) != 0)
  285.         Py_FatalError("can't initialize Waste.callbackdict");
  286.     upp_new_handler = NewWENewObjectProc(my_new_handler);
  287.     upp_dispose_handler = NewWEDisposeObjectProc(my_dispose_handler);
  288.     upp_draw_handler = NewWEDrawObjectProc(my_draw_handler);
  289.     upp_click_handler = NewWEClickObjectProc(my_click_handler);
  290. """
  291.  
  292.  
  293. # From here on it's basically all boiler plate...
  294.  
  295. # Test types used for existence
  296. ## execfile(TYPETESTFILE)
  297.  
  298. # Create the generator groups and link them
  299. module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff)
  300. object = WEObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
  301. object2 = WEOObjectDefinition("WEO", "WEOObj", "WEObjectReference")
  302. module.addobject(object2)
  303. module.addobject(object)
  304.  
  305. # Create the generator classes used to populate the lists
  306. Function = OSErrFunctionGenerator
  307. Method = WEMethodGenerator
  308. Method2 = OSErrMethodGenerator
  309.  
  310. # Create and populate the lists
  311. functions = []
  312. methods = []
  313. methods2 = []
  314. execfile(INPUTFILE)
  315.  
  316. # A function written by hand:
  317. stdhandlers_body = """
  318.     OSErr err;
  319.     // install the sample object handlers for pictures and sounds
  320. #define    kTypePicture            'PICT'
  321. #define    kTypeSound                'snd '
  322.     
  323.     if ( !PyArg_ParseTuple(_args, "") ) return NULL;
  324.     
  325.     if ((err = WEInstallObjectHandler(kTypePicture, weNewHandler,
  326.                 (UniversalProcPtr) NewWENewObjectProc(HandleNewPicture), NULL)) != noErr)
  327.         goto cleanup;
  328.     
  329.     if ((err = WEInstallObjectHandler(kTypePicture, weDisposeHandler,
  330.                 (UniversalProcPtr) NewWEDisposeObjectProc(HandleDisposePicture), NULL)) != noErr)
  331.         goto cleanup;
  332.     
  333.     if ((err = WEInstallObjectHandler(kTypePicture, weDrawHandler,
  334.                 (UniversalProcPtr) NewWEDrawObjectProc(HandleDrawPicture), NULL)) != noErr)
  335.         goto cleanup;
  336.     
  337.     if ((err = WEInstallObjectHandler(kTypeSound, weNewHandler,
  338.                 (UniversalProcPtr) NewWENewObjectProc(HandleNewSound), NULL)) != noErr)
  339.         goto cleanup;
  340.     
  341.     if ((err = WEInstallObjectHandler(kTypeSound, weDrawHandler,
  342.                 (UniversalProcPtr) NewWEDrawObjectProc(HandleDrawSound), NULL)) != noErr)
  343.         goto cleanup;
  344.     
  345.     if ((err = WEInstallObjectHandler(kTypeSound, weClickHandler,
  346.                 (UniversalProcPtr) NewWEClickObjectProc(HandleClickSound), NULL)) != noErr)
  347.         goto cleanup;
  348.     Py_INCREF(Py_None);
  349.     return Py_None;
  350.     
  351. cleanup:
  352.     return PyMac_Error(err);
  353. """
  354.  
  355. inshandler_body = """
  356.     OSErr err;
  357.     FlavorType objectType;
  358.     WESelector selector;
  359.     PyObject *py_handler;
  360.     UniversalProcPtr handler;
  361.     WEReference we = NULL;
  362.     PyObject *key;
  363.     
  364.     
  365.     if ( !PyArg_ParseTuple(_args, "O&O&O|O&",
  366.             PyMac_GetOSType, &objectType,
  367.             PyMac_GetOSType, &selector,
  368.             &py_handler,
  369.             WEOObj_Convert, &we) ) return NULL;
  370.             
  371.     if ( selector == weNewHandler ) handler = (UniversalProcPtr)upp_new_handler;
  372.     else if ( selector == weDisposeHandler ) handler = (UniversalProcPtr)upp_dispose_handler;
  373.     else if ( selector == weDrawHandler ) handler = (UniversalProcPtr)upp_draw_handler;
  374.     else if ( selector == weClickHandler ) handler = (UniversalProcPtr)upp_click_handler;
  375.     else return PyMac_Error(weUndefinedSelectorErr);
  376.             
  377.     if ((key = Py_BuildValue("O&O&", 
  378.             PyMac_BuildOSType, objectType, 
  379.             PyMac_BuildOSType, selector)) == NULL )
  380.         return NULL;
  381.         
  382.     PyDict_SetItem(callbackdict, key, py_handler);
  383.     
  384.     err = WEInstallObjectHandler(objectType, selector, handler, we);
  385.     if ( err ) return PyMac_Error(err);
  386.     Py_INCREF(Py_None);
  387.     return Py_None;
  388. """
  389.  
  390. stdhand = ManualGenerator("STDObjectHandlers", stdhandlers_body)
  391. inshand = ManualGenerator("WEInstallObjectHandler", inshandler_body)
  392.  
  393. f = Method(OSErr, 'WEInstallTabHooks', (WEReference, 'we', InMode))
  394. methods.append(f)
  395. f = Method(OSErr, 'WERemoveTabHooks', (WEReference, 'we', InMode))
  396. methods.append(f)
  397. f = Method(Boolean, 'WEIsTabHooks', (WEReference, 'we', InMode))
  398. methods.append(f)
  399.  
  400. # add the populated lists to the generator groups
  401. # (in a different wordl the scan program would generate this)
  402. for f in functions: module.add(f)
  403. module.add(stdhand)
  404. module.add(inshand)
  405. for f in methods: object.add(f)
  406. for f in methods2: object2.add(f)
  407.  
  408. # generate output (open the output file as late as possible)
  409. SetOutputFileName(OUTPUTFILE)
  410. module.generate()
  411.  
  412.