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 | #error missing SetActionFilter
|
---|
7 |
|
---|
8 | import string
|
---|
9 |
|
---|
10 | # Declarations that change for each manager
|
---|
11 | MODNAME = '_CF' # The name of the module
|
---|
12 |
|
---|
13 | # The following is *usually* unchanged but may still require tuning
|
---|
14 | MODPREFIX = 'CF' # The prefix for module-wide routines
|
---|
15 | INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
|
---|
16 | OUTPUTFILE = MODNAME + "module.c" # The file generated by this program
|
---|
17 |
|
---|
18 | from macsupport import *
|
---|
19 |
|
---|
20 | # Special case generator for the functions that have an AllocatorRef first argument,
|
---|
21 | # which we skip anyway, and the object as the second arg.
|
---|
22 | class MethodSkipArg1(MethodGenerator):
|
---|
23 | """Similar to MethodGenerator, but has self as last argument"""
|
---|
24 |
|
---|
25 | def parseArgumentList(self, args):
|
---|
26 | if len(args) < 2:
|
---|
27 | raise ValueError, "MethodSkipArg1 expects at least 2 args"
|
---|
28 | a0, a1, args = args[0], args[1], args[2:]
|
---|
29 | t0, n0, m0 = a0
|
---|
30 | if t0 != "CFAllocatorRef" and m0 != InMode:
|
---|
31 | raise ValueError, "MethodSkipArg1 should have dummy AllocatorRef first arg"
|
---|
32 | t1, n1, m1 = a1
|
---|
33 | if m1 != InMode:
|
---|
34 | raise ValueError, "method's 'self' must be 'InMode'"
|
---|
35 | dummy = Variable(t0, n0, m0)
|
---|
36 | self.argumentList.append(dummy)
|
---|
37 | self.itself = Variable(t1, "_self->ob_itself", SelfMode)
|
---|
38 | self.argumentList.append(self.itself)
|
---|
39 | FunctionGenerator.parseArgumentList(self, args)
|
---|
40 |
|
---|
41 |
|
---|
42 | # Create the type objects
|
---|
43 |
|
---|
44 | includestuff = includestuff + """
|
---|
45 | #include <CoreServices/CoreServices.h>
|
---|
46 |
|
---|
47 | #include "pycfbridge.h"
|
---|
48 |
|
---|
49 | #ifdef USE_TOOLBOX_OBJECT_GLUE
|
---|
50 | extern PyObject *_CFObj_New(CFTypeRef);
|
---|
51 | extern int _CFObj_Convert(PyObject *, CFTypeRef *);
|
---|
52 | #define CFObj_New _CFObj_New
|
---|
53 | #define CFObj_Convert _CFObj_Convert
|
---|
54 |
|
---|
55 | extern PyObject *_CFTypeRefObj_New(CFTypeRef);
|
---|
56 | extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *);
|
---|
57 | #define CFTypeRefObj_New _CFTypeRefObj_New
|
---|
58 | #define CFTypeRefObj_Convert _CFTypeRefObj_Convert
|
---|
59 |
|
---|
60 | extern PyObject *_CFStringRefObj_New(CFStringRef);
|
---|
61 | extern int _CFStringRefObj_Convert(PyObject *, CFStringRef *);
|
---|
62 | #define CFStringRefObj_New _CFStringRefObj_New
|
---|
63 | #define CFStringRefObj_Convert _CFStringRefObj_Convert
|
---|
64 |
|
---|
65 | extern PyObject *_CFMutableStringRefObj_New(CFMutableStringRef);
|
---|
66 | extern int _CFMutableStringRefObj_Convert(PyObject *, CFMutableStringRef *);
|
---|
67 | #define CFMutableStringRefObj_New _CFMutableStringRefObj_New
|
---|
68 | #define CFMutableStringRefObj_Convert _CFMutableStringRefObj_Convert
|
---|
69 |
|
---|
70 | extern PyObject *_CFArrayRefObj_New(CFArrayRef);
|
---|
71 | extern int _CFArrayRefObj_Convert(PyObject *, CFArrayRef *);
|
---|
72 | #define CFArrayRefObj_New _CFArrayRefObj_New
|
---|
73 | #define CFArrayRefObj_Convert _CFArrayRefObj_Convert
|
---|
74 |
|
---|
75 | extern PyObject *_CFMutableArrayRefObj_New(CFMutableArrayRef);
|
---|
76 | extern int _CFMutableArrayRefObj_Convert(PyObject *, CFMutableArrayRef *);
|
---|
77 | #define CFMutableArrayRefObj_New _CFMutableArrayRefObj_New
|
---|
78 | #define CFMutableArrayRefObj_Convert _CFMutableArrayRefObj_Convert
|
---|
79 |
|
---|
80 | extern PyObject *_CFDataRefObj_New(CFDataRef);
|
---|
81 | extern int _CFDataRefObj_Convert(PyObject *, CFDataRef *);
|
---|
82 | #define CFDataRefObj_New _CFDataRefObj_New
|
---|
83 | #define CFDataRefObj_Convert _CFDataRefObj_Convert
|
---|
84 |
|
---|
85 | extern PyObject *_CFMutableDataRefObj_New(CFMutableDataRef);
|
---|
86 | extern int _CFMutableDataRefObj_Convert(PyObject *, CFMutableDataRef *);
|
---|
87 | #define CFMutableDataRefObj_New _CFMutableDataRefObj_New
|
---|
88 | #define CFMutableDataRefObj_Convert _CFMutableDataRefObj_Convert
|
---|
89 |
|
---|
90 | extern PyObject *_CFDictionaryRefObj_New(CFDictionaryRef);
|
---|
91 | extern int _CFDictionaryRefObj_Convert(PyObject *, CFDictionaryRef *);
|
---|
92 | #define CFDictionaryRefObj_New _CFDictionaryRefObj_New
|
---|
93 | #define CFDictionaryRefObj_Convert _CFDictionaryRefObj_Convert
|
---|
94 |
|
---|
95 | extern PyObject *_CFMutableDictionaryRefObj_New(CFMutableDictionaryRef);
|
---|
96 | extern int _CFMutableDictionaryRefObj_Convert(PyObject *, CFMutableDictionaryRef *);
|
---|
97 | #define CFMutableDictionaryRefObj_New _CFMutableDictionaryRefObj_New
|
---|
98 | #define CFMutableDictionaryRefObj_Convert _CFMutableDictionaryRefObj_Convert
|
---|
99 |
|
---|
100 | extern PyObject *_CFURLRefObj_New(CFURLRef);
|
---|
101 | extern int _CFURLRefObj_Convert(PyObject *, CFURLRef *);
|
---|
102 | extern int _OptionalCFURLRefObj_Convert(PyObject *, CFURLRef *);
|
---|
103 | #define CFURLRefObj_New _CFURLRefObj_New
|
---|
104 | #define CFURLRefObj_Convert _CFURLRefObj_Convert
|
---|
105 | #define OptionalCFURLRefObj_Convert _OptionalCFURLRefObj_Convert
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | /*
|
---|
109 | ** Parse/generate CFRange records
|
---|
110 | */
|
---|
111 | PyObject *CFRange_New(CFRange *itself)
|
---|
112 | {
|
---|
113 |
|
---|
114 | return Py_BuildValue("ll", (long)itself->location, (long)itself->length);
|
---|
115 | }
|
---|
116 |
|
---|
117 | int
|
---|
118 | CFRange_Convert(PyObject *v, CFRange *p_itself)
|
---|
119 | {
|
---|
120 | long location, length;
|
---|
121 |
|
---|
122 | if( !PyArg_ParseTuple(v, "ll", &location, &length) )
|
---|
123 | return 0;
|
---|
124 | p_itself->location = (CFIndex)location;
|
---|
125 | p_itself->length = (CFIndex)length;
|
---|
126 | return 1;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* Optional CFURL argument or None (passed as NULL) */
|
---|
130 | int
|
---|
131 | OptionalCFURLRefObj_Convert(PyObject *v, CFURLRef *p_itself)
|
---|
132 | {
|
---|
133 | if ( v == Py_None ) {
|
---|
134 | p_itself = NULL;
|
---|
135 | return 1;
|
---|
136 | }
|
---|
137 | return CFURLRefObj_Convert(v, p_itself);
|
---|
138 | }
|
---|
139 | """
|
---|
140 |
|
---|
141 | finalstuff = finalstuff + """
|
---|
142 |
|
---|
143 | /* Routines to convert any CF type to/from the corresponding CFxxxObj */
|
---|
144 | PyObject *CFObj_New(CFTypeRef itself)
|
---|
145 | {
|
---|
146 | if (itself == NULL)
|
---|
147 | {
|
---|
148 | PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
|
---|
149 | return NULL;
|
---|
150 | }
|
---|
151 | if (CFGetTypeID(itself) == CFArrayGetTypeID()) return CFArrayRefObj_New((CFArrayRef)itself);
|
---|
152 | if (CFGetTypeID(itself) == CFDictionaryGetTypeID()) return CFDictionaryRefObj_New((CFDictionaryRef)itself);
|
---|
153 | if (CFGetTypeID(itself) == CFDataGetTypeID()) return CFDataRefObj_New((CFDataRef)itself);
|
---|
154 | if (CFGetTypeID(itself) == CFStringGetTypeID()) return CFStringRefObj_New((CFStringRef)itself);
|
---|
155 | if (CFGetTypeID(itself) == CFURLGetTypeID()) return CFURLRefObj_New((CFURLRef)itself);
|
---|
156 | /* XXXX Or should we use PyCF_CF2Python here?? */
|
---|
157 | return CFTypeRefObj_New(itself);
|
---|
158 | }
|
---|
159 | int CFObj_Convert(PyObject *v, CFTypeRef *p_itself)
|
---|
160 | {
|
---|
161 |
|
---|
162 | if (v == Py_None) { *p_itself = NULL; return 1; }
|
---|
163 | /* Check for other CF objects here */
|
---|
164 |
|
---|
165 | if (!CFTypeRefObj_Check(v) &&
|
---|
166 | !CFArrayRefObj_Check(v) &&
|
---|
167 | !CFMutableArrayRefObj_Check(v) &&
|
---|
168 | !CFDictionaryRefObj_Check(v) &&
|
---|
169 | !CFMutableDictionaryRefObj_Check(v) &&
|
---|
170 | !CFDataRefObj_Check(v) &&
|
---|
171 | !CFMutableDataRefObj_Check(v) &&
|
---|
172 | !CFStringRefObj_Check(v) &&
|
---|
173 | !CFMutableStringRefObj_Check(v) &&
|
---|
174 | !CFURLRefObj_Check(v) )
|
---|
175 | {
|
---|
176 | /* XXXX Or should we use PyCF_Python2CF here?? */
|
---|
177 | PyErr_SetString(PyExc_TypeError, "CF object required");
|
---|
178 | return 0;
|
---|
179 | }
|
---|
180 | *p_itself = ((CFTypeRefObject *)v)->ob_itself;
|
---|
181 | return 1;
|
---|
182 | }
|
---|
183 | """
|
---|
184 |
|
---|
185 | initstuff = initstuff + """
|
---|
186 | PyMac_INIT_TOOLBOX_OBJECT_NEW(CFTypeRef, CFObj_New);
|
---|
187 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFTypeRef, CFObj_Convert);
|
---|
188 | PyMac_INIT_TOOLBOX_OBJECT_NEW(CFTypeRef, CFTypeRefObj_New);
|
---|
189 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFTypeRef, CFTypeRefObj_Convert);
|
---|
190 | PyMac_INIT_TOOLBOX_OBJECT_NEW(CFStringRef, CFStringRefObj_New);
|
---|
191 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFStringRef, CFStringRefObj_Convert);
|
---|
192 | PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableStringRef, CFMutableStringRefObj_New);
|
---|
193 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableStringRef, CFMutableStringRefObj_Convert);
|
---|
194 | PyMac_INIT_TOOLBOX_OBJECT_NEW(CFArrayRef, CFArrayRefObj_New);
|
---|
195 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFArrayRef, CFArrayRefObj_Convert);
|
---|
196 | PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableArrayRef, CFMutableArrayRefObj_New);
|
---|
197 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableArrayRef, CFMutableArrayRefObj_Convert);
|
---|
198 | PyMac_INIT_TOOLBOX_OBJECT_NEW(CFDictionaryRef, CFDictionaryRefObj_New);
|
---|
199 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFDictionaryRef, CFDictionaryRefObj_Convert);
|
---|
200 | PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableDictionaryRef, CFMutableDictionaryRefObj_New);
|
---|
201 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableDictionaryRef, CFMutableDictionaryRefObj_Convert);
|
---|
202 | PyMac_INIT_TOOLBOX_OBJECT_NEW(CFURLRef, CFURLRefObj_New);
|
---|
203 | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFURLRef, CFURLRefObj_Convert);
|
---|
204 | """
|
---|
205 |
|
---|
206 | variablestuff="""
|
---|
207 | #define _STRINGCONST(name) PyModule_AddObject(m, #name, CFStringRefObj_New(name))
|
---|
208 | _STRINGCONST(kCFPreferencesAnyApplication);
|
---|
209 | _STRINGCONST(kCFPreferencesCurrentApplication);
|
---|
210 | _STRINGCONST(kCFPreferencesAnyHost);
|
---|
211 | _STRINGCONST(kCFPreferencesCurrentHost);
|
---|
212 | _STRINGCONST(kCFPreferencesAnyUser);
|
---|
213 | _STRINGCONST(kCFPreferencesCurrentUser);
|
---|
214 |
|
---|
215 | """
|
---|
216 |
|
---|
217 | Boolean = Type("Boolean", "l")
|
---|
218 | CFTypeID = Type("CFTypeID", "l") # XXXX a guess, seems better than OSTypeType.
|
---|
219 | CFHashCode = Type("CFHashCode", "l")
|
---|
220 | CFIndex = Type("CFIndex", "l")
|
---|
221 | CFRange = OpaqueByValueType('CFRange', 'CFRange')
|
---|
222 | CFOptionFlags = Type("CFOptionFlags", "l")
|
---|
223 | CFStringEncoding = Type("CFStringEncoding", "l")
|
---|
224 | CFComparisonResult = Type("CFComparisonResult", "l") # a bit dangerous, it's an enum
|
---|
225 | CFURLPathStyle = Type("CFURLPathStyle", "l") # a bit dangerous, it's an enum
|
---|
226 |
|
---|
227 | char_ptr = stringptr
|
---|
228 | return_stringptr = Type("char *", "s") # ONLY FOR RETURN VALUES!!
|
---|
229 |
|
---|
230 | CFAllocatorRef = FakeType("(CFAllocatorRef)NULL")
|
---|
231 | CFArrayCallBacks_ptr = FakeType("&kCFTypeArrayCallBacks")
|
---|
232 | CFDictionaryKeyCallBacks_ptr = FakeType("&kCFTypeDictionaryKeyCallBacks")
|
---|
233 | CFDictionaryValueCallBacks_ptr = FakeType("&kCFTypeDictionaryValueCallBacks")
|
---|
234 | # The real objects
|
---|
235 | CFTypeRef = OpaqueByValueType("CFTypeRef", "CFTypeRefObj")
|
---|
236 | CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
|
---|
237 | CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
|
---|
238 | CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
|
---|
239 | CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
|
---|
240 | CFDataRef = OpaqueByValueType("CFDataRef", "CFDataRefObj")
|
---|
241 | CFMutableDataRef = OpaqueByValueType("CFMutableDataRef", "CFMutableDataRefObj")
|
---|
242 | CFDictionaryRef = OpaqueByValueType("CFDictionaryRef", "CFDictionaryRefObj")
|
---|
243 | CFMutableDictionaryRef = OpaqueByValueType("CFMutableDictionaryRef", "CFMutableDictionaryRefObj")
|
---|
244 | CFStringRef = OpaqueByValueType("CFStringRef", "CFStringRefObj")
|
---|
245 | CFMutableStringRef = OpaqueByValueType("CFMutableStringRef", "CFMutableStringRefObj")
|
---|
246 | CFURLRef = OpaqueByValueType("CFURLRef", "CFURLRefObj")
|
---|
247 | OptionalCFURLRef = OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")
|
---|
248 | ##CFPropertyListRef = OpaqueByValueType("CFPropertyListRef", "CFTypeRefObj")
|
---|
249 | # ADD object type here
|
---|
250 |
|
---|
251 | # Our (opaque) objects
|
---|
252 |
|
---|
253 | class MyGlobalObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
|
---|
254 | def outputCheckNewArg(self):
|
---|
255 | Output('if (itself == NULL)')
|
---|
256 | OutLbrace()
|
---|
257 | Output('PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");')
|
---|
258 | Output('return NULL;')
|
---|
259 | OutRbrace()
|
---|
260 | def outputStructMembers(self):
|
---|
261 | GlobalObjectDefinition.outputStructMembers(self)
|
---|
262 | Output("void (*ob_freeit)(CFTypeRef ptr);")
|
---|
263 | def outputInitStructMembers(self):
|
---|
264 | GlobalObjectDefinition.outputInitStructMembers(self)
|
---|
265 | ## Output("it->ob_freeit = NULL;")
|
---|
266 | Output("it->ob_freeit = CFRelease;")
|
---|
267 | def outputCheckConvertArg(self):
|
---|
268 | Out("""
|
---|
269 | if (v == Py_None) { *p_itself = NULL; return 1; }
|
---|
270 | /* Check for other CF objects here */
|
---|
271 | """)
|
---|
272 | def outputCleanupStructMembers(self):
|
---|
273 | Output("if (self->ob_freeit && self->ob_itself)")
|
---|
274 | OutLbrace()
|
---|
275 | Output("self->ob_freeit((CFTypeRef)self->ob_itself);")
|
---|
276 | Output("self->ob_itself = NULL;")
|
---|
277 | OutRbrace()
|
---|
278 |
|
---|
279 | def outputCompare(self):
|
---|
280 | Output()
|
---|
281 | Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, self.objecttype)
|
---|
282 | OutLbrace()
|
---|
283 | Output("/* XXXX Or should we use CFEqual?? */")
|
---|
284 | Output("if ( self->ob_itself > other->ob_itself ) return 1;")
|
---|
285 | Output("if ( self->ob_itself < other->ob_itself ) return -1;")
|
---|
286 | Output("return 0;")
|
---|
287 | OutRbrace()
|
---|
288 |
|
---|
289 | def outputHash(self):
|
---|
290 | Output()
|
---|
291 | Output("static int %s_hash(%s *self)", self.prefix, self.objecttype)
|
---|
292 | OutLbrace()
|
---|
293 | Output("/* XXXX Or should we use CFHash?? */")
|
---|
294 | Output("return (int)self->ob_itself;")
|
---|
295 | OutRbrace()
|
---|
296 |
|
---|
297 | def outputRepr(self):
|
---|
298 | Output()
|
---|
299 | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
|
---|
300 | OutLbrace()
|
---|
301 | Output("char buf[100];")
|
---|
302 | Output("""sprintf(buf, "<CFTypeRef type-%%d object at 0x%%8.8x for 0x%%8.8x>", (int)CFGetTypeID(self->ob_itself), (unsigned)self, (unsigned)self->ob_itself);""")
|
---|
303 | Output("return PyString_FromString(buf);")
|
---|
304 | OutRbrace()
|
---|
305 |
|
---|
306 | def output_tp_newBody(self):
|
---|
307 | Output("PyObject *self;")
|
---|
308 | Output
|
---|
309 | Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
|
---|
310 | Output("((%s *)self)->ob_itself = NULL;", self.objecttype)
|
---|
311 | Output("((%s *)self)->ob_freeit = CFRelease;", self.objecttype)
|
---|
312 | Output("return self;")
|
---|
313 |
|
---|
314 | def output_tp_initBody(self):
|
---|
315 | Output("%s itself;", self.itselftype)
|
---|
316 | Output("char *kw[] = {\"itself\", 0};")
|
---|
317 | Output()
|
---|
318 | Output("if (PyArg_ParseTupleAndKeywords(_args, _kwds, \"O&\", kw, %s_Convert, &itself))",
|
---|
319 | self.prefix)
|
---|
320 | OutLbrace()
|
---|
321 | Output("((%s *)_self)->ob_itself = itself;", self.objecttype)
|
---|
322 | Output("return 0;")
|
---|
323 | OutRbrace()
|
---|
324 | if self.prefix != 'CFTypeRefObj':
|
---|
325 | Output()
|
---|
326 | Output("/* Any CFTypeRef descendent is allowed as initializer too */")
|
---|
327 | Output("if (PyArg_ParseTupleAndKeywords(_args, _kwds, \"O&\", kw, CFTypeRefObj_Convert, &itself))")
|
---|
328 | OutLbrace()
|
---|
329 | Output("((%s *)_self)->ob_itself = itself;", self.objecttype)
|
---|
330 | Output("return 0;")
|
---|
331 | OutRbrace()
|
---|
332 | Output("return -1;")
|
---|
333 |
|
---|
334 | class CFTypeRefObjectDefinition(MyGlobalObjectDefinition):
|
---|
335 | pass
|
---|
336 |
|
---|
337 | class CFArrayRefObjectDefinition(MyGlobalObjectDefinition):
|
---|
338 | basetype = "CFTypeRef_Type"
|
---|
339 |
|
---|
340 | def outputRepr(self):
|
---|
341 | Output()
|
---|
342 | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
|
---|
343 | OutLbrace()
|
---|
344 | Output("char buf[100];")
|
---|
345 | Output("""sprintf(buf, "<CFArrayRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
|
---|
346 | Output("return PyString_FromString(buf);")
|
---|
347 | OutRbrace()
|
---|
348 |
|
---|
349 | class CFMutableArrayRefObjectDefinition(MyGlobalObjectDefinition):
|
---|
350 | basetype = "CFArrayRef_Type"
|
---|
351 |
|
---|
352 | def outputRepr(self):
|
---|
353 | Output()
|
---|
354 | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
|
---|
355 | OutLbrace()
|
---|
356 | Output("char buf[100];")
|
---|
357 | Output("""sprintf(buf, "<CFMutableArrayRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
|
---|
358 | Output("return PyString_FromString(buf);")
|
---|
359 | OutRbrace()
|
---|
360 |
|
---|
361 | class CFDictionaryRefObjectDefinition(MyGlobalObjectDefinition):
|
---|
362 | basetype = "CFTypeRef_Type"
|
---|
363 |
|
---|
364 | def outputRepr(self):
|
---|
365 | Output()
|
---|
366 | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
|
---|
367 | OutLbrace()
|
---|
368 | Output("char buf[100];")
|
---|
369 | Output("""sprintf(buf, "<CFDictionaryRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
|
---|
370 | Output("return PyString_FromString(buf);")
|
---|
371 | OutRbrace()
|
---|
372 |
|
---|
373 | class CFMutableDictionaryRefObjectDefinition(MyGlobalObjectDefinition):
|
---|
374 | basetype = "CFDictionaryRef_Type"
|
---|
375 |
|
---|
376 | def outputRepr(self):
|
---|
377 | Output()
|
---|
378 | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
|
---|
379 | OutLbrace()
|
---|
380 | Output("char buf[100];")
|
---|
381 | Output("""sprintf(buf, "<CFMutableDictionaryRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
|
---|
382 | Output("return PyString_FromString(buf);")
|
---|
383 | OutRbrace()
|
---|
384 |
|
---|
385 | class CFDataRefObjectDefinition(MyGlobalObjectDefinition):
|
---|
386 | basetype = "CFTypeRef_Type"
|
---|
387 |
|
---|
388 | def outputCheckConvertArg(self):
|
---|
389 | Out("""
|
---|
390 | if (v == Py_None) { *p_itself = NULL; return 1; }
|
---|
391 | if (PyString_Check(v)) {
|
---|
392 | char *cStr;
|
---|
393 | int cLen;
|
---|
394 | if( PyString_AsStringAndSize(v, &cStr, &cLen) < 0 ) return 0;
|
---|
395 | *p_itself = CFDataCreate((CFAllocatorRef)NULL, (unsigned char *)cStr, cLen);
|
---|
396 | return 1;
|
---|
397 | }
|
---|
398 | """)
|
---|
399 |
|
---|
400 | def outputRepr(self):
|
---|
401 | Output()
|
---|
402 | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
|
---|
403 | OutLbrace()
|
---|
404 | Output("char buf[100];")
|
---|
405 | Output("""sprintf(buf, "<CFDataRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
|
---|
406 | Output("return PyString_FromString(buf);")
|
---|
407 | OutRbrace()
|
---|
408 |
|
---|
409 | class CFMutableDataRefObjectDefinition(MyGlobalObjectDefinition):
|
---|
410 | basetype = "CFDataRef_Type"
|
---|
411 |
|
---|
412 | def outputRepr(self):
|
---|
413 | Output()
|
---|
414 | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
|
---|
415 | OutLbrace()
|
---|
416 | Output("char buf[100];")
|
---|
417 | Output("""sprintf(buf, "<CFMutableDataRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
|
---|
418 | Output("return PyString_FromString(buf);")
|
---|
419 | OutRbrace()
|
---|
420 |
|
---|
421 | class CFStringRefObjectDefinition(MyGlobalObjectDefinition):
|
---|
422 | basetype = "CFTypeRef_Type"
|
---|
423 |
|
---|
424 | def outputCheckConvertArg(self):
|
---|
425 | Out("""
|
---|
426 | if (v == Py_None) { *p_itself = NULL; return 1; }
|
---|
427 | if (PyString_Check(v)) {
|
---|
428 | char *cStr;
|
---|
429 | if (!PyArg_Parse(v, "es", "ascii", &cStr))
|
---|
430 | return NULL;
|
---|
431 | *p_itself = CFStringCreateWithCString((CFAllocatorRef)NULL, cStr, kCFStringEncodingASCII);
|
---|
432 | PyMem_Free(cStr);
|
---|
433 | return 1;
|
---|
434 | }
|
---|
435 | if (PyUnicode_Check(v)) {
|
---|
436 | /* We use the CF types here, if Python was configured differently that will give an error */
|
---|
437 | CFIndex size = PyUnicode_GetSize(v);
|
---|
438 | UniChar *unichars = PyUnicode_AsUnicode(v);
|
---|
439 | if (!unichars) return 0;
|
---|
440 | *p_itself = CFStringCreateWithCharacters((CFAllocatorRef)NULL, unichars, size);
|
---|
441 | return 1;
|
---|
442 | }
|
---|
443 |
|
---|
444 | """)
|
---|
445 |
|
---|
446 | def outputRepr(self):
|
---|
447 | Output()
|
---|
448 | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
|
---|
449 | OutLbrace()
|
---|
450 | Output("char buf[100];")
|
---|
451 | Output("""sprintf(buf, "<CFStringRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
|
---|
452 | Output("return PyString_FromString(buf);")
|
---|
453 | OutRbrace()
|
---|
454 |
|
---|
455 | class CFMutableStringRefObjectDefinition(CFStringRefObjectDefinition):
|
---|
456 | basetype = "CFStringRef_Type"
|
---|
457 |
|
---|
458 | def outputCheckConvertArg(self):
|
---|
459 | # Mutable, don't allow Python strings
|
---|
460 | return MyGlobalObjectDefinition.outputCheckConvertArg(self)
|
---|
461 |
|
---|
462 | def outputRepr(self):
|
---|
463 | Output()
|
---|
464 | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
|
---|
465 | OutLbrace()
|
---|
466 | Output("char buf[100];")
|
---|
467 | Output("""sprintf(buf, "<CFMutableStringRef object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
|
---|
468 | Output("return PyString_FromString(buf);")
|
---|
469 | OutRbrace()
|
---|
470 |
|
---|
471 | class CFURLRefObjectDefinition(MyGlobalObjectDefinition):
|
---|
472 | basetype = "CFTypeRef_Type"
|
---|
473 |
|
---|
474 | def outputRepr(self):
|
---|
475 | Output()
|
---|
476 | Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
|
---|
477 | OutLbrace()
|
---|
478 | Output("char buf[100];")
|
---|
479 | Output("""sprintf(buf, "<CFURL object at 0x%%8.8x for 0x%%8.8x>", (unsigned)self, (unsigned)self->ob_itself);""")
|
---|
480 | Output("return PyString_FromString(buf);")
|
---|
481 | OutRbrace()
|
---|
482 |
|
---|
483 |
|
---|
484 | # ADD object class here
|
---|
485 |
|
---|
486 | # From here on it's basically all boiler plate...
|
---|
487 |
|
---|
488 | # Create the generator groups and link them
|
---|
489 | module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff)
|
---|
490 | CFTypeRef_object = CFTypeRefObjectDefinition('CFTypeRef', 'CFTypeRefObj', 'CFTypeRef')
|
---|
491 | CFArrayRef_object = CFArrayRefObjectDefinition('CFArrayRef', 'CFArrayRefObj', 'CFArrayRef')
|
---|
492 | CFMutableArrayRef_object = CFMutableArrayRefObjectDefinition('CFMutableArrayRef', 'CFMutableArrayRefObj', 'CFMutableArrayRef')
|
---|
493 | CFDictionaryRef_object = CFDictionaryRefObjectDefinition('CFDictionaryRef', 'CFDictionaryRefObj', 'CFDictionaryRef')
|
---|
494 | CFMutableDictionaryRef_object = CFMutableDictionaryRefObjectDefinition('CFMutableDictionaryRef', 'CFMutableDictionaryRefObj', 'CFMutableDictionaryRef')
|
---|
495 | CFDataRef_object = CFDataRefObjectDefinition('CFDataRef', 'CFDataRefObj', 'CFDataRef')
|
---|
496 | CFMutableDataRef_object = CFMutableDataRefObjectDefinition('CFMutableDataRef', 'CFMutableDataRefObj', 'CFMutableDataRef')
|
---|
497 | CFStringRef_object = CFStringRefObjectDefinition('CFStringRef', 'CFStringRefObj', 'CFStringRef')
|
---|
498 | CFMutableStringRef_object = CFMutableStringRefObjectDefinition('CFMutableStringRef', 'CFMutableStringRefObj', 'CFMutableStringRef')
|
---|
499 | CFURLRef_object = CFURLRefObjectDefinition('CFURLRef', 'CFURLRefObj', 'CFURLRef')
|
---|
500 |
|
---|
501 | # ADD object here
|
---|
502 |
|
---|
503 | module.addobject(CFTypeRef_object)
|
---|
504 | module.addobject(CFArrayRef_object)
|
---|
505 | module.addobject(CFMutableArrayRef_object)
|
---|
506 | module.addobject(CFDictionaryRef_object)
|
---|
507 | module.addobject(CFMutableDictionaryRef_object)
|
---|
508 | module.addobject(CFDataRef_object)
|
---|
509 | module.addobject(CFMutableDataRef_object)
|
---|
510 | module.addobject(CFStringRef_object)
|
---|
511 | module.addobject(CFMutableStringRef_object)
|
---|
512 | module.addobject(CFURLRef_object)
|
---|
513 | # ADD addobject call here
|
---|
514 |
|
---|
515 | # Create the generator classes used to populate the lists
|
---|
516 | Function = OSErrWeakLinkFunctionGenerator
|
---|
517 | Method = OSErrWeakLinkMethodGenerator
|
---|
518 |
|
---|
519 | # Create and populate the lists
|
---|
520 | functions = []
|
---|
521 | CFTypeRef_methods = []
|
---|
522 | CFArrayRef_methods = []
|
---|
523 | CFMutableArrayRef_methods = []
|
---|
524 | CFDictionaryRef_methods = []
|
---|
525 | CFMutableDictionaryRef_methods = []
|
---|
526 | CFDataRef_methods = []
|
---|
527 | CFMutableDataRef_methods = []
|
---|
528 | CFStringRef_methods = []
|
---|
529 | CFMutableStringRef_methods = []
|
---|
530 | CFURLRef_methods = []
|
---|
531 |
|
---|
532 | # ADD _methods initializer here
|
---|
533 | execfile(INPUTFILE)
|
---|
534 |
|
---|
535 |
|
---|
536 | # add the populated lists to the generator groups
|
---|
537 | # (in a different wordl the scan program would generate this)
|
---|
538 | for f in functions: module.add(f)
|
---|
539 | for f in CFTypeRef_methods: CFTypeRef_object.add(f)
|
---|
540 | for f in CFArrayRef_methods: CFArrayRef_object.add(f)
|
---|
541 | for f in CFMutableArrayRef_methods: CFMutableArrayRef_object.add(f)
|
---|
542 | for f in CFDictionaryRef_methods: CFDictionaryRef_object.add(f)
|
---|
543 | for f in CFMutableDictionaryRef_methods: CFMutableDictionaryRef_object.add(f)
|
---|
544 | for f in CFDataRef_methods: CFDataRef_object.add(f)
|
---|
545 | for f in CFMutableDataRef_methods: CFMutableDataRef_object.add(f)
|
---|
546 | for f in CFStringRef_methods: CFStringRef_object.add(f)
|
---|
547 | for f in CFMutableStringRef_methods: CFMutableStringRef_object.add(f)
|
---|
548 | for f in CFURLRef_methods: CFURLRef_object.add(f)
|
---|
549 |
|
---|
550 | # Manual generators for getting data out of strings
|
---|
551 |
|
---|
552 | getasstring_body = """
|
---|
553 | int size = CFStringGetLength(_self->ob_itself)+1;
|
---|
554 | char *data = malloc(size);
|
---|
555 |
|
---|
556 | if( data == NULL ) return PyErr_NoMemory();
|
---|
557 | if ( CFStringGetCString(_self->ob_itself, data, size, 0) ) {
|
---|
558 | _res = (PyObject *)PyString_FromString(data);
|
---|
559 | } else {
|
---|
560 | PyErr_SetString(PyExc_RuntimeError, "CFStringGetCString could not fit the string");
|
---|
561 | _res = NULL;
|
---|
562 | }
|
---|
563 | free(data);
|
---|
564 | return _res;
|
---|
565 | """
|
---|
566 |
|
---|
567 | f = ManualGenerator("CFStringGetString", getasstring_body);
|
---|
568 | f.docstring = lambda: "() -> (string _rv)"
|
---|
569 | CFStringRef_object.add(f)
|
---|
570 |
|
---|
571 | getasunicode_body = """
|
---|
572 | int size = CFStringGetLength(_self->ob_itself)+1;
|
---|
573 | Py_UNICODE *data = malloc(size*sizeof(Py_UNICODE));
|
---|
574 | CFRange range;
|
---|
575 |
|
---|
576 | range.location = 0;
|
---|
577 | range.length = size;
|
---|
578 | if( data == NULL ) return PyErr_NoMemory();
|
---|
579 | CFStringGetCharacters(_self->ob_itself, range, data);
|
---|
580 | _res = (PyObject *)PyUnicode_FromUnicode(data, size-1);
|
---|
581 | free(data);
|
---|
582 | return _res;
|
---|
583 | """
|
---|
584 |
|
---|
585 | f = ManualGenerator("CFStringGetUnicode", getasunicode_body);
|
---|
586 | f.docstring = lambda: "() -> (unicode _rv)"
|
---|
587 | CFStringRef_object.add(f)
|
---|
588 |
|
---|
589 | # Get data from CFDataRef
|
---|
590 | getasdata_body = """
|
---|
591 | int size = CFDataGetLength(_self->ob_itself);
|
---|
592 | char *data = (char *)CFDataGetBytePtr(_self->ob_itself);
|
---|
593 |
|
---|
594 | _res = (PyObject *)PyString_FromStringAndSize(data, size);
|
---|
595 | return _res;
|
---|
596 | """
|
---|
597 |
|
---|
598 | f = ManualGenerator("CFDataGetData", getasdata_body);
|
---|
599 | f.docstring = lambda: "() -> (string _rv)"
|
---|
600 | CFDataRef_object.add(f)
|
---|
601 |
|
---|
602 | # Manual generator for CFPropertyListCreateFromXMLData because of funny error return
|
---|
603 | fromxml_body = """
|
---|
604 | CFTypeRef _rv;
|
---|
605 | CFOptionFlags mutabilityOption;
|
---|
606 | CFStringRef errorString;
|
---|
607 | if (!PyArg_ParseTuple(_args, "l",
|
---|
608 | &mutabilityOption))
|
---|
609 | return NULL;
|
---|
610 | _rv = CFPropertyListCreateFromXMLData((CFAllocatorRef)NULL,
|
---|
611 | _self->ob_itself,
|
---|
612 | mutabilityOption,
|
---|
613 | &errorString);
|
---|
614 | if (errorString)
|
---|
615 | CFRelease(errorString);
|
---|
616 | if (_rv == NULL) {
|
---|
617 | PyErr_SetString(PyExc_RuntimeError, "Parse error in XML data");
|
---|
618 | return NULL;
|
---|
619 | }
|
---|
620 | _res = Py_BuildValue("O&",
|
---|
621 | CFTypeRefObj_New, _rv);
|
---|
622 | return _res;
|
---|
623 | """
|
---|
624 | f = ManualGenerator("CFPropertyListCreateFromXMLData", fromxml_body)
|
---|
625 | f.docstring = lambda: "(CFOptionFlags mutabilityOption) -> (CFTypeRefObj)"
|
---|
626 | CFTypeRef_object.add(f)
|
---|
627 |
|
---|
628 | # Convert CF objects to Python objects
|
---|
629 | toPython_body = """
|
---|
630 | _res = PyCF_CF2Python(_self->ob_itself);
|
---|
631 | return _res;
|
---|
632 | """
|
---|
633 |
|
---|
634 | f = ManualGenerator("toPython", toPython_body);
|
---|
635 | f.docstring = lambda: "() -> (python_object)"
|
---|
636 | CFTypeRef_object.add(f)
|
---|
637 |
|
---|
638 | toCF_body = """
|
---|
639 | CFTypeRef rv;
|
---|
640 | CFTypeID typeid;
|
---|
641 |
|
---|
642 | if (!PyArg_ParseTuple(_args, "O&", PyCF_Python2CF, &rv))
|
---|
643 | return NULL;
|
---|
644 | typeid = CFGetTypeID(rv);
|
---|
645 |
|
---|
646 | if (typeid == CFStringGetTypeID())
|
---|
647 | return Py_BuildValue("O&", CFStringRefObj_New, rv);
|
---|
648 | if (typeid == CFArrayGetTypeID())
|
---|
649 | return Py_BuildValue("O&", CFArrayRefObj_New, rv);
|
---|
650 | if (typeid == CFDictionaryGetTypeID())
|
---|
651 | return Py_BuildValue("O&", CFDictionaryRefObj_New, rv);
|
---|
652 | if (typeid == CFURLGetTypeID())
|
---|
653 | return Py_BuildValue("O&", CFURLRefObj_New, rv);
|
---|
654 |
|
---|
655 | _res = Py_BuildValue("O&", CFTypeRefObj_New, rv);
|
---|
656 | return _res;
|
---|
657 | """
|
---|
658 | f = ManualGenerator("toCF", toCF_body);
|
---|
659 | f.docstring = lambda: "(python_object) -> (CF_object)"
|
---|
660 | module.add(f)
|
---|
661 |
|
---|
662 | # ADD add forloop here
|
---|
663 |
|
---|
664 | # generate output (open the output file as late as possible)
|
---|
665 | SetOutputFileName(OUTPUTFILE)
|
---|
666 | module.generate()
|
---|