Convert dbus_mainloop C code to Cython.

This commit is contained in:
Kai Huuhko 2013-04-07 17:40:21 +03:00
parent 89ec2edd88
commit 79022bf27b
3 changed files with 87 additions and 2 deletions

View File

@ -0,0 +1,27 @@
from cpython cimport PyObject
from libc.string cimport const_char
cdef extern from "dbus/dbus.h":
ctypedef int dbus_bool_t
ctypedef struct DBusConnection
ctypedef struct DBusServer
cdef extern from "dbus/dbus-python.h":
ctypedef dbus_bool_t (*_dbus_py_conn_setup_func)(DBusConnection *, void *)
ctypedef dbus_bool_t (*_dbus_py_srv_setup_func)(DBusServer *, void *)
ctypedef void (*_dbus_py_free_func)(void *)
PyObject *DBusPyNativeMainLoop_New4(_dbus_py_conn_setup_func conn_func, _dbus_py_srv_setup_func srv_func, _dbus_py_free_func free_func, void *)
int import_dbus_bindings(const_char *this_module_name)
cdef extern from "Ecore.h":
int ecore_init()
void ecore_shutdown()
cdef extern from "e_dbus.h":
ctypedef struct E_DBus_Connection
int e_dbus_init()
int e_dbus_shutdown()
E_DBus_Connection *e_dbus_connection_setup(DBusConnection *conn)
void e_dbus_connection_close(E_DBus_Connection *conn)

View File

@ -0,0 +1,57 @@
# Copyright (C) 2007-2013 various contributors (see AUTHORS)
#
# This file is part of Python-EFL.
#
# Python-EFL is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# Python-EFL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this Python-EFL. If not, see <http://www.gnu.org/licenses/>.
"""
D-Bus python integration for Ecore main loop.
"""
import dbus
import dbus.mainloop
import atexit
cdef dbus_bool_t dbus_py_ecore_set_up_conn(DBusConnection *conn, void *data) with gil:
e_dbus_connection_setup(conn)
return True
cdef object dbus_ecore_native_mainloop():
return <object>DBusPyNativeMainLoop_New4(dbus_py_ecore_set_up_conn,
NULL, NULL, NULL)
def DBusEcoreMainLoop(set_as_default = None):
"""Returns a NativeMainLoop to attach the Ecore main loop to D-Bus
from within Python."""
ml = dbus_ecore_native_mainloop()
if ml is not None and set_as_default:
dbus.set_default_main_loop(ml)
return ml
def module_cleanup():
e_dbus_shutdown()
ecore_shutdown()
if import_dbus_bindings("efl.dbus_mainloop") < 0:
raise ImportError("failed to import D-Bus bindings")
ecore_init()
e_dbus_init()
atexit.register(module_cleanup)

View File

@ -123,10 +123,11 @@ else:
# dbus mainloop integration
dbus_cflags, dbus_libs = pkg_config('DBus', 'dbus-python', "0.83.0")
dbus_ml_ext = Extension("efl.dbus_mainloop",
["efl/dbus_mainloop/module.c",
["efl/dbus_mainloop/dbus_mainloop.pyx",
"efl/dbus_mainloop/e_dbus.c"],
extra_compile_args = dbus_cflags + ecore_cflags,
extra_link_args = dbus_libs + ecore_libs)
modules.append(dbus_ml_ext)
# Elementary
elm_exts = [
@ -228,5 +229,5 @@ if __name__ == "__main__":
#"builder": (None, "coverage"),
},
},
ext_modules = cythonize(modules, include_path=["include"]) + [dbus_ml_ext],
ext_modules = cythonize(modules, include_path=["include"]),
)