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 | MODNAME = '_Launch' # The name of the module
|
---|
10 | OBJECTNAME = 'UNUSED' # The basic name of the objects used here
|
---|
11 | KIND = 'Record' # Usually 'Ptr' or 'Handle'
|
---|
12 |
|
---|
13 | # The following is *usually* unchanged but may still require tuning
|
---|
14 | MODPREFIX = 'Launch' # The prefix for module-wide routines
|
---|
15 | OBJECTTYPE = OBJECTNAME + KIND # 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 | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
|
---|
19 |
|
---|
20 | from macsupport import *
|
---|
21 |
|
---|
22 | # Create the type objects
|
---|
23 | LSAcceptanceFlags = Type("LSAcceptanceFlags", "l")
|
---|
24 | LSInitializeFlags = Type("LSInitializeFlags", "l")
|
---|
25 | LSRequestedInfo = Type("LSRequestedInfo", "l")
|
---|
26 | LSRolesMask = Type("LSRolesMask", "l")
|
---|
27 | UniCharCount = Type("UniCharCount", "l")
|
---|
28 | OptCFStringRef = OpaqueByValueType("CFStringRef", "OptCFStringRefObj")
|
---|
29 | LSItemInfoRecord = OpaqueType("LSItemInfoRecord", "LSItemInfoRecord")
|
---|
30 |
|
---|
31 | includestuff = includestuff + """
|
---|
32 | #if PY_VERSION_HEX < 0x02040000
|
---|
33 | PyObject *PyMac_GetOSErrException(void);
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include <ApplicationServices/ApplicationServices.h>
|
---|
37 |
|
---|
38 | /*
|
---|
39 | ** Optional CFStringRef. None will pass NULL
|
---|
40 | */
|
---|
41 | static int
|
---|
42 | OptCFStringRefObj_Convert(PyObject *v, CFStringRef *spec)
|
---|
43 | {
|
---|
44 | if (v == Py_None) {
|
---|
45 | *spec = NULL;
|
---|
46 | return 1;
|
---|
47 | }
|
---|
48 | return CFStringRefObj_Convert(v, spec);
|
---|
49 | }
|
---|
50 |
|
---|
51 | PyObject *
|
---|
52 | OptCFStringRefObj_New(CFStringRef it)
|
---|
53 | {
|
---|
54 | if (it == NULL) {
|
---|
55 | Py_INCREF(Py_None);
|
---|
56 | return Py_None;
|
---|
57 | }
|
---|
58 | return CFStringRefObj_New(it);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /*
|
---|
62 | ** Convert LSItemInfoRecord to Python.
|
---|
63 | */
|
---|
64 | PyObject *
|
---|
65 | LSItemInfoRecord_New(LSItemInfoRecord *it)
|
---|
66 | {
|
---|
67 | return Py_BuildValue("{s:is:O&s:O&s:O&s:O&s:i}",
|
---|
68 | "flags", it->flags,
|
---|
69 | "filetype", PyMac_BuildOSType, it->filetype,
|
---|
70 | "creator", PyMac_BuildOSType, it->creator,
|
---|
71 | "extension", OptCFStringRefObj_New, it->extension,
|
---|
72 | "iconFileName", OptCFStringRefObj_New, it->iconFileName,
|
---|
73 | "kindID", it->kindID);
|
---|
74 | }
|
---|
75 | """
|
---|
76 |
|
---|
77 | # From here on it's basically all boiler plate...
|
---|
78 | execfile(string.lower(MODPREFIX) + 'typetest.py')
|
---|
79 |
|
---|
80 | # Create the generator groups and link them
|
---|
81 | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
|
---|
82 | ##object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
|
---|
83 | ##module.addobject(object)
|
---|
84 |
|
---|
85 | # Create the generator classes used to populate the lists
|
---|
86 | Function = OSErrFunctionGenerator
|
---|
87 | ##Method = OSErrMethodGenerator
|
---|
88 |
|
---|
89 | # Create and populate the lists
|
---|
90 | functions = []
|
---|
91 | ##methods = []
|
---|
92 | execfile(INPUTFILE)
|
---|
93 |
|
---|
94 | # add the populated lists to the generator groups
|
---|
95 | # (in a different wordl the scan program would generate this)
|
---|
96 | for f in functions: module.add(f)
|
---|
97 | ##for f in methods: object.add(f)
|
---|
98 |
|
---|
99 | # generate output (open the output file as late as possible)
|
---|
100 | SetOutputFileName(OUTPUTFILE)
|
---|
101 | module.generate()
|
---|