diff --git a/urwid/main_loop.py b/urwid/main_loop.py new file mode 100644 index 0000000..8333792 --- /dev/null +++ b/urwid/main_loop.py @@ -0,0 +1,98 @@ +import typing + +from urwid import signals +from urwid.command_map import REDRAW_SCREEN, command_map +# from urwid.compat import reraise # module removed, its code is copied below +from urwid.display_common import INPUT_DESCRIPTORS_CHANGED +from urwid.util import StoppingContext, is_mouse_event +from urwid.wimp import PopUpTarget +from urwid.event_loop import EventLoop, SelectEventLoop, AsyncioEventLoop +from urwid.event_loop.main_loop import CantUseExternalLoop, ExitMainLoop, MainLoop +try: + from urwid.event_loop.twisted_loop import FileDescriptor, TwistedEventLoop, _TwistedInputDescriptor as TwistedInputDescriptor +except ImportError: + pass + +try: + from urwid.event_loop import TornadoEventLoop +except ImportError: + pass + +try: + from urwid.event_loop import GLibEventLoop +except ImportError: + pass + +try: + from urwid.event_loop import TrioEventLoop +except ImportError: + pass + +if typing.TYPE_CHECKING: + from .display_common import BaseScreen + from .widget import Widget + + +def reraise(tp, value, tb=None): + """ + Reraise an exception. + Taken from "six" library (https://pythonhosted.org/six/). + """ + try: + if value is None: + value = tp() + if value.__traceback__ is not tb: + raise value.with_traceback(tb) + raise value + finally: + value = None + tb = None + + +def _refl(name: str, rval=None, exit=False): + """ + This function is used to test the main loop classes. + >>> scr = _refl("screen") + >>> scr.function("argument") + screen.function('argument') + >>> scr.callme(when="now") + screen.callme(when='now') + >>> scr.want_something_rval = 42 + >>> x = scr.want_something() + screen.want_something() + >>> x + 42 + """ + class Reflect: + def __init__(self, name: int, rval=None): + self._name = name + self._rval = rval + + def __call__(self, *argl, **argd): + args = ", ".join([repr(a) for a in argl]) + if args and argd: + args = f"{args}, " + args = args + \ + ", ".join([f"{k}={repr(v)}" for k, v in argd.items()]) + print(f"{self._name}({args})") + if exit: + raise ExitMainLoop() + return self._rval + + def __getattr__(self, attr): + if attr.endswith("_rval"): + raise AttributeError() + # print(self._name+"."+attr) + if hasattr(self, f"{attr}_rval"): + return Reflect(f"{self._name}.{attr}", getattr(self, f"{attr}_rval")) + return Reflect(f"{self._name}.{attr}") + return Reflect(name) + + +def _test(): + import doctest + doctest.testmod() + + +if __name__ == '__main__': + _test()