Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Abstract classes.""" 

2from __future__ import absolute_import, unicode_literals 

3 

4import abc 

5 

6from .five import with_metaclass, Callable 

7 

8__all__ = ['Thenable'] 

9 

10 

11@with_metaclass(abc.ABCMeta) 

12class Thenable(Callable): # pragma: no cover 

13 """Object that supports ``.then()``.""" 

14 

15 __slots__ = () 

16 

17 @abc.abstractmethod 

18 def then(self, on_success, on_error=None): 

19 raise NotImplementedError() 

20 

21 @abc.abstractmethod 

22 def throw(self, exc=None, tb=None, propagate=True): 

23 raise NotImplementedError() 

24 

25 @abc.abstractmethod 

26 def cancel(self): 

27 raise NotImplementedError() 

28 

29 @classmethod 

30 def __subclasshook__(cls, C): 

31 if cls is Thenable: 

32 if any('then' in B.__dict__ for B in C.__mro__): 

33 return True 

34 return NotImplemented 

35 

36 @classmethod 

37 def register(cls, other): 

38 # overide to return other so `register` can be used as a decorator 

39 type(cls).register(cls, other) 

40 return other 

41 

42 

43@Thenable.register 

44class ThenableProxy(object): 

45 """Proxy to object that supports ``.then()``.""" 

46 

47 def _set_promise_target(self, p): 

48 self._p = p 

49 

50 def then(self, on_success, on_error=None): 

51 return self._p.then(on_success, on_error) 

52 

53 def cancel(self): 

54 return self._p.cancel() 

55 

56 def throw1(self, exc=None): 

57 return self._p.throw1(exc) 

58 

59 def throw(self, exc=None, tb=None, propagate=True): 

60 return self._p.throw(exc, tb=tb, propagate=propagate) 

61 

62 @property 

63 def cancelled(self): 

64 return self._p.cancelled 

65 

66 @property 

67 def ready(self): 

68 return self._p.ready 

69 

70 @property 

71 def failed(self): 

72 return self._p.failed