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 = '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
|
---|
20 |
|
---|
21 | from macsupport import *
|
---|
22 |
|
---|
23 | # Create the type objects
|
---|
24 |
|
---|
25 | MenuHandle = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
|
---|
26 | MenuRef = MenuHandle
|
---|
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")
|
---|
40 |
|
---|
41 | includestuff = includestuff + """
|
---|
42 | #include <Carbon/Carbon.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | #ifdef USE_TOOLBOX_OBJECT_GLUE
|
---|
46 |
|
---|
47 | extern PyObject *_MenuObj_New(MenuHandle);
|
---|
48 | extern int _MenuObj_Convert(PyObject *, MenuHandle *);
|
---|
49 |
|
---|
50 | #define MenuObj_New _MenuObj_New
|
---|
51 | #define MenuObj_Convert _MenuObj_Convert
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | #define as_Menu(h) ((MenuHandle)h)
|
---|
55 | #define as_Resource(h) ((Handle)h)
|
---|
56 |
|
---|
57 |
|
---|
58 | /* Alternative version of MenuObj_New, which returns None for NULL argument */
|
---|
59 | PyObject *OptMenuObj_New(MenuRef itself)
|
---|
60 | {
|
---|
61 | if (itself == NULL) {
|
---|
62 | Py_INCREF(Py_None);
|
---|
63 | return Py_None;
|
---|
64 | }
|
---|
65 | return MenuObj_New(itself);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* Alternative version of MenuObj_Convert, which returns NULL for a None argument */
|
---|
69 | int OptMenuObj_Convert(PyObject *v, MenuRef *p_itself)
|
---|
70 | {
|
---|
71 | if ( v == Py_None ) {
|
---|
72 | *p_itself = NULL;
|
---|
73 | return 1;
|
---|
74 | }
|
---|
75 | return MenuObj_Convert(v, p_itself);
|
---|
76 | }
|
---|
77 | """
|
---|
78 |
|
---|
79 | initstuff = initstuff + """
|
---|
80 | PyMac_INIT_TOOLBOX_OBJECT_NEW(MenuHandle, MenuObj_New);
|
---|
81 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(MenuHandle, MenuObj_Convert);
|
---|
82 | """
|
---|
83 |
|
---|
84 | class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
|
---|
85 | pass
|
---|
86 |
|
---|
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)
|
---|
91 |
|
---|
92 | # Create the generator classes used to populate the lists
|
---|
93 | Function = OSErrWeakLinkFunctionGenerator
|
---|
94 | Method = OSErrWeakLinkMethodGenerator
|
---|
95 |
|
---|
96 | # Create and populate the lists
|
---|
97 | functions = []
|
---|
98 | methods = []
|
---|
99 | execfile(INPUTFILE)
|
---|
100 | execfile(EXTRAFILE)
|
---|
101 |
|
---|
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)
|
---|
105 |
|
---|
106 | # generate output (open the output file as late as possible)
|
---|
107 | SetOutputFileName(OUTPUTFILE)
|
---|
108 | module.generate()
|
---|