python-efl/efl/ecore/efl.ecore_fd_handler.pxi

163 lines
5.3 KiB
Cython
Raw Normal View History

# Copyright (C) 2007-2013 various contributors (see AUTHORS)
Put in a first, still wip, version of the python bindings in a merged tree. This is meant to be the 1.8 version of the wrappers and will include everything that now is in the python folder. Atm this include evas, ecore, edje, elementary and emotion (emotion still commented in the build couse it need some more testing). Eo is used as a base for all the objects that inherit from it in C, but in real nothing is used from Eo, it is used more like a container to share code between the libs. All the docs has been stripped out because we want to use the new sphinx style docs that Kay has done in his git repo. (Kay: please wait a little bit to include it, as working on the libs without docs is much more easy) The new wrappers include a new container module called efl and thus you can live with both the old and the new installation. This also means that you need to import the new modules as: "from efl import evas" (instead of the old "import evas") The idea here is that you can make your code works with both version doing something like: try: import evas except: from efl import evas ...like is done in the gtk bindings Some stuff has been leaved out on purpose, because was old stuff (like the hacked evas rotation stuff) or because was not working as expected (like all the ecore.evas.XXX modules). See the TODO.txt file for more info. Probably some stuff is out just because I missed them, let me know if you miss something. Improvements from the old version: - Py3 compatible (still some work to be done, but really only TODO, no problems to resolv) - Should also works on other platforms, like windoz (but not tested) - Unittests greatly improved, you can also run ALL tests at once - much more simpler :) I will contine the works in the next weeks and hope someone will help too. NOTE: I switched back to setup.py instead of autotools, because that is the right way to compile python stuff. So to build just use: python setup.py install or python3 setup.py install Enjoy davemds SVN revision: 83831
2013-02-11 14:32:50 -08:00
#
# This file is part of Python-EFL.
Put in a first, still wip, version of the python bindings in a merged tree. This is meant to be the 1.8 version of the wrappers and will include everything that now is in the python folder. Atm this include evas, ecore, edje, elementary and emotion (emotion still commented in the build couse it need some more testing). Eo is used as a base for all the objects that inherit from it in C, but in real nothing is used from Eo, it is used more like a container to share code between the libs. All the docs has been stripped out because we want to use the new sphinx style docs that Kay has done in his git repo. (Kay: please wait a little bit to include it, as working on the libs without docs is much more easy) The new wrappers include a new container module called efl and thus you can live with both the old and the new installation. This also means that you need to import the new modules as: "from efl import evas" (instead of the old "import evas") The idea here is that you can make your code works with both version doing something like: try: import evas except: from efl import evas ...like is done in the gtk bindings Some stuff has been leaved out on purpose, because was old stuff (like the hacked evas rotation stuff) or because was not working as expected (like all the ecore.evas.XXX modules). See the TODO.txt file for more info. Probably some stuff is out just because I missed them, let me know if you miss something. Improvements from the old version: - Py3 compatible (still some work to be done, but really only TODO, no problems to resolv) - Should also works on other platforms, like windoz (but not tested) - Unittests greatly improved, you can also run ALL tests at once - much more simpler :) I will contine the works in the next weeks and hope someone will help too. NOTE: I switched back to setup.py instead of autotools, because that is the right way to compile python stuff. So to build just use: python setup.py install or python3 setup.py install Enjoy davemds SVN revision: 83831
2013-02-11 14:32:50 -08:00
#
# Python-EFL is free software; you can redistribute it and/or
Put in a first, still wip, version of the python bindings in a merged tree. This is meant to be the 1.8 version of the wrappers and will include everything that now is in the python folder. Atm this include evas, ecore, edje, elementary and emotion (emotion still commented in the build couse it need some more testing). Eo is used as a base for all the objects that inherit from it in C, but in real nothing is used from Eo, it is used more like a container to share code between the libs. All the docs has been stripped out because we want to use the new sphinx style docs that Kay has done in his git repo. (Kay: please wait a little bit to include it, as working on the libs without docs is much more easy) The new wrappers include a new container module called efl and thus you can live with both the old and the new installation. This also means that you need to import the new modules as: "from efl import evas" (instead of the old "import evas") The idea here is that you can make your code works with both version doing something like: try: import evas except: from efl import evas ...like is done in the gtk bindings Some stuff has been leaved out on purpose, because was old stuff (like the hacked evas rotation stuff) or because was not working as expected (like all the ecore.evas.XXX modules). See the TODO.txt file for more info. Probably some stuff is out just because I missed them, let me know if you miss something. Improvements from the old version: - Py3 compatible (still some work to be done, but really only TODO, no problems to resolv) - Should also works on other platforms, like windoz (but not tested) - Unittests greatly improved, you can also run ALL tests at once - much more simpler :) I will contine the works in the next weeks and hope someone will help too. NOTE: I switched back to setup.py instead of autotools, because that is the right way to compile python stuff. So to build just use: python setup.py install or python3 setup.py install Enjoy davemds SVN revision: 83831
2013-02-11 14:32:50 -08:00
# 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,
Put in a first, still wip, version of the python bindings in a merged tree. This is meant to be the 1.8 version of the wrappers and will include everything that now is in the python folder. Atm this include evas, ecore, edje, elementary and emotion (emotion still commented in the build couse it need some more testing). Eo is used as a base for all the objects that inherit from it in C, but in real nothing is used from Eo, it is used more like a container to share code between the libs. All the docs has been stripped out because we want to use the new sphinx style docs that Kay has done in his git repo. (Kay: please wait a little bit to include it, as working on the libs without docs is much more easy) The new wrappers include a new container module called efl and thus you can live with both the old and the new installation. This also means that you need to import the new modules as: "from efl import evas" (instead of the old "import evas") The idea here is that you can make your code works with both version doing something like: try: import evas except: from efl import evas ...like is done in the gtk bindings Some stuff has been leaved out on purpose, because was old stuff (like the hacked evas rotation stuff) or because was not working as expected (like all the ecore.evas.XXX modules). See the TODO.txt file for more info. Probably some stuff is out just because I missed them, let me know if you miss something. Improvements from the old version: - Py3 compatible (still some work to be done, but really only TODO, no problems to resolv) - Should also works on other platforms, like windoz (but not tested) - Unittests greatly improved, you can also run ALL tests at once - much more simpler :) I will contine the works in the next weeks and hope someone will help too. NOTE: I switched back to setup.py instead of autotools, because that is the right way to compile python stuff. So to build just use: python setup.py install or python3 setup.py install Enjoy davemds SVN revision: 83831
2013-02-11 14:32:50 -08:00
# 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/>.
Put in a first, still wip, version of the python bindings in a merged tree. This is meant to be the 1.8 version of the wrappers and will include everything that now is in the python folder. Atm this include evas, ecore, edje, elementary and emotion (emotion still commented in the build couse it need some more testing). Eo is used as a base for all the objects that inherit from it in C, but in real nothing is used from Eo, it is used more like a container to share code between the libs. All the docs has been stripped out because we want to use the new sphinx style docs that Kay has done in his git repo. (Kay: please wait a little bit to include it, as working on the libs without docs is much more easy) The new wrappers include a new container module called efl and thus you can live with both the old and the new installation. This also means that you need to import the new modules as: "from efl import evas" (instead of the old "import evas") The idea here is that you can make your code works with both version doing something like: try: import evas except: from efl import evas ...like is done in the gtk bindings Some stuff has been leaved out on purpose, because was old stuff (like the hacked evas rotation stuff) or because was not working as expected (like all the ecore.evas.XXX modules). See the TODO.txt file for more info. Probably some stuff is out just because I missed them, let me know if you miss something. Improvements from the old version: - Py3 compatible (still some work to be done, but really only TODO, no problems to resolv) - Should also works on other platforms, like windoz (but not tested) - Unittests greatly improved, you can also run ALL tests at once - much more simpler :) I will contine the works in the next weeks and hope someone will help too. NOTE: I switched back to setup.py instead of autotools, because that is the right way to compile python stuff. So to build just use: python setup.py install or python3 setup.py install Enjoy davemds SVN revision: 83831
2013-02-11 14:32:50 -08:00
cdef void fd_handler_prepare_cb(void *data, Ecore_Fd_Handler *fdh) with gil:
cdef FdHandler obj = <FdHandler>data
cdef int r
if obj._prepare_callback is None:
return
func, args, kargs = obj._prepare_callback
try:
func(obj, *args, **kargs)
except Exception, e:
traceback.print_exc()
cdef flags2str(int value):
flags = []
if value & <int>ECORE_FD_READ:
flags.append("READ")
if value & <int>ECORE_FD_WRITE:
flags.append("WRITE")
if value & <int>ECORE_FD_ERROR:
flags.append("ERROR")
return ", ".join(flags)
cdef Eina_Bool fd_handler_cb(void *data, Ecore_Fd_Handler *fdh) with gil:
cdef FdHandler obj = <FdHandler>data
cdef Eina_Bool r
try:
r = bool(obj._exec())
except Exception, e:
traceback.print_exc()
r = 0
if not r:
obj.delete()
return r
cdef class FdHandler(object):
def __init__(self, fd, int flags, func, *args, **kargs):
if not callable(func):
raise TypeError("Parameter 'func' must be callable")
self.func = func
self.args = args
self.kargs = kargs
self._prepare_callback = None
if self.obj == NULL:
if not isinstance(fd, (int, long)):
try:
fd = fd.fileno()
except AttributeError, e:
raise ValueError("fd must be integer or have fileno()")
self.obj = ecore_main_fd_handler_add(fd,
<Ecore_Fd_Handler_Flags>flags,
fd_handler_cb, <void *>self,
NULL, NULL)
if self.obj != NULL:
Py_INCREF(self)
def __str__(self):
if self.obj == NULL:
fd = None
flags = ""
else:
fd = self.fd_get()
flags = flags2str(self.active_get(7))
return "%s(func=%s, args=%s, kargs=%s, fd=%s, flags=[%s])" % \
(self.__class__.__name__, self.func, self.args, self.kargs,
fd, flags)
def __repr__(self):
if self.obj == NULL:
fd = None
flags = ""
else:
fd = self.fd_get()
flags = flags2str(self.active_get(7))
return ("%s(%#x, func=%s, args=%s, kargs=%s, fd=%s, flags=[%s], "
"Ecore_Fd_Handler=%#x, refcount=%d)") % \
(self.__class__.__name__, <unsigned long><void *>self,
self.func, self.args, self.kargs, fd, flags,
<unsigned long>self.obj, PY_REFCOUNT(self))
def __dealloc__(self):
if self.obj != NULL:
ecore_main_fd_handler_del(self.obj)
self.obj = NULL
self.func = None
self.args = None
self.kargs = None
cdef object _exec(self):
return self.func(self, *self.args, **self.kargs)
def delete(self):
if self.obj != NULL:
ecore_main_fd_handler_del(self.obj)
self.obj = NULL
Py_DECREF(self)
def stop(self):
self.delete()
def fd_get(self):
return ecore_main_fd_handler_fd_get(self.obj)
property fd:
def __get__(self):
return self.fd_get()
def active_get(self, int flags):
cdef Ecore_Fd_Handler_Flags v = <Ecore_Fd_Handler_Flags>flags
return bool(ecore_main_fd_handler_active_get(self.obj, v))
def active_set(self, int flags):
cdef Ecore_Fd_Handler_Flags v = <Ecore_Fd_Handler_Flags>flags
ecore_main_fd_handler_active_set(self.obj, v)
def can_read(self):
return bool(ecore_main_fd_handler_active_get(self.obj, ECORE_FD_READ))
def can_write(self):
return bool(ecore_main_fd_handler_active_get(self.obj, ECORE_FD_WRITE))
def has_error(self):
return bool(ecore_main_fd_handler_active_get(self.obj, ECORE_FD_ERROR))
def prepare_callback_set(self, func, *args, **kargs):
if func is None:
self._prepare_callback = None
ecore_main_fd_handler_prepare_callback_set(self.obj, NULL, NULL)
elif callable(func):
self._prepare_callback = (func, args, kargs)
ecore_main_fd_handler_prepare_callback_set(self.obj,
fd_handler_prepare_cb,
<void *>self)
else:
raise TypeError("Parameter 'func' must be callable")
def fd_handler_add(fd, int flags, func, *args, **kargs):
return FdHandler(fd, flags, func, *args, **kargs)