source: interfaces/python/generator.py @ 6fd8d7e

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

Merge branch 'develop' into io

  • Property mode set to 100755
File size: 6.3 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():
31  if os.path.isdir('generated'): shutil.rmtree('generated')
32  os.mkdir('generated')
33
34  generated_objects = []
35  cpp_output, cpp_objects = get_cpp_objects()
36  skip_objects = ['fft',
37      'pvoc',
38      'filter',
39      'filterbank',
40      'resampler',
41      'sndfile',
42      'sink',
43      'sink_apple_audio',
44      'sink_sndfile',
45      'source_apple_audio',
46      'source_sndfile']
47
48  write_msg("-- INFO: %d objects in total" % len(cpp_objects))
49
50  for this_object in cpp_objects:
51      lint = 0
52   
53      if this_object[-2:] == '_t':
54          object_name = this_object[:-2]
55      else:
56          object_name = this_object
57          write_msg("-- WARNING: %s does not end in _t" % this_object)
58
59      if object_name[:len('aubio_')] != 'aubio_':
60          write_msg("-- WARNING: %s does not start n aubio_" % this_object)
61
62      write_msg("-- INFO: looking at", object_name)
63      object_methods = filter(lambda x: this_object in x, cpp_output)
64      object_methods = [a.strip() for a in object_methods]
65      object_methods = filter(lambda x: not x.startswith('typedef'), object_methods)
66      #for method in object_methods:
67      #    write_msg(method)
68      new_methods = filter(lambda x: 'new_'+object_name in x, object_methods)
69      if len(new_methods) > 1:
70          write_msg("-- WARNING: more than one new method for", object_name)
71          for method in new_methods:
72              write_msg(method)
73      elif len(new_methods) < 1:
74          write_msg("-- WARNING: no new method for", object_name)
75      elif 0:
76          for method in new_methods:
77              write_msg(method)
78
79      del_methods = filter(lambda x: 'del_'+object_name in x, object_methods)
80      if len(del_methods) > 1:
81          write_msg("-- WARNING: more than one del method for", object_name)
82          for method in del_methods:
83              write_msg(method)
84      elif len(del_methods) < 1:
85          write_msg("-- WARNING: no del method for", object_name)
86
87      do_methods = filter(lambda x: object_name+'_do' in x, object_methods)
88      if len(do_methods) > 1:
89          pass
90          #write_msg("-- WARNING: more than one do method for", object_name)
91          #for method in do_methods:
92          #    write_msg(method)
93      elif len(do_methods) < 1:
94          write_msg("-- WARNING: no do method for", object_name)
95      elif 0:
96          for method in do_methods:
97              write_msg(method)
98
99      # check do methods return void
100      for method in do_methods:
101          if (method.split()[0] != 'void'):
102              write_msg("-- ERROR: _do method does not return void:", method )
103
104      get_methods = filter(lambda x: object_name+'_get_' in x, object_methods)
105
106      set_methods = filter(lambda x: object_name+'_set_' in x, object_methods)
107      for method in set_methods:
108          if (method.split()[0] != 'uint_t'):
109              write_msg("-- ERROR: _set method does not return uint_t:", method )
110
111      other_methods = filter(lambda x: x not in new_methods, object_methods)
112      other_methods = filter(lambda x: x not in del_methods, other_methods)
113      other_methods = filter(lambda x: x not in    do_methods, other_methods)
114      other_methods = filter(lambda x: x not in get_methods, other_methods)
115      other_methods = filter(lambda x: x not in set_methods, other_methods)
116
117      if len(other_methods) > 0:
118          write_msg("-- WARNING: some methods for", object_name, "were unidentified")
119          for method in other_methods:
120              write_msg(method)
121
122
123      # generate this_object
124      short_name = object_name[len('aubio_'):]
125      if short_name in skip_objects:
126              write_msg("-- INFO: skipping object", short_name )
127              continue
128      if 1: #try:
129          s = gen_new_init(new_methods[0], short_name)
130          s += gen_do(do_methods[0], short_name) 
131          s += gen_members(new_methods[0], short_name)
132          s += gen_methods(get_methods, set_methods, short_name)
133          s += gen_finish(short_name)
134          generated_filepath = 'generated/gen-'+short_name+'.c'
135          fd = open(generated_filepath, 'w')
136          fd.write(s)
137      #except Exception, e:
138      #        write_msg("-- ERROR:", type(e), str(e), "in", short_name)
139      #        continue
140      generated_objects += [this_object]
141
142  s = """// generated list of objects created with generator.py
143
144"""
145
146  for each in generated_objects:
147      s += "extern PyTypeObject Py_%sType;\n" % \
148              each.replace('aubio_','').replace('_t','')
149
150  types_ready = []
151  for each in generated_objects:
152      types_ready.append("  PyType_Ready (&Py_%sType) < 0" % \
153              each.replace('aubio_','').replace('_t','') )
154
155  s += """
156  int
157  generated_types_ready (void)
158  {
159    return (
160  """
161  s += ('\n     ||').join(types_ready)
162  s += """);
163  }
164  """
165
166  s += """
167  void
168  add_generated_objects ( PyObject *m )
169  {"""
170  for each in generated_objects:
171      s += """  Py_INCREF (&Py_%(name)sType);
172    PyModule_AddObject (m, "%(name)s", (PyObject *) & Py_%(name)sType);""" % \
173            { 'name': ( each.replace('aubio_','').replace('_t','') ) }
174
175  s += """
176  }"""
177
178  fd = open('generated/aubio-generated.h', 'w')
179  fd.write(s)
180
181  from os import listdir
182  generated_files = listdir('generated')
183  generated_files = filter(lambda x: x.endswith('.c'), generated_files)
184  generated_files = ['generated/'+f for f in generated_files]
185  return generated_files
186
187if __name__ == '__main__':
188  generate_object_files() 
Note: See TracBrowser for help on using the repository browser.