History

python/lib/gen_code.py: use new proxy functions

Paul Brossier committed 10 years ago

Commit a7f398d1 parent b5bef112 files · +83 −50Browse files

Showing the changes to python/lib/gen_code.py (1 of 2 files). Show all

Unified · Split

python/lib/gen_code.py+81−48
@@ -38,7 +38,7 @@ pyfromtype_fn = { · 37 unmodified lines
38pytoaubio_fn = {38pytoaubio_fn = {
39 'fvec_t*': 'PyAubio_ArrayToCFvec',39 'fvec_t*': 'PyAubio_ArrayToCFvec',
40 'cvec_t*': 'PyAubio_ArrayToCCvec',40 'cvec_t*': 'PyAubio_ArrayToCCvec',
41 'fmat_t*': 'PyAubio_ArrayToCFmat',41 #'fmat_t*': 'PyAubio_ArrayToCFmat',
42 }42 }
43 43
44pyfromaubio_fn = {44pyfromaubio_fn = {
@@ -74,6 +74,7 @@ pyargparse_chars = { · 29 unmodified lines
74 'char_t*': 's',74 'char_t*': 's',
75 'fmat_t*': 'O',75 'fmat_t*': 'O',
76 'fvec_t*': 'O',76 'fvec_t*': 'O',
77 'cvec_t*': 'O',
77 }78 }
78 79
79objoutsize = {80objoutsize = {
@@ -83,8 +84,9 @@ objoutsize = { · 3 unmodified lines
83 'sampler': 'self->hop_size',84 'sampler': 'self->hop_size',
84 'mfcc': 'self->n_coeffs',85 'mfcc': 'self->n_coeffs',
85 'specdesc': '1',86 'specdesc': '1',
86 'tempo': '1',87 'tempo': '2',
87 'filterbank': 'self->n_filters',88 'filterbank': 'self->n_filters',
89 'tss': 'self->hop_size',
88 }90 }
89 91
90def get_name(proto):92def get_name(proto):
@@ -104,20 +106,20 @@ def split_type(arg): · 13 unmodified lines
104 """ arg = 'foo *name' 106 """ arg = 'foo *name'
105 return ['foo*', 'name'] """107 return ['foo*', 'name'] """
106 l = arg.split()108 l = arg.split()
107 type_arg = {'type': l[0], 'name': l[1]}109 type_arg = {} #'type': l[0], 'name': l[1]}
108 # ['foo', '*name'] -> ['foo*', 'name']110 type_arg['type'] = " ".join(l[:-1])
109 if l[-1].startswith('*'):111 type_arg['name'] = l[-1]
110 #return [l[0]+'*', l[1][1:]]112 # fix up type / name
111 type_arg['type'] = l[0] + '*'113 if type_arg['name'].startswith('*'):
112 type_arg['name'] = l[1][1:]114 # ['foo', '*name'] -> ['foo*', 'name']
113 # ['foo', '*', 'name'] -> ['foo*', 'name']115 type_arg['type'] += '*'
114 if len(l) == 3:116 type_arg['name'] = type_arg['name'][1:]
115 #return [l[0]+l[1], l[2]]117 if type_arg['type'].endswith(' *'):
116 type_arg['type'] = l[0]+l[1]118 # ['foo *', 'name'] -> ['foo*', 'name']
117 type_arg['name'] = l[2]119 type_arg['type'] = type_arg['type'].replace(' *','*')
118 else:120 if type_arg['type'].startswith('const '):
119 #return l121 # ['foo *', 'name'] -> ['foo*', 'name']
120 pass122 type_arg['type'] = type_arg['type'].replace('const ','')
121 return type_arg123 return type_arg
122 124
123def get_params(proto):125def get_params(proto):
@@ -126,16 +128,28 @@ def get_params(proto): · 2 unmodified lines
126 returns: ['int argc', 'char ** argv']128 returns: ['int argc', 'char ** argv']
127 """129 """
128 import re130 import re
129 paramregex = re.compile('[\(, ](\w+ \*?\*? ?\w+)[, \)]')131 paramregex = re.compile('.*\((.*)\);')
130 return paramregex.findall(proto)132 a = paramregex.findall(proto)[0].split(', ')
133 #a = [i.replace('const ', '') for i in a]
134 return a
135
136def get_input_params(proto):
137 a = get_params(proto)
138 return [i.replace('const ', '') for i in a if (i.startswith('const ') or i.startswith('uint_t ') or i.startswith('smpl_t '))]
139
140def get_output_params(proto):
141 a = get_params(proto)
142 return [i for i in a if not i.startswith('const ')][1:]
131 143
132def get_params_types_names(proto):144def get_params_types_names(proto):
133 """ get the list of parameters from a function prototype145 """ get the list of parameters from a function prototype
134 example: proto = "int main (int argc, char ** argv)"146 example: proto = "int main (int argc, char ** argv)"
135 returns: [['int', 'argc'], ['char **','argv']]147 returns: [['int', 'argc'], ['char **','argv']]
136 """148 """
137 return list(map(split_type, get_params(proto)))149 a = list(map(split_type, get_params(proto)))
138 150 #print proto, a
151 #import sys; sys.exit(1)
152 return a
139 153
140class MappedObject(object):154class MappedObject(object):
141 155
@@ -148,10 +162,15 @@ class MappedObject(object): · 6 unmodified lines
148 self.del_proto = prototypes['del'][0]162 self.del_proto = prototypes['del'][0]
149 self.do_proto = prototypes['do'][0]163 self.do_proto = prototypes['do'][0]
150 self.input_params = get_params_types_names(self.new_proto)164 self.input_params = get_params_types_names(self.new_proto)
151 self.input_params_list = "; ".join(get_params(self.new_proto))165 self.input_params_list = "; ".join(get_input_params(self.new_proto))
152 self.outputs = get_params_types_names(self.do_proto)[2:]166 self.outputs = get_params_types_names(self.do_proto)[2:]
153 self.outputs_flat = get_params(self.do_proto)[2:]167 self.do_inputs = [get_params_types_names(self.do_proto)[1]]
154 self.output_results = ", ".join(self.outputs_flat)168 self.do_outputs = get_params_types_names(self.do_proto)[2:]
169 self.outputs_flat = get_output_params(self.do_proto)
170 self.output_results = "; ".join(self.outputs_flat)
171
172 print "input_params", map(split_type, get_input_params(self.do_proto))
173 print "output_params", map(split_type, get_output_params(self.do_proto))
155 174
156 def gen_code(self):175 def gen_code(self):
157 out = ""176 out = ""
@@ -177,17 +196,19 @@ typedef struct{{ · 19 unmodified lines
177 {longname} *o;196 {longname} *o;
178 // input parameters197 // input parameters
179 {input_params_list};198 {input_params_list};
199 // do input vectors
200 {do_inputs_list};
180 // output results201 // output results
181 {output_results};202 {output_results};
182}} Py_{shortname};203}} Py_{shortname};
183"""204"""
184 return out.format(**self.__dict__)205 return out.format(do_inputs_list = "; ".join(get_input_params(self.do_proto)), **self.__dict__)
185 206
186 def gen_doc(self):207 def gen_doc(self):
187 out = """208 out = """
188// TODO: add documentation209// TODO: add documentation
189static char Py_{shortname}_doc[] = \"undefined\";210static char Py_{shortname}_doc[] = \"undefined\";
190 """211"""
191 return out.format(**self.__dict__)212 return out.format(**self.__dict__)
192 213
193 def gen_new(self):214 def gen_new(self):
@@ -221,7 +242,7 @@ Py_{shortname}_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) · 27 unmodified lines
221""".format(**self.__dict__)242""".format(**self.__dict__)
222 params = self.input_params243 params = self.input_params
223 for p in params:244 for p in params:
224 out += self.check_valid(p) 245 out += self.check_valid(p)
225 out += """246 out += """
226 return (PyObject *)self;247 return (PyObject *)self;
227}248}
@@ -289,6 +310,9 @@ Py_{shortname}_init (Py_{shortname} * self, PyObject * args, PyObject * kwds) · 61 unmodified lines
289 out += """310 out += """
290 // TODO get internal params after actual object creation?311 // TODO get internal params after actual object creation?
291"""312"""
313 for input_param in self.do_inputs:
314 out += """
315 self->{0} = ({1})malloc(sizeof({2}));""".format(input_param['name'], input_param['type'], input_param['type'][:-1])
292 out += """316 out += """
293 // create outputs{output_create}317 // create outputs{output_create}
294""".format(output_create = output_create)318""".format(output_create = output_create)
@@ -317,6 +341,9 @@ static PyMemberDef Py_{shortname}_members[] = {{ · 22 unmodified lines
317static void341static void
318Py_{shortname}_del (Py_{shortname} * self, PyObject * unused)342Py_{shortname}_del (Py_{shortname} * self, PyObject * unused)
319{{""".format(**self.__dict__)343{{""".format(**self.__dict__)
344 for input_param in self.do_inputs:
345 out += """
346 free(self->{0[name]});""".format(input_param)
320 for o in self.outputs:347 for o in self.outputs:
321 name = o['name']348 name = o['name']
322 del_out = delfromtype_fn[o['type']]349 del_out = delfromtype_fn[o['type']]
@@ -331,39 +358,45 @@ Py_{shortname}_del (Py_{shortname} * self, PyObject * unused) · 8 unmodified lines
331 return out358 return out
332 359
333 def gen_do(self):360 def gen_do(self):
334 do_fn = get_name(self.do_proto)
335 input_param = get_params_types_names(self.do_proto)[1];
336 pytoaubio = pytoaubio_fn[input_param['type']]
337 output = self.outputs[0]361 output = self.outputs[0]
338 out = """362 out = """
339// do {shortname}363// do {shortname}
340static PyObject*364static PyObject*
341Py_{shortname}_do (Py_{shortname} * self, PyObject * args)365Py_{shortname}_do (Py_{shortname} * self, PyObject * args)
342{{366{{""".format(**self.__dict__)
343 PyObject * in_obj;367 input_params = self.do_inputs
344 {input_type} {input_name};368 output_params = self.do_outputs
345 369 #print input_params
346 if (!PyArg_ParseTuple (args, "O", &in_obj)) {{370 #print output_params
371 for input_param in input_params:
372 out += """
373 PyObject *py_{0};""".format(input_param['name'], input_param['type'])
374 refs = ", ".join(["&py_%s" % p['name'] for p in input_params])
375 pyparamtypes = "".join([pyargparse_chars[p['type']] for p in input_params])
376 out += """
377 if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{
347 return NULL;378 return NULL;
348 }}379 }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__)
349 {input_name} = {pytoaubio} (in_obj); 380 for p in input_params:
350 if ({input_name} == NULL) {{381 out += """
382 if (!{pytoaubio}(py_{0[name]}, self->{0[name]})) {{
351 return NULL;383 return NULL;
352 }}384 }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']])
385 do_fn = get_name(self.do_proto)
386 inputs = ", ".join(['self->'+p['name'] for p in input_params])
387 outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs])
388 out += """
353 389
354 {do_fn}(self->o, {input_name}, {outputs});390 {do_fn}(self->o, {inputs}, {outputs});
355 391
356 return (PyObject *) {aubiotonumpy} ({outputs});392 return (PyObject *) {aubiotonumpy} ({outputs});
357}}393}}
358"""394""".format(
359 return out.format(do_fn = do_fn,395 do_fn = do_fn,
360 shortname = self.prototypes['shortname'],396 aubiotonumpy = pyfromaubio_fn[output['type']],
361 input_name = input_param['name'],397 inputs = inputs, outputs = outputs,
362 input_type= input_param['type'],398 )
363 pytoaubio = pytoaubio,399 return out
364 outputs = ", ".join(["self->%s" % p['name'] for p in self.outputs]),
365 aubiotonumpy = pyfromaubio_fn[output['type']],
366 )
367 400
368 def gen_set(self):401 def gen_set(self):
369 out = """402 out = """

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