abstract.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Abstract classes."""
  2. from __future__ import absolute_import, unicode_literals
  3. import abc
  4. from collections import Callable
  5. from .five import with_metaclass
  6. __all__ = ['Thenable']
  7. @with_metaclass(abc.ABCMeta)
  8. class Thenable(Callable): # pragma: no cover
  9. """Object that supports ``.then()``."""
  10. __slots__ = ()
  11. @abc.abstractmethod
  12. def then(self, on_success, on_error=None):
  13. raise NotImplementedError()
  14. @abc.abstractmethod
  15. def throw(self, exc=None, tb=None, propagate=True):
  16. raise NotImplementedError()
  17. @abc.abstractmethod
  18. def cancel(self):
  19. raise NotImplementedError()
  20. @classmethod
  21. def __subclasshook__(cls, C):
  22. if cls is Thenable:
  23. if any('then' in B.__dict__ for B in C.__mro__):
  24. return True
  25. return NotImplemented
  26. @classmethod
  27. def register(cls, other):
  28. # overide to return other so `register` can be used as a decorator
  29. type(cls).register(cls, other)
  30. return other
  31. @Thenable.register
  32. class ThenableProxy(object):
  33. """Proxy to object that supports ``.then()``."""
  34. def _set_promise_target(self, p):
  35. self._p = p
  36. def then(self, on_success, on_error=None):
  37. return self._p.then(on_success, on_error)
  38. def cancel(self):
  39. return self._p.cancel()
  40. def throw1(self, exc=None):
  41. return self._p.throw1(exc)
  42. def throw(self, exc=None, tb=None, propagate=True):
  43. return self._p.throw(exc, tb=tb, propagate=propagate)
  44. @property
  45. def cancelled(self):
  46. return self._p.cancelled
  47. @property
  48. def ready(self):
  49. return self._p.ready
  50. @property
  51. def failed(self):
  52. return self._p.failed