Eolian: WIP

This commit is contained in:
Kai Huuhko 2014-09-22 12:09:49 +03:00
parent 99d5ccd87b
commit 9af2bbe472
2 changed files with 84 additions and 45 deletions

View File

@ -21,7 +21,7 @@ complex_types = (
"Eina_List"
)
param_type_mapping = {
mapping_in = {
# c_type: pyx_type
"Eina_Bool": "bint",
"char *": "",
@ -29,7 +29,7 @@ param_type_mapping = {
"Eo *": "_Eo_Base",
}
param_conversions = {
conversions_in = {
# c_type: (conversion, c_param_convert)
"Eina_Bool": (None, None),
"char *": (
@ -40,7 +40,7 @@ param_conversions = {
"Eo *": (None, "{0}.obj"),
}
return_type_mapping = {
mapping_out = {
# c_type: pyx_type
"Eina_Bool": "bint",
"char *": "unicode",
@ -49,9 +49,9 @@ return_type_mapping = {
"Eo *": "_Eo_Base",
}
return_conversions = {
conversions_out = {
# c_type: conversion
"Eina_Bool": None,
"Eina_Bool": "{0}",
"char *": '{0}.decode("utf-8")',
"Elm_Object_Item *": 'object_item_to_python({0})',
"Evas_Object *": 'object_from_instance({0})',
@ -74,12 +74,15 @@ def remove_type_prefixes(ctype):
def convert_in_param(c_type, name):
pass
c_type = conversions_in.get(c_type, c_type)
return c_type, name
def convert_out_param(c_type, name):
pass
conv = conversions_out.get(c_type)
if not conv:
return c_type, name
name = conv.format(name)
return c_type, name

View File

@ -101,6 +101,9 @@ class Generator(object):
class PyxGenerator(Generator):
RET_PARAM = "py_efl_ret"
def method_header_write(self, name, params, clsm=False):
params2 = []
if clsm:
@ -117,6 +120,8 @@ class PyxGenerator(Generator):
self.write(define)
def out_cdefs_write(self, out_cdefs):
if not out_cdefs:
return
self.write("cdef:")
self.indent()
types = {}
@ -134,9 +139,10 @@ class PyxGenerator(Generator):
self.dedent()
def c_call_write(self, c_name, c_params, ret_type=None, returns=None):
c_params = ", ".join(c_params)
c_call = "eo_do(self.obj, %s(%s))" % (c_name, c_params)
if ret_type:
c_call = self.RET_PARAM + " = " + c_call
c_call = "cdef " + ret_type + " " + self.RET_PARAM + " = " + "<{0}>".format(ret_type) + c_call
self.write(c_call)
@ -180,11 +186,13 @@ class Function(object):
def generate(self, gen):
func = self.func
py_name = None
c_name = self.c_name
header_params = []
c_call_params = []
return_params = []
conv_params = []
out_cdefs = []
@ -207,23 +215,19 @@ class Function(object):
name = p.name
if di == eolian.ParameterDir.IN:
#c_type = convert_in_param((c_type, name))
#c_type, name = convert_in_param(c_type, name)
header_params.append((c_type, name))
c_call_params.append(name)
elif di == eolian.ParameterDir.OUT:
out_cdefs.append((c_type, "&"+name))
#c_type = convert_out_param((c_type, name))
c_type, name = convert_out_param(c_type, name)
return_params.append((c_type, name))
elif di == eolian.ParameterDir.INOUT:
pass
gen.method_header_write(py_name, header_params)
gen.indent()
log.error("TODO: INOUT params")
ret_type = func.return_type_get(eolian.FunctionType.METHOD)
if ret_type.type != eolian.TypeType.UNKNOWN:
out_cdefs.append((ret_type.c_type, self.RET_PARAM))
if func.type == ftypes.PROP_SET or func.type == ftypes.PROPERTY and not self.isget:
if (func.type == ftypes.PROP_SET or func.type == ftypes.PROPERTY) and not self.isget:
py_name = "__set__"
c_name = "_".join((c_name, "set"))
@ -236,26 +240,18 @@ class Function(object):
c_type = p.type.c_type
name = p.name
#c_type, name = convert_in_param(c_type, name)
conv_params.append((c_type, name))
#c_params.append((c_type, name))
conv_params.append(name)
c_call_params.append(name)
assert conv_params, "params should not be empty for setter of: %s" % (func.name)
header_params = (("", "value"),)
gen.method_header_write(py_name, header_params)
gen.indent()
#params_conv = "%s = value" % (conv_params)
gen.c_call_write(c_name, c_call_params, ret_type, return_params)
ret_type = func.return_type_get(eolian.FunctionType.PROP_SET)
if ret_type.type != eolian.TypeType.UNKNOWN:
ret_type = ret_type.c_type
if func.type == ftypes.PROP_GET or func.type == ftypes.PROPERTY and self.isget:
if (func.type == ftypes.PROP_GET or func.type == ftypes.PROPERTY) and self.isget:
py_name = "__get__"
c_name = "_".join((c_name, "get"))
@ -264,23 +260,39 @@ class Function(object):
#assert p.direction == eolian.ParameterDir.OUT, "prop %s getter has param other than OUT" % (c_name)
out_cdefs.append((p.type.c_type, p.name))
c_call_params.append((p.type.c_type, "&%s" % (p.name)))
return_params.append((p.type.c_type, p.name))
c_type = p.type.c_type
name = p.name
gen.method_header_write(py_name, header_params)
gen.indent()
gen.c_call_write(c_name, c_call_params, ret_type, return_params)
out_cdefs.append((c_type, name))
c_call_params.append("&%s" % (name))
c_type, name = convert_out_param(c_type, name)
return_params.append((c_type, name))
ret_type = func.return_type_get(eolian.FunctionType.PROP_GET)
if ret_type.type != eolian.TypeType.UNKNOWN:
ret_type = ret_type.c_type
if not py_name:
return
gen.method_header_write(py_name, header_params)
gen.indent()
if conv_params:
conv_params = ", ".join(conv_params)
gen.write("%s = value" % (conv_params))
gen.out_cdefs_write(out_cdefs)
if ret_type.type != eolian.TypeType.UNKNOWN:
ret_type2 = ret_type.c_type
else:
ret_type2 = None
gen.c_call_write(c_name, c_call_params, ret_type2, return_params)
if return_params:
ret = return_params[:]
gen.write("return %s" % (", ".join([r[1] for r in ret])))
elif ret_type != eolian.TypeType.UNKNOWN:
elif ret_type.type != eolian.TypeType.UNKNOWN:
ret = self.RET_PARAM
gen.write("return %s" % (ret))
@ -438,8 +450,9 @@ class Class(object):
o = EoConstructor.parse(ctor, prefix)
except Exception as e:
log.warn(
"Skipping %s.%s because of an exception"
% (cls.name, ctor.full_name))
"Skipping %s.%s because of an exception",
cls.name, ctor.full_name
)
log.warn(e)
log.debug(format_exc())
continue
@ -453,19 +466,32 @@ class Class(object):
props = cls.functions_get(eolian.FunctionType.PROPERTY)
for prop in props:
if not prop.scope == eolian.ObjectScope.PUBLIC:
log.debug(
"Skipping non-public property %s.%s",
cls.name, prop.name
)
continue
function_counter["_".join((prefix, prop.name))] += 1
try:
o = Property(prop, prefix)
except Exception as e:
log.exception(
"Skipping %s.%s because of an exception"
% (cls.name, prop.name))
"Skipping %s.%s because of an exception",
cls.name, prop.name
)
continue
self.props.append(o)
generated_function_counter["_".join((prefix, prop.name))] += 1
methods = cls.functions_get(eolian.FunctionType.METHOD)
for method in methods:
if not method.scope == eolian.ObjectScope.PUBLIC:
log.debug(
"Skipping non-public method %s.%s",
cls.name, method.name
)
continue
function_counter["_".join((prefix, method.name))] += 1
try:
o = Function(method, prefix)
@ -602,8 +628,18 @@ with open(py_path, "a") as py_f:
pyxgen = PyxGenerator()
gencls.pyx_generate(pyxgen)
f_base = os.path.splitext(py_path)[0]
pxi_path = os.path.join(args.output, cls.name.lower() + ".pxi")
#f_base = os.path.splitext(py_path)[0]
path = [args.output]
namespaces = []
for ns in cls.namespaces:
namespaces.append(ns.lower())
filename = cls.name.lower() + ".pxi"
namespace = ".".join(namespaces)
filename = ".".join((namespace, filename))
path.append(filename)
pxi_path = os.path.join(*path)
o = pyxgen.printout()
if o:
with open(pxi_path, "w") as f: