| 1 | n/a | # This script generates a Python interface for an Apple Macintosh Manager. |
|---|
| 2 | n/a | # It uses the "bgen" package to generate C code. |
|---|
| 3 | n/a | # The function specifications are generated by scanning the mamager's header file, |
|---|
| 4 | n/a | # using the "scantools" package (customized for this particular manager). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | import string |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | # Declarations that change for each manager |
|---|
| 9 | n/a | MACHEADERFILE = 'Menus.h' # The Apple header file |
|---|
| 10 | n/a | MODNAME = '_Menu' # The name of the module |
|---|
| 11 | n/a | OBJECTNAME = 'Menu' # The basic name of the objects used here |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | # The following is *usually* unchanged but may still require tuning |
|---|
| 14 | n/a | MODPREFIX = 'Menu' # The prefix for module-wide routines |
|---|
| 15 | n/a | OBJECTTYPE = OBJECTNAME + 'Handle' # The C type used to represent them |
|---|
| 16 | n/a | OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods |
|---|
| 17 | n/a | INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner |
|---|
| 18 | n/a | EXTRAFILE = string.lower(MODPREFIX) + 'edit.py' # A similar file but hand-made |
|---|
| 19 | n/a | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | from macsupport import * |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | # Create the type objects |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | MenuHandle = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) |
|---|
| 26 | n/a | MenuRef = MenuHandle |
|---|
| 27 | n/a | OptMenuRef = OpaqueByValueType(OBJECTTYPE, "Opt" + OBJECTPREFIX) |
|---|
| 28 | n/a | Handle = OpaqueByValueType("Handle", "ResObj") |
|---|
| 29 | n/a | MenuBarHandle = OpaqueByValueType("MenuBarHandle", "ResObj") |
|---|
| 30 | n/a | MenuID = Type("MenuID", "h") |
|---|
| 31 | n/a | MenuItemIndex = Type("MenuItemIndex", "h") |
|---|
| 32 | n/a | MenuItemID = Type("MenuItemID", "l") |
|---|
| 33 | n/a | MenuCommand = Type("MenuCommand", "l") |
|---|
| 34 | n/a | MenuAttributes = Type("MenuAttributes", "l") |
|---|
| 35 | n/a | MenuItemAttributes = Type("MenuItemAttributes", "l") |
|---|
| 36 | n/a | unsigned_char = Type('unsigned char', 'b') |
|---|
| 37 | n/a | FMFontFamily = Type("FMFontFamily", "h") |
|---|
| 38 | n/a | FMFontStyle = Type("FMFontStyle", "h") |
|---|
| 39 | n/a | UniChar = Type("UniChar", "h") |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | includestuff = includestuff + """ |
|---|
| 42 | n/a | #include <Carbon/Carbon.h> |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | #ifdef USE_TOOLBOX_OBJECT_GLUE |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | extern PyObject *_MenuObj_New(MenuHandle); |
|---|
| 48 | n/a | extern int _MenuObj_Convert(PyObject *, MenuHandle *); |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | #define MenuObj_New _MenuObj_New |
|---|
| 51 | n/a | #define MenuObj_Convert _MenuObj_Convert |
|---|
| 52 | n/a | #endif |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | #define as_Menu(h) ((MenuHandle)h) |
|---|
| 55 | n/a | #define as_Resource(h) ((Handle)h) |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | /* Alternative version of MenuObj_New, which returns None for NULL argument */ |
|---|
| 59 | n/a | PyObject *OptMenuObj_New(MenuRef itself) |
|---|
| 60 | n/a | { |
|---|
| 61 | n/a | if (itself == NULL) { |
|---|
| 62 | n/a | Py_INCREF(Py_None); |
|---|
| 63 | n/a | return Py_None; |
|---|
| 64 | n/a | } |
|---|
| 65 | n/a | return MenuObj_New(itself); |
|---|
| 66 | n/a | } |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | /* Alternative version of MenuObj_Convert, which returns NULL for a None argument */ |
|---|
| 69 | n/a | int OptMenuObj_Convert(PyObject *v, MenuRef *p_itself) |
|---|
| 70 | n/a | { |
|---|
| 71 | n/a | if ( v == Py_None ) { |
|---|
| 72 | n/a | *p_itself = NULL; |
|---|
| 73 | n/a | return 1; |
|---|
| 74 | n/a | } |
|---|
| 75 | n/a | return MenuObj_Convert(v, p_itself); |
|---|
| 76 | n/a | } |
|---|
| 77 | n/a | """ |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | initstuff = initstuff + """ |
|---|
| 80 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(MenuHandle, MenuObj_New); |
|---|
| 81 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(MenuHandle, MenuObj_Convert); |
|---|
| 82 | n/a | """ |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 85 | n/a | pass |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | # Create the generator groups and link them |
|---|
| 88 | n/a | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff) |
|---|
| 89 | n/a | object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
|---|
| 90 | n/a | module.addobject(object) |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | # Create the generator classes used to populate the lists |
|---|
| 93 | n/a | Function = OSErrWeakLinkFunctionGenerator |
|---|
| 94 | n/a | Method = OSErrWeakLinkMethodGenerator |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | # Create and populate the lists |
|---|
| 97 | n/a | functions = [] |
|---|
| 98 | n/a | methods = [] |
|---|
| 99 | n/a | execfile(INPUTFILE) |
|---|
| 100 | n/a | execfile(EXTRAFILE) |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | # add the populated lists to the generator groups |
|---|
| 103 | n/a | for f in functions: module.add(f) |
|---|
| 104 | n/a | for f in methods: object.add(f) |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | # generate output (open the output file as late as possible) |
|---|
| 107 | n/a | SetOutputFileName(OUTPUTFILE) |
|---|
| 108 | n/a | module.generate() |
|---|