Changeset abbd910 for interfaces
- Timestamp:
- Jul 11, 2012, 10:43:00 PM (13 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- 312826c
- Parents:
- 195b424
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
interfaces/python/gen_pyobject.py
r195b424 rabbd910 35 35 return ['foo*', 'name'] """ 36 36 l = arg.split() 37 type_arg = {'type': l[0], 'name': l[1]} 37 38 # ['foo', '*name'] -> ['foo*', 'name'] 38 39 if l[-1].startswith('*'): 39 return [l[0]+'*', l[1][1:]] 40 #return [l[0]+'*', l[1][1:]] 41 type_arg['type'] = l[0] + '*' 42 type_arg['name'] = l[1][1:] 40 43 # ['foo', '*', 'name'] -> ['foo*', 'name'] 41 44 if len(l) == 3: 42 return [l[0]+l[1], l[2]] 45 #return [l[0]+l[1], l[2]] 46 type_arg['type'] = l[0]+l[1] 47 type_arg['name'] = l[2] 43 48 else: 44 return l 49 #return l 50 pass 51 return type_arg 45 52 46 53 def get_params(proto): … … 145 152 # self->param1, self->param2, self->param3 146 153 if len(newparams): 147 selfparams = ', self->'+', self->'.join([p[ 1] for p in newparams])154 selfparams = ', self->'+', self->'.join([p['name'] for p in newparams]) 148 155 else: 149 156 selfparams = '' 150 157 # "param1", "param2", "param3" 151 paramnames = ", ".join(["\""+p[ 1]+"\"" for p in newparams])152 pyparams = "".join(map(lambda p: aubio2pytypes[p[ 0]], newparams))153 paramrefs = ", ".join(["&" + p[ 1] for p in newparams])158 paramnames = ", ".join(["\""+p['name']+"\"" for p in newparams]) 159 pyparams = "".join(map(lambda p: aubio2pytypes[p['type']], newparams)) 160 paramrefs = ", ".join(["&" + p['name'] for p in newparams]) 154 161 s = """\ 155 162 // WARNING: this file is generated, DO NOT EDIT … … 163 170 aubio_%(name)s_t * o; 164 171 """ % locals() 165 for ptype, pname in newparams: 172 for p in newparams: 173 ptype = p['type'] 174 pname = p['name'] 166 175 s += """\ 167 176 %(ptype)s %(pname)s; … … 177 186 Py_%(name)s *self; 178 187 """ % locals() 179 for ptype, pname in newparams: 188 for p in newparams: 189 ptype = p['type'] 190 pname = p['name'] 180 191 initval = aubioinitvalue[ptype] 181 192 s += """\ … … 200 211 } 201 212 """ % locals() 202 for ptype, pname in newparams: 213 for p in newparams: 214 ptype = p['type'] 215 pname = p['name'] 203 216 defval = aubiodefvalue[pname] 204 217 if ptype == 'char_t*': … … 248 261 doparams = get_params_types_names(dofunc) 249 262 # make sure the first parameter is the object 250 assert doparams[0][ 0] == "aubio_"+name+"_t*", \263 assert doparams[0]['type'] == "aubio_"+name+"_t*", \ 251 264 "method is not in 'aubio_<name>_t" 252 265 # and remove it … … 261 274 inputparams = [doparams[0]] 262 275 # build the parsing string for PyArg_ParseTuple 263 pytypes = "".join([aubio2pytypes[p[0]] for p in doparams[0:1]]) 264 inputdefs = "\n ".join(["PyObject * " + p[-1] + "_obj;" for p in inputparams]) 265 inputvecs = "\n ".join(map(lambda p: \ 266 p[0] + p[-1] + ";", inputparams)) 267 parseinput = "" 276 pytypes = "".join([aubio2pytypes[p['type']] for p in doparams[0:1]]) 277 278 inputdefs = "/* input vectors python prototypes */\n " 279 inputdefs += "\n ".join(["PyObject * " + p['name'] + "_obj;" for p in inputparams]) 280 281 inputvecs = "/* input vectors prototypes */\n " 282 inputvecs += "\n ".join(map(lambda p: p['type'] + ' ' + p['name'] + ";", inputparams)) 283 284 parseinput = "/* input vectors parsing */\n " 268 285 for p in inputparams: 269 inputvec = p[ -1]270 inputdef = p[ -1] + "_obj"271 converter = aubiovecfrompyobj[p[ 0]]286 inputvec = p['name'] 287 inputdef = p['name'] + "_obj" 288 converter = aubiovecfrompyobj[p['type']] 272 289 parseinput += """%(inputvec)s = %(converter)s (%(inputdef)s); 273 290 … … 275 292 return NULL; 276 293 }""" % locals() 294 277 295 # build the string for the input objects references 278 inputrefs = ", ".join(["&" + p[ -1] + "_obj" for p in inputparams])296 inputrefs = ", ".join(["&" + p['name'] + "_obj" for p in inputparams]) 279 297 # end of inputs strings 280 298 … … 284 302 #assert len(outputparams) == 1, \ 285 303 # "too many output parameters" 286 outputvecs = "\n ".join([p[0] + p[-1] + ";" for p in outputparams]) 287 outputcreate = "\n ".join(["""\ 288 %(name)s = new_%(autype)s (%(length)s);""" % \ 289 {'name': p[-1], 'pytype': p[0], 'autype': p[0][:-3], 290 'length': defaultsizes[name]} \ 291 for p in outputparams]) 304 outputvecs = "\n /* output vectors prototypes */\n " 305 outputvecs += "\n ".join([p['type'] + ' ' + p['name'] + ";" for p in outputparams]) 306 params = { 307 'name': p['name'], 'pytype': p['type'], 'autype': p['type'][:-3], 308 'length': defaultsizes[name]} 309 outputcreate = "\n ".join(["""/* creating output %(name)s as a new_%(autype)s of length %(length)s */""" % \ 310 params for p in outputparams]) 311 outputcreate += "\n" 312 outputcreate += "\n ".join([""" %(name)s = new_%(autype)s (%(length)s);""" % \ 313 params for p in outputparams]) 314 returnval = "" 292 315 if len(outputparams) > 1: 293 returnval = "PyObject *outputs = PyList_New(0);\n"316 returnval += "PyObject *outputs = PyList_New(0);\n" 294 317 for p in outputparams: 295 returnval += " PyList_Append( outputs, (PyObject *)" + aubiovectopyobj[p[ 0]] + " (" + p[-1] + ")" +");\n"318 returnval += " PyList_Append( outputs, (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")" +");\n" 296 319 returnval += " return outputs;" 297 320 else: 298 returnval = "return (PyObject *)" + aubiovectopyobj[p[0]] + " (" + p[-1] + ")" 321 if defaultsizes[name] == '1': 322 returnval += "return (PyObject *)PyFloat_FromDouble(" + p['name'] + "->data[0])" 323 else: 324 returnval += "return (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")" 299 325 else: 300 326 # no output … … 302 328 outputcreate = "" 303 329 #returnval = "Py_None"; 304 returnval = "return (PyObject *)" + aubiovectopyobj[p[ 0]] + " (" + p[-1] + ")"330 returnval = "return (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")" 305 331 # end of output strings 306 332 307 333 # build the parameters for the _do() call 308 doparams_string = "self->o, " + ", ".join([p[ -1] for p in doparams])334 doparams_string = "self->o, " + ", ".join([p['name'] for p in doparams]) 309 335 310 336 # put it all together 311 337 s = """\ 338 /* function Py_%(name)s_do */ 312 339 static PyObject * 313 340 Py_%(name)s_do(Py_%(name)s * self, PyObject * args) … … 338 365 AUBIO_MEMBERS_START(%(name)s)""" % locals() 339 366 for param in newparams: 340 if param[ 0] == 'char_t*':367 if param['type'] == 'char_t*': 341 368 s += """ 342 369 {"%(pname)s", T_STRING, offsetof (Py_%(name)s, %(pname)s), READONLY, ""},""" \ 343 % { 'pname': param[ 1], 'ptype': param[0], 'name': name}344 elif param[ 0] == 'uint_t':370 % { 'pname': param['name'], 'ptype': param['type'], 'name': name} 371 elif param['type'] == 'uint_t': 345 372 s += """ 346 373 {"%(pname)s", T_INT, offsetof (Py_%(name)s, %(pname)s), READONLY, ""},""" \ 347 % { 'pname': param[ 1], 'ptype': param[0], 'name': name}348 elif param[ 0] == 'smpl_t':374 % { 'pname': param['name'], 'ptype': param['type'], 'name': name} 375 elif param['type'] == 'smpl_t': 349 376 s += """ 350 377 {"%(pname)s", T_FLOAT, offsetof (Py_%(name)s, %(pname)s), READONLY, ""},""" \ 351 % { 'pname': param[ 1], 'ptype': param[0], 'name': name}378 % { 'pname': param['name'], 'ptype': param['type'], 'name': name} 352 379 else: 353 380 write_msg ("-- ERROR, unknown member type ", param ) … … 366 393 params = get_params_types_names(method) 367 394 out_type = get_return_type(method) 368 assert params[0][ 0] == "aubio_"+name+"_t*", \395 assert params[0]['type'] == "aubio_"+name+"_t*", \ 369 396 "get method is not in 'aubio_<name>_t" 370 397 write_msg (method ) 371 398 write_msg (params[1:]) 372 setter_args = "self->o, " +",".join([p[ 1] for p in params[1:]])399 setter_args = "self->o, " +",".join([p['name'] for p in params[1:]]) 373 400 parse_args = "" 374 401 for p in params[1:]: 375 parse_args += p[ 0] + " " + p[1] + ";\n"376 argmap = "".join([aubio2pytypes[p[ 0]] for p in params[1:]])377 arglist = ", ".join(["&"+p[ 1] for p in params[1:]])402 parse_args += p['type'] + " " + p['name'] + ";\n" 403 argmap = "".join([aubio2pytypes[p['type']] for p in params[1:]]) 404 arglist = ", ".join(["&"+p['name'] for p in params[1:]]) 378 405 parse_args += """ 379 406 if (!PyArg_ParseTuple (args, "%(argmap)s", %(arglist)s)) { … … 409 436 params = get_params_types_names(method) 410 437 out_type = get_return_type(method) 411 assert params[0][ 0] == "aubio_"+name+"_t*", \412 "get method is not in 'aubio_<name>_t %s" % params[0][ 0]438 assert params[0]['type'] == "aubio_"+name+"_t*", \ 439 "get method is not in 'aubio_<name>_t %s" % params[0]['type'] 413 440 assert len(params) == 1, \ 414 441 "get method has more than one parameter %s" % params
Note: See TracChangeset
for help on using the changeset viewer.