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
= 'Lists.h' # The Apple header file
10 MODNAME
= 'List' # The name of the module
11 OBJECTNAME
= 'List' # The basic name of the objects used here
12 KIND
= 'Handle' # Usually 'Ptr' or 'Handle'
14 # The following is *usually* unchanged but may still require tuning
15 MODPREFIX
= MODNAME
# The prefix for module-wide routines
16 OBJECTTYPE
= "ListHandle" # The C type used to represent them
17 OBJECTPREFIX
= MODPREFIX
+ 'Obj' # The prefix for object methods
18 INPUTFILE
= string
.lower(MODPREFIX
) + 'gen.py' # The file generated by the scanner
19 OUTPUTFILE
= MODNAME
+ "module.c" # The file generated by this program
21 from macsupport
import *
23 # Create the type objects
24 ListHandle
= OpaqueByValueType("ListHandle", "ListObj")
26 VarOutBufferShortsize
= VarHeapOutputBufferType('char', 'short', 's') # (buf, &len)
27 InBufferShortsize
= VarInputBufferType('char', 'short', 's') # (buf, len)
29 RgnHandle
= OpaqueByValueType("RgnHandle", "ResObj")
30 Handle
= OpaqueByValueType("Handle", "ResObj")
32 includestuff
= includestuff
+ """
33 #include <%s>""" % MACHEADERFILE
+ """
35 #define as_List(x) ((ListHandle)x)
36 #define as_Resource(lh) ((Handle)lh)
39 class ListMethodGenerator(MethodGenerator
):
40 """Similar to MethodGenerator, but has self as last argument"""
42 def parseArgumentList(self
, args
):
43 args
, a0
= args
[:-1], args
[-1]
46 raise ValueError, "method's 'self' must be 'InMode'"
47 self
.itself
= Variable(t0
, "_self->ob_itself", SelfMode
)
48 FunctionGenerator
.parseArgumentList(self
, args
)
49 self
.argumentList
.append(self
.itself
)
51 getattrHookCode
= """{
52 /* XXXX Should we HLock() here?? */
53 if ( strcmp(name, "listFlags") == 0 )
54 return Py_BuildValue("l", (long)(*self->ob_itself)->listFlags & 0xff);
55 if ( strcmp(name, "selFlags") == 0 )
56 return Py_BuildValue("l", (long)(*self->ob_itself)->selFlags & 0xff);
61 ListObj_setattr(self, name, value)
68 if ( value == NULL || !PyInt_Check(value) )
70 intval = PyInt_AsLong(value);
71 if (strcmp(name, "listFlags") == 0 ) {
72 /* XXXX Should we HLock the handle here?? */
73 (*self->ob_itself)->listFlags = intval;
76 if (strcmp(name, "selFlags") == 0 ) {
77 (*self->ob_itself)->selFlags = intval;
85 class MyObjectDefinition(GlobalObjectDefinition
):
87 def outputStructMembers(self
):
88 ObjectDefinition
.outputStructMembers(self
)
89 Output("int ob_must_be_disposed;")
91 def outputCheckNewArg(self
):
92 Output("""if (itself == NULL) {
93 PyErr_SetString(List_Error,"Cannot create null List");
97 def outputInitStructMembers(self
):
98 ObjectDefinition
.outputInitStructMembers(self
)
99 Output("it->ob_must_be_disposed = 1;")
101 def outputFreeIt(self
, itselfname
):
102 Output("if (self->ob_must_be_disposed && %s) LDispose(%s);", itselfname
, itselfname
)
104 def outputGetattrHook(self
):
105 Output(getattrHookCode
)
107 def outputSetattr(self
):
110 # From here on it's basically all boiler plate...
112 # Create the generator groups and link them
113 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
)
114 object = MyObjectDefinition(OBJECTNAME
, OBJECTPREFIX
, OBJECTTYPE
)
115 module
.addobject(object)
117 # Create the generator classes used to populate the lists
118 Function
= FunctionGenerator
119 Method
= ListMethodGenerator
121 # Create and populate the lists
126 # Function to convert any handle to a list and vv.
127 ##f = Function(ListHandle, 'as_List', (Handle, 'h', InMode))
131 if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
133 l = (ListObject *)ListObj_New(as_List(h));
134 l->ob_must_be_disposed = 0;
135 return Py_BuildValue("O", l);
137 f
= ManualGenerator("as_List", as_List_body
)
138 f
.docstring
= lambda: "(Resource)->List.\nReturns List object (which is not auto-freed!)"
141 f
= Method(Handle
, 'as_Resource', (ListHandle
, 'lh', InMode
))
144 # add the populated lists to the generator groups
145 # (in a different wordl the scan program would generate this)
146 for f
in functions
: module
.add(f
)
147 for f
in methods
: object.add(f
)
149 # generate output (open the output file as late as possible)
150 SetOutputFileName(OUTPUTFILE
)