python-efl/scripts/converters.py

106 lines
2.4 KiB
Python

import logging
log = logging.getLogger("efl.eolian.conv")
import re
docstring_replacements = (
("@brief ", ""),
(re.compile(r"@ingroup .+", re.S), r""),
(re.compile(r"@see (.+)(?!@)"), r":see: \1"),
("@return ", ":return: "),
(re.compile(r"@(note|warning) "), r".. \1:: "),
(re.compile(r"@(p|c) (\w+)"), r"``\2``"),
(re.compile(r"@(b) (\w+)"), r"**\2**"),
(re.compile(r"@(i) (\w+)"), r"*\2*"),
("EINA_TRUE", "True"),
("EINA_FALSE", "False"),
("NULL", "None"),
)
complex_types = (
"Eina_List"
)
mapping_in = {
# c_type: pyx_type
"Eina_Bool": "bint",
"char *": "",
"Evas_Object *": "_Eo_Base",
"Eo *": "_Eo_Base",
}
conversions_in = {
# c_type: (conversion, c_param_convert)
"Eina_Bool": (None, None),
"char *": (
"if isinstance({0}, unicode): {0} = PyUnicode_AsUTF8String({0})",
"<{1}>if {0} is not None else NULL"
),
"Evas_Object *": (None, "{0}.obj"),
"Eo *": (None, "{0}.obj"),
}
mapping_out = {
# c_type: pyx_type
"Eina_Bool": "bint",
"char *": "unicode",
"Elm_Object_Item *": "_ObjectItem",
"Evas_Object *": "_Eo_Base",
"Eo *": "_Eo_Base",
}
conversions_out = {
# c_type: conversion
"Eina_Bool": "{0}",
"char *": '{0}.decode("utf-8")',
"Elm_Object_Item *": 'object_item_to_python({0})',
"Evas_Object *": 'object_from_instance({0})',
"Eo *": 'object_from_instance({0})',
}
def conv_cls_name(name):
s = name.split("_")
if len(s) > 1:
return s[0], "_".join(s[1:])
else:
return name, name
def remove_type_prefixes(ctype):
for t in "const ", "unsigned ", "short ":
ctype = ctype.replace(t, "")
return ctype
def convert_in_param(c_type, name):
c_type = conversions_in.get(c_type, c_type)
return c_type, name
def convert_out_param(c_type, name):
conv = conversions_out.get(c_type)
if not conv:
return c_type, name
name = conv.format(name)
return c_type, name
def conv_type_ret(tp):
tp = tp.return_type.c_type
if not tp:
log.error("ret type empty")
return
if tp in return_type_mapping:
c_t = remove_type_prefixes(tp)
py_t = return_type_mapping[c_t][0]
if py_t is not None:
py_ret_type = py_t
else:
log.warn("Unknown ret type")
c_ret_type = remove_type_prefixes(tp)
return py_ret_type, c_ret_type