Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +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 = 'Menus.h' # The Apple header file |
| 10 | MODNAME = 'Menu' # The name of the module |
| 11 | OBJECTNAME = 'Menu' # The basic name of the objects used here |
| 12 | |
| 13 | # The following is *usually* unchanged but may still require tuning |
| 14 | MODPREFIX = MODNAME # The prefix for module-wide routines |
| 15 | OBJECTTYPE = OBJECTNAME + 'Handle' # The C type used to represent them |
| 16 | OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods |
| 17 | INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner |
Guido van Rossum | 86c3af7 | 1995-03-19 22:42:51 +0000 | [diff] [blame] | 18 | EXTRAFILE = string.lower(MODPREFIX) + 'edit.py' # A similar file but hand-made |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 19 | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program |
| 20 | |
| 21 | from macsupport import * |
| 22 | |
| 23 | # Create the type objects |
| 24 | |
| 25 | MenuHandle = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 26 | MenuRef = MenuHandle |
Jack Jansen | a1a0fef | 1999-12-23 14:32:06 +0000 | [diff] [blame] | 27 | Handle = OpaqueByValueType("Handle", "ResObj") |
Jack Jansen | f7d5aa6 | 2000-12-10 23:43:49 +0000 | [diff] [blame] | 28 | MenuBarHandle = OpaqueByValueType("MenuBarHandle", "ResObj") |
| 29 | MenuID = Type("MenuID", "h") |
| 30 | MenuItemIndex = Type("MenuItemIndex", "h") |
| 31 | MenuCommand = Type("MenuCommand", "l") |
| 32 | MenuAttributes = Type("MenuAttributes", "l") |
| 33 | MenuItemAttributes = Type("MenuItemAttributes", "l") |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 34 | unsigned_char = Type('unsigned char', 'b') |
Jack Jansen | f7d5aa6 | 2000-12-10 23:43:49 +0000 | [diff] [blame] | 35 | FMFontFamily = Type("FMFontFamily", "h") |
| 36 | FMFontStyle = Type("FMFontStyle", "h") |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 37 | |
| 38 | includestuff = includestuff + """ |
Guido van Rossum | 86c3af7 | 1995-03-19 22:42:51 +0000 | [diff] [blame] | 39 | #include <Devices.h> /* Defines OpenDeskAcc in universal headers */ |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 40 | #include <%s>""" % MACHEADERFILE + """ |
| 41 | |
Jack Jansen | f7d5aa6 | 2000-12-10 23:43:49 +0000 | [diff] [blame] | 42 | #if !ACCESSOR_CALLS_ARE_FUNCTIONS |
| 43 | #define GetMenuID(menu) ((*(menu))->menuID) |
| 44 | #define GetMenuWidth(menu) ((*(menu))->menuWidth) |
| 45 | #define GetMenuHeight(menu) ((*(menu))->menuHeight) |
| 46 | |
| 47 | #define SetMenuID(menu, id) ((*(menu))->menuID = (id)) |
| 48 | #define SetMenuWidth(menu, width) ((*(menu))->menuWidth = (width)) |
| 49 | #define SetMenuHeight(menu, height) ((*(menu))->menuHeight = (height)) |
| 50 | #endif |
| 51 | |
Jack Jansen | e058189 | 1999-02-07 14:02:03 +0000 | [diff] [blame] | 52 | #define as_Menu(h) ((MenuHandle)h) |
Jack Jansen | a1a0fef | 1999-12-23 14:32:06 +0000 | [diff] [blame] | 53 | #define as_Resource(h) ((Handle)h) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 54 | """ |
| 55 | |
| 56 | class MyObjectDefinition(GlobalObjectDefinition): |
| 57 | pass |
| 58 | |
| 59 | # Create the generator groups and link them |
| 60 | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
| 61 | object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
| 62 | module.addobject(object) |
| 63 | |
| 64 | # Create the generator classes used to populate the lists |
| 65 | Function = OSErrFunctionGenerator |
| 66 | Method = OSErrMethodGenerator |
| 67 | |
| 68 | # Create and populate the lists |
| 69 | functions = [] |
| 70 | methods = [] |
| 71 | execfile(INPUTFILE) |
Guido van Rossum | 86c3af7 | 1995-03-19 22:42:51 +0000 | [diff] [blame] | 72 | execfile(EXTRAFILE) |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 73 | |
| 74 | # add the populated lists to the generator groups |
| 75 | for f in functions: module.add(f) |
| 76 | for f in methods: object.add(f) |
| 77 | |
| 78 | # generate output (open the output file as late as possible) |
| 79 | SetOutputFileName(OUTPUTFILE) |
| 80 | module.generate() |