Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | import distutils.ccompiler |
| 2 | −import sys, os, subprocess, glob | |
| 2 | +import sys | |
| 3 | +import os | |
| 4 | +import subprocess | |
| 5 | +import glob | |
| 3 | 6 | |
| 4 | 7 | header = os.path.join('src', 'aubio.h') |
| 5 | 8 | output_path = os.path.join('python', 'gen') |
| @@ -9,38 +12,39 @@ source_header = """// this file is generated! do not modify · 3 unmodified lines | ||
| 9 | 12 | """ |
| 10 | 13 | |
| 11 | 14 | 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 | + | |
| 44 | 48 | |
| 45 | 49 | def get_preprocessor(): |
| 46 | 50 | # findout which compiler to use |
| @@ -59,12 +63,12 @@ def get_preprocessor(): · 12 unmodified lines | ||
| 59 | 63 | print("Warning: failed initializing compiler ({:s})".format(repr(e))) |
| 60 | 64 | |
| 61 | 65 | cpp_cmd = None |
| 62 | − if hasattr(compiler, 'preprocessor'): # for unixccompiler | |
| 66 | + if hasattr(compiler, 'preprocessor'): # for unixccompiler | |
| 63 | 67 | cpp_cmd = compiler.preprocessor |
| 64 | − elif hasattr(compiler, 'compiler'): # for ccompiler | |
| 68 | + elif hasattr(compiler, 'compiler'): # for ccompiler | |
| 65 | 69 | cpp_cmd = compiler.compiler.split() |
| 66 | 70 | cpp_cmd += ['-E'] |
| 67 | − elif hasattr(compiler, 'cc'): # for msvccompiler | |
| 71 | + elif hasattr(compiler, 'cc'): # for msvccompiler | |
| 68 | 72 | cpp_cmd = compiler.cc.split() |
| 69 | 73 | cpp_cmd += ['-E'] |
| 70 | 74 | |
| @@ -94,15 +98,15 @@ def get_c_declarations(header=header, usedouble=False): · 23 unmodified lines | ||
| 94 | 98 | |
| 95 | 99 | print("Running command: {:s}".format(" ".join(cpp_cmd))) |
| 96 | 100 | proc = subprocess.Popen(cpp_cmd, |
| 97 | − stderr=subprocess.PIPE, | |
| 98 | − stdout=subprocess.PIPE) | |
| 101 | + stderr=subprocess.PIPE, | |
| 102 | + stdout=subprocess.PIPE) | |
| 99 | 103 | assert proc, 'Proc was none' |
| 100 | 104 | cpp_output = proc.stdout.read() |
| 101 | 105 | err_output = proc.stderr.read() |
| 102 | 106 | if not cpp_output: |
| 103 | 107 | raise Exception("preprocessor output is empty:\n%s" % err_output) |
| 104 | 108 | elif err_output: |
| 105 | − print ("Warning: preprocessor produced warnings:\n%s" % err_output) | |
| 109 | + print("Warning: preprocessor produced warnings:\n%s" % err_output) | |
| 106 | 110 | if not isinstance(cpp_output, list): |
| 107 | 111 | cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')] |
| 108 | 112 | |
| @@ -111,10 +115,11 @@ def get_c_declarations(header=header, usedouble=False): · 2 unmodified lines | ||
| 111 | 115 | |
| 112 | 116 | i = 1 |
| 113 | 117 | while 1: |
| 114 | − if i >= len(cpp_output):break | |
| 118 | + if i >= len(cpp_output): | |
| 119 | + break | |
| 115 | 120 | 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) | |
| 118 | 123 | elif ('}' in cpp_output[i]): |
| 119 | 124 | cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i] |
| 120 | 125 | cpp_output.pop(i - 1) |
| @@ -124,14 +129,14 @@ def get_c_declarations(header=header, usedouble=False): · 3 unmodified lines | ||
| 124 | 129 | # clean pointer notations |
| 125 | 130 | tmp = [] |
| 126 | 131 | for l in cpp_output: |
| 127 | − tmp+=[ l.replace(' *','* ')] | |
| 128 | − cpp_output = tmp; | |
| 129 | − | |
| 132 | + tmp += [l.replace(' *', '* ')] | |
| 133 | + cpp_output = tmp | |
| 130 | 134 | |
| 131 | 135 | return cpp_output |
| 132 | 136 | |
| 137 | + | |
| 133 | 138 | 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) | |
| 135 | 140 | cpp_objects = [a.split()[3][:-1] for a in typedefs] |
| 136 | 141 | return cpp_objects |
| 137 | 142 | |
| @@ -173,10 +178,10 @@ def generate_lib_from_c_declarations(cpp_objects, c_declarations): · 35 unmodified lines | ||
| 173 | 178 | if o[:6] == 'aubio_': |
| 174 | 179 | shortname = o[6:-2] # without aubio_ |
| 175 | 180 | longname = o[:-2] # without _t |
| 176 | − else: # support object not starting with aubio_ (fvec...) | |
| 181 | + else: # support object not starting with aubio_ (fvec...) | |
| 177 | 182 | shortname = o |
| 178 | 183 | longname = shortname |
| 179 | − | |
| 184 | + | |
| 180 | 185 | if shortname in skip_objects: |
| 181 | 186 | continue |
| 182 | 187 | 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 | ||
| 204 | 209 | elif '_set_' in fn: |
| 205 | 210 | lib[shortname]['set'].append(fn) |
| 206 | 211 | else: |
| 207 | − #print "no idea what to do about", fn | |
| 212 | + # print "no idea what to do about", fn | |
| 208 | 213 | lib[shortname]['other'].append(fn) |
| 209 | 214 | return lib |
| 210 | 215 | |
| 216 | + | |
| 211 | 217 | def print_c_declarations_results(lib, c_declarations): |
| 212 | 218 | for fn in c_declarations: |
| 213 | 219 | found = 0 |
| @@ -216,21 +222,23 @@ def print_c_declarations_results(lib, c_declarations): · 2 unmodified lines | ||
| 216 | 222 | if fn in lib[o][family]: |
| 217 | 223 | found = 1 |
| 218 | 224 | if found == 0: |
| 219 | − print ("missing", fn) | |
| 225 | + print("missing", fn) | |
| 220 | 226 | |
| 221 | 227 | for o in lib: |
| 222 | 228 | for family in lib[o]: |
| 223 | 229 | 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])) | |
| 225 | 231 | 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])) | |
| 227 | 233 | else: |
| 228 | − print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) ) | |
| 234 | + print("{:15s} {:10s} {:s}".format(o, family, lib[o][family])) | |
| 229 | 235 | |
| 230 | 236 | |
| 231 | 237 | 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'))) | |
| 234 | 242 | |
| 235 | 243 | c_declarations = get_c_declarations(header, usedouble=usedouble) |
| 236 | 244 | 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 | ||
| 245 | 253 | from gen_code import MappedObject |
| 246 | 254 | for o in lib: |
| 247 | 255 | out = source_header |
| 248 | − mapped = MappedObject(lib[o], usedouble = usedouble) | |
| 256 | + mapped = MappedObject(lib[o], usedouble=usedouble) | |
| 249 | 257 | out += mapped.gen_code() |
| 250 | 258 | output_file = os.path.join(output_path, 'gen-%s.c' % o) |
| 251 | 259 | with open(output_file, 'w') as f: |
| 252 | 260 | f.write(out) |
| 253 | − print ("wrote %s" % output_file ) | |
| 261 | + print("wrote %s" % output_file) | |
| 254 | 262 | sources_list.append(output_file) |
| 255 | 263 | |
| 256 | 264 | out = source_header |
| @@ -262,23 +270,23 @@ int generated_types_ready (void) · 5 unmodified lines | ||
| 262 | 270 | {{ |
| 263 | 271 | return ({pycheck_types}); |
| 264 | 272 | }} |
| 265 | −""".format(pycheck_types = check_types) | |
| 273 | +""".format(pycheck_types=check_types) | |
| 266 | 274 | |
| 267 | 275 | add_types = "".join([""" |
| 268 | 276 | 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]) | |
| 270 | 278 | out += """ |
| 271 | 279 | |
| 272 | 280 | void add_generated_objects ( PyObject *m ) |
| 273 | 281 | {{ |
| 274 | 282 | {add_types} |
| 275 | 283 | }} |
| 276 | −""".format(add_types = add_types) | |
| 284 | +""".format(add_types=add_types) | |
| 277 | 285 | |
| 278 | 286 | output_file = os.path.join(output_path, 'aubio-generated.c') |
| 279 | 287 | with open(output_file, 'w') as f: |
| 280 | 288 | f.write(out) |
| 281 | − print ("wrote %s" % output_file ) | |
| 289 | + print("wrote %s" % output_file) | |
| 282 | 290 | sources_list.append(output_file) |
| 283 | 291 | |
| 284 | 292 | 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 | ||
| 296 | 304 | {objlist} |
| 297 | 305 | int generated_objects ( void ); |
| 298 | 306 | void add_generated_objects( PyObject *m ); |
| 299 | −""".format(objlist = objlist) | |
| 307 | +""".format(objlist=objlist) | |
| 300 | 308 | |
| 301 | 309 | output_file = os.path.join(output_path, 'aubio-generated.h') |
| 302 | 310 | with open(output_file, 'w') as f: |
| 303 | 311 | f.write(out) |
| 304 | − print ("wrote %s" % output_file ) | |
| 312 | + print("wrote %s" % output_file) | |
| 305 | 313 | # no need to add header to list of sources |
| 306 | 314 | |
| 307 | 315 | return sorted(sources_list) |
| 308 | 316 | |
| 309 | 317 | 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] | |
| 312 | 322 | generate_external(header, output_path) |