source: python/lib/generator.py @ 8f86f0e

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 8f86f0e was 8f86f0e, checked in by Paul Brossier <piem@piem.org>, 11 years ago

python/lib/generator.py: remove most unstable objects

  • Property mode set to 100755
File size: 6.9 KB
Line 
1#! /usr/bin/python
2
3""" This file generates a c file from a list of cpp prototypes. """
4
5import os, sys, shutil
6from gen_pyobject import write_msg, gen_new_init, gen_do, gen_members, gen_methods, gen_finish
7
8def get_cpp_objects():
9
10  cpp_output = [l.strip() for l in os.popen('cpp -DAUBIO_UNSTABLE=1 -I../build/src ../src/aubio.h').readlines()]
11
12  cpp_output = filter(lambda y: len(y) > 1, cpp_output)
13  cpp_output = filter(lambda y: not y.startswith('#'), cpp_output)
14
15  i = 1
16  while 1:
17      if i >= len(cpp_output): break
18      if cpp_output[i-1].endswith(',') or cpp_output[i-1].endswith('{') or cpp_output[i].startswith('}'):
19          cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i]
20          cpp_output.pop(i-1)
21      else:
22          i += 1
23
24  typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), cpp_output)
25
26  cpp_objects = [a.split()[3][:-1] for a in typedefs]
27
28  return cpp_output, cpp_objects
29
30def generate_object_files(output_path):
31  if os.path.isdir(output_path): shutil.rmtree(output_path)
32  os.mkdir(output_path)
33
34  generated_objects = []
35  cpp_output, cpp_objects = get_cpp_objects()
36  skip_objects = [
37      # already in ext/
38      'fft',
39      'pvoc',
40      'filter',
41      'filterbank',
42      #'resampler',
43      # AUBIO_UNSTABLE
44      'hist',
45      'scale',
46      'beattracking',
47      'resampler',
48      'sndfile',
49      'peakpicker',
50      'pitchfcomb',
51      'pitchmcomb',
52      'pitchschmitt',
53      'pitchyin',
54      'pitchyinfft',
55      'sink_apple_audio',
56      'sink_sndfile',
57      'source_apple_audio',
58      'source_sndfile',
59      #'sampler',
60      ]
61
62  write_msg("-- INFO: %d objects in total" % len(cpp_objects))
63
64  for this_object in cpp_objects:
65      lint = 0
66
67      if this_object[-2:] == '_t':
68          object_name = this_object[:-2]
69      else:
70          object_name = this_object
71          write_msg("-- WARNING: %s does not end in _t" % this_object)
72
73      if object_name[:len('aubio_')] != 'aubio_':
74          write_msg("-- WARNING: %s does not start n aubio_" % this_object)
75
76      write_msg("-- INFO: looking at", object_name)
77      object_methods = filter(lambda x: this_object in x, cpp_output)
78      object_methods = [a.strip() for a in object_methods]
79      object_methods = filter(lambda x: not x.startswith('typedef'), object_methods)
80      #for method in object_methods:
81      #    write_msg(method)
82      new_methods = filter(lambda x: 'new_'+object_name in x, object_methods)
83      if len(new_methods) > 1:
84          write_msg("-- WARNING: more than one new method for", object_name)
85          for method in new_methods:
86              write_msg(method)
87      elif len(new_methods) < 1:
88          write_msg("-- WARNING: no new method for", object_name)
89      elif 0:
90          for method in new_methods:
91              write_msg(method)
92
93      del_methods = filter(lambda x: 'del_'+object_name in x, object_methods)
94      if len(del_methods) > 1:
95          write_msg("-- WARNING: more than one del method for", object_name)
96          for method in del_methods:
97              write_msg(method)
98      elif len(del_methods) < 1:
99          write_msg("-- WARNING: no del method for", object_name)
100
101      do_methods = filter(lambda x: object_name+'_do' in x, object_methods)
102      if len(do_methods) > 1:
103          pass
104          #write_msg("-- WARNING: more than one do method for", object_name)
105          #for method in do_methods:
106          #    write_msg(method)
107      elif len(do_methods) < 1:
108          write_msg("-- WARNING: no do method for", object_name)
109      elif 0:
110          for method in do_methods:
111              write_msg(method)
112
113      # check do methods return void
114      for method in do_methods:
115          if (method.split()[0] != 'void'):
116              write_msg("-- ERROR: _do method does not return void:", method )
117
118      get_methods = filter(lambda x: object_name+'_get_' in x, object_methods)
119
120      set_methods = filter(lambda x: object_name+'_set_' in x, object_methods)
121      for method in set_methods:
122          if (method.split()[0] != 'uint_t'):
123              write_msg("-- ERROR: _set method does not return uint_t:", method )
124
125      other_methods = filter(lambda x: x not in new_methods, object_methods)
126      other_methods = filter(lambda x: x not in del_methods, other_methods)
127      other_methods = filter(lambda x: x not in    do_methods, other_methods)
128      other_methods = filter(lambda x: x not in get_methods, other_methods)
129      other_methods = filter(lambda x: x not in set_methods, other_methods)
130
131      if len(other_methods) > 0:
132          write_msg("-- WARNING: some methods for", object_name, "were unidentified")
133          for method in other_methods:
134              write_msg(method)
135
136
137      # generate this_object
138      short_name = object_name[len('aubio_'):]
139      if short_name in skip_objects:
140              write_msg("-- INFO: skipping object", short_name )
141              continue
142      if 1: #try:
143          s = gen_new_init(new_methods[0], short_name)
144          s += gen_do(do_methods[0], short_name)
145          s += gen_members(new_methods[0], short_name)
146          s += gen_methods(get_methods, set_methods, short_name)
147          s += gen_finish(short_name)
148          generated_filepath = os.path.join(output_path,'gen-'+short_name+'.c')
149          fd = open(generated_filepath, 'w')
150          fd.write(s)
151      #except Exception, e:
152      #        write_msg("-- ERROR:", type(e), str(e), "in", short_name)
153      #        continue
154      generated_objects += [this_object]
155
156  s = """// generated list of objects created with generator.py
157
158"""
159
160  types_ready = []
161  for each in generated_objects:
162      types_ready.append("  PyType_Ready (&Py_%sType) < 0" % \
163              each.replace('aubio_','').replace('_t','') )
164
165  s = """// generated list of objects created with generator.py
166
167#include "aubio-generated.h"
168"""
169
170  s += """
171int generated_types_ready (void)
172{
173  return (
174"""
175  s += ('\n     ||').join(types_ready)
176  s += """);
177}
178"""
179
180  s += """
181void add_generated_objects ( PyObject *m )
182{"""
183  for each in generated_objects:
184    s += """
185  Py_INCREF (&Py_%(name)sType);
186  PyModule_AddObject (m, "%(name)s", (PyObject *) & Py_%(name)sType);""" % \
187          { 'name': ( each.replace('aubio_','').replace('_t','') ) }
188
189  s += """
190}"""
191
192  fd = open(os.path.join(output_path,'aubio-generated.c'), 'w')
193  fd.write(s)
194
195  s = """// generated list of objects created with generator.py
196
197#include <Python.h>
198
199"""
200
201  for each in generated_objects:
202      s += "extern PyTypeObject Py_%sType;\n" % \
203              each.replace('aubio_','').replace('_t','')
204
205  s+= "int generated_objects ( void );\n"
206  s+= "void add_generated_objects( PyObject *m );\n"
207
208  fd = open(os.path.join(output_path,'aubio-generated.h'), 'w')
209  fd.write(s)
210
211  from os import listdir
212  generated_files = listdir(output_path)
213  generated_files = filter(lambda x: x.endswith('.c'), generated_files)
214  generated_files = [output_path+'/'+f for f in generated_files]
215  return generated_files
216
217if __name__ == '__main__':
218  generate_object_files('gen')
Note: See TracBrowser for help on using the repository browser.