append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Mac / Modules / qdoffs / qdoffssupport.py
blob057f0a96ce365b9cd2d9e827842a82b7e8e459e3
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).
6 import string
8 # Declarations that change for each manager
9 MACHEADERFILE = 'QDOffscreen.h' # The Apple header file
10 MODNAME = '_Qdoffs' # The name of the module
11 OBJECTNAME = 'GWorld' # The basic name of the objects used here
13 # The following is *usually* unchanged but may still require tuning
14 MODPREFIX = 'Qdoffs' # The prefix for module-wide routines
15 OBJECTTYPE = OBJECTNAME + 'Ptr' # The C type used to represent them
16 OBJECTPREFIX = OBJECTNAME + 'Obj' # The prefix for object methods
17 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
18 #EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions
19 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
21 from macsupport import *
23 # Create the type objects
25 GWorldPtr = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
26 GWorldFlags = Type("GWorldFlags", "l")
27 GDHandle = OpaqueByValueType("GDHandle", "ResObj")
28 OptGDHandle = OpaqueByValueType("GDHandle", "OptResObj")
29 CTabHandle = OpaqueByValueType("CTabHandle", "OptResObj")
30 PixPatHandle = OpaqueByValueType("PixPatHandle", "ResObj")
31 PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj")
32 CGrafPtr = OpaqueByValueType("CGrafPtr", "GrafObj")
33 GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj")
34 QDErr = OSErrType("QDErr", 'h')
36 includestuff = includestuff + """
37 #ifdef WITHOUT_FRAMEWORKS
38 #include <QDOffscreen.h>
39 #else
40 #include <Carbon/Carbon.h>
41 #endif
43 #ifdef USE_TOOLBOX_OBJECT_GLUE
44 extern PyObject *_GWorldObj_New(GWorldPtr);
45 extern int _GWorldObj_Convert(PyObject *, GWorldPtr *);
47 #define GWorldObj_New _GWorldObj_New
48 #define GWorldObj_Convert _GWorldObj_Convert
49 #endif
51 #define as_GrafPtr(gworld) ((GrafPtr)(gworld))
53 """
55 initstuff = initstuff + """
56 PyMac_INIT_TOOLBOX_OBJECT_NEW(GWorldPtr, GWorldObj_New);
57 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(GWorldPtr, GWorldObj_Convert);
58 """
60 class MyObjectDefinition(GlobalObjectDefinition):
61 def outputCheckNewArg(self):
62 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
63 ## def outputInitStructMembers(self):
64 ## GlobalObjectDefinition.outputInitStructMembers(self)
65 ## Output("SetWRefCon(itself, (long)it);")
66 ## def outputCheckConvertArg(self):
67 ## OutLbrace("if (DlgObj_Check(v))")
68 ## Output("*p_itself = ((WindowObject *)v)->ob_itself;")
69 ## Output("return 1;")
70 ## OutRbrace()
71 ## Out("""
72 ## if (v == Py_None) { *p_itself = NULL; return 1; }
73 ## if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
74 ## """)
75 def outputFreeIt(self, itselfname):
76 Output("DisposeGWorld(%s);", itselfname)
77 # From here on it's basically all boiler plate...
79 # Create the generator groups and link them
80 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
81 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
82 module.addobject(object)
85 # Create the generator classes used to populate the lists
86 Function = OSErrWeakLinkFunctionGenerator
87 Method = OSErrWeakLinkMethodGenerator
89 # Create and populate the lists
90 functions = []
91 methods = []
92 execfile(INPUTFILE)
94 # A method to convert a GWorldPtr to a GrafPtr
95 f = Method(GrafPtr, 'as_GrafPtr', (GWorldPtr, 'p', InMode))
96 methods.append(f)
99 # Manual generator: get data out of a pixmap
100 pixmapgetbytes_body = """
101 PixMapHandle pm;
102 int from, length;
103 char *cp;
105 if ( !PyArg_ParseTuple(_args, "O&ii", ResObj_Convert, &pm, &from, &length) )
106 return NULL;
107 cp = GetPixBaseAddr(pm)+from;
108 _res = PyString_FromStringAndSize(cp, length);
109 return _res;
111 f = ManualGenerator("GetPixMapBytes", pixmapgetbytes_body)
112 f.docstring = lambda: """(pixmap, int start, int size) -> string. Return bytes from the pixmap"""
113 functions.append(f)
115 # Manual generator: store data in a pixmap
116 pixmapputbytes_body = """
117 PixMapHandle pm;
118 int from, length;
119 char *cp, *icp;
121 if ( !PyArg_ParseTuple(_args, "O&is#", ResObj_Convert, &pm, &from, &icp, &length) )
122 return NULL;
123 cp = GetPixBaseAddr(pm)+from;
124 memcpy(cp, icp, length);
125 Py_INCREF(Py_None);
126 _res = Py_None;
127 return _res;
129 f = ManualGenerator("PutPixMapBytes", pixmapputbytes_body)
130 f.docstring = lambda: """(pixmap, int start, string data). Store bytes into the pixmap"""
131 functions.append(f)
133 # add the populated lists to the generator groups
134 # (in a different wordl the scan program would generate this)
135 for f in functions: module.add(f)
136 for f in methods: object.add(f)
140 # generate output (open the output file as late as possible)
141 SetOutputFileName(OUTPUTFILE)
142 module.generate()