PageRenderTime 530ms CodeModel.GetById 181ms app.highlight 157ms RepoModel.GetById 185ms app.codeStats 1ms

/Mac/Modules/cf/cfsupport.py

http://unladen-swallow.googlecode.com/
Python | 666 lines | 645 code | 10 blank | 11 comment | 16 complexity | 194ac2e387429cb01165680c38400ed3 MD5 | raw file
  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
  8import string
  9
 10# Declarations that change for each manager
 11MODNAME = '_CF'                         # The name of the module
 12
 13# The following is *usually* unchanged but may still require tuning
 14MODPREFIX = 'CF'                        # The prefix for module-wide routines
 15INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
 16OUTPUTFILE = MODNAME + "module.c"       # The file generated by this program
 17
 18from 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.
 22class 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
 44includestuff = includestuff + """
 45#include <CoreServices/CoreServices.h>
 46
 47#include "pycfbridge.h"
 48
 49#ifdef USE_TOOLBOX_OBJECT_GLUE
 50extern PyObject *_CFObj_New(CFTypeRef);
 51extern int _CFObj_Convert(PyObject *, CFTypeRef *);
 52#define CFObj_New _CFObj_New
 53#define CFObj_Convert _CFObj_Convert
 54
 55extern PyObject *_CFTypeRefObj_New(CFTypeRef);
 56extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *);
 57#define CFTypeRefObj_New _CFTypeRefObj_New
 58#define CFTypeRefObj_Convert _CFTypeRefObj_Convert
 59
 60extern PyObject *_CFStringRefObj_New(CFStringRef);
 61extern int _CFStringRefObj_Convert(PyObject *, CFStringRef *);
 62#define CFStringRefObj_New _CFStringRefObj_New
 63#define CFStringRefObj_Convert _CFStringRefObj_Convert
 64
 65extern PyObject *_CFMutableStringRefObj_New(CFMutableStringRef);
 66extern int _CFMutableStringRefObj_Convert(PyObject *, CFMutableStringRef *);
 67#define CFMutableStringRefObj_New _CFMutableStringRefObj_New
 68#define CFMutableStringRefObj_Convert _CFMutableStringRefObj_Convert
 69
 70extern PyObject *_CFArrayRefObj_New(CFArrayRef);
 71extern int _CFArrayRefObj_Convert(PyObject *, CFArrayRef *);
 72#define CFArrayRefObj_New _CFArrayRefObj_New
 73#define CFArrayRefObj_Convert _CFArrayRefObj_Convert
 74
 75extern PyObject *_CFMutableArrayRefObj_New(CFMutableArrayRef);
 76extern int _CFMutableArrayRefObj_Convert(PyObject *, CFMutableArrayRef *);
 77#define CFMutableArrayRefObj_New _CFMutableArrayRefObj_New
 78#define CFMutableArrayRefObj_Convert _CFMutableArrayRefObj_Convert
 79
 80extern PyObject *_CFDataRefObj_New(CFDataRef);
 81extern int _CFDataRefObj_Convert(PyObject *, CFDataRef *);
 82#define CFDataRefObj_New _CFDataRefObj_New
 83#define CFDataRefObj_Convert _CFDataRefObj_Convert
 84
 85extern PyObject *_CFMutableDataRefObj_New(CFMutableDataRef);
 86extern int _CFMutableDataRefObj_Convert(PyObject *, CFMutableDataRef *);
 87#define CFMutableDataRefObj_New _CFMutableDataRefObj_New
 88#define CFMutableDataRefObj_Convert _CFMutableDataRefObj_Convert
 89
 90extern PyObject *_CFDictionaryRefObj_New(CFDictionaryRef);
 91extern int _CFDictionaryRefObj_Convert(PyObject *, CFDictionaryRef *);
 92#define CFDictionaryRefObj_New _CFDictionaryRefObj_New
 93#define CFDictionaryRefObj_Convert _CFDictionaryRefObj_Convert
 94
 95extern PyObject *_CFMutableDictionaryRefObj_New(CFMutableDictionaryRef);
 96extern int _CFMutableDictionaryRefObj_Convert(PyObject *, CFMutableDictionaryRef *);
 97#define CFMutableDictionaryRefObj_New _CFMutableDictionaryRefObj_New
 98#define CFMutableDictionaryRefObj_Convert _CFMutableDictionaryRefObj_Convert
 99
100extern PyObject *_CFURLRefObj_New(CFURLRef);
101extern int _CFURLRefObj_Convert(PyObject *, CFURLRef *);
102extern 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*/
111PyObject *CFRange_New(CFRange *itself)
112{
113
114        return Py_BuildValue("ll", (long)itself->location, (long)itself->length);
115}
116
117int
118CFRange_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) */
130int
131OptionalCFURLRefObj_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
141finalstuff = finalstuff + """
142
143/* Routines to convert any CF type to/from the corresponding CFxxxObj */
144PyObject *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}
159int 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
185initstuff = initstuff + """
186PyMac_INIT_TOOLBOX_OBJECT_NEW(CFTypeRef, CFObj_New);
187PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFTypeRef, CFObj_Convert);
188PyMac_INIT_TOOLBOX_OBJECT_NEW(CFTypeRef, CFTypeRefObj_New);
189PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFTypeRef, CFTypeRefObj_Convert);
190PyMac_INIT_TOOLBOX_OBJECT_NEW(CFStringRef, CFStringRefObj_New);
191PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFStringRef, CFStringRefObj_Convert);
192PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableStringRef, CFMutableStringRefObj_New);
193PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableStringRef, CFMutableStringRefObj_Convert);
194PyMac_INIT_TOOLBOX_OBJECT_NEW(CFArrayRef, CFArrayRefObj_New);
195PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFArrayRef, CFArrayRefObj_Convert);
196PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableArrayRef, CFMutableArrayRefObj_New);
197PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableArrayRef, CFMutableArrayRefObj_Convert);
198PyMac_INIT_TOOLBOX_OBJECT_NEW(CFDictionaryRef, CFDictionaryRefObj_New);
199PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFDictionaryRef, CFDictionaryRefObj_Convert);
200PyMac_INIT_TOOLBOX_OBJECT_NEW(CFMutableDictionaryRef, CFMutableDictionaryRefObj_New);
201PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFMutableDictionaryRef, CFMutableDictionaryRefObj_Convert);
202PyMac_INIT_TOOLBOX_OBJECT_NEW(CFURLRef, CFURLRefObj_New);
203PyMac_INIT_TOOLBOX_OBJECT_CONVERT(CFURLRef, CFURLRefObj_Convert);
204"""
205
206variablestuff="""
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
217Boolean = Type("Boolean", "l")
218CFTypeID = Type("CFTypeID", "l") # XXXX a guess, seems better than OSTypeType.
219CFHashCode = Type("CFHashCode", "l")
220CFIndex = Type("CFIndex", "l")
221CFRange = OpaqueByValueType('CFRange', 'CFRange')
222CFOptionFlags = Type("CFOptionFlags", "l")
223CFStringEncoding = Type("CFStringEncoding", "l")
224CFComparisonResult = Type("CFComparisonResult", "l")  # a bit dangerous, it's an enum
225CFURLPathStyle = Type("CFURLPathStyle", "l") #  a bit dangerous, it's an enum
226
227char_ptr = stringptr
228return_stringptr = Type("char *", "s")  # ONLY FOR RETURN VALUES!!
229
230CFAllocatorRef = FakeType("(CFAllocatorRef)NULL")
231CFArrayCallBacks_ptr = FakeType("&kCFTypeArrayCallBacks")
232CFDictionaryKeyCallBacks_ptr = FakeType("&kCFTypeDictionaryKeyCallBacks")
233CFDictionaryValueCallBacks_ptr = FakeType("&kCFTypeDictionaryValueCallBacks")
234# The real objects
235CFTypeRef = OpaqueByValueType("CFTypeRef", "CFTypeRefObj")
236CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
237CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
238CFArrayRef = OpaqueByValueType("CFArrayRef", "CFArrayRefObj")
239CFMutableArrayRef = OpaqueByValueType("CFMutableArrayRef", "CFMutableArrayRefObj")
240CFDataRef = OpaqueByValueType("CFDataRef", "CFDataRefObj")
241CFMutableDataRef = OpaqueByValueType("CFMutableDataRef", "CFMutableDataRefObj")
242CFDictionaryRef = OpaqueByValueType("CFDictionaryRef", "CFDictionaryRefObj")
243CFMutableDictionaryRef = OpaqueByValueType("CFMutableDictionaryRef", "CFMutableDictionaryRefObj")
244CFStringRef = OpaqueByValueType("CFStringRef", "CFStringRefObj")
245CFMutableStringRef = OpaqueByValueType("CFMutableStringRef", "CFMutableStringRefObj")
246CFURLRef = OpaqueByValueType("CFURLRef", "CFURLRefObj")
247OptionalCFURLRef  = OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")
248##CFPropertyListRef = OpaqueByValueType("CFPropertyListRef", "CFTypeRefObj")
249# ADD object type here
250
251# Our (opaque) objects
252
253class 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
334class CFTypeRefObjectDefinition(MyGlobalObjectDefinition):
335    pass
336
337class 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
349class 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
361class 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
373class 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
385class 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
409class 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
421class 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
455class 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
471class 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
489module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff)
490CFTypeRef_object = CFTypeRefObjectDefinition('CFTypeRef', 'CFTypeRefObj', 'CFTypeRef')
491CFArrayRef_object = CFArrayRefObjectDefinition('CFArrayRef', 'CFArrayRefObj', 'CFArrayRef')
492CFMutableArrayRef_object = CFMutableArrayRefObjectDefinition('CFMutableArrayRef', 'CFMutableArrayRefObj', 'CFMutableArrayRef')
493CFDictionaryRef_object = CFDictionaryRefObjectDefinition('CFDictionaryRef', 'CFDictionaryRefObj', 'CFDictionaryRef')
494CFMutableDictionaryRef_object = CFMutableDictionaryRefObjectDefinition('CFMutableDictionaryRef', 'CFMutableDictionaryRefObj', 'CFMutableDictionaryRef')
495CFDataRef_object = CFDataRefObjectDefinition('CFDataRef', 'CFDataRefObj', 'CFDataRef')
496CFMutableDataRef_object = CFMutableDataRefObjectDefinition('CFMutableDataRef', 'CFMutableDataRefObj', 'CFMutableDataRef')
497CFStringRef_object = CFStringRefObjectDefinition('CFStringRef', 'CFStringRefObj', 'CFStringRef')
498CFMutableStringRef_object = CFMutableStringRefObjectDefinition('CFMutableStringRef', 'CFMutableStringRefObj', 'CFMutableStringRef')
499CFURLRef_object = CFURLRefObjectDefinition('CFURLRef', 'CFURLRefObj', 'CFURLRef')
500
501# ADD object here
502
503module.addobject(CFTypeRef_object)
504module.addobject(CFArrayRef_object)
505module.addobject(CFMutableArrayRef_object)
506module.addobject(CFDictionaryRef_object)
507module.addobject(CFMutableDictionaryRef_object)
508module.addobject(CFDataRef_object)
509module.addobject(CFMutableDataRef_object)
510module.addobject(CFStringRef_object)
511module.addobject(CFMutableStringRef_object)
512module.addobject(CFURLRef_object)
513# ADD addobject call here
514
515# Create the generator classes used to populate the lists
516Function = OSErrWeakLinkFunctionGenerator
517Method = OSErrWeakLinkMethodGenerator
518
519# Create and populate the lists
520functions = []
521CFTypeRef_methods = []
522CFArrayRef_methods = []
523CFMutableArrayRef_methods = []
524CFDictionaryRef_methods = []
525CFMutableDictionaryRef_methods = []
526CFDataRef_methods = []
527CFMutableDataRef_methods = []
528CFStringRef_methods = []
529CFMutableStringRef_methods = []
530CFURLRef_methods = []
531
532# ADD _methods initializer here
533execfile(INPUTFILE)
534
535
536# add the populated lists to the generator groups
537# (in a different wordl the scan program would generate this)
538for f in functions: module.add(f)
539for f in CFTypeRef_methods: CFTypeRef_object.add(f)
540for f in CFArrayRef_methods: CFArrayRef_object.add(f)
541for f in CFMutableArrayRef_methods: CFMutableArrayRef_object.add(f)
542for f in CFDictionaryRef_methods: CFDictionaryRef_object.add(f)
543for f in CFMutableDictionaryRef_methods: CFMutableDictionaryRef_object.add(f)
544for f in CFDataRef_methods: CFDataRef_object.add(f)
545for f in CFMutableDataRef_methods: CFMutableDataRef_object.add(f)
546for f in CFStringRef_methods: CFStringRef_object.add(f)
547for f in CFMutableStringRef_methods: CFMutableStringRef_object.add(f)
548for f in CFURLRef_methods: CFURLRef_object.add(f)
549
550# Manual generators for getting data out of strings
551
552getasstring_body = """
553int size = CFStringGetLength(_self->ob_itself)+1;
554char *data = malloc(size);
555
556if( data == NULL ) return PyErr_NoMemory();
557if ( 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}
563free(data);
564return _res;
565"""
566
567f = ManualGenerator("CFStringGetString", getasstring_body);
568f.docstring = lambda: "() -> (string _rv)"
569CFStringRef_object.add(f)
570
571getasunicode_body = """
572int size = CFStringGetLength(_self->ob_itself)+1;
573Py_UNICODE *data = malloc(size*sizeof(Py_UNICODE));
574CFRange range;
575
576range.location = 0;
577range.length = size;
578if( data == NULL ) return PyErr_NoMemory();
579CFStringGetCharacters(_self->ob_itself, range, data);
580_res = (PyObject *)PyUnicode_FromUnicode(data, size-1);
581free(data);
582return _res;
583"""
584
585f = ManualGenerator("CFStringGetUnicode", getasunicode_body);
586f.docstring = lambda: "() -> (unicode _rv)"
587CFStringRef_object.add(f)
588
589# Get data from CFDataRef
590getasdata_body = """
591int size = CFDataGetLength(_self->ob_itself);
592char *data = (char *)CFDataGetBytePtr(_self->ob_itself);
593
594_res = (PyObject *)PyString_FromStringAndSize(data, size);
595return _res;
596"""
597
598f = ManualGenerator("CFDataGetData", getasdata_body);
599f.docstring = lambda: "() -> (string _rv)"
600CFDataRef_object.add(f)
601
602# Manual generator for CFPropertyListCreateFromXMLData because of funny error return
603fromxml_body = """
604CFTypeRef _rv;
605CFOptionFlags mutabilityOption;
606CFStringRef errorString;
607if (!PyArg_ParseTuple(_args, "l",
608                      &mutabilityOption))
609        return NULL;
610_rv = CFPropertyListCreateFromXMLData((CFAllocatorRef)NULL,
611                                      _self->ob_itself,
612                                      mutabilityOption,
613                                      &errorString);
614if (errorString)
615        CFRelease(errorString);
616if (_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);
622return _res;
623"""
624f = ManualGenerator("CFPropertyListCreateFromXMLData", fromxml_body)
625f.docstring = lambda: "(CFOptionFlags mutabilityOption) -> (CFTypeRefObj)"
626CFTypeRef_object.add(f)
627
628# Convert CF objects to Python objects
629toPython_body = """
630_res = PyCF_CF2Python(_self->ob_itself);
631return _res;
632"""
633
634f = ManualGenerator("toPython", toPython_body);
635f.docstring = lambda: "() -> (python_object)"
636CFTypeRef_object.add(f)
637
638toCF_body = """
639CFTypeRef rv;
640CFTypeID typeid;
641
642if (!PyArg_ParseTuple(_args, "O&", PyCF_Python2CF, &rv))
643        return NULL;
644typeid = CFGetTypeID(rv);
645
646if (typeid == CFStringGetTypeID())
647        return Py_BuildValue("O&", CFStringRefObj_New, rv);
648if (typeid == CFArrayGetTypeID())
649        return Py_BuildValue("O&", CFArrayRefObj_New, rv);
650if (typeid == CFDictionaryGetTypeID())
651        return Py_BuildValue("O&", CFDictionaryRefObj_New, rv);
652if (typeid == CFURLGetTypeID())
653        return Py_BuildValue("O&", CFURLRefObj_New, rv);
654
655_res = Py_BuildValue("O&", CFTypeRefObj_New, rv);
656return _res;
657"""
658f = ManualGenerator("toCF", toCF_body);
659f.docstring = lambda: "(python_object) -> (CF_object)"
660module.add(f)
661
662# ADD add forloop here
663
664# generate output (open the output file as late as possible)
665SetOutputFileName(OUTPUTFILE)
666module.generate()