Define methods to get string representation of enum values in the Python wrapper.

This commit is contained in:
Ghislain MARY 2014-08-04 16:24:32 +02:00
parent c420236597
commit 123e4ccdfe
2 changed files with 30 additions and 4 deletions

View file

@ -181,16 +181,42 @@ static PyTypeObject pylinphone_{{class_name}}Type = {
static PyMethodDef pylinphone_ModuleMethods[] = {
/* Sentinel */
{ "set_log_handler", pylinphone_module_method_set_log_handler, METH_VARARGS, "" },
/* Sentinel */
{ NULL, NULL, 0, NULL }
};
static PyMethodDef pylinphone_NoMethods[] = {
{{#enums}}
static PyObject * pylinphone_{{enum_name}}_module_method_string(PyObject *self, PyObject *args) {
const char *value_str = "[invalid]";
int value;
PyObject *pyret;
if (!PyArg_ParseTuple(args, "i", &value)) {
return NULL;
}
pylinphone_trace(1, "[PYLINPHONE] >>> %s(%d)", __FUNCTION__, value);
switch (value) {
{{#enum_values}}
case {{enum_value_cname}}:
value_str = "{{enum_value_name}}";
break;
{{/enum_values}}
default:
break;
}
pyret = Py_BuildValue("z", value_str);
pylinphone_trace(-1, "[PYLINPHONE] <<< %s -> %p", __FUNCTION__, pyret);
return pyret;
}
static PyMethodDef pylinphone_{{enum_name}}_ModuleMethods[] = {
{ "string", pylinphone_{{enum_name}}_module_method_string, METH_VARARGS, "" },
/* Sentinel */
{ NULL, NULL, 0, NULL }
};
{{/enums}}
PyMODINIT_FUNC initlinphone(void) {
PyObject *m;
PyObject *menum;
@ -205,7 +231,7 @@ PyMODINIT_FUNC initlinphone(void) {
if (m == NULL) return;
{{#enums}}
menum = Py_InitModule3("{{enum_name}}", pylinphone_NoMethods, {{{enum_doc}}});
menum = Py_InitModule3("{{enum_name}}", pylinphone_{{enum_name}}_ModuleMethods, {{{enum_doc}}});
if (menum == NULL) return;
if (PyModule_AddObject(m, "{{enum_name}}", menum) < 0) return;
{{#enum_values}}

View file

@ -63,7 +63,7 @@ class RegisterStatusResponse(Response):
self.body += \
"""Id: {id}
State: {state}
""".format(id=id, state=str(proxy_cfg.state))
""".format(id=id, state=str(linphone.RegistrationState.string(proxy_cfg.state)))
class CommandExample:
def __init__(self, command, output):