History

gen_external.py : pepify

Martin Hermant committed 9 years ago

Commit 541ea281 parent 6cbf34b1 file · +75 −65Browse files

Showing the changes to python/lib (1 of 1 file). Show all

Unified · Split

python/lib/gen_external.py+75−65
@@ -1,5 +1,8 @@
11 import distutils.ccompiler
2−import sys, os, subprocess, glob
2+import sys
3+import os
4+import subprocess
5+import glob
36
47 header = os.path.join('src', 'aubio.h')
58 output_path = os.path.join('python', 'gen')
@@ -9,38 +12,39 @@ source_header = """// this file is generated! do not modify · 3 unmodified lines
912 """
1013
1114 skip_objects = [
12− # already in ext/
13− 'fft',
14− 'pvoc',
15− 'filter',
16− 'filterbank',
17− # AUBIO_UNSTABLE
18− 'hist',
19− 'parameter',
20− 'scale',
21− 'beattracking',
22− 'resampler',
23− 'peakpicker',
24− 'pitchfcomb',
25− 'pitchmcomb',
26− 'pitchschmitt',
27− 'pitchspecacf',
28− 'pitchyin',
29− 'pitchyinfft',
30− 'sink',
31− 'sink_apple_audio',
32− 'sink_sndfile',
33− 'sink_wavwrite',
34− #'mfcc',
35− 'source',
36− 'source_apple_audio',
37− 'source_sndfile',
38− 'source_avcodec',
39− 'source_wavread',
40− #'sampler',
41− 'audio_unit',
42− 'spectral_whitening',
43− ]
15+ # already in ext/
16+ 'fft',
17+ 'pvoc',
18+ 'filter',
19+ 'filterbank',
20+ # AUBIO_UNSTABLE
21+ 'hist',
22+ 'parameter',
23+ 'scale',
24+ 'beattracking',
25+ 'resampler',
26+ 'peakpicker',
27+ 'pitchfcomb',
28+ 'pitchmcomb',
29+ 'pitchschmitt',
30+ 'pitchspecacf',
31+ 'pitchyin',
32+ 'pitchyinfft',
33+ 'sink',
34+ 'sink_apple_audio',
35+ 'sink_sndfile',
36+ 'sink_wavwrite',
37+ #'mfcc',
38+ 'source',
39+ 'source_apple_audio',
40+ 'source_sndfile',
41+ 'source_avcodec',
42+ 'source_wavread',
43+ #'sampler',
44+ 'audio_unit',
45+ 'spectral_whitening',
46+]
47+
4448
4549 def get_preprocessor():
4650 # findout which compiler to use
@@ -59,12 +63,12 @@ def get_preprocessor(): · 12 unmodified lines
5963 print("Warning: failed initializing compiler ({:s})".format(repr(e)))
6064
6165 cpp_cmd = None
62− if hasattr(compiler, 'preprocessor'): # for unixccompiler
66+ if hasattr(compiler, 'preprocessor'): # for unixccompiler
6367 cpp_cmd = compiler.preprocessor
64− elif hasattr(compiler, 'compiler'): # for ccompiler
68+ elif hasattr(compiler, 'compiler'): # for ccompiler
6569 cpp_cmd = compiler.compiler.split()
6670 cpp_cmd += ['-E']
67− elif hasattr(compiler, 'cc'): # for msvccompiler
71+ elif hasattr(compiler, 'cc'): # for msvccompiler
6872 cpp_cmd = compiler.cc.split()
6973 cpp_cmd += ['-E']
7074
@@ -94,15 +98,15 @@ def get_c_declarations(header=header, usedouble=False): · 23 unmodified lines
9498
9599 print("Running command: {:s}".format(" ".join(cpp_cmd)))
96100 proc = subprocess.Popen(cpp_cmd,
97− stderr=subprocess.PIPE,
98− stdout=subprocess.PIPE)
101+ stderr=subprocess.PIPE,
102+ stdout=subprocess.PIPE)
99103 assert proc, 'Proc was none'
100104 cpp_output = proc.stdout.read()
101105 err_output = proc.stderr.read()
102106 if not cpp_output:
103107 raise Exception("preprocessor output is empty:\n%s" % err_output)
104108 elif err_output:
105− print ("Warning: preprocessor produced warnings:\n%s" % err_output)
109+ print("Warning: preprocessor produced warnings:\n%s" % err_output)
106110 if not isinstance(cpp_output, list):
107111 cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')]
108112
@@ -111,10 +115,11 @@ def get_c_declarations(header=header, usedouble=False): · 2 unmodified lines
111115
112116 i = 1
113117 while 1:
114− if i >= len(cpp_output):break
118+ if i >= len(cpp_output):
119+ break
115120 if ('{' in cpp_output[i - 1]) and (not '}' in cpp_output[i - 1]) or (not ';' in cpp_output[i - 1]):
116− cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i]
117− cpp_output.pop(i-1)
121+ cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
122+ cpp_output.pop(i - 1)
118123 elif ('}' in cpp_output[i]):
119124 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
120125 cpp_output.pop(i - 1)
@@ -124,14 +129,14 @@ def get_c_declarations(header=header, usedouble=False): · 3 unmodified lines
124129 # clean pointer notations
125130 tmp = []
126131 for l in cpp_output:
127− tmp+=[ l.replace(' *','* ')]
128− cpp_output = tmp;
129−
132+ tmp += [l.replace(' *', '* ')]
133+ cpp_output = tmp
130134
131135 return cpp_output
132136
137+
133138 def get_cpp_objects_from_c_declarations(c_declarations):
134− typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), c_declarations)
139+ typedefs = filter(lambda y: y.startswith('typedef struct _aubio'), c_declarations)
135140 cpp_objects = [a.split()[3][:-1] for a in typedefs]
136141 return cpp_objects
137142
@@ -173,10 +178,10 @@ def generate_lib_from_c_declarations(cpp_objects, c_declarations): · 35 unmodified lines
173178 if o[:6] == 'aubio_':
174179 shortname = o[6:-2] # without aubio_
175180 longname = o[:-2] # without _t
176− else: # support object not starting with aubio_ (fvec...)
181+ else: # support object not starting with aubio_ (fvec...)
177182 shortname = o
178183 longname = shortname
179−
184+
180185 if shortname in skip_objects:
181186 continue
182187 lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []}
@@ -204,10 +209,11 @@ def generate_lib_from_c_declarations(cpp_objects, c_declarations): · 21 unmodified lines
204209 elif '_set_' in fn:
205210 lib[shortname]['set'].append(fn)
206211 else:
207− #print "no idea what to do about", fn
212+ # print "no idea what to do about", fn
208213 lib[shortname]['other'].append(fn)
209214 return lib
210215
216+
211217 def print_c_declarations_results(lib, c_declarations):
212218 for fn in c_declarations:
213219 found = 0
@@ -216,21 +222,23 @@ def print_c_declarations_results(lib, c_declarations): · 2 unmodified lines
216222 if fn in lib[o][family]:
217223 found = 1
218224 if found == 0:
219− print ("missing", fn)
225+ print("missing", fn)
220226
221227 for o in lib:
222228 for family in lib[o]:
223229 if type(lib[o][family]) == str:
224− print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )
230+ print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
225231 elif len(lib[o][family]) == 1:
226− print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family][0] ) )
232+ print("{:15s} {:10s} {:s}".format(o, family, lib[o][family][0]))
227233 else:
228− print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )
234+ print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
229235
230236
231237 def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True):
232− if not os.path.isdir(output_path): os.mkdir(output_path)
233− elif not overwrite: return sorted(glob.glob(os.path.join(output_path, '*.c')))
238+ if not os.path.isdir(output_path):
239+ os.mkdir(output_path)
240+ elif not overwrite:
241+ return sorted(glob.glob(os.path.join(output_path, '*.c')))
234242
235243 c_declarations = get_c_declarations(header, usedouble=usedouble)
236244 cpp_objects = get_cpp_objects_from_c_declarations(c_declarations)
@@ -245,12 +253,12 @@ def generate_external(header=header, output_path=output_path, usedouble=False, o · 8 unmodified lines
245253 from gen_code import MappedObject
246254 for o in lib:
247255 out = source_header
248− mapped = MappedObject(lib[o], usedouble = usedouble)
256+ mapped = MappedObject(lib[o], usedouble=usedouble)
249257 out += mapped.gen_code()
250258 output_file = os.path.join(output_path, 'gen-%s.c' % o)
251259 with open(output_file, 'w') as f:
252260 f.write(out)
253− print ("wrote %s" % output_file )
261+ print("wrote %s" % output_file)
254262 sources_list.append(output_file)
255263
256264 out = source_header
@@ -262,23 +270,23 @@ int generated_types_ready (void) · 5 unmodified lines
262270 {{
263271 return ({pycheck_types});
264272 }}
265−""".format(pycheck_types = check_types)
273+""".format(pycheck_types=check_types)
266274
267275 add_types = "".join(["""
268276 Py_INCREF (&Py_{name}Type);
269− PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name = o) for o in lib])
277+ PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name=o) for o in lib])
270278 out += """
271279
272280 void add_generated_objects ( PyObject *m )
273281 {{
274282 {add_types}
275283 }}
276−""".format(add_types = add_types)
284+""".format(add_types=add_types)
277285
278286 output_file = os.path.join(output_path, 'aubio-generated.c')
279287 with open(output_file, 'w') as f:
280288 f.write(out)
281− print ("wrote %s" % output_file )
289+ print("wrote %s" % output_file)
282290 sources_list.append(output_file)
283291
284292 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
@@ -296,17 +304,19 @@ void add_generated_objects ( PyObject *m ) · 11 unmodified lines
296304 {objlist}
297305 int generated_objects ( void );
298306 void add_generated_objects( PyObject *m );
299−""".format(objlist = objlist)
307+""".format(objlist=objlist)
300308
301309 output_file = os.path.join(output_path, 'aubio-generated.h')
302310 with open(output_file, 'w') as f:
303311 f.write(out)
304− print ("wrote %s" % output_file )
312+ print("wrote %s" % output_file)
305313 # no need to add header to list of sources
306314
307315 return sorted(sources_list)
308316
309317 if __name__ == '__main__':
310− if len(sys.argv) > 1: header = sys.argv[1]
311− if len(sys.argv) > 2: output_path = sys.argv[2]
318+ if len(sys.argv) > 1:
319+ header = sys.argv[1]
320+ if len(sys.argv) > 2:
321+ output_path = sys.argv[2]
312322 generate_external(header, output_path)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t