2013-04-11 16:34:04 +02:00
|
|
|
#!/usr/bin/env python
|
2012-11-23 19:57:22 +01:00
|
|
|
|
2013-06-12 22:02:30 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
import hdr_parser, sys, re, os
|
2010-10-29 15:09:49 +02:00
|
|
|
from string import Template
|
|
|
|
|
2013-06-12 22:02:30 +02:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
from io import StringIO
|
|
|
|
else:
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
2013-04-09 11:10:54 +02:00
|
|
|
ignored_arg_types = ["RNG*"]
|
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
gen_template_check_self = Template(""" if(!PyObject_TypeCheck(self, &pyopencv_${name}_Type))
|
2010-11-02 18:58:22 +01:00
|
|
|
return failmsgp("Incorrect type of self (must be '${name}' or its derivative)");
|
2013-08-14 10:32:41 +02:00
|
|
|
$cname* _self_ = ${amp}((pyopencv_${name}_t*)self)->v${get};
|
2010-10-29 15:09:49 +02:00
|
|
|
""")
|
|
|
|
|
2012-09-11 14:47:25 +02:00
|
|
|
gen_template_check_self_algo = Template(""" if(!PyObject_TypeCheck(self, &pyopencv_${name}_Type))
|
|
|
|
return failmsgp("Incorrect type of self (must be '${name}' or its derivative)");
|
2013-08-14 10:32:41 +02:00
|
|
|
$cname* _self_ = dynamic_cast<$cname*>(${amp}((pyopencv_${name}_t*)self)->v.get());
|
2012-09-11 14:47:25 +02:00
|
|
|
""")
|
|
|
|
|
2013-08-14 10:32:41 +02:00
|
|
|
gen_template_call_constructor_prelude = Template("""self = PyObject_NEW(pyopencv_${name}_t, &pyopencv_${name}_Type);
|
2011-09-07 11:38:22 +02:00
|
|
|
new (&(self->v)) Ptr<$cname>(); // init Ptr with placement new
|
2013-08-14 10:32:41 +02:00
|
|
|
if(self) """)
|
2011-09-07 11:38:22 +02:00
|
|
|
|
2013-08-14 10:32:41 +02:00
|
|
|
gen_template_call_constructor = Template("""self->v.reset(new ${cname}${args})""")
|
|
|
|
|
|
|
|
gen_template_simple_call_constructor_prelude = Template("""self = PyObject_NEW(pyopencv_${name}_t, &pyopencv_${name}_Type);
|
|
|
|
if(self) """)
|
|
|
|
|
|
|
|
gen_template_simple_call_constructor = Template("""self->v = ${cname}${args}""")
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
gen_template_parse_args = Template("""const char* keywords[] = { $kw_list, NULL };
|
|
|
|
if( PyArg_ParseTupleAndKeywords(args, kw, "$fmtspec", (char**)keywords, $parse_arglist)$code_cvt )""")
|
|
|
|
|
|
|
|
gen_template_func_body = Template("""$code_decl
|
|
|
|
$code_parse
|
|
|
|
{
|
2013-08-14 10:32:41 +02:00
|
|
|
${code_prelude}ERRWRAP2($code_fcall);
|
2010-11-02 18:58:22 +01:00
|
|
|
$code_ret;
|
2010-10-29 15:09:49 +02:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
2013-06-12 22:02:30 +02:00
|
|
|
py_major_version = sys.version_info[0]
|
|
|
|
if py_major_version >= 3:
|
|
|
|
head_init_str = "PyVarObject_HEAD_INIT(&PyType_Type, 0)"
|
|
|
|
else:
|
|
|
|
head_init_str = """PyObject_HEAD_INIT(&PyType_Type)
|
|
|
|
0,"""
|
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
gen_template_simple_type_decl = Template("""
|
|
|
|
struct pyopencv_${name}_t
|
|
|
|
{
|
|
|
|
PyObject_HEAD
|
|
|
|
${cname} v;
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyTypeObject pyopencv_${name}_Type =
|
|
|
|
{
|
2013-06-12 22:02:30 +02:00
|
|
|
%s
|
2010-11-02 18:58:22 +01:00
|
|
|
MODULESTR".$wname",
|
|
|
|
sizeof(pyopencv_${name}_t),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void pyopencv_${name}_dealloc(PyObject* self)
|
|
|
|
{
|
|
|
|
PyObject_Del(self);
|
|
|
|
}
|
|
|
|
|
2013-04-12 20:35:46 +02:00
|
|
|
template<> PyObject* pyopencv_from(const ${cname}& r)
|
2010-11-02 18:58:22 +01:00
|
|
|
{
|
|
|
|
pyopencv_${name}_t *m = PyObject_NEW(pyopencv_${name}_t, &pyopencv_${name}_Type);
|
|
|
|
m->v = r;
|
|
|
|
return (PyObject*)m;
|
|
|
|
}
|
|
|
|
|
2013-04-12 20:35:46 +02:00
|
|
|
template<> bool pyopencv_to(PyObject* src, ${cname}& dst, const char* name)
|
2010-11-02 18:58:22 +01:00
|
|
|
{
|
2010-11-03 18:57:51 +01:00
|
|
|
if( src == NULL || src == Py_None )
|
2010-11-02 18:58:22 +01:00
|
|
|
return true;
|
|
|
|
if(!PyObject_TypeCheck(src, &pyopencv_${name}_Type))
|
2010-10-29 15:09:49 +02:00
|
|
|
{
|
2013-06-12 22:02:30 +02:00
|
|
|
failmsg("Expected ${cname} for argument '%%s'", name);
|
2010-11-02 18:58:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dst = ((pyopencv_${name}_t*)src)->v;
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-12 22:02:30 +02:00
|
|
|
""" % head_init_str)
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
gen_template_type_decl = Template("""
|
2010-10-29 15:09:49 +02:00
|
|
|
struct pyopencv_${name}_t
|
|
|
|
{
|
|
|
|
PyObject_HEAD
|
2012-09-11 14:47:25 +02:00
|
|
|
Ptr<${cname1}> v;
|
2010-10-29 15:09:49 +02:00
|
|
|
};
|
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
static PyTypeObject pyopencv_${name}_Type =
|
|
|
|
{
|
2013-06-12 22:02:30 +02:00
|
|
|
%s
|
2010-11-02 18:58:22 +01:00
|
|
|
MODULESTR".$wname",
|
|
|
|
sizeof(pyopencv_${name}_t),
|
|
|
|
};
|
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
static void pyopencv_${name}_dealloc(PyObject* self)
|
|
|
|
{
|
2012-09-11 14:47:25 +02:00
|
|
|
((pyopencv_${name}_t*)self)->v.release();
|
2010-10-29 15:09:49 +02:00
|
|
|
PyObject_Del(self);
|
|
|
|
}
|
2011-09-07 11:38:22 +02:00
|
|
|
|
2013-04-12 20:35:46 +02:00
|
|
|
template<> PyObject* pyopencv_from(const Ptr<${cname}>& r)
|
2011-09-07 11:38:22 +02:00
|
|
|
{
|
|
|
|
pyopencv_${name}_t *m = PyObject_NEW(pyopencv_${name}_t, &pyopencv_${name}_Type);
|
2012-09-11 14:47:25 +02:00
|
|
|
new (&(m->v)) Ptr<$cname1>(); // init Ptr with placement new
|
2011-09-07 11:38:22 +02:00
|
|
|
m->v = r;
|
|
|
|
return (PyObject*)m;
|
|
|
|
}
|
|
|
|
|
2013-04-12 20:35:46 +02:00
|
|
|
template<> bool pyopencv_to(PyObject* src, Ptr<${cname}>& dst, const char* name)
|
2011-09-07 11:38:22 +02:00
|
|
|
{
|
|
|
|
if( src == NULL || src == Py_None )
|
|
|
|
return true;
|
|
|
|
if(!PyObject_TypeCheck(src, &pyopencv_${name}_Type))
|
|
|
|
{
|
2013-06-12 22:02:30 +02:00
|
|
|
failmsg("Expected ${cname} for argument '%%s'", name);
|
2011-09-07 11:38:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-14 10:32:41 +02:00
|
|
|
dst = ((pyopencv_${name}_t*)src)->v.dynamicCast<${cname}>();
|
2011-09-07 11:38:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-12 22:02:30 +02:00
|
|
|
""" % head_init_str)
|
2010-11-02 18:58:22 +01:00
|
|
|
|
|
|
|
gen_template_map_type_cvt = Template("""
|
2013-04-12 20:35:46 +02:00
|
|
|
template<> bool pyopencv_to(PyObject* src, ${cname}& dst, const char* name);
|
2010-11-02 18:58:22 +01:00
|
|
|
""")
|
2010-10-29 15:09:49 +02:00
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
gen_template_set_prop_from_map = Template("""
|
|
|
|
if( PyMapping_HasKeyString(src, (char*)"$propname") )
|
|
|
|
{
|
|
|
|
tmp = PyMapping_GetItemString(src, (char*)"$propname");
|
|
|
|
ok = tmp && pyopencv_to(tmp, dst.$propname);
|
|
|
|
Py_DECREF(tmp);
|
|
|
|
if(!ok) return false;
|
|
|
|
}""")
|
|
|
|
|
|
|
|
gen_template_type_impl = Template("""
|
2010-10-29 15:09:49 +02:00
|
|
|
static PyObject* pyopencv_${name}_repr(PyObject* self)
|
|
|
|
{
|
|
|
|
char str[1000];
|
|
|
|
sprintf(str, "<$wname %p>", self);
|
|
|
|
return PyString_FromString(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
${getset_code}
|
|
|
|
|
|
|
|
static PyGetSetDef pyopencv_${name}_getseters[] =
|
|
|
|
{${getset_inits}
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
${methods_code}
|
|
|
|
|
|
|
|
static PyMethodDef pyopencv_${name}_methods[] =
|
|
|
|
{
|
|
|
|
${methods_inits}
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void pyopencv_${name}_specials(void)
|
|
|
|
{
|
|
|
|
pyopencv_${name}_Type.tp_base = ${baseptr};
|
|
|
|
pyopencv_${name}_Type.tp_dealloc = pyopencv_${name}_dealloc;
|
|
|
|
pyopencv_${name}_Type.tp_repr = pyopencv_${name}_repr;
|
|
|
|
pyopencv_${name}_Type.tp_getset = pyopencv_${name}_getseters;
|
|
|
|
pyopencv_${name}_Type.tp_methods = pyopencv_${name}_methods;${extra_specials}
|
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
gen_template_get_prop = Template("""
|
|
|
|
static PyObject* pyopencv_${name}_get_${member}(pyopencv_${name}_t* p, void *closure)
|
|
|
|
{
|
2010-11-02 18:58:22 +01:00
|
|
|
return pyopencv_from(p->v${access}${member});
|
2010-10-29 15:09:49 +02:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
2012-09-11 18:04:18 +02:00
|
|
|
gen_template_get_prop_algo = Template("""
|
|
|
|
static PyObject* pyopencv_${name}_get_${member}(pyopencv_${name}_t* p, void *closure)
|
|
|
|
{
|
2013-08-14 10:32:41 +02:00
|
|
|
return pyopencv_from(dynamic_cast<$cname*>(p->v.get())${access}${member});
|
2012-09-11 18:04:18 +02:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
gen_template_set_prop = Template("""
|
|
|
|
static int pyopencv_${name}_set_${member}(pyopencv_${name}_t* p, PyObject *value, void *closure)
|
|
|
|
{
|
|
|
|
if (value == NULL)
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Cannot delete the ${member} attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-11-02 18:58:22 +01:00
|
|
|
return pyopencv_to(value, p->v${access}${member}) ? 0 : -1;
|
2010-10-29 15:09:49 +02:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
2012-09-11 18:04:18 +02:00
|
|
|
gen_template_set_prop_algo = Template("""
|
|
|
|
static int pyopencv_${name}_set_${member}(pyopencv_${name}_t* p, PyObject *value, void *closure)
|
|
|
|
{
|
|
|
|
if (value == NULL)
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Cannot delete the ${member} attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-14 10:32:41 +02:00
|
|
|
return pyopencv_to(value, dynamic_cast<$cname*>(p->v.get())${access}${member}) ? 0 : -1;
|
2012-09-11 18:04:18 +02:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
gen_template_prop_init = Template("""
|
|
|
|
{(char*)"${member}", (getter)pyopencv_${name}_get_${member}, NULL, (char*)"${member}", NULL},""")
|
|
|
|
|
|
|
|
gen_template_rw_prop_init = Template("""
|
|
|
|
{(char*)"${member}", (getter)pyopencv_${name}_get_${member}, (setter)pyopencv_${name}_set_${member}, (char*)"${member}", NULL},""")
|
|
|
|
|
|
|
|
simple_argtype_mapping = {
|
2010-11-02 18:58:22 +01:00
|
|
|
"bool": ("bool", "b", "0"),
|
|
|
|
"int": ("int", "i", "0"),
|
|
|
|
"float": ("float", "f", "0.f"),
|
|
|
|
"double": ("double", "d", "0"),
|
2013-03-21 08:30:38 +01:00
|
|
|
"c_string": ("char*", "s", '(char*)""')
|
2010-10-29 15:09:49 +02:00
|
|
|
}
|
|
|
|
|
2011-07-14 01:04:39 +02:00
|
|
|
def normalize_class_name(name):
|
|
|
|
return re.sub(r"^cv\.", "", name).replace(".", "_")
|
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
class ClassProp(object):
|
|
|
|
def __init__(self, decl):
|
2010-11-02 18:58:22 +01:00
|
|
|
self.tp = decl[0].replace("*", "_ptr")
|
2010-10-29 15:09:49 +02:00
|
|
|
self.name = decl[1]
|
|
|
|
self.readonly = True
|
|
|
|
if "/RW" in decl[3]:
|
|
|
|
self.readonly = False
|
|
|
|
|
|
|
|
class ClassInfo(object):
|
|
|
|
def __init__(self, name, decl=None):
|
|
|
|
self.cname = name.replace(".", "::")
|
2011-07-14 01:04:39 +02:00
|
|
|
self.name = self.wname = normalize_class_name(name)
|
2010-10-29 15:09:49 +02:00
|
|
|
self.ismap = False
|
2010-11-02 18:58:22 +01:00
|
|
|
self.issimple = False
|
2012-09-11 14:47:25 +02:00
|
|
|
self.isalgorithm = False
|
2010-10-29 15:09:49 +02:00
|
|
|
self.methods = {}
|
|
|
|
self.props = []
|
|
|
|
self.consts = {}
|
2015-03-26 09:22:52 +01:00
|
|
|
self.base = None
|
2010-10-29 15:09:49 +02:00
|
|
|
customname = False
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
if decl:
|
2015-03-26 09:22:52 +01:00
|
|
|
bases = decl[1].split()[1:]
|
|
|
|
if len(bases) > 1:
|
2013-06-12 22:02:30 +02:00
|
|
|
print("Note: Class %s has more than 1 base class (not supported by Python C extensions)" % (self.name,))
|
2015-03-26 09:22:52 +01:00
|
|
|
print(" Bases: ", " ".join(bases))
|
2013-06-12 22:02:30 +02:00
|
|
|
print(" Only the first base class will be used")
|
2012-01-25 14:25:35 +01:00
|
|
|
#return sys.exit(-1)
|
2015-03-26 09:22:52 +01:00
|
|
|
elif len(bases) == 1:
|
|
|
|
self.base = bases[0].strip(",")
|
|
|
|
if self.base.startswith("cv::"):
|
|
|
|
self.base = self.base[4:]
|
|
|
|
if self.base == "Algorithm":
|
|
|
|
self.isalgorithm = True
|
|
|
|
self.base = self.base.replace("::", "_")
|
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
for m in decl[2]:
|
|
|
|
if m.startswith("="):
|
|
|
|
self.wname = m[1:]
|
|
|
|
customname = True
|
|
|
|
elif m == "/Map":
|
|
|
|
self.ismap = True
|
2010-11-02 18:58:22 +01:00
|
|
|
elif m == "/Simple":
|
|
|
|
self.issimple = True
|
2010-10-29 15:09:49 +02:00
|
|
|
self.props = [ClassProp(p) for p in decl[3]]
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
if not customname and self.wname.startswith("Cv"):
|
|
|
|
self.wname = self.wname[2:]
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
def gen_map_code(self, all_classes):
|
|
|
|
code = "static bool pyopencv_to(PyObject* src, %s& dst, const char* name)\n{\n PyObject* tmp;\n bool ok;\n" % (self.cname)
|
2010-10-29 15:09:49 +02:00
|
|
|
code += "".join([gen_template_set_prop_from_map.substitute(propname=p.name,proptype=p.tp) for p in self.props])
|
2015-03-26 09:22:52 +01:00
|
|
|
if self.base:
|
|
|
|
code += "\n return pyopencv_to(src, (%s&)dst, name);\n}\n" % all_classes[self.base].cname
|
2010-11-02 18:58:22 +01:00
|
|
|
else:
|
|
|
|
code += "\n return true;\n}\n"
|
2010-10-29 15:09:49 +02:00
|
|
|
return code
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
def gen_code(self, all_classes):
|
|
|
|
if self.ismap:
|
2010-11-02 18:58:22 +01:00
|
|
|
return self.gen_map_code(all_classes)
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2013-06-12 22:02:30 +02:00
|
|
|
getset_code = StringIO()
|
|
|
|
getset_inits = StringIO()
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
sorted_props = [(p.name, p) for p in self.props]
|
|
|
|
sorted_props.sort()
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
access_op = "->"
|
|
|
|
if self.issimple:
|
|
|
|
access_op = "."
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
for pname, p in sorted_props:
|
2012-09-11 18:04:18 +02:00
|
|
|
if self.isalgorithm:
|
|
|
|
getset_code.write(gen_template_get_prop_algo.substitute(name=self.name, cname=self.cname, member=pname, membertype=p.tp, access=access_op))
|
|
|
|
else:
|
|
|
|
getset_code.write(gen_template_get_prop.substitute(name=self.name, member=pname, membertype=p.tp, access=access_op))
|
2010-10-29 15:09:49 +02:00
|
|
|
if p.readonly:
|
2010-11-02 18:58:22 +01:00
|
|
|
getset_inits.write(gen_template_prop_init.substitute(name=self.name, member=pname))
|
2010-10-29 15:09:49 +02:00
|
|
|
else:
|
2012-09-11 18:04:18 +02:00
|
|
|
if self.isalgorithm:
|
|
|
|
getset_code.write(gen_template_set_prop_algo.substitute(name=self.name, cname=self.cname, member=pname, membertype=p.tp, access=access_op))
|
|
|
|
else:
|
|
|
|
getset_code.write(gen_template_set_prop.substitute(name=self.name, member=pname, membertype=p.tp, access=access_op))
|
2010-11-02 18:58:22 +01:00
|
|
|
getset_inits.write(gen_template_rw_prop_init.substitute(name=self.name, member=pname))
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2013-06-12 22:02:30 +02:00
|
|
|
methods_code = StringIO()
|
|
|
|
methods_inits = StringIO()
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2013-06-12 22:02:30 +02:00
|
|
|
sorted_methods = list(self.methods.items())
|
2010-10-29 15:09:49 +02:00
|
|
|
sorted_methods.sort()
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
for mname, m in sorted_methods:
|
2010-11-02 18:58:22 +01:00
|
|
|
methods_code.write(m.gen_code(all_classes))
|
|
|
|
methods_inits.write(m.get_tab_entry())
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
baseptr = "NULL"
|
2015-03-26 09:22:52 +01:00
|
|
|
if self.base and self.base in all_classes:
|
|
|
|
baseptr = "&pyopencv_" + all_classes[self.base].name + "_Type"
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
code = gen_template_type_impl.substitute(name=self.name, wname=self.wname, cname=self.cname,
|
|
|
|
getset_code=getset_code.getvalue(), getset_inits=getset_inits.getvalue(),
|
|
|
|
methods_code=methods_code.getvalue(), methods_inits=methods_inits.getvalue(),
|
2010-10-29 15:09:49 +02:00
|
|
|
baseptr=baseptr, extra_specials="")
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
return code
|
2012-05-31 13:04:11 +02:00
|
|
|
|
|
|
|
|
2014-05-12 12:58:54 +02:00
|
|
|
def handle_ptr(tp):
|
|
|
|
if tp.startswith('Ptr_'):
|
|
|
|
tp = 'Ptr<' + "::".join(tp.split('_')[1:]) + '>'
|
|
|
|
return tp
|
|
|
|
|
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
class ArgInfo(object):
|
|
|
|
def __init__(self, arg_tuple):
|
2014-05-12 12:58:54 +02:00
|
|
|
self.tp = handle_ptr(arg_tuple[0])
|
2010-10-29 15:09:49 +02:00
|
|
|
self.name = arg_tuple[1]
|
|
|
|
self.defval = arg_tuple[2]
|
|
|
|
self.isarray = False
|
|
|
|
self.arraylen = 0
|
|
|
|
self.arraycvt = None
|
|
|
|
self.inputarg = True
|
|
|
|
self.outputarg = False
|
2011-07-14 16:13:10 +02:00
|
|
|
self.returnarg = False
|
2010-10-29 15:09:49 +02:00
|
|
|
for m in arg_tuple[3]:
|
|
|
|
if m == "/O":
|
|
|
|
self.inputarg = False
|
|
|
|
self.outputarg = True
|
2011-07-14 16:13:10 +02:00
|
|
|
self.returnarg = True
|
2010-10-29 15:09:49 +02:00
|
|
|
elif m == "/IO":
|
|
|
|
self.inputarg = True
|
|
|
|
self.outputarg = True
|
2013-03-15 13:55:58 +01:00
|
|
|
self.returnarg = True
|
2010-10-29 15:09:49 +02:00
|
|
|
elif m.startswith("/A"):
|
|
|
|
self.isarray = True
|
|
|
|
self.arraylen = m[2:].strip()
|
|
|
|
elif m.startswith("/CA"):
|
|
|
|
self.isarray = True
|
|
|
|
self.arraycvt = m[2:].strip()
|
|
|
|
self.py_inputarg = False
|
|
|
|
self.py_outputarg = False
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
def isbig(self):
|
2010-11-02 18:58:22 +01:00
|
|
|
return self.tp == "Mat" or self.tp == "vector_Mat"# or self.tp.startswith("vector")
|
2010-10-29 15:09:49 +02:00
|
|
|
|
2012-08-07 20:03:17 +02:00
|
|
|
def crepr(self):
|
|
|
|
return "ArgInfo(\"%s\", %d)" % (self.name, self.outputarg)
|
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
class FuncVariant(object):
|
2010-11-02 18:58:22 +01:00
|
|
|
def __init__(self, classname, name, decl, isconstructor):
|
|
|
|
self.classname = classname
|
|
|
|
self.name = self.wname = name
|
2010-10-29 15:09:49 +02:00
|
|
|
self.isconstructor = isconstructor
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2015-05-06 14:21:23 +02:00
|
|
|
self.rettype = decl[4] if len(decl) >=5 else handle_ptr(decl[1])
|
2010-10-29 15:09:49 +02:00
|
|
|
if self.rettype == "void":
|
|
|
|
self.rettype = ""
|
|
|
|
self.args = []
|
|
|
|
self.array_counters = {}
|
|
|
|
for a in decl[3]:
|
|
|
|
ainfo = ArgInfo(a)
|
|
|
|
if ainfo.isarray and not ainfo.arraycvt:
|
|
|
|
c = ainfo.arraylen
|
|
|
|
c_arrlist = self.array_counters.get(c, [])
|
|
|
|
if c_arrlist:
|
|
|
|
c_arrlist.append(ainfo.name)
|
|
|
|
else:
|
|
|
|
self.array_counters[c] = [ainfo.name]
|
|
|
|
self.args.append(ainfo)
|
|
|
|
self.init_pyproto()
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
def init_pyproto(self):
|
|
|
|
# string representation of argument list, with '[', ']' symbols denoting optional arguments, e.g.
|
|
|
|
# "src1, src2[, dst[, mask]]" for cv.add
|
|
|
|
argstr = ""
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
# list of all input arguments of the Python function, with the argument numbers:
|
|
|
|
# [("src1", 0), ("src2", 1), ("dst", 2), ("mask", 3)]
|
|
|
|
# we keep an argument number to find the respective argument quickly, because
|
|
|
|
# some of the arguments of C function may not present in the Python function (such as array counters)
|
|
|
|
# or even go in a different order ("heavy" output parameters of the C function
|
|
|
|
# become the first optional input parameters of the Python function, and thus they are placed right after
|
2012-05-31 13:04:11 +02:00
|
|
|
# non-optional input parameters)
|
2010-10-29 15:09:49 +02:00
|
|
|
arglist = []
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
# the list of "heavy" output parameters. Heavy parameters are the parameters
|
|
|
|
# that can be expensive to allocate each time, such as vectors and matrices (see isbig).
|
|
|
|
outarr_list = []
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
# the list of output parameters. Also includes input/output parameters.
|
|
|
|
outlist = []
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
firstoptarg = 1000000
|
2010-10-29 15:09:49 +02:00
|
|
|
argno = -1
|
|
|
|
for a in self.args:
|
|
|
|
argno += 1
|
|
|
|
if a.name in self.array_counters:
|
|
|
|
continue
|
2013-04-09 11:10:54 +02:00
|
|
|
if a.tp in ignored_arg_types:
|
|
|
|
continue
|
2011-07-14 16:13:10 +02:00
|
|
|
if a.returnarg:
|
2010-10-29 15:09:49 +02:00
|
|
|
outlist.append((a.name, argno))
|
2013-03-15 13:55:58 +01:00
|
|
|
if (not a.inputarg) and a.isbig():
|
2011-08-14 21:46:39 +02:00
|
|
|
outarr_list.append((a.name, argno))
|
2011-08-31 14:36:19 +02:00
|
|
|
continue
|
2011-08-14 21:46:39 +02:00
|
|
|
if not a.inputarg:
|
2010-10-29 15:09:49 +02:00
|
|
|
continue
|
|
|
|
if not a.defval:
|
|
|
|
arglist.append((a.name, argno))
|
|
|
|
else:
|
2010-11-02 18:58:22 +01:00
|
|
|
firstoptarg = min(firstoptarg, len(arglist))
|
2010-10-29 15:09:49 +02:00
|
|
|
# if there are some array output parameters before the first default parameter, they
|
|
|
|
# are added as optional parameters before the first optional parameter
|
|
|
|
if outarr_list:
|
|
|
|
arglist += outarr_list
|
|
|
|
outarr_list = []
|
|
|
|
arglist.append((a.name, argno))
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
if outarr_list:
|
2010-11-02 18:58:22 +01:00
|
|
|
firstoptarg = min(firstoptarg, len(arglist))
|
2010-10-29 15:09:49 +02:00
|
|
|
arglist += outarr_list
|
2010-11-02 18:58:22 +01:00
|
|
|
firstoptarg = min(firstoptarg, len(arglist))
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
noptargs = len(arglist) - firstoptarg
|
|
|
|
argnamelist = [aname for aname, argno in arglist]
|
|
|
|
argstr = ", ".join(argnamelist[:firstoptarg])
|
|
|
|
argstr = "[, ".join([argstr] + argnamelist[firstoptarg:])
|
|
|
|
argstr += "]" * noptargs
|
|
|
|
if self.rettype:
|
|
|
|
outlist = [("retval", -1)] + outlist
|
|
|
|
elif self.isconstructor:
|
|
|
|
assert outlist == []
|
|
|
|
outlist = [("self", -1)]
|
2010-11-02 18:58:22 +01:00
|
|
|
if self.isconstructor:
|
|
|
|
classname = self.classname
|
|
|
|
if classname.startswith("Cv"):
|
|
|
|
classname=classname[2:]
|
|
|
|
outstr = "<%s object>" % (classname,)
|
|
|
|
elif outlist:
|
2010-10-29 15:09:49 +02:00
|
|
|
outstr = ", ".join([o[0] for o in outlist])
|
|
|
|
else:
|
|
|
|
outstr = "None"
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
self.py_docstring = "%s(%s) -> %s" % (self.wname, argstr, outstr)
|
2010-10-29 15:09:49 +02:00
|
|
|
self.py_noptargs = noptargs
|
|
|
|
self.py_arglist = arglist
|
|
|
|
for aname, argno in arglist:
|
|
|
|
self.args[argno].py_inputarg = True
|
|
|
|
for aname, argno in outlist:
|
|
|
|
if argno >= 0:
|
|
|
|
self.args[argno].py_outputarg = True
|
|
|
|
self.py_outlist = outlist
|
|
|
|
|
|
|
|
|
|
|
|
class FuncInfo(object):
|
2014-08-15 18:44:32 +02:00
|
|
|
def __init__(self, classname, name, cname, isconstructor, namespace):
|
2010-10-29 15:09:49 +02:00
|
|
|
self.classname = classname
|
|
|
|
self.name = name
|
|
|
|
self.cname = cname
|
|
|
|
self.isconstructor = isconstructor
|
2014-08-15 18:44:32 +02:00
|
|
|
self.namespace = namespace
|
2010-10-29 15:09:49 +02:00
|
|
|
self.variants = []
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
def add_variant(self, decl):
|
2010-11-02 18:58:22 +01:00
|
|
|
self.variants.append(FuncVariant(self.classname, self.name, decl, self.isconstructor))
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
def get_wrapper_name(self):
|
2010-11-02 18:58:22 +01:00
|
|
|
name = self.name
|
2010-10-29 15:09:49 +02:00
|
|
|
if self.classname:
|
2010-11-02 18:58:22 +01:00
|
|
|
classname = self.classname + "_"
|
|
|
|
if "[" in name:
|
|
|
|
name = "getelem"
|
2010-10-29 15:09:49 +02:00
|
|
|
else:
|
2010-11-02 18:58:22 +01:00
|
|
|
classname = ""
|
2014-08-21 15:31:01 +02:00
|
|
|
return "pyopencv_" + self.namespace.replace('.','_') + '_' + classname + name
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
def get_wrapper_prototype(self):
|
|
|
|
full_fname = self.get_wrapper_name()
|
|
|
|
if self.classname and not self.isconstructor:
|
|
|
|
self_arg = "self"
|
|
|
|
else:
|
|
|
|
self_arg = ""
|
|
|
|
return "static PyObject* %s(PyObject* %s, PyObject* args, PyObject* kw)" % (full_fname, self_arg)
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
def get_tab_entry(self):
|
|
|
|
docstring_list = []
|
|
|
|
have_empty_constructor = False
|
|
|
|
for v in self.variants:
|
|
|
|
s = v.py_docstring
|
|
|
|
if (not v.py_arglist) and self.isconstructor:
|
|
|
|
have_empty_constructor = True
|
|
|
|
if s not in docstring_list:
|
|
|
|
docstring_list.append(s)
|
|
|
|
# if there are just 2 constructors: default one and some other,
|
|
|
|
# we simplify the notation.
|
|
|
|
# Instead of ClassName(args ...) -> object or ClassName() -> object
|
|
|
|
# we write ClassName([args ...]) -> object
|
|
|
|
if have_empty_constructor and len(self.variants) == 2:
|
2010-11-02 18:58:22 +01:00
|
|
|
idx = self.variants[1].py_arglist != []
|
2011-06-30 00:06:42 +02:00
|
|
|
s = self.variants[idx].py_docstring
|
|
|
|
p1 = s.find("(")
|
|
|
|
p2 = s.rfind(")")
|
2011-07-14 01:04:39 +02:00
|
|
|
docstring_list = [s[:p1+1] + "[" + s[p1+1:p2] + "]" + s[p2:]]
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2013-06-10 23:29:36 +02:00
|
|
|
return Template(' {"$py_funcname", (PyCFunction)$wrap_funcname, METH_VARARGS | METH_KEYWORDS, "$py_docstring"},\n'
|
2010-11-02 18:58:22 +01:00
|
|
|
).substitute(py_funcname = self.variants[0].wname, wrap_funcname=self.get_wrapper_name(),
|
2010-10-29 15:09:49 +02:00
|
|
|
py_docstring = " or ".join(docstring_list))
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
def gen_code(self, all_classes):
|
|
|
|
proto = self.get_wrapper_prototype()
|
|
|
|
code = "%s\n{\n" % (proto,)
|
2014-08-15 18:44:32 +02:00
|
|
|
code += " using namespace %s;\n\n" % self.namespace.replace('.', '::')
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
selfinfo = ClassInfo("")
|
|
|
|
ismethod = self.classname != "" and not self.isconstructor
|
|
|
|
# full name is needed for error diagnostic in PyArg_ParseTupleAndKeywords
|
|
|
|
fullname = self.name
|
|
|
|
|
|
|
|
if self.classname:
|
|
|
|
selfinfo = all_classes[self.classname]
|
|
|
|
if not self.isconstructor:
|
2013-08-14 10:32:41 +02:00
|
|
|
amp = "&" if selfinfo.issimple else ""
|
2012-09-11 14:47:25 +02:00
|
|
|
if selfinfo.isalgorithm:
|
|
|
|
code += gen_template_check_self_algo.substitute(name=selfinfo.name, cname=selfinfo.cname, amp=amp)
|
|
|
|
else:
|
2013-08-14 10:32:41 +02:00
|
|
|
get = "" if selfinfo.issimple else ".get()"
|
|
|
|
code += gen_template_check_self.substitute(name=selfinfo.name, cname=selfinfo.cname, amp=amp, get=get)
|
2010-10-29 15:09:49 +02:00
|
|
|
fullname = selfinfo.wname + "." + fullname
|
|
|
|
|
|
|
|
all_code_variants = []
|
|
|
|
declno = -1
|
|
|
|
for v in self.variants:
|
|
|
|
code_decl = ""
|
|
|
|
code_ret = ""
|
|
|
|
code_cvt_list = []
|
|
|
|
|
2013-08-14 10:32:41 +02:00
|
|
|
code_args = "("
|
2010-10-29 15:09:49 +02:00
|
|
|
all_cargs = []
|
|
|
|
parse_arglist = []
|
|
|
|
|
|
|
|
# declare all the C function arguments,
|
|
|
|
# add necessary conversions from Python objects to code_cvt_list,
|
|
|
|
# form the function/method call,
|
|
|
|
# for the list of type mappings
|
|
|
|
for a in v.args:
|
2013-04-09 11:10:54 +02:00
|
|
|
if a.tp in ignored_arg_types:
|
|
|
|
defval = a.defval
|
|
|
|
if not defval and a.tp.endswith("*"):
|
|
|
|
defval = 0
|
|
|
|
assert defval
|
2013-08-14 10:32:41 +02:00
|
|
|
if not code_args.endswith("("):
|
|
|
|
code_args += ", "
|
|
|
|
code_args += defval
|
2013-04-09 11:10:54 +02:00
|
|
|
all_cargs.append([[None, ""], ""])
|
|
|
|
continue
|
2010-10-29 15:09:49 +02:00
|
|
|
tp1 = tp = a.tp
|
|
|
|
amp = ""
|
|
|
|
defval0 = ""
|
|
|
|
if tp.endswith("*"):
|
|
|
|
tp = tp1 = tp[:-1]
|
|
|
|
amp = "&"
|
|
|
|
if tp.endswith("*"):
|
|
|
|
defval0 = "0"
|
|
|
|
tp1 = tp.replace("*", "_ptr")
|
|
|
|
if tp1.endswith("*"):
|
2013-06-12 22:02:30 +02:00
|
|
|
print("Error: type with star: a.tp=%s, tp=%s, tp1=%s" % (a.tp, tp, tp1))
|
2010-10-29 15:09:49 +02:00
|
|
|
sys.exit(-1)
|
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
amapping = simple_argtype_mapping.get(tp, (tp, "O", defval0))
|
2010-11-26 00:16:32 +01:00
|
|
|
parse_name = a.name
|
2010-10-29 15:09:49 +02:00
|
|
|
if a.py_inputarg:
|
|
|
|
if amapping[1] == "O":
|
|
|
|
code_decl += " PyObject* pyobj_%s = NULL;\n" % (a.name,)
|
2010-11-26 00:16:32 +01:00
|
|
|
parse_name = "pyobj_" + a.name
|
2013-04-12 15:39:16 +02:00
|
|
|
if a.tp == 'char':
|
|
|
|
code_cvt_list.append("convert_to_char(pyobj_%s, &%s, %s)"% (a.name, a.name, a.crepr()))
|
|
|
|
else:
|
|
|
|
code_cvt_list.append("pyopencv_to(pyobj_%s, %s, %s)" % (a.name, a.name, a.crepr()))
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-11-26 00:16:32 +01:00
|
|
|
all_cargs.append([amapping, parse_name])
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
defval = a.defval
|
|
|
|
if not defval:
|
2010-11-02 18:58:22 +01:00
|
|
|
defval = amapping[2]
|
2010-10-29 15:09:49 +02:00
|
|
|
# "tp arg = tp();" is equivalent to "tp arg;" in the case of complex types
|
|
|
|
if defval == tp + "()" and amapping[1] == "O":
|
|
|
|
defval = ""
|
2010-11-02 18:58:22 +01:00
|
|
|
if a.outputarg and not a.inputarg:
|
|
|
|
defval = ""
|
2010-10-29 15:09:49 +02:00
|
|
|
if defval:
|
|
|
|
code_decl += " %s %s=%s;\n" % (amapping[0], a.name, defval)
|
|
|
|
else:
|
|
|
|
code_decl += " %s %s;\n" % (amapping[0], a.name)
|
|
|
|
|
2013-08-14 10:32:41 +02:00
|
|
|
if not code_args.endswith("("):
|
|
|
|
code_args += ", "
|
|
|
|
code_args += amp + a.name
|
|
|
|
|
|
|
|
code_args += ")"
|
|
|
|
|
|
|
|
if self.isconstructor:
|
|
|
|
code_decl += " pyopencv_%s_t* self = 0;\n" % selfinfo.name
|
|
|
|
if selfinfo.issimple:
|
|
|
|
templ_prelude = gen_template_simple_call_constructor_prelude
|
|
|
|
templ = gen_template_simple_call_constructor
|
|
|
|
else:
|
|
|
|
templ_prelude = gen_template_call_constructor_prelude
|
|
|
|
templ = gen_template_call_constructor
|
2010-10-29 15:09:49 +02:00
|
|
|
|
2013-08-14 10:32:41 +02:00
|
|
|
code_prelude = templ_prelude.substitute(name=selfinfo.name, cname=selfinfo.cname)
|
|
|
|
code_fcall = templ.substitute(name=selfinfo.name, cname=selfinfo.cname, args=code_args)
|
|
|
|
else:
|
|
|
|
code_prelude = ""
|
|
|
|
code_fcall = ""
|
|
|
|
if v.rettype:
|
|
|
|
code_decl += " " + v.rettype + " retval;\n"
|
|
|
|
code_fcall += "retval = "
|
|
|
|
if ismethod:
|
|
|
|
code_fcall += "_self_->" + self.cname
|
|
|
|
else:
|
|
|
|
code_fcall += self.cname
|
|
|
|
code_fcall += code_args
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
if code_cvt_list:
|
|
|
|
code_cvt_list = [""] + code_cvt_list
|
|
|
|
|
|
|
|
# add info about return value, if any, to all_cargs. if there non-void return value,
|
|
|
|
# it is encoded in v.py_outlist as ("retval", -1) pair.
|
|
|
|
# As [-1] in Python accesses the last element of a list, we automatically handle the return value by
|
|
|
|
# adding the necessary info to the end of all_cargs list.
|
|
|
|
if v.rettype:
|
|
|
|
tp = v.rettype
|
|
|
|
tp1 = tp.replace("*", "_ptr")
|
2010-11-02 18:58:22 +01:00
|
|
|
amapping = simple_argtype_mapping.get(tp, (tp, "O", "0"))
|
2010-10-29 15:09:49 +02:00
|
|
|
all_cargs.append(amapping)
|
|
|
|
|
2011-08-10 12:28:01 +02:00
|
|
|
if v.args and v.py_arglist:
|
2010-10-29 15:09:49 +02:00
|
|
|
# form the format spec for PyArg_ParseTupleAndKeywords
|
2010-11-26 00:16:32 +01:00
|
|
|
fmtspec = "".join([all_cargs[argno][0][1] for aname, argno in v.py_arglist])
|
2010-10-29 15:09:49 +02:00
|
|
|
if v.py_noptargs > 0:
|
|
|
|
fmtspec = fmtspec[:-v.py_noptargs] + "|" + fmtspec[-v.py_noptargs:]
|
|
|
|
fmtspec += ":" + fullname
|
|
|
|
|
|
|
|
# form the argument parse code that:
|
|
|
|
# - declares the list of keyword parameters
|
|
|
|
# - calls PyArg_ParseTupleAndKeywords
|
|
|
|
# - converts complex arguments from PyObject's to native OpenCV types
|
|
|
|
code_parse = gen_template_parse_args.substitute(
|
|
|
|
kw_list = ", ".join(['"' + aname + '"' for aname, argno in v.py_arglist]),
|
|
|
|
fmtspec = fmtspec,
|
2010-11-26 00:16:32 +01:00
|
|
|
parse_arglist = ", ".join(["&" + all_cargs[argno][1] for aname, argno in v.py_arglist]),
|
2010-10-29 15:09:49 +02:00
|
|
|
code_cvt = " &&\n ".join(code_cvt_list))
|
|
|
|
else:
|
2011-05-16 12:52:20 +02:00
|
|
|
code_parse = "if(PyObject_Size(args) == 0 && (kw == NULL || PyObject_Size(kw) == 0))"
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
if len(v.py_outlist) == 0:
|
2010-11-02 18:58:22 +01:00
|
|
|
code_ret = "Py_RETURN_NONE"
|
2010-10-29 15:09:49 +02:00
|
|
|
elif len(v.py_outlist) == 1:
|
|
|
|
if self.isconstructor:
|
2010-11-02 18:58:22 +01:00
|
|
|
code_ret = "return (PyObject*)self"
|
2010-10-29 15:09:49 +02:00
|
|
|
else:
|
|
|
|
aname, argno = v.py_outlist[0]
|
2010-11-02 18:58:22 +01:00
|
|
|
code_ret = "return pyopencv_from(%s)" % (aname,)
|
2010-10-29 15:09:49 +02:00
|
|
|
else:
|
|
|
|
# ther is more than 1 return parameter; form the tuple out of them
|
|
|
|
fmtspec = "N"*len(v.py_outlist)
|
|
|
|
backcvt_arg_list = []
|
|
|
|
for aname, argno in v.py_outlist:
|
2010-11-26 00:16:32 +01:00
|
|
|
amapping = all_cargs[argno][0]
|
2010-10-29 15:09:49 +02:00
|
|
|
backcvt_arg_list.append("%s(%s)" % (amapping[2], aname))
|
2010-11-02 18:58:22 +01:00
|
|
|
code_ret = "return Py_BuildValue(\"(%s)\", %s)" % \
|
2012-05-31 13:04:11 +02:00
|
|
|
(fmtspec, ", ".join(["pyopencv_from(" + aname + ")" for aname, argno in v.py_outlist]))
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
all_code_variants.append(gen_template_func_body.substitute(code_decl=code_decl,
|
2013-08-14 10:32:41 +02:00
|
|
|
code_parse=code_parse, code_prelude=code_prelude, code_fcall=code_fcall, code_ret=code_ret))
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
if len(all_code_variants)==1:
|
|
|
|
# if the function/method has only 1 signature, then just put it
|
|
|
|
code += all_code_variants[0]
|
|
|
|
else:
|
|
|
|
# try to execute each signature
|
|
|
|
code += " PyErr_Clear();\n\n".join([" {\n" + v + " }\n" for v in all_code_variants])
|
|
|
|
code += "\n return NULL;\n}\n\n"
|
2012-05-31 13:04:11 +02:00
|
|
|
return code
|
|
|
|
|
|
|
|
|
2014-08-18 14:52:01 +02:00
|
|
|
class Namespace(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.funcs = {}
|
|
|
|
self.consts = {}
|
|
|
|
|
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
class PythonWrapperGenerator(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.clear()
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
def clear(self):
|
|
|
|
self.classes = {}
|
2014-08-18 14:52:01 +02:00
|
|
|
self.namespaces = {}
|
2010-10-29 15:09:49 +02:00
|
|
|
self.consts = {}
|
2014-05-12 12:58:54 +02:00
|
|
|
self.code_include = StringIO()
|
2013-06-12 22:02:30 +02:00
|
|
|
self.code_types = StringIO()
|
|
|
|
self.code_funcs = StringIO()
|
|
|
|
self.code_type_reg = StringIO()
|
2014-08-14 17:01:49 +02:00
|
|
|
self.code_ns_reg = StringIO()
|
2011-06-10 15:22:40 +02:00
|
|
|
self.class_idx = 0
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
def add_class(self, stype, name, decl):
|
|
|
|
classinfo = ClassInfo(name, decl)
|
2011-06-10 15:22:40 +02:00
|
|
|
classinfo.decl_idx = self.class_idx
|
|
|
|
self.class_idx += 1
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2013-06-12 22:02:30 +02:00
|
|
|
if classinfo.name in self.classes:
|
|
|
|
print("Generator error: class %s (cname=%s) already exists" \
|
|
|
|
% (classinfo.name, classinfo.cname))
|
2012-05-31 13:04:11 +02:00
|
|
|
sys.exit(-1)
|
2010-10-29 15:09:49 +02:00
|
|
|
self.classes[classinfo.name] = classinfo
|
2014-08-21 12:51:03 +02:00
|
|
|
|
2015-03-26 09:22:52 +01:00
|
|
|
if classinfo.base:
|
|
|
|
chunks = classinfo.base.split('_')
|
2014-08-21 12:51:03 +02:00
|
|
|
base = '_'.join(chunks)
|
|
|
|
while base not in self.classes and len(chunks)>1:
|
|
|
|
del chunks[-2]
|
|
|
|
base = '_'.join(chunks)
|
|
|
|
if base not in self.classes:
|
2014-08-21 15:31:01 +02:00
|
|
|
print("Generator error: unable to resolve base %s for %s"
|
2015-03-26 09:22:52 +01:00
|
|
|
% (classinfo.base, classinfo.name))
|
2014-08-21 12:51:03 +02:00
|
|
|
sys.exit(-1)
|
2015-03-26 09:22:52 +01:00
|
|
|
classinfo.base = base
|
2014-08-21 12:51:03 +02:00
|
|
|
classinfo.isalgorithm |= self.classes[base].isalgorithm
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2014-08-19 11:35:46 +02:00
|
|
|
def split_decl_name(self, name):
|
|
|
|
chunks = name.split('.')
|
|
|
|
namespace = chunks[:-1]
|
|
|
|
classes = []
|
|
|
|
while namespace and '.'.join(namespace) not in self.parser.namespaces:
|
|
|
|
classes.insert(0, namespace.pop())
|
|
|
|
return namespace, classes, chunks[-1]
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2014-08-19 11:35:46 +02:00
|
|
|
|
|
|
|
def add_const(self, name, decl):
|
|
|
|
cname = name.replace('.','::')
|
|
|
|
namespace, classes, name = self.split_decl_name(name)
|
|
|
|
namespace = '.'.join(namespace)
|
|
|
|
name = '_'.join(classes+[name])
|
|
|
|
ns = self.namespaces.setdefault(namespace, Namespace())
|
|
|
|
if name in ns.consts:
|
2013-06-12 22:02:30 +02:00
|
|
|
print("Generator error: constant %s (cname=%s) already exists" \
|
2014-08-19 11:35:46 +02:00
|
|
|
% (name, cname))
|
2012-05-31 13:04:11 +02:00
|
|
|
sys.exit(-1)
|
2014-08-19 11:35:46 +02:00
|
|
|
ns.consts[name] = cname
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
def add_func(self, decl):
|
2014-08-19 11:35:46 +02:00
|
|
|
namespace, classes, barename = self.split_decl_name(decl[0])
|
|
|
|
cname = "::".join(namespace+classes+[barename])
|
|
|
|
name = barename
|
2014-08-18 14:15:30 +02:00
|
|
|
classname = ''
|
|
|
|
bareclassname = ''
|
|
|
|
if classes:
|
|
|
|
classname = normalize_class_name('.'.join(namespace+classes))
|
|
|
|
bareclassname = classes[-1]
|
|
|
|
namespace = '.'.join(namespace)
|
|
|
|
|
2014-08-14 17:01:49 +02:00
|
|
|
isconstructor = name == bareclassname
|
2010-10-29 15:09:49 +02:00
|
|
|
isclassmethod = False
|
|
|
|
for m in decl[2]:
|
|
|
|
if m == "/S":
|
|
|
|
isclassmethod = True
|
|
|
|
elif m.startswith("="):
|
|
|
|
name = m[1:]
|
2014-08-14 17:01:49 +02:00
|
|
|
if isclassmethod:
|
2014-08-18 14:15:30 +02:00
|
|
|
name = "_".join(classes+[name])
|
2014-08-15 18:44:32 +02:00
|
|
|
classname = ''
|
2014-08-18 14:15:30 +02:00
|
|
|
elif isconstructor:
|
|
|
|
name = "_".join(classes[:-1]+[name])
|
2014-08-14 17:01:49 +02:00
|
|
|
|
2014-08-15 18:44:32 +02:00
|
|
|
if classname and not isconstructor:
|
2014-08-19 11:35:46 +02:00
|
|
|
cname = barename
|
2014-08-15 18:44:32 +02:00
|
|
|
func_map = self.classes[classname].methods
|
2010-10-29 15:09:49 +02:00
|
|
|
else:
|
2014-08-18 14:52:01 +02:00
|
|
|
func_map = self.namespaces.setdefault(namespace, Namespace()).funcs
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2014-08-15 18:44:32 +02:00
|
|
|
func = func_map.setdefault(name, FuncInfo(classname, name, cname, isconstructor, namespace))
|
2010-10-29 15:09:49 +02:00
|
|
|
func.add_variant(decl)
|
2012-05-31 13:04:11 +02:00
|
|
|
|
|
|
|
|
2014-08-14 17:01:49 +02:00
|
|
|
def gen_namespace(self, ns_name):
|
2014-08-19 11:35:46 +02:00
|
|
|
ns = self.namespaces[ns_name]
|
2014-08-14 17:01:49 +02:00
|
|
|
wname = normalize_class_name(ns_name)
|
2014-08-19 11:35:46 +02:00
|
|
|
|
2014-08-14 17:01:49 +02:00
|
|
|
self.code_ns_reg.write('static PyMethodDef methods_%s[] = {\n'%wname)
|
2014-08-19 11:35:46 +02:00
|
|
|
for name, func in sorted(ns.funcs.items()):
|
2014-08-14 17:01:49 +02:00
|
|
|
self.code_ns_reg.write(func.get_tab_entry())
|
|
|
|
self.code_ns_reg.write(' {NULL, NULL}\n};\n\n')
|
|
|
|
|
2014-08-19 11:35:46 +02:00
|
|
|
self.code_ns_reg.write('static ConstDef consts_%s[] = {\n'%wname)
|
|
|
|
for name, cname in sorted(ns.consts.items()):
|
2014-08-19 14:57:37 +02:00
|
|
|
self.code_ns_reg.write(' {"%s", %s},\n'%(name, cname))
|
|
|
|
compat_name = re.sub(r"([a-z])([A-Z])", r"\1_\2", name).upper()
|
|
|
|
if name != compat_name:
|
|
|
|
self.code_ns_reg.write(' {"%s", %s},\n'%(compat_name, cname))
|
|
|
|
self.code_ns_reg.write(' {NULL, 0}\n};\n\n')
|
2014-08-19 11:35:46 +02:00
|
|
|
|
2014-08-15 18:44:32 +02:00
|
|
|
def gen_namespaces_reg(self):
|
|
|
|
self.code_ns_reg.write('static void init_submodules(PyObject * root) \n{\n')
|
2014-08-18 14:52:01 +02:00
|
|
|
for ns_name in sorted(self.namespaces):
|
2014-08-19 11:35:46 +02:00
|
|
|
if ns_name.split('.')[0] == 'cv':
|
|
|
|
wname = normalize_class_name(ns_name)
|
|
|
|
self.code_ns_reg.write(' init_submodule(root, MODULESTR"%s", methods_%s, consts_%s);\n' % (ns_name[2:], wname, wname))
|
2014-08-15 18:44:32 +02:00
|
|
|
self.code_ns_reg.write('};\n')
|
|
|
|
|
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
def save(self, path, name, buf):
|
|
|
|
f = open(path + "/" + name, "wt")
|
|
|
|
f.write(buf.getvalue())
|
|
|
|
f.close()
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
def gen(self, srcfiles, output_path):
|
2010-10-29 15:09:49 +02:00
|
|
|
self.clear()
|
2014-08-19 11:35:46 +02:00
|
|
|
self.parser = hdr_parser.CppHeaderParser()
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-11-02 18:58:22 +01:00
|
|
|
# step 1: scan the headers and build more descriptive maps of classes, consts, functions
|
|
|
|
for hdr in srcfiles:
|
2014-08-19 11:35:46 +02:00
|
|
|
decls = self.parser.parse(hdr)
|
2014-07-22 10:36:36 +02:00
|
|
|
if len(decls) == 0:
|
|
|
|
continue
|
2015-01-02 23:41:44 +01:00
|
|
|
self.code_include.write( '#include "{0}"\n'.format(hdr[hdr.rindex('opencv2/'):]) )
|
2010-11-02 18:58:22 +01:00
|
|
|
for decl in decls:
|
|
|
|
name = decl[0]
|
|
|
|
if name.startswith("struct") or name.startswith("class"):
|
|
|
|
# class/struct
|
|
|
|
p = name.find(" ")
|
|
|
|
stype = name[:p]
|
|
|
|
name = name[p+1:].strip()
|
|
|
|
self.add_class(stype, name, decl)
|
|
|
|
elif name.startswith("const"):
|
|
|
|
# constant
|
|
|
|
self.add_const(name.replace("const ", "").strip(), decl)
|
|
|
|
else:
|
|
|
|
# function
|
|
|
|
self.add_func(decl)
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
# step 2: generate code for the classes and their methods
|
2013-06-12 22:02:30 +02:00
|
|
|
classlist = list(self.classes.items())
|
2010-10-29 15:09:49 +02:00
|
|
|
classlist.sort()
|
2010-11-02 18:58:22 +01:00
|
|
|
for name, classinfo in classlist:
|
|
|
|
if classinfo.ismap:
|
|
|
|
self.code_types.write(gen_template_map_type_cvt.substitute(name=name, cname=classinfo.cname))
|
|
|
|
else:
|
|
|
|
if classinfo.issimple:
|
|
|
|
templ = gen_template_simple_type_decl
|
|
|
|
else:
|
|
|
|
templ = gen_template_type_decl
|
2012-09-11 14:47:25 +02:00
|
|
|
self.code_types.write(templ.substitute(name=name, wname=classinfo.wname, cname=classinfo.cname,
|
|
|
|
cname1=("cv::Algorithm" if classinfo.isalgorithm else classinfo.cname)))
|
2012-10-17 01:18:30 +02:00
|
|
|
|
2011-06-10 15:22:40 +02:00
|
|
|
# register classes in the same order as they have been declared.
|
|
|
|
# this way, base classes will be registered in Python before their derivatives.
|
|
|
|
classlist1 = [(classinfo.decl_idx, name, classinfo) for name, classinfo in classlist]
|
|
|
|
classlist1.sort()
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2011-06-10 15:22:40 +02:00
|
|
|
for decl_idx, name, classinfo in classlist1:
|
2010-10-29 15:09:49 +02:00
|
|
|
code = classinfo.gen_code(self.classes)
|
|
|
|
self.code_types.write(code)
|
|
|
|
if not classinfo.ismap:
|
|
|
|
self.code_type_reg.write("MKTYPE2(%s);\n" % (classinfo.name,) )
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
# step 3: generate the code for all the global functions
|
2014-08-18 14:52:01 +02:00
|
|
|
for ns_name, ns in sorted(self.namespaces.items()):
|
2014-08-19 11:35:46 +02:00
|
|
|
if ns_name.split('.')[0] != 'cv':
|
|
|
|
continue
|
2014-08-18 14:52:01 +02:00
|
|
|
for name, func in sorted(ns.funcs.items()):
|
2014-08-15 18:44:32 +02:00
|
|
|
code = func.gen_code(self.classes)
|
|
|
|
self.code_funcs.write(code)
|
2014-08-18 14:52:01 +02:00
|
|
|
self.gen_namespace(ns_name)
|
2014-08-15 18:44:32 +02:00
|
|
|
self.gen_namespaces_reg()
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
# step 4: generate the code for constants
|
2013-06-12 22:02:30 +02:00
|
|
|
constlist = list(self.consts.items())
|
2010-10-29 15:09:49 +02:00
|
|
|
constlist.sort()
|
|
|
|
for name, constinfo in constlist:
|
|
|
|
self.gen_const_reg(constinfo)
|
2012-05-31 13:04:11 +02:00
|
|
|
|
2010-10-29 15:09:49 +02:00
|
|
|
# That's it. Now save all the files
|
2014-05-12 12:58:54 +02:00
|
|
|
self.save(output_path, "pyopencv_generated_include.h", self.code_include)
|
2010-10-29 15:09:49 +02:00
|
|
|
self.save(output_path, "pyopencv_generated_funcs.h", self.code_funcs)
|
|
|
|
self.save(output_path, "pyopencv_generated_types.h", self.code_types)
|
|
|
|
self.save(output_path, "pyopencv_generated_type_reg.h", self.code_type_reg)
|
2014-08-14 17:01:49 +02:00
|
|
|
self.save(output_path, "pyopencv_generated_ns_reg.h", self.code_ns_reg)
|
2010-10-29 15:09:49 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2010-11-02 18:58:22 +01:00
|
|
|
srcfiles = hdr_parser.opencv_hdr_list
|
|
|
|
dstdir = "/Users/vp/tmp"
|
2011-05-17 14:52:07 +02:00
|
|
|
if len(sys.argv) > 1:
|
2010-11-02 18:58:22 +01:00
|
|
|
dstdir = sys.argv[1]
|
2011-05-17 14:52:07 +02:00
|
|
|
if len(sys.argv) > 2:
|
2014-07-27 14:35:35 +02:00
|
|
|
srcfiles = open(sys.argv[2], 'r').read().split(';')
|
2010-11-02 18:58:22 +01:00
|
|
|
generator = PythonWrapperGenerator()
|
|
|
|
generator.gen(srcfiles, dstdir)
|