base.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import unicode_literals
  2. from abc import ABCMeta, abstractmethod
  3. from six import with_metaclass
  4. __all__ = (
  5. 'EventLoop',
  6. 'INPUT_TIMEOUT',
  7. )
  8. #: When to trigger the `onInputTimeout` event.
  9. INPUT_TIMEOUT = .5
  10. class EventLoop(with_metaclass(ABCMeta, object)):
  11. """
  12. Eventloop interface.
  13. """
  14. def run(self, stdin, callbacks):
  15. """
  16. Run the eventloop until stop() is called. Report all
  17. input/timeout/terminal-resize events to the callbacks.
  18. :param stdin: :class:`~prompt_toolkit.input.Input` instance.
  19. :param callbacks: :class:`~prompt_toolkit.eventloop.callbacks.EventLoopCallbacks` instance.
  20. """
  21. raise NotImplementedError("This eventloop doesn't implement synchronous 'run()'.")
  22. def run_as_coroutine(self, stdin, callbacks):
  23. """
  24. Similar to `run`, but this is a coroutine. (For asyncio integration.)
  25. """
  26. raise NotImplementedError("This eventloop doesn't implement 'run_as_coroutine()'.")
  27. @abstractmethod
  28. def stop(self):
  29. """
  30. Stop the `run` call. (Normally called by
  31. :class:`~prompt_toolkit.interface.CommandLineInterface`, when a result
  32. is available, or Abort/Quit has been called.)
  33. """
  34. @abstractmethod
  35. def close(self):
  36. """
  37. Clean up of resources. Eventloop cannot be reused a second time after
  38. this call.
  39. """
  40. @abstractmethod
  41. def add_reader(self, fd, callback):
  42. """
  43. Start watching the file descriptor for read availability and then call
  44. the callback.
  45. """
  46. @abstractmethod
  47. def remove_reader(self, fd):
  48. """
  49. Stop watching the file descriptor for read availability.
  50. """
  51. @abstractmethod
  52. def run_in_executor(self, callback):
  53. """
  54. Run a long running function in a background thread. (This is
  55. recommended for code that could block the event loop.)
  56. Similar to Twisted's ``deferToThread``.
  57. """
  58. @abstractmethod
  59. def call_from_executor(self, callback, _max_postpone_until=None):
  60. """
  61. Call this function in the main event loop. Similar to Twisted's
  62. ``callFromThread``.
  63. :param _max_postpone_until: `None` or `time.time` value. For interal
  64. use. If the eventloop is saturated, consider this task to be low
  65. priority and postpone maximum until this timestamp. (For instance,
  66. repaint is done using low priority.)
  67. Note: In the past, this used to be a datetime.datetime instance,
  68. but apparently, executing `time.time` is more efficient: it
  69. does fewer system calls. (It doesn't read /etc/localtime.)
  70. """