Jack Jansen | 90ecdf4 | 1996-04-16 14:29:15 +0000 | [diff] [blame^] | 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 | WEObjectReference = OpaqueByValueType("WEObjectReference", "WEOObj") |
| 27 | ##CharsHandle = OpaqueByValueType("CharsHandle", "ResObj") |
| 28 | ##Handle = OpaqueByValueType("Handle", "ResObj") |
| 29 | StScrpHandle = OpaqueByValueType("StScrpHandle", "ResObj") |
| 30 | ##TEStyleHandle = OpaqueByValueType("TEStyleHandle", "ResObj") |
| 31 | RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
| 32 | EventModifiers = Type("EventModifiers", "h") |
| 33 | FlavorType = OSTypeType("FlavorType") |
| 34 | WESelector = OSTypeType("WESelector") |
| 35 | |
| 36 | WEStyleMode = Type("WEStyleMode", "h") |
| 37 | WEActionKind = Type("WEActionKind", "h") |
| 38 | WEAlignment = Type("WEAlignment", "b") |
| 39 | WESoupHandle = OpaqueByValueType("WESoupHandle", "ResObj") |
| 40 | WERunInfo = OpaqueType("WERunInfo", "RunInfo") |
| 41 | |
| 42 | TextStyle = OpaqueType("TextStyle", "TextStyle") |
| 43 | TextStyle_ptr = TextStyle |
| 44 | LongPt = OpaqueType("LongPt", "LongPt") |
| 45 | LongPt_ptr = LongPt |
| 46 | LongRect = OpaqueType("LongRect", "LongRect") |
| 47 | LongRect_ptr = LongRect |
| 48 | |
| 49 | includestuff = includestuff + """ |
| 50 | #include <%s>""" % MACHEADERFILE + """ |
| 51 | |
| 52 | /* Exported by Qdmodule.c: */ |
| 53 | extern PyObject *QdRGB_New(RGBColor *); |
| 54 | extern int QdRGB_Convert(PyObject *, RGBColor *); |
| 55 | |
| 56 | /* Forward declaration */ |
| 57 | staticforward PyObject *WEOObj_New(WEObjectReference); |
| 58 | |
| 59 | /* |
| 60 | ** Parse/generate TextStyle records |
| 61 | */ |
| 62 | static |
| 63 | PyObject *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 | |
| 71 | static |
| 72 | TextStyle_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 | */ |
| 89 | static |
| 90 | PyObject *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 */ |
| 99 | int |
| 100 | LongRect_Convert(PyObject *v, LongRect *r) |
| 101 | { |
| 102 | return PyArg_Parse(v, "(llll)", &r->left, &r->top, &r->right, &r->bottom); |
| 103 | } |
| 104 | |
| 105 | PyObject * |
| 106 | LongRect_New(LongRect *r) |
| 107 | { |
| 108 | return Py_BuildValue("(llll)", r->left, r->top, r->right, r->bottom); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | LongPt_Convert(PyObject *v, LongPt *p) |
| 113 | { |
| 114 | return PyArg_Parse(v, "(ll)", &p->h, &p->v); |
| 115 | } |
| 116 | |
| 117 | PyObject * |
| 118 | LongPt_New(LongPt *p) |
| 119 | { |
| 120 | return Py_BuildValue("(ll)", p->h, p->v); |
| 121 | } |
| 122 | """ |
| 123 | |
| 124 | class 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 | |
| 138 | class 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 | |
| 147 | class 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 |
| 157 | execfile(TYPETESTFILE) |
| 158 | |
| 159 | # Create the generator groups and link them |
| 160 | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
| 161 | object = WEObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
| 162 | object2 = WEOObjectDefinition("WEO", "WEOObj", "WEObjectReference") |
| 163 | module.addobject(object2) |
| 164 | module.addobject(object) |
| 165 | |
| 166 | # Create the generator classes used to populate the lists |
| 167 | Function = OSErrFunctionGenerator |
| 168 | Method = WEMethodGenerator |
| 169 | Method2 = OSErrMethodGenerator |
| 170 | |
| 171 | # Create and populate the lists |
| 172 | functions = [] |
| 173 | methods = [] |
| 174 | methods2 = [] |
| 175 | execfile(INPUTFILE) |
| 176 | |
| 177 | # add the populated lists to the generator groups |
| 178 | # (in a different wordl the scan program would generate this) |
| 179 | for f in functions: module.add(f) |
| 180 | for f in methods: object.add(f) |
| 181 | for f in methods2: object2.add(f) |
| 182 | |
| 183 | # generate output (open the output file as late as possible) |
| 184 | SetOutputFileName(OUTPUTFILE) |
| 185 | module.generate() |
| 186 | |