blob: 0f081a6e67458faa29024e150835a53f338823f5 [file] [log] [blame]
Jack Jansen90ecdf41996-04-16 14:29:15 +00001# 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
6import string
7
8# Declarations that change for each manager
9MACHEADERFILE = 'WASTE.h' # The Apple header file
10MODNAME = 'waste' # The name of the module
11OBJECTNAME = 'waste' # The basic name of the objects used here
12KIND = 'Ptr' # Usually 'Ptr' or 'Handle'
13
14# The following is *usually* unchanged but may still require tuning
15MODPREFIX = MODNAME # The prefix for module-wide routines
16OBJECTTYPE = "WEReference" # The C type used to represent them
17OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
18INPUTFILE = 'wastegen.py' # The file generated by the scanner
19TYPETESTFILE = 'wastetypetest.py' # Another file generated by the scanner
20OUTPUTFILE = "wastemodule.c" # The file generated by this program
21
22from macsupport import *
23
24# Create the type objects
25WEReference = OpaqueByValueType("WEReference", "wasteObj")
26WEObjectReference = OpaqueByValueType("WEObjectReference", "WEOObj")
27##CharsHandle = OpaqueByValueType("CharsHandle", "ResObj")
28##Handle = OpaqueByValueType("Handle", "ResObj")
29StScrpHandle = OpaqueByValueType("StScrpHandle", "ResObj")
30##TEStyleHandle = OpaqueByValueType("TEStyleHandle", "ResObj")
31RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
32EventModifiers = Type("EventModifiers", "h")
33FlavorType = OSTypeType("FlavorType")
34WESelector = OSTypeType("WESelector")
35
36WEStyleMode = Type("WEStyleMode", "h")
37WEActionKind = Type("WEActionKind", "h")
38WEAlignment = Type("WEAlignment", "b")
39WESoupHandle = OpaqueByValueType("WESoupHandle", "ResObj")
40WERunInfo = OpaqueType("WERunInfo", "RunInfo")
41
42TextStyle = OpaqueType("TextStyle", "TextStyle")
43TextStyle_ptr = TextStyle
44LongPt = OpaqueType("LongPt", "LongPt")
45LongPt_ptr = LongPt
46LongRect = OpaqueType("LongRect", "LongRect")
47LongRect_ptr = LongRect
48
49includestuff = includestuff + """
50#include <%s>""" % MACHEADERFILE + """
51
52/* Exported by Qdmodule.c: */
53extern PyObject *QdRGB_New(RGBColor *);
54extern int QdRGB_Convert(PyObject *, RGBColor *);
55
56/* Forward declaration */
57staticforward PyObject *WEOObj_New(WEObjectReference);
58
59/*
60** Parse/generate TextStyle records
61*/
62static
63PyObject *TextStyle_New(itself)
64 TextStylePtr itself;
65{
66
67 return Py_BuildValue("lllO&", (long)itself->tsFont, (long)itself->tsFace, (long)itself->tsSize, QdRGB_New,
68 &itself->tsColor);
69}
70
71static
72TextStyle_Convert(v, p_itself)
73 PyObject *v;
74 TextStylePtr p_itself;
75{
76 long font, face, size;
77
78 if( !PyArg_ParseTuple(v, "lllO&", &font, &face, &size, QdRGB_Convert, &p_itself->tsColor) )
79 return 0;
80 p_itself->tsFont = (short)font;
81 p_itself->tsFace = (Style)face;
82 p_itself->tsSize = (short)size;
83 return 1;
84}
85
86/*
87** Parse/generate RunInfo records
88*/
89static
90PyObject *RunInfo_New(itself)
91 WERunInfo *itself;
92{
93
94 return Py_BuildValue("llhhO&O&", itself->runStart, itself->runEnd, itself->runHeight,
95 itself->runAscent, TextStyle_New, &itself->runStyle, WEOObj_New, itself->runObject);
96}
97
98/* Conversion of long points and rects */
99int
100LongRect_Convert(PyObject *v, LongRect *r)
101{
102 return PyArg_Parse(v, "(llll)", &r->left, &r->top, &r->right, &r->bottom);
103}
104
105PyObject *
106LongRect_New(LongRect *r)
107{
108 return Py_BuildValue("(llll)", r->left, r->top, r->right, r->bottom);
109}
110
111
112LongPt_Convert(PyObject *v, LongPt *p)
113{
114 return PyArg_Parse(v, "(ll)", &p->h, &p->v);
115}
116
117PyObject *
118LongPt_New(LongPt *p)
119{
120 return Py_BuildValue("(ll)", p->h, p->v);
121}
122"""
123
124class WEMethodGenerator(OSErrMethodGenerator):
125 """Similar to MethodGenerator, but has self as last argument"""
126
127 def parseArgumentList(self, args):
128 args, a0 = args[:-1], args[-1]
129 t0, n0, m0 = a0
130 if m0 != InMode:
131 raise ValueError, "method's 'self' must be 'InMode'"
132 self.itself = Variable(t0, "_self->ob_itself", SelfMode)
133 FunctionGenerator.parseArgumentList(self, args)
134 self.argumentList.append(self.itself)
135
136
137
138class WEObjectDefinition(GlobalObjectDefinition):
139 def outputCheckNewArg(self):
140 Output("""if (itself == NULL) {
141 PyErr_SetString(waste_Error,"Cannot create null WE");
142 return NULL;
143 }""")
144 def outputFreeIt(self, itselfname):
145 Output("WEDispose(%s);", itselfname)
146
147class WEOObjectDefinition(GlobalObjectDefinition):
148 def outputCheckNewArg(self):
149 Output("""if (itself == NULL) {
150 Py_INCREF(Py_None);
151 return Py_None;
152 }""")
153
154# From here on it's basically all boiler plate...
155
156# Test types used for existence
157execfile(TYPETESTFILE)
158
159# Create the generator groups and link them
160module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
161object = WEObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
162object2 = WEOObjectDefinition("WEO", "WEOObj", "WEObjectReference")
163module.addobject(object2)
164module.addobject(object)
165
166# Create the generator classes used to populate the lists
167Function = OSErrFunctionGenerator
168Method = WEMethodGenerator
169Method2 = OSErrMethodGenerator
170
171# Create and populate the lists
172functions = []
173methods = []
174methods2 = []
175execfile(INPUTFILE)
176
177# add the populated lists to the generator groups
178# (in a different wordl the scan program would generate this)
179for f in functions: module.add(f)
180for f in methods: object.add(f)
181for f in methods2: object2.add(f)
182
183# generate output (open the output file as late as possible)
184SetOutputFileName(OUTPUTFILE)
185module.generate()
186