generator.py: fix indentation, do not allow any error
[aubio.git] / interfaces / python / generator.py
1 #! /usr/bin/python
2
3 """ This file generates a c file from a list of cpp prototypes. """
4
5 import os, sys
6
7 skip_objects = ['fft', 'pvoc', 'filter', 'filterbank', 'biquad']
8
9 cpp_output = [l.strip() for l in os.popen('cpp -I ../../build/default/src ../../src/aubio.h').readlines()]
10
11 cpp_output = filter(lambda y: len(y) > 1, cpp_output)
12 cpp_output = filter(lambda y: not y.startswith('#'), cpp_output)
13
14 i = 1
15 while 1:
16     if i >= len(cpp_output): break
17     if cpp_output[i-1].endswith(',') or cpp_output[i-1].endswith('{') or cpp_output[i].startswith('}'):
18         cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i]
19         cpp_output.pop(i-1)
20     else:
21         i += 1
22
23 typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), cpp_output)
24
25 objects = [a.split()[3][:-1] for a in typedefs]
26
27 print "-- INFO: %d objects in total" % len(objects)
28
29 for object in objects:
30     lint = 0
31  
32     if object[-2:] == '_t':
33         object_name = object[:-2]
34     else:
35         object_name = object
36         print "-- WARNING: %s does not end in _t" % object
37
38     if object_name[:len('aubio_')] != 'aubio_':
39         print "-- WARNING: %s does not start n aubio_" % object
40
41     print "-- INFO: looking at", object_name
42     object_methods = filter(lambda x: object in x, cpp_output)
43     object_methods = [a.strip() for a in object_methods]
44     object_methods = filter(lambda x: not x.startswith('typedef'), object_methods)
45     #for method in object_methods:
46     #    print method
47
48     new_methods = filter(lambda x: 'new_'+object_name in x, object_methods)
49     if len(new_methods) > 1:
50         print "-- WARNING: more than one new method for", object_name
51         for method in new_methods:
52             print method
53     elif len(new_methods) < 1:
54         print "-- WARNING: no new method for", object_name
55     elif 0:
56         for method in new_methods:
57             print method
58
59     del_methods = filter(lambda x: 'del_'+object_name in x, object_methods)
60     if len(del_methods) > 1:
61         print "-- WARNING: more than one del method for", object_name
62         for method in del_methods:
63             print method
64     elif len(del_methods) < 1:
65         print "-- WARNING: no del method for", object_name
66
67     do_methods = filter(lambda x: object_name+'_do' in x, object_methods)
68     if len(do_methods) > 1:
69         pass
70         #print "-- WARNING: more than one do method for", object_name
71         #for method in do_methods:
72         #    print method
73     elif len(do_methods) < 1:
74         print "-- WARNING: no do method for", object_name
75     elif 0:
76         for method in do_methods:
77             print method
78
79     # check do methods return void
80     for method in do_methods:
81         if (method.split()[0] != 'void'):
82             print "-- ERROR: _do method does not return void:", method 
83
84     get_methods = filter(lambda x: object_name+'_get_' in x, object_methods)
85
86     set_methods = filter(lambda x: object_name+'_set_' in x, object_methods)
87     for method in set_methods:
88         if (method.split()[0] != 'uint_t'):
89             print "-- ERROR: _set method does not return uint_t:", method 
90
91     other_methods = filter(lambda x: x not in new_methods, object_methods)
92     other_methods = filter(lambda x: x not in del_methods, other_methods)
93     other_methods = filter(lambda x: x not in    do_methods, other_methods)
94     other_methods = filter(lambda x: x not in get_methods, other_methods)
95     other_methods = filter(lambda x: x not in set_methods, other_methods)
96
97     if len(other_methods) > 0:
98         print "-- WARNING: some methods for", object_name, "were unidentified"
99         for method in other_methods:
100             print method
101
102     # generate object
103     if not os.path.isdir('generated'): os.mkdir('generated')
104     from gen_pyobject import *
105     short_name = object_name[len('aubio_'):]
106     if short_name in skip_objects:
107             print "-- INFO: skipping object", short_name 
108             continue
109     if 1: #try:
110         s = gen_new_init(new_methods[0], short_name)
111         s += gen_do(do_methods[0], short_name) 
112         s += gen_members(new_methods[0], short_name)
113         s += gen_methods(get_methods, set_methods, short_name)
114         s += gen_finish(short_name)
115         fd = open('generated/gen-'+short_name+'.c', 'w')
116         fd.write(s)
117     #except Exception, e:
118     #        print "-- ERROR:", type(e), str(e), "in", short_name
119     #        continue