• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6import string
7
8# Declarations that change for each manager
9MACHEADERFILE = 'Menus.h'               # The Apple header file
10MODNAME = '_Menu'                       # The name of the module
11OBJECTNAME = 'Menu'                     # The basic name of the objects used here
12
13# The following is *usually* unchanged but may still require tuning
14MODPREFIX = 'Menu'                      # The prefix for module-wide routines
15OBJECTTYPE = OBJECTNAME + 'Handle'      # The C type used to represent them
16OBJECTPREFIX = MODPREFIX + 'Obj'        # The prefix for object methods
17INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
18EXTRAFILE = string.lower(MODPREFIX) + 'edit.py' # A similar file but hand-made
19OUTPUTFILE = MODNAME + "module.c"       # The file generated by this program
20
21from macsupport import *
22
23# Create the type objects
24
25MenuHandle = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
26MenuRef = MenuHandle
27OptMenuRef = OpaqueByValueType(OBJECTTYPE, "Opt" + OBJECTPREFIX)
28Handle = OpaqueByValueType("Handle", "ResObj")
29MenuBarHandle = OpaqueByValueType("MenuBarHandle", "ResObj")
30MenuID = Type("MenuID", "h")
31MenuItemIndex = Type("MenuItemIndex", "h")
32MenuItemID = Type("MenuItemID", "l")
33MenuCommand = Type("MenuCommand", "l")
34MenuAttributes = Type("MenuAttributes", "l")
35MenuItemAttributes = Type("MenuItemAttributes", "l")
36unsigned_char = Type('unsigned char', 'b')
37FMFontFamily = Type("FMFontFamily", "h")
38FMFontStyle = Type("FMFontStyle", "h")
39UniChar = Type("UniChar", "h")
40
41includestuff = includestuff + """
42#include <Carbon/Carbon.h>
43
44
45#ifdef USE_TOOLBOX_OBJECT_GLUE
46
47extern PyObject *_MenuObj_New(MenuHandle);
48extern 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 */
59PyObject *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 */
69int 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
79initstuff = initstuff + """
80        PyMac_INIT_TOOLBOX_OBJECT_NEW(MenuHandle, MenuObj_New);
81        PyMac_INIT_TOOLBOX_OBJECT_CONVERT(MenuHandle, MenuObj_Convert);
82"""
83
84class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
85    pass
86
87# Create the generator groups and link them
88module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
89object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
90module.addobject(object)
91
92# Create the generator classes used to populate the lists
93Function = OSErrWeakLinkFunctionGenerator
94Method = OSErrWeakLinkMethodGenerator
95
96# Create and populate the lists
97functions = []
98methods = []
99execfile(INPUTFILE)
100execfile(EXTRAFILE)
101
102# add the populated lists to the generator groups
103for f in functions: module.add(f)
104for f in methods: object.add(f)
105
106# generate output (open the output file as late as possible)
107SetOutputFileName(OUTPUTFILE)
108module.generate()
109