1 |
commit: 24f861173ebe747a470deb8489887c067cd46b0f |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Apr 2 03:46:10 2018 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Sun Apr 8 22:04:37 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=24f86117 |
7 |
|
8 |
EventLoop: implement add/remove_reader/writer for asyncio compat (bug 649588) |
9 |
|
10 |
Bug: https://bugs.gentoo.org/649588 |
11 |
|
12 |
pym/portage/util/_eventloop/EventLoop.py | 51 ++++++++++++++++++++++++++++++++ |
13 |
1 file changed, 51 insertions(+) |
14 |
|
15 |
diff --git a/pym/portage/util/_eventloop/EventLoop.py b/pym/portage/util/_eventloop/EventLoop.py |
16 |
index 1bf606354..00568c997 100644 |
17 |
--- a/pym/portage/util/_eventloop/EventLoop.py |
18 |
+++ b/pym/portage/util/_eventloop/EventLoop.py |
19 |
@@ -93,6 +93,21 @@ class EventLoop(object): |
20 |
self._callback(*self._args) |
21 |
return False |
22 |
|
23 |
+ class _repeat_callback(object): |
24 |
+ """ |
25 |
+ Wraps an callback, and always returns True, for callbacks that |
26 |
+ are supposed to run repeatedly. |
27 |
+ """ |
28 |
+ __slots__ = ("_args", "_callback") |
29 |
+ |
30 |
+ def __init__(self, callback, args): |
31 |
+ self._callback = callback |
32 |
+ self._args = args |
33 |
+ |
34 |
+ def __call__(self, fd, event): |
35 |
+ self._callback(*self._args) |
36 |
+ return True |
37 |
+ |
38 |
def __init__(self, main=True): |
39 |
""" |
40 |
@param main: If True then this is a singleton instance for use |
41 |
@@ -569,6 +584,42 @@ class EventLoop(object): |
42 |
|
43 |
return bool(calls) |
44 |
|
45 |
+ def add_reader(self, fd, callback, *args): |
46 |
+ """ |
47 |
+ Start watching the file descriptor for read availability and then |
48 |
+ call the callback with specified arguments. |
49 |
+ |
50 |
+ Use functools.partial to pass keywords to the callback. |
51 |
+ """ |
52 |
+ self.io_add_watch(fd, self.IO_IN, self._repeat_callback(callback, args)) |
53 |
+ |
54 |
+ def remove_reader(self, fd): |
55 |
+ """ |
56 |
+ Stop watching the file descriptor for read availability. |
57 |
+ """ |
58 |
+ handler = self._poll_event_handlers.get(fd) |
59 |
+ if fd is not None: |
60 |
+ return self.source_remove(handler.source_id) |
61 |
+ return False |
62 |
+ |
63 |
+ def add_writer(self, fd, callback, *args): |
64 |
+ """ |
65 |
+ Start watching the file descriptor for write availability and then |
66 |
+ call the callback with specified arguments. |
67 |
+ |
68 |
+ Use functools.partial to pass keywords to the callback. |
69 |
+ """ |
70 |
+ self.io_add_watch(fd, self.IO_OUT, self._repeat_callback(callback, args)) |
71 |
+ |
72 |
+ def remove_writer(self, fd): |
73 |
+ """ |
74 |
+ Stop watching the file descriptor for write availability. |
75 |
+ """ |
76 |
+ handler = self._poll_event_handlers.get(fd) |
77 |
+ if fd is not None: |
78 |
+ return self.source_remove(handler.source_id) |
79 |
+ return False |
80 |
+ |
81 |
def io_add_watch(self, f, condition, callback, *args): |
82 |
""" |
83 |
Like glib.io_add_watch(), your function should return False to |