Changeset abbd910


Ignore:
Timestamp:
Jul 11, 2012, 10:43:00 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/constantq, feature/pitchshift, feature/pydocstrings, feature/timestretch, master, pitchshift, sampler, timestretch, yinfft+
Children:
312826c
Parents:
195b424
Message:

gen_pyobject.py: clean up, output a float instead of a vector with a single element

File:
1 edited

Legend:

Unmodified
Added
Removed
  • interfaces/python/gen_pyobject.py

    r195b424 rabbd910  
    3535        return ['foo*', 'name'] """
    3636    l = arg.split()
     37    type_arg = {'type': l[0], 'name': l[1]}
    3738    # ['foo', '*name'] -> ['foo*', 'name']
    3839    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:]
    4043    # ['foo', '*', 'name'] -> ['foo*', 'name']
    4144    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]
    4348    else:
    44         return l
     49        #return l
     50        pass
     51    return type_arg
    4552
    4653def get_params(proto):
     
    145152    # self->param1, self->param2, self->param3
    146153    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])
    148155    else:
    149156        selfparams = ''
    150157    # "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])
    154161    s = """\
    155162// WARNING: this file is generated, DO NOT EDIT
     
    163170  aubio_%(name)s_t * o;
    164171""" % locals()
    165     for ptype, pname in newparams:
     172    for p in newparams:
     173        ptype = p['type']
     174        pname = p['name']
    166175        s += """\
    167176  %(ptype)s %(pname)s;
     
    177186  Py_%(name)s *self;
    178187""" % locals()
    179     for ptype, pname in newparams:
     188    for p in newparams:
     189        ptype = p['type']
     190        pname = p['name']
    180191        initval = aubioinitvalue[ptype]
    181192        s += """\
     
    200211  }
    201212""" % locals()
    202     for ptype, pname in newparams:
     213    for p in newparams:
     214        ptype = p['type']
     215        pname = p['name']
    203216        defval = aubiodefvalue[pname]
    204217        if ptype == 'char_t*':
     
    248261    doparams = get_params_types_names(dofunc)
    249262    # make sure the first parameter is the object
    250     assert doparams[0][0] == "aubio_"+name+"_t*", \
     263    assert doparams[0]['type'] == "aubio_"+name+"_t*", \
    251264        "method is not in 'aubio_<name>_t"
    252265    # and remove it
     
    261274    inputparams = [doparams[0]]
    262275    # 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  "
    268285    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']]
    272289        parseinput += """%(inputvec)s = %(converter)s (%(inputdef)s);
    273290
     
    275292    return NULL;
    276293  }""" % locals()
     294
    277295    # 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])
    279297    # end of inputs strings
    280298
     
    284302        #assert len(outputparams) == 1, \
    285303        #    "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 = ""
    292315        if len(outputparams) > 1:
    293             returnval = "PyObject *outputs = PyList_New(0);\n"
     316            returnval += "PyObject *outputs = PyList_New(0);\n"
    294317            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"
    296319            returnval += "  return outputs;"
    297320        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'] + ")"
    299325    else:
    300326        # no output
     
    302328        outputcreate = ""
    303329        #returnval = "Py_None";
    304         returnval = "return (PyObject *)" + aubiovectopyobj[p[0]] + " (" + p[-1] + ")"
     330        returnval = "return (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")"
    305331    # end of output strings
    306332
    307333    # 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])
    309335
    310336    # put it all together
    311337    s = """\
     338/* function Py_%(name)s_do */
    312339static PyObject *
    313340Py_%(name)s_do(Py_%(name)s * self, PyObject * args)
     
    338365AUBIO_MEMBERS_START(%(name)s)""" % locals()
    339366    for param in newparams:
    340         if param[0] == 'char_t*':
     367        if param['type'] == 'char_t*':
    341368            s += """
    342369  {"%(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':
    345372            s += """
    346373  {"%(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':
    349376            s += """
    350377  {"%(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}
    352379        else:
    353380            write_msg ("-- ERROR, unknown member type ", param )
     
    366393        params = get_params_types_names(method)
    367394        out_type = get_return_type(method)
    368         assert params[0][0] == "aubio_"+name+"_t*", \
     395        assert params[0]['type'] == "aubio_"+name+"_t*", \
    369396            "get method is not in 'aubio_<name>_t"
    370397        write_msg (method )
    371398        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:]])
    373400        parse_args = ""
    374401        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:]])
    378405        parse_args += """
    379406  if (!PyArg_ParseTuple (args, "%(argmap)s", %(arglist)s)) {
     
    409436        params = get_params_types_names(method)
    410437        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']
    413440        assert len(params) == 1, \
    414441            "get method has more than one parameter %s" % params
Note: See TracChangeset for help on using the changeset viewer.