abstract.py 1.7 KB

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