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).
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
13 # The following is *usually* unchanged but may still require tuning
14 MODPREFIX
= 'Menu' # 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
18 EXTRAFILE
= string
.lower(MODPREFIX
) + 'edit.py' # A similar file but hand-made
19 OUTPUTFILE
= MODNAME
+ "module.c" # The file generated by this program
21 from macsupport
import *
23 # Create the type objects
25 MenuHandle
= OpaqueByValueType(OBJECTTYPE
, OBJECTPREFIX
)
27 OptMenuRef
= OpaqueByValueType(OBJECTTYPE
, "Opt" + OBJECTPREFIX
)
28 Handle
= OpaqueByValueType("Handle", "ResObj")
29 MenuBarHandle
= OpaqueByValueType("MenuBarHandle", "ResObj")
30 MenuID
= Type("MenuID", "h")
31 MenuItemIndex
= Type("MenuItemIndex", "h")
32 MenuItemID
= Type("MenuItemID", "l")
33 MenuCommand
= Type("MenuCommand", "l")
34 MenuAttributes
= Type("MenuAttributes", "l")
35 MenuItemAttributes
= Type("MenuItemAttributes", "l")
36 unsigned_char
= Type('unsigned char', 'b')
37 FMFontFamily
= Type("FMFontFamily", "h")
38 FMFontStyle
= Type("FMFontStyle", "h")
39 UniChar
= Type("UniChar", "h")
41 includestuff
= includestuff
+ """
42 #include <Carbon/Carbon.h>
45 #ifdef USE_TOOLBOX_OBJECT_GLUE
47 extern PyObject *_MenuObj_New(MenuHandle);
48 extern int _MenuObj_Convert(PyObject *, MenuHandle *);
50 #define MenuObj_New _MenuObj_New
51 #define MenuObj_Convert _MenuObj_Convert
54 #define as_Menu(h) ((MenuHandle)h)
55 #define as_Resource(h) ((Handle)h)
58 /* Alternative version of MenuObj_New, which returns None for NULL argument */
59 PyObject *OptMenuObj_New(MenuRef itself)
65 return MenuObj_New(itself);
68 /* Alternative version of MenuObj_Convert, which returns NULL for a None argument */
69 int OptMenuObj_Convert(PyObject *v, MenuRef *p_itself)
75 return MenuObj_Convert(v, p_itself);
79 initstuff
= initstuff
+ """
80 PyMac_INIT_TOOLBOX_OBJECT_NEW(MenuHandle, MenuObj_New);
81 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(MenuHandle, MenuObj_Convert);
84 class MyObjectDefinition(PEP253Mixin
, GlobalObjectDefinition
):
87 # Create the generator groups and link them
88 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
)
89 object = MyObjectDefinition(OBJECTNAME
, OBJECTPREFIX
, OBJECTTYPE
)
90 module
.addobject(object)
92 # Create the generator classes used to populate the lists
93 Function
= OSErrWeakLinkFunctionGenerator
94 Method
= OSErrWeakLinkMethodGenerator
96 # Create and populate the lists
102 # add the populated lists to the generator groups
103 for f
in functions
: module
.add(f
)
104 for f
in methods
: object.add(f
)
106 # generate output (open the output file as late as possible)
107 SetOutputFileName(OUTPUTFILE
)