From 123e4ccdfe5c515ad4e751b91e45e3f8a9fb8482 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Mon, 4 Aug 2014 16:24:32 +0200 Subject: [PATCH] Define methods to get string representation of enum values in the Python wrapper. --- .../apixml2python/linphone_module.mustache | 32 +++++++++++++++++-- tools/python/linphone-daemon.py | 2 +- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/tools/python/apixml2python/linphone_module.mustache b/tools/python/apixml2python/linphone_module.mustache index 9b54a2073..b85253611 100644 --- a/tools/python/apixml2python/linphone_module.mustache +++ b/tools/python/apixml2python/linphone_module.mustache @@ -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}} diff --git a/tools/python/linphone-daemon.py b/tools/python/linphone-daemon.py index 1d2243c33..ce24fca61 100644 --- a/tools/python/linphone-daemon.py +++ b/tools/python/linphone-daemon.py @@ -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):