3 header = """// this file is generated! do not modify
4 #include "aubio-types.h"
44 def get_cpp_objects():
46 cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=1 -I../build/src ../src/aubio.h').readlines()]
47 #cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=0 -I../build/src ../src/onset/onset.h').readlines()]
48 #cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=0 -I../build/src ../src/pitch/pitch.h').readlines()]
50 cpp_output = filter(lambda y: len(y) > 1, cpp_output)
51 cpp_output = filter(lambda y: not y.startswith('#'), cpp_output)
52 cpp_output = list(cpp_output)
56 if i >= len(cpp_output): break
57 if cpp_output[i-1].endswith(',') or cpp_output[i-1].endswith('{') or cpp_output[i].startswith('}'):
58 cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i]
63 typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), cpp_output)
65 cpp_objects = [a.split()[3][:-1] for a in typedefs]
67 return cpp_output, cpp_objects
69 def generate_external(output_path):
72 cpp_output, cpp_objects = get_cpp_objects()
79 if shortname in skip_objects:
81 lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []}
82 lib[shortname]['longname'] = o
83 lib[shortname]['shortname'] = shortname
86 #print "found", o[:-1], "in", fn
87 if 'typedef struct ' in fn:
88 lib[shortname]['struct'].append(fn)
90 lib[shortname]['do'].append(fn)
92 lib[shortname]['new'].append(fn)
94 lib[shortname]['del'].append(fn)
96 lib[shortname]['get'].append(fn)
98 lib[shortname]['set'].append(fn)
100 #print "no idea what to do about", fn
101 lib[shortname]['other'].append(fn)
104 for fn in cpp_output:
107 for family in lib[o]:
108 if fn in lib[o][family]:
114 for family in lib[o]:
115 if type(lib[o][family]) == str:
116 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )
117 elif len(lib[o][family]) == 1:
118 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family][0] ) )
120 print ( "{:15s} {:10s} {:d}".format(o, family, len(lib[o][family]) ) )
123 from .gen_code import MappedObject
126 mapped = MappedObject(lib[o])
127 out += mapped.gen_code()
128 output_file = os.path.join(output_path, 'gen-%s.c' % o)
129 with open(output_file, 'w') as f:
131 print ("wrote %s" % output_file )
132 sources_list.append(output_file)
135 out += "#include \"aubio-generated.h\""
136 check_types = "\n || ".join(["PyType_Ready(&Py_%sType) < 0" % o for o in lib])
139 int generated_types_ready (void)
141 return ({pycheck_types});
143 """.format(pycheck_types = check_types)
145 add_types = "".join(["""
146 Py_INCREF (&Py_{name}Type);
147 PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name = o) for o in lib])
150 void add_generated_objects ( PyObject *m )
154 """.format(add_types = add_types)
156 output_file = os.path.join(output_path, 'aubio-generated.c')
157 with open(output_file, 'w') as f:
159 print ("wrote %s" % output_file )
160 sources_list.append(output_file)
162 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
164 // generated list of objects created with gen_external.py
168 int generated_objects ( void );
169 void add_generated_objects( PyObject *m );
170 """.format(objlist = objlist)
172 output_file = os.path.join(output_path, 'aubio-generated.h')
173 with open(output_file, 'w') as f:
175 print ("wrote %s" % output_file )
176 # no need to add header to list of sources
180 if __name__ == '__main__':
182 generate_external(output_path)