summaryrefslogtreecommitdiff
path: root/efl/ecore/efl.ecore_exe.pxi
blob: 1ebaa8679c5c87df549f33df847805d279fba7f3 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# Copyright (C) 2007-2016 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/>.

from cpython cimport PyUnicode_AsUTF8String

cdef extern from "Python.h":
    object PyUnicode_FromStringAndSize(char *s, Py_ssize_t len)
    int PyObject_GetBuffer(obj, Py_buffer *view, int flags)
    void PyBuffer_Release(Py_buffer *view)


cdef exe_flags2str(int value):
    flags = []
    if value & enums.ECORE_EXE_PIPE_READ:
        flags.append("PIPE_READ")
    if value & enums.ECORE_EXE_PIPE_WRITE:
        flags.append("PIPE_WRITE")
    if value & enums.ECORE_EXE_PIPE_ERROR:
        flags.append("PIPE_ERROR")
    if value & enums.ECORE_EXE_PIPE_READ_LINE_BUFFERED:
        flags.append("PIPE_READ_LINE_BUFFERED")
    if value & enums.ECORE_EXE_PIPE_ERROR_LINE_BUFFERED:
        flags.append("PIPE_ERROR_LINE_BUFFERED")
    if value & enums.ECORE_EXE_PIPE_AUTO:
        flags.append("PIPE_AUTO")
    if value & enums.ECORE_EXE_RESPAWN:
        flags.append("RESPAWN")
    if value & enums.ECORE_EXE_USE_SH:
        flags.append("USE_SH")
    if value & enums.ECORE_EXE_NOT_LEADER:
        flags.append("NOT_LEADER")
    if value & enums.ECORE_EXE_TERM_WITH_PARENT:
        flags.append("ECORE_EXE_TERM_WITH_PARENT")
    return ", ".join(flags)


cdef Eina_Bool _exe_event_filter_cb(void *data, int type, void *event) with gil:
    cdef:
        ExeEventFilter self = <ExeEventFilter>data
        Ecore_Exe_Event_Add *e_add
        Ecore_Exe_Event_Del *e_del
        Ecore_Exe_Event_Data *e_data
        Event e
        list cbs
        tuple cb

    try:
        assert self.event_type == type, "event is not what we asked? impossible"
        if type == enums.ECORE_EXE_EVENT_ADD:
            e_add = <Ecore_Exe_Event_Add *>event
            if e_add.exe != self.exe:
                return 1
            e = EventExeAdd()
        elif type == enums.ECORE_EXE_EVENT_DEL:
            e_del = <Ecore_Exe_Event_Del *>event
            if e_del.exe != self.exe:
                return 1
            e = EventExeDel()
        elif type == enums.ECORE_EXE_EVENT_DATA or type == enums.ECORE_EXE_EVENT_ERROR:
            e_data = <Ecore_Exe_Event_Data *>event
            if e_data.exe != self.exe:
                return 1
            e = EventExeData()
        else:
            raise SystemError("unknown event type=%d" % type)

        r = e._set_obj(event)
        assert r != -1, "exe is not known?! impossible!"

        cbs = self.callbacks[:] # copy, so we can change self.callbacks
        for cb in cbs:
            try:
                cb[0](self.owner, e, *cb[1], **cb[2])
            except Exception:
                traceback.print_exc()

    except Exception:
        traceback.print_exc()

    return 1 # always return true, no matter what


cdef class ExeEventFilter:
    def __cinit__(self, *a, **ka):
        self.event_type = -1
        self.callbacks = []

    def __dealloc__(self):
        if self.handler != NULL:
            ecore_event_handler_del(self.handler)
            self.handler = NULL

        self.exe = NULL
        self.owner = None
        self.event_type = -1
        self.callbacks = None

    def __init__(self, Exe exe not None, int event_type):
        self.exe = exe.exe
        self.owner = exe
        self.event_type = event_type
        self.callbacks = []

    def delete(self):
        if self.handler != NULL:
            ecore_event_handler_del(self.handler)
            self.handler = NULL
        self.callbacks = None

    def callback_add(self, func, args, kargs):
        if self.handler == NULL:
            self.handler = ecore_event_handler_add(
                self.event_type, _exe_event_filter_cb, <void *>self)
        self.callbacks.append((func, args, kargs))

    def callback_del(self, func, args, kargs):
        try:
            self.callbacks.remove((func, args, kargs))
        except ValueError:
            raise ValueError(
                "callback is not registered: %s, args=%s, kargs=%s" %
                (func, args, kargs))

        if self.callbacks:
            return

        if self.handler != NULL:
            ecore_event_handler_del(self.handler)
            self.handler = NULL


def exe_run_priority_set(int pri):
    ecore_exe_run_priority_set(pri)


def exe_run_priority_get():
    return ecore_exe_run_priority_get()


cdef object _ecore_exe_event_mapping
_ecore_exe_event_mapping = {}


cdef void _ecore_exe_pre_free_cb(void *data, const Ecore_Exe *exe) with gil:
    cdef Exe obj
    try:
        if data == NULL:
            raise ValueError("data parameter is NULL")
        else:
            obj = <Exe>data
            obj._unset_obj()
    except Exception:
        traceback.print_exc()


cdef class Exe(object):
    """

    Spawns a child process with its stdin/out available for communication.

    This function forks and runs the given command using ``/bin/sh``.

    Note that the process handle is only valid until a child process
    terminated event is received.  After all handlers for the child
    process terminated event have been called, the handle will be
    freed by Ecore. In this case the Python wrapper becomes "shallow"
    and all operations will fail or return bogus/dummy values,
    although it should not crash.

    This class behavior is configurable by means of given constructor
    *flags*, that will make Ecore monitor process' stdout and stderr,
    emitting events on main loop.

    To write use :py:meth:`send`.  To read listen to ``ECORE_EXE_EVENT_DATA``
    or ``ECORE_EXE_EVENT_ERROR`` events (see below). Ecore may
    buffer read and error data until a newline character if asked for
    with the *flags*.  All data will be included in the events
    (newlines will be replaced with NULLS if line is buffered).

    ``ECORE_EXE_EVENT_DATA`` events will only happen if the process is
    run with ``ECORE_EXE_PIPE_READ`` enabled in the *flags*.  The same
    with the error version.  Writing will only be allowed with
    ``ECORE_EXE_PIPE_WRITE`` enabled in the *flags*.

    .. rubric:: Instance Event Handling

    To make use easier, there are methods that automatically filter
    events for this instance and deletes them when the ``Exe`` is
    deleted:

    - on_add_event_add()
    - on_add_event_del()
    - on_del_event_add()
    - on_del_event_del()
    - on_data_event_add()
    - on_data_event_del()
    - on_error_event_add()
    - on_error_event_del()

    The callback signatures are::

        func(exe, event, *args, **kargs)

    In contrast with C-api conformant functions. This only receives
    the events from this exact exe instance. The signature is also
    very different, the first parameter is the ``Exe`` reference and
    the return value does **not** removes the event listener!

    Using this method is likely more efficient than the C-api since it
    will not convert from C to Python lots of times, possibly useless.

    However, there are C-api conformat functions as well.

    .. rubric:: Event Handling (C-api conformant)

    Getting data from executed processed is done by means of event
    handling, which is also used to notify whenever this process
    really started or died.

    One should listen to events in the main loop, such as:

    EventExeAdd
        listen with ``on_exe_add_event_add()`` to know when sub processes
        were started and ready to be used.

    EventExeDel
        listen with ``on_exe_del_event_add()`` to know when sub processes died.

    EventExeData
        listen with ``on_exe_data_event_add()`` to know when sub processes
        output data to their stdout.

    EventExeError
        listen with ``on_exe_error_event_add()`` to know when sub processes
        output data to their stderr.

    Events will have the following signature, as explained in
    ``EventHandler``::

       func(event, *args, **kargs): bool

    That mean once registered, your callback ``func`` will be called for all
    known ``Exe`` instances (that were created from Python!). You can query
    which instance created such event with ``event.exe`` property. Thus you
    often need to filter if the event you got is from the instance you need!
    (This is designed to match C-api).

    Once your function returns evaluates to *False* (note: not returning
    means returning *None*, that evaluates to *False*!), your callback
    will not be called anymore and your handler is deleted.

    One may delete handlers explicitly with ``EventHandler.delete()``
    method.


    :param exe_cmd: command to execute as subprocess.
    :type exe_cmd: str
    :param flags:
        if given (!= 0), should be bitwise OR of

        ECORE_EXE_PIPE_READ
            Exe Pipe Read mask

        ECORE_EXE_PIPE_WRITE
            Exe Pipe Write mask

        ECORE_EXE_PIPE_ERROR
            Exe Pipe error mask

        ECORE_EXE_PIPE_READ_LINE_BUFFERED
            Reads are buffered until a newline and delivered 1 event per line.

        ECORE_EXE_PIPE_ERROR_LINE_BUFFERED
            Errors are buffered until a newline and delivered 1 event per line.

        ECORE_EXE_PIPE_AUTO
            stdout and stderr are buffered automatically

        ECORE_EXE_RESPAWN
            Exe is restarted if it dies

        ECORE_EXE_USE_SH
            Use /bin/sh to run the command.

        ECORE_EXE_NOT_LEADER
            Do not use setsid() to have the executed process be its own
            session leader

        ECORE_EXE_TERM_WITH_PARENT
            Makes child receive SIGTERM when parent dies

    :type flags: int
    :param data: extra data to be associated and available with ``data_get()``

    """
    def __cinit__(self, *a, **ka):
        self.exe = NULL
        self.__data = None
        self.__callbacks = {}

    def __init__(self, exe_cmd, int flags=0, data=None):
        if not exe_cmd:
            raise ValueError("exe_cmd must not be empty!")

        if flags is None:
            flags = 0

        if isinstance(exe_cmd, unicode): exe_cmd = PyUnicode_AsUTF8String(exe_cmd)
        self._set_obj(exe_cmd, flags)
        self.__data = data
        self.__callbacks = {}

    cdef int _set_obj(self, char *exe_cmd, int flags) except 0:
        cdef Ecore_Exe *exe

        assert self.exe == NULL, "Exe must be clean, not wrapping any Ecore_Exe"

        exe = ecore_exe_pipe_run(exe_cmd, <Ecore_Exe_Flags>flags, <void *>self)
        if exe == NULL:
            raise SystemError("could not run subprocess %r, flags=%#x" %
                              (exe_cmd, flags))

        Py_INCREF(self)
        self.exe = exe
        ecore_exe_callback_pre_free_set(exe, _ecore_exe_pre_free_cb)
        _ecore_exe_event_mapping[<uintptr_t><void *>exe] = self
        return 1

    cdef int _unset_obj(self) except 0:
        assert self.exe != NULL, "Exe must wrap something"
        for t, filter in self.__callbacks.iteritems():
            filter.delete()
        self.__callbacks = None

        _ecore_exe_event_mapping.pop(<uintptr_t><void *>self.exe)
        self.exe = NULL
        Py_DECREF(self)
        return 1

    def __repr__(self):
        if self.exe == NULL:
            pid = None
            cmd = None
            flags = ""
            data = None
        else:
            pid = self.pid
            cmd = self.cmd
            flags = exe_flags2str(self.flags)
            data = None
        return ("<%s(%#x, Ecore_Exe=%#x, refcount=%d, pid=%s, cmd=%r, "
                "flags=[%s], data=%r)>") % \
                (self.__class__.__name__, <uintptr_t><void *>self,
                 <uintptr_t>self.exe, PY_REFCOUNT(self),
                 pid, cmd, flags, data)

    def is_deleted(self):
        """Check if the object has been deleted thus leaving the object shallow.

        :return: True if the object has been deleted yet, False otherwise.
        :rtype: bool

        .. versionadded:: 1.18

        """
        return bool(self.exe == NULL)

    def delete(self):
        """Forcefully frees the given process handle.

        Note that the process that the handle represents is unaffected
        by this function, this just stops monitoring the stdout/stderr
        and emitting related events.

        To finish the process call :py:meth:`terminate` or :py:meth:`kill`.
        """
        if self.exe == NULL:
            raise ValueError("%s already deleted" % self.__class__.__name__)
        ecore_exe_free(self.exe)

    def free(self):
        """Alias for :py:meth:`delete` to keep compatibility with C-api."""
        self.delete()

    def send(self, buf, long size=0):
        """Sends data to the executed process, which it receives on stdin.

        This function writes to a child processes standard in, with
        unlimited buffering. This call will never block. It may fail
        if the system runs out of memory.

        :param buffer: object that implements buffer interface, such
               as strings (str).
        :param size: if greater than zero, then this will limit the
               size of given buffer. If None, then the exact buffer
               size is used.

        :raise ValueError: if size is larger than buffer size.
        :return: success or failure.
        :rtype: bool
        """
        cdef:
            Py_buffer buf_view
            bint ret

        if isinstance(buf, unicode):
            buf = PyUnicode_AsUTF8String(buf)

        PyObject_GetBuffer(buf, &buf_view, 0)

        if size <= 0:
            size = buf_view.len
        elif size > buf_view.len:
            raise ValueError(
                "given size (%d) is larger than buffer size (%d)." %
                (size, buf_view.len))

        ret = ecore_exe_send(self.exe, <const void *>buf_view.buf, buf_view.len)
        PyBuffer_Release(&buf_view)
        return ret

    def close_stdin(self):
        """Close executed process' stdin.

        The stdin of the given child process will not be closed
        immediately. Instead it will be closed when the write buffer
        is empty.
        """
        ecore_exe_close_stdin(self.exe)

    def auto_limits_set(self, int start_bytes, int end_bytes,
                        int start_lines, int end_lines):
        """Sets the auto pipe limits for the given process handle

        :param start_bytes: limit of bytes at start of output to buffer.
        :param end_bytes: limit of bytes at end of output to buffer.
        :param start_lines: limit of lines at start of output to buffer.
        :param end_lines: limit of lines at end of output to buffer.

        """
        ecore_exe_auto_limits_set(self.exe, start_bytes, end_bytes,
                                  start_lines, end_lines)

    # TODO:
    #def event_data_get(self, int flags):
        #Ecore_Exe_Event_Data *ecore_exe_event_data_get(Ecore_Exe *exe, Ecore_Exe_Flags flags)
        #void ecore_exe_event_data_free(Ecore_Exe_Event_Data *data)

    def cmd_get(self):
        """Retrieves the command of the executed process.

        :return: the command line string if execution succeeded, None otherwise.
        :rtype: str or None

        """
        cdef const char *cmd = ecore_exe_cmd_get(self.exe)
        if cmd != NULL:
            return cmd
        return None

    property cmd:
        def __get__(self):
            return self.cmd_get()

    def pid_get(self):
        """Retrieves the process ID of the executed process.

        :rtype: int

        """
        return ecore_exe_pid_get(self.exe)

    property pid:
        def __get__(self):
            return self.pid_get()

    def tag_set(self, char *tag):
        """Sets the string tag for the given process.


        This is a string that is attached to this handle and may serve
        as further information.

        .. note:: not much useful in Python, but kept for compatibility
                  with C-api.

        """
        cdef char *s
        if tag is None:
            s = NULL
        else:
            s = tag
        ecore_exe_tag_set(self.exe, s)

    def tag_get(self):
        """Retrieves the tag attached to the given process.

        This is a string that is attached to this handle and may serve
        as further information.

        .. note:: not much useful in Python, but kept for compatibility
                  with C-api.

        :rtype: str or None

        """
        cdef const char *tag = ecore_exe_tag_get(self.exe)
        if tag != NULL:
            return tag
        return None

    property tag:
        def __set__(self, char *tag):
            self.tag_set(tag)

        def __get__(self):
            return self.tag_get()

    def data_get(self):
        return self.__data

    property data:
        def __get__(self):
            return self.data_get()

    def flags_get(self):
        """Retrieves the flags attached to the given process handle.

         - ECORE_EXE_PIPE_READ: Exe Pipe Read mask
         - ECORE_EXE_PIPE_WRITE: Exe Pipe Write mask
         - ECORE_EXE_PIPE_ERROR: Exe Pipe error mask
         - ECORE_EXE_PIPE_READ_LINE_BUFFERED: Reads are buffered until
           a newline and delivered 1 event per line.
         - ECORE_EXE_PIPE_ERROR_LINE_BUFFERED: Errors are buffered
           until a newline and delivered 1 event per line
         - ECORE_EXE_PIPE_AUTO: stdout and stderr are buffered automatically
         - ECORE_EXE_RESPAWN: Exe is restarted if it dies
         - ECORE_EXE_USE_SH: Use /bin/sh to run the command.
         - ECORE_EXE_NOT_LEADER Do not use setsid() to have the
           executed process be its own session leader

        :return: set of masks, ORed.

        """
        return ecore_exe_flags_get(self.exe)

    property flags:
        def __get__(self):
            return self.flags_get()

    def signal(self, int num):
        """Send SIGUSR1 or SIGUSR2 to executed process.

        :parm num: user signal number, either 1 or 2.

        :see: POSIX kill(2) and kill(1) man pages.
        :raise ValueError: if num is not 1 or 2.

        """
        if num != 1 or num != 2:
            raise ValueError("num must be either 1 or 2. Got %d." % num)
        ecore_exe_signal(self.exe, num)

    def pause(self):
        """Send pause signal (SIGSTOP) to executed process.

        In order to resume application execution, use :py:meth:`continue_`
        """
        ecore_exe_pause(self.exe)

    def stop(self):
        """Alias for ``pause``"""
        self.pause()

    def continue_(self):
        """Send continue signal (SIGCONT) to executed process.

        This resumes application previously paused with :py:meth:`pause`

        """
        ecore_exe_continue(self.exe)

    def resume(self):
        """Alias for ``continue_()``"""
        self.continue_()

    def interrupt(self):
        """Send interrupt signal (SIGINT) to executed process.

        .. note:: Python usually installs SIGINT handler to generate
                  *KeyboardInterrupt*, however Ecore will *override*
                  this handler with its own that generates
                  *ECORE_EVENT_SIGNAL_EXIT* in its main loop for the
                  application to handle. Pay attention to this detail if
                  your *child* process is also using Ecore.
        """
        ecore_exe_interrupt(self.exe)

    def quit(self):
        """Send quit signal (SIGQUIT) to executed process."""
        ecore_exe_quit(self.exe)

    def terminate(self):
        """Send terminate signal (SIGTERM) to executed process."""
        ecore_exe_terminate(self.exe)

    def kill(self):
        """Send kill signal (SIGKILL) to executed process.

        This signal is fatal and will exit the application as it
        cannot be blocked.

        """
        ecore_exe_kill(self.exe)

    def hup(self):
        """Send hup signal (SIGHUP) to executed process."""
        ecore_exe_hup(self.exe)

    def on_add_event_add(self, func, *args, **kargs):
        """Adds event listener to know when this Exe was actually started.

        The given function will be called with the following signature
        every time this Exe receives an ``ECORE_EXE_EVENT_ADD`` signal::

            func(exe, event, *args, **kargs)

        In contrast with on_exe_add_event_add(), this only receives
        the events from this exact exe instance. The signature is also
        very different, the first parameter is the ``Exe`` reference
        and the return value does **not** removes the event listener!

        :see: on_add_event_del()
        :see: on_exe_add_event_add()

        """
        filter = self.__callbacks.get(enums.ECORE_EXE_EVENT_ADD)
        if filter is None:
            filter = ExeEventFilter(self, enums.ECORE_EXE_EVENT_ADD)
            self.__callbacks[enums.ECORE_EXE_EVENT_ADD] = filter
        filter.callback_add(func, args, kargs)

    def on_add_event_del(self, func, *args, **kargs):
        """Removes the event listener registered with ``on_add_event_add()``.

        Parameters must be exactly the same.

        :raise ValueError: if parameters don't match an already
                           registered callback.
        """
        filter = self.__callbacks.get(enums.ECORE_EXE_EVENT_ADD)
        if filter is None:
            raise ValueError("callback not registered %s, args=%s, kargs=%s" %
                             (func, args, kargs))
        filter.callback_del(func, args, kargs)

    def on_del_event_add(self, func, *args, **kargs):
        """Adds event listener to know when this Exe was actually started.

        The given function will be called with the following signature
        every time this Exe receives an ``ECORE_EXE_EVENT_DEL`` signal::

            func(exe, event, *args, **kargs)

        In contrast with ``on_exe_del_event_add()``, this only receives
        the events from this exact exe instance. The signature is also
        very different, the first parameter is the ``Exe`` reference
        and the return value does **not** removes the event listener!

        :see: on_del_event_del()
        :see: on_exe_del_event_add()
        """
        filter = self.__callbacks.get(enums.ECORE_EXE_EVENT_DEL)
        if filter is None:
            filter = ExeEventFilter(self, enums.ECORE_EXE_EVENT_DEL)
            self.__callbacks[enums.ECORE_EXE_EVENT_DEL] = filter
        filter.callback_add(func, args, kargs)

    def on_del_event_del(self, func, *args, **kargs):
        """Removes the event listener registered with :py:func`on_del_event_add`.

        Parameters must be exactly the same.

        :raise ValueError: if parameters don't match an already
                           registered callback.
        """
        filter = self.__callbacks.get(enums.ECORE_EXE_EVENT_DEL)
        if filter is None:
            raise ValueError("callback not registered %s, args=%s, kargs=%s" %
                             (func, args, kargs))
        filter.callback_del(func, args, kargs)

    def on_data_event_add(self, func, *args, **kargs):
        """Adds event listener to know when this Exe was actually started.

        The given function will be called with the following signature
        every time this Exe receives an ``ECORE_EXE_EVENT_DATA`` signal::

            func(exe, event, *args, **kargs)

        In contrast with ``on_exe_data_event_add()``, this only receives
        the events from this exact exe instance. The signature is also
        very different, the first parameter is the ``Exe`` reference
        and the return value does **not** removes the event listener!

        :see: on_data_event_del()
        :see: on_exe_data_event_add()
        """
        filter = self.__callbacks.get(enums.ECORE_EXE_EVENT_DATA)
        if filter is None:
            filter = ExeEventFilter(self, enums.ECORE_EXE_EVENT_DATA)
            self.__callbacks[enums.ECORE_EXE_EVENT_DATA] = filter
        filter.callback_add(func, args, kargs)

    def on_data_event_del(self, func, *args, **kargs):
        """Removes the event listener registered with :py:func:`on_data_event_add()`.

        Parameters must be exactly the same.

        :raise ValueError: if parameters don't match an already
                           registered callback.
        """
        filter = self.__callbacks.get(enums.ECORE_EXE_EVENT_DATA)
        if filter is None:
            raise ValueError("callback not registered %s, args=%s, kargs=%s" %
                             (func, args, kargs))
        filter.callback_del(func, args, kargs)

    def on_error_event_add(self, func, *args, **kargs):
        """Adds event listener to know when this Exe was actually started.

        The given function will be called with the following signature
        every time this Exe receives an ``ECORE_EXE_EVENT_ERROR`` signal::

            func(exe, event, *args, **kargs)

        In contrast with ``on_exe_error_event_add()``, this only receives
        the events from this exact exe instance. The signature is also
        very different, the first parameter is the ``Exe`` reference
        and the return value does **not** remove the event listener!

        :see: on_error_event_del()
        :see: on_exe_error_event_add()
        """
        filter = self.__callbacks.get(enums.ECORE_EXE_EVENT_ERROR)
        if filter is None:
            filter = ExeEventFilter(self, enums.ECORE_EXE_EVENT_ERROR)
            self.__callbacks[enums.ECORE_EXE_EVENT_ERROR] = filter
        filter.callback_add(func, args, kargs)

    def on_error_event_del(self, func, *args, **kargs):
        """Removes the event listener registered with :py:func:`on_error_event_add()`.

        Parameters must be exactly the same.

        :raise ValueError: if parameters don't match an already
                           registered callback.
        """
        filter = self.__callbacks.get(enums.ECORE_EXE_EVENT_ERROR)
        if filter is None:
            raise ValueError("callback not registered %s, args=%s, kargs=%s" %
                             (func, args, kargs))
        filter.callback_del(func, args, kargs)


def exe_run(exe_cmd, data=None):
    """`efl.ecore.Exe` factory, for C-api compatibility."""
    return Exe(exe_cmd, data=data)


def exe_pipe_run(exe_cmd, int flags=0, data=None):
    """`efl.ecore.Exe` factory, for C-api compatibility."""
    return Exe(exe_cmd, flags, data)


cdef class EventExeAdd(Event):
    """Represents Ecore_Exe_Event_Add event from C-api.

    This event notifies that the process created with :py:class:`Exe` has been started.

    :ivar Exe exe: Instance of :py:class:`Exe` that created this event.
    """
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Exe_Event_Add *obj
        obj = <Ecore_Exe_Event_Add*>o
        self.exe = _ecore_exe_event_mapping.get(<uintptr_t>obj.exe)
        if self.exe is None:
            return -1
        return 1

    def __repr__(self):
        return "<%s(exe=%r)>" % (self.__class__.__name__, self.exe)


cdef class EventExeDel(Event):
    """Represents Ecore_Exe_Event_Del from C-api.

    This event notifies that the process created with ``Exe`` is now dead.

    :ivar Exe exe: Instance of :py:class:`Exe` that created this event.
    :ivar int pid: Process ID
    :ivar int exit_code: Exit code
    :ivar int exit_signal: Exit signal
    :ivar bool exited: Has process exited
    :ivar bool signalled: Has process been signalled
    """
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Exe_Event_Del *obj
        obj = <Ecore_Exe_Event_Del*>o
        self.exe = _ecore_exe_event_mapping.get(<uintptr_t>obj.exe)
        if self.exe is None:
            return -1
        self.pid = obj.pid
        self.exit_code = obj.exit_code
        self.exit_signal = obj.exit_signal
        self.exited = bool(obj.exited)
        self.signalled = bool(obj.signalled)
        return 1

    def __repr__(self):
        return ("<%s(pid=%s, exit_code=%s, exit_signal=%s, exited=%s, "
                "signalled=%s, exe=%r)>") % \
                (self.__class__.__name__, self.pid, self.exit_code,
                 self.exit_signal, self.exited, self.signalled, self.exe)


cdef class EventExeData(Event):
    """Represents Ecore_Exe_Event_Data from C-api.

    This event is issued by :py:class:`Exe` instances created with flags that
    allow reading from either stdout or stderr.

    :ivar Exe exe: Instance of :py:class:`Exe` that created this event.
    :ivar string ~EventExeData.data: The raw string buffer with binary data from child process.
    :ivar int ~EventExeData.size: The size of **data** (same as ``len(data)``)
    :ivar list lines: List of strings with all text lines
    """
    cdef int _set_obj(self, void *o) except 0:
        cdef Ecore_Exe_Event_Data *obj
        cdef int i
        obj = <Ecore_Exe_Event_Data*>o
        self.exe = _ecore_exe_event_mapping.get(<uintptr_t>obj.exe)
        if self.exe is None:
            return -1
        self.data = PyUnicode_FromStringAndSize(<char*>obj.data, obj.size)
        self.size = obj.size
        self.lines = []

        line_append = self.lines.append
        if obj.lines:
            i = 0
            while obj.lines[i].line != NULL:
                line_append(PyUnicode_FromStringAndSize(
                        obj.lines[i].line, obj.lines[i].size))
                i += 1

        return 1

    def __repr__(self):
        if self.lines is None:
            count = None
        else:
            count = len(self.lines)
        return "<%s(size=%d, lines=#%d, exe=%r)>" % \
            (self.__class__.__name__, self.size, count, self.exe)


cdef class EventHandlerExe(EventHandler):
    """Specialized event handler that creates specialized event instances.

    This class is responsible by filtering out the events created from
    C without associated Python wrappers.
    """
    cdef Eina_Bool _exec(self, void *event) except 2:
        cdef Event e
        e = self.event_cls()
        if e._set_obj(event) == -1: # no exe
            return True
        return bool(self.func(e, *self.args, **self.kargs))


def on_exe_add_event_add(func, *args, **kargs):
    """Create an ecore event handler for ECORE_EXE_EVENT_ADD

       :see: EventHandler
       :see: EventHandlerExe
    """
    return EventHandlerExe(enums.ECORE_EXE_EVENT_ADD, func, *args, **kargs)


def on_exe_del_event_add(func, *args, **kargs):
    """Create an ecore event handler for ECORE_EXE_EVENT_DEL

       :see: EventHandler
       :see: EventHandlerExe
    """
    return EventHandlerExe(enums.ECORE_EXE_EVENT_DEL, func, *args, **kargs)


def on_exe_data_event_add(func, *args, **kargs):
    """Create an ecore event handler for ECORE_EXE_EVENT_DATA

       :see: EventHandler
       :see: EventHandlerExe
    """
    return EventHandlerExe(enums.ECORE_EXE_EVENT_DATA, func, *args, **kargs)


def on_exe_error_event_add(func, *args, **kargs):
    """Create an ecore event handler for ECORE_EXE_EVENT_ERROR

       :see: :py:class:`EventHandler`
       :see: :py:class:`EventHandlerExe`
    """
    return EventHandlerExe(enums.ECORE_EXE_EVENT_ERROR, func, *args, **kargs)