From e49552e4449bc02daa2aa08738a29810dbcd07cb Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 10 Apr 2021 11:11:59 +0200 Subject: refactor: rename poezio.asyncio module no clash with stdlib pls --- poezio/asyncio.py | 43 ------------------------------------------- poezio/asyncio_fix.py | 43 +++++++++++++++++++++++++++++++++++++++++++ poezio/poezio.py | 2 +- 3 files changed, 44 insertions(+), 44 deletions(-) delete mode 100644 poezio/asyncio.py create mode 100644 poezio/asyncio_fix.py diff --git a/poezio/asyncio.py b/poezio/asyncio.py deleted file mode 100644 index d333ffa6..00000000 --- a/poezio/asyncio.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -A module that monkey patches the standard asyncio module to add an -idle_call() method to the main loop. This method is used to execute a -callback whenever the loop is not busy handling anything else. This means -that it is a callback with lower priority than IO, timer, or even -call_soon() ones. These callback are called only once each. -""" - -import asyncio -import functools -import collections -from asyncio import events - -import slixmpp - - -def monkey_patch_asyncio_slixmpp(): - def idle_call(self, callback): - if asyncio.iscoroutinefunction(callback): - raise TypeError("coroutines cannot be used with idle_call()") - handle = events.Handle(callback, [], self) - self._idle.append(handle) - - def my_run_once(self): - if self._idle: - self._ready.append(events.Handle(lambda: None, (), self)) - real_run_once(self) - if self._idle: - handle = self._idle.popleft() - handle._run() - - cls = asyncio.get_event_loop().__class__ - cls._idle = collections.deque() - cls.idle_call = idle_call - real_run_once = cls._run_once - cls._run_once = my_run_once - - spawn_event = slixmpp.xmlstream.XMLStream._spawn_event - - def patchy(self, xml): - self.loop.idle_call(functools.partial(spawn_event, self, xml)) - - slixmpp.xmlstream.XMLStream._spawn_event = patchy diff --git a/poezio/asyncio_fix.py b/poezio/asyncio_fix.py new file mode 100644 index 00000000..d333ffa6 --- /dev/null +++ b/poezio/asyncio_fix.py @@ -0,0 +1,43 @@ +""" +A module that monkey patches the standard asyncio module to add an +idle_call() method to the main loop. This method is used to execute a +callback whenever the loop is not busy handling anything else. This means +that it is a callback with lower priority than IO, timer, or even +call_soon() ones. These callback are called only once each. +""" + +import asyncio +import functools +import collections +from asyncio import events + +import slixmpp + + +def monkey_patch_asyncio_slixmpp(): + def idle_call(self, callback): + if asyncio.iscoroutinefunction(callback): + raise TypeError("coroutines cannot be used with idle_call()") + handle = events.Handle(callback, [], self) + self._idle.append(handle) + + def my_run_once(self): + if self._idle: + self._ready.append(events.Handle(lambda: None, (), self)) + real_run_once(self) + if self._idle: + handle = self._idle.popleft() + handle._run() + + cls = asyncio.get_event_loop().__class__ + cls._idle = collections.deque() + cls.idle_call = idle_call + real_run_once = cls._run_once + cls._run_once = my_run_once + + spawn_event = slixmpp.xmlstream.XMLStream._spawn_event + + def patchy(self, xml): + self.loop.idle_call(functools.partial(spawn_event, self, xml)) + + slixmpp.xmlstream.XMLStream._spawn_event = patchy diff --git a/poezio/poezio.py b/poezio/poezio.py index 361e75eb..1954acd9 100644 --- a/poezio/poezio.py +++ b/poezio/poezio.py @@ -94,7 +94,7 @@ def main(): config.check_config() sys.exit(0) - from poezio.asyncio import monkey_patch_asyncio_slixmpp + from poezio.asyncio_fix import monkey_patch_asyncio_slixmpp monkey_patch_asyncio_slixmpp() from poezio import theming -- cgit v1.2.3