summaryrefslogtreecommitdiff
path: root/efl/ecore/efl.ecore_idler.pxi
blob: bb3bed6b62b768e566a5f95dd81f289048934f2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Copyright (C) 2007-2015 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 3 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/>.


cdef class Idler(Eo):
    """

    Add an idler handler.

    This class represents an idler on the event loop that will
    call ``func`` when there is nothing more to do. The function will
    be passed any extra parameters given to constructor.

    When the idler ``func`` is called, it must return a value of either
    **True** or **False** (remember that Python returns **None** if no value
    is explicitly returned and **None** evaluates to False). If it returns
    **True**, it will be called again when system become idle, or if it
    returns **False** it will be deleted automatically making any
    references/handles for it invalid.

    Idlers should be stopped/deleted by means of delete()or
    returning False from ``func``, otherwise they'll continue alive, even
    if the current python context delete it's reference to it.

    Idlers are useful for progressively prossessing data without blocking.

    """
    def __init__(self, func, *args, **kargs):
        """Idler(...)

        :param func: Function to call when system is idle.
        :type func: callable
        :param \*args: All the remaining arguments will be passed
                       back in the callback function.
        :param \**kwargs: All the remaining keyword arguments will be passed
                          back in the callback function.

        Expected **func** signature::

            func(*args, **kargs): bool

        """
        if not callable(func):
            raise TypeError("Parameter 'func' must be callable")
        self.func = func
        self.args = args
        self.kargs = kargs
        self._set_obj(ecore_idler_add(_ecore_task_cb, <void *>self))

    def __str__(self):
        return "%s Idler(func=%s, args=%s, kargs=%s)" % (Eo.__repr__(self),
               self.func, self.args, self.kargs)

    def __repr__(self):
        return "%s Idler(func=%s, args=%s, kargs=%s)" % (Eo.__repr__(self),
                self.func, self.args, self.kargs)

    cpdef bint _task_exec(self) except *:
        return self.func(*self.args, **self.kargs)

    def delete(self):
        """Stop callback emission and free internal resources."""
        ecore_idler_del(self.obj)

    def stop(self):
        """Alias for stop()."""
        self.delete()


cdef class IdleEnterer(Idler):
    """

    Add an idle enterer handler.

    This class represents a function that will be called before systems
    enter idle. The function will be passed any extra parameters given
    to constructor.

    When the idle enterer ``func`` is called, it must return a value of
    either **True** or **False** (remember that Python returns **None** if
    no value is explicitly returned and **None** evaluates to **False**). If
    it returns **True**, it will be called again when system become idle, or
    if it returns **False** it will be deleted automatically making any
    references/handles for it invalid.

    Idle enterers should be stopped/deleted by means of delete() or
    returning **False** from ``func``, otherwise they'll continue alive, even
    if the current python context delete it's reference to it.

    Idle enterer are useful for post-work jobs, like garbage collection.



    """
    def __init__(self, func, *args, **kargs):
        """IdleEnterer(...)

        :param func: Function to call when system enters idle.
        :type func: callable
        :param \*args: All the remaining arguments will be passed
                       back in the callback function.
        :param \**kwargs: All the remaining keyword arguments will be passed
                          back in the callback function.

        Expected **func** signature::

            func(*args, **kargs): bool

        """
        if not callable(func):
            raise TypeError("Parameter 'func' must be callable")
        self.func = func
        self.args = args
        self.kargs = kargs
        self._set_obj(ecore_idle_enterer_add(_ecore_task_cb, <void *>self))

    def delete(self):
        """Stop callback emission and free internal resources."""
        ecore_idle_enterer_del(self.obj)


cdef class IdleExiter(Idler):
    """

    Add an idle exiter handler.

    This class represents a function that will be called before systems
    exits idle. The function will be passed any extra parameters given
    to constructor.

    When the idle exiter ``func`` is called, it must return a value of
    either *True* or *False* (remember that Python returns *None* if no value
    is explicitly returned and *None* evaluates to *False*). If it returns
    *True*, it will be called again when system become idle, or if it
    returns *False* it will be deleted automatically making any
    references/handles for it invalid.

    Idle exiters should be stopped/deleted by means of delete() or
    returning *False* from ``func``, otherwise they'll continue alive, even
    if the current python context delete it's reference to it.

    """
    def __init__(self, func, *args, **kargs):
        """ IdleExiter(...)

        :param func: Function to call when system exits idle.
        :param \*args: All the remaining arguments will be passed
                       back in the callback function.
        :param \**kwargs: All the remaining keyword arguments will be passed
                          back in the callback function.

        Expected **func** signature::

            func(*args, **kargs): bool

        """
        if not callable(func):
            raise TypeError("Parameter 'func' must be callable")
        self.func = func
        self.args = args
        self.kargs = kargs
        self._set_obj(ecore_idle_exiter_add(_ecore_task_cb, <void *>self))

    def delete(self):
        """Stop callback emission and free internal resources."""
        ecore_idle_exiter_del(self.obj)


def idler_add(func, *args, **kargs):
    """efl.ecore.Idler factory, for C-api compatibility.

    :param func:
        function to call when system is idle.
        Expected signature::

            func(*args, **kargs): bool

    :return: a new Idler instance
    :rtype: efl.ecore.Idler

    """
    return Idler(func, *args, **kargs)


def idle_enterer_add(func, *args, **kargs):
    """efl.ecore.IdleEnterer factory, for C-api compatibility.

    :param func:
        Function to call when system enters idle.
        Expected signature::

            func(*args, **kargs): bool

    :return: a new IdleEnterer instance
    :rtype: efl.ecore.IdleEnterer
    """
    return IdleEnterer(func, *args, **kargs)


def idle_exiter_add(func, *args, **kargs):
    """efl.ecore.IdleExiter factory, for C-api compatibility.

    :param func:
        Function to call when system exits idle.
        Expected signature::

            func(*args, **kargs): bool

    :return: a new IdleExiter instance
    :rtype: efl.ecore.IdleExiter
    """
    return IdleExiter(func, *args, **kargs)