Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Mac / Modules / list / listsupport.py
blobeca08cbeb0062b60da976ecd5c3b4a8749021d00
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).
6 import string
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")
25 Cell = Point
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)
37 """
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]
44 t0, n0, m0 = a0
45 if m0 != InMode:
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);
57 }"""
59 setattrCode = """
60 static int
61 ListObj_setattr(self, name, value)
62 ListObject *self;
63 char *name;
64 PyObject *value;
66 long intval;
68 if ( value == NULL || !PyInt_Check(value) )
69 return -1;
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;
74 return 0;
76 if (strcmp(name, "selFlags") == 0 ) {
77 (*self->ob_itself)->selFlags = intval;
78 return 0;
80 return -1;
82 """
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");
94 return NULL;
95 }""")
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):
108 Output(setattrCode)
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
122 functions = []
123 methods = []
124 execfile(INPUTFILE)
126 # Function to convert any handle to a list and vv.
127 ##f = Function(ListHandle, 'as_List', (Handle, 'h', InMode))
128 as_List_body = """
129 Handle h;
130 ListObject *l;
131 if (!PyArg_ParseTuple(_args, "O&", ResObj_Convert, &h))
132 return NULL;
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!)"
139 functions.append(f)
141 f = Method(Handle, 'as_Resource', (ListHandle, 'lh', InMode))
142 methods.append(f)
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)
151 module.generate()