abstract.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # -*- coding: utf-8 -*-
  2. """Abstract classes."""
  3. from abc import ABCMeta, abstractmethod, abstractproperty
  4. from collections import Callable
  5. from typing import Any, Sequence, Tuple
  6. __all__ = ['CallableTask', 'CallableSignature']
  7. def _hasattr(C, attr):
  8. return any(attr in B.__dict__ for B in C.__mro__)
  9. class _AbstractClass(metaclass=ABCMeta):
  10. __required_attributes__ = frozenset()
  11. @classmethod
  12. def _subclasshook_using(cls, parent, C):
  13. return (
  14. cls is parent and
  15. all(_hasattr(C, attr) for attr in cls.__required_attributes__)
  16. ) or NotImplemented
  17. @classmethod
  18. def register(cls, other: Any):
  19. # we override `register` to return other for use as a decorator.
  20. type(cls).register(cls, other)
  21. return other
  22. class AbstractApp(_AbstractClass): # pragma: no cover
  23. __required_attributes = frozenset({
  24. 'close' 'start', 'task', 'AsyncResult', 'finalize',
  25. })
  26. @abstractmethod
  27. def close(self):
  28. pass
  29. @abstractmethod
  30. def start(self):
  31. pass
  32. @abstractmethod
  33. def task(self) -> 'CallableTask':
  34. pass
  35. @abstractmethod
  36. def finalize(self):
  37. pass
  38. @abstractproperty
  39. def conf(self):
  40. pass
  41. @abstractproperty
  42. def AsyncResult(self):
  43. pass
  44. class AbstractResult(_AbstractClass): # pragma: no cover
  45. __required_attributes__ = frozenset({
  46. 'as_tuple', 'forget', 'get', 'ready', 'successful', 'failed',
  47. })
  48. @abstractmethod
  49. def as_tuple(self) -> Tuple:
  50. pass
  51. @abstractmethod
  52. def forget(self) -> None:
  53. pass
  54. @abstractmethod
  55. def get(self, *args, **kwargs) -> Any:
  56. pass
  57. @abstractmethod
  58. def ready(self) -> bool:
  59. pass
  60. @abstractmethod
  61. def successful(self) -> bool:
  62. pass
  63. @abstractmethod
  64. def failed(self) -> bool:
  65. pass
  66. @abstractproperty
  67. def backend(self) -> Any:
  68. pass
  69. @abstractproperty
  70. def children(self) -> Sequence['AbstractResult']:
  71. pass
  72. @abstractproperty
  73. def result(self) -> Any:
  74. pass
  75. @abstractproperty
  76. def traceback(self) -> str:
  77. pass
  78. @abstractproperty
  79. def state(self) -> str:
  80. pass
  81. class CallableTask(_AbstractClass, Callable): # pragma: no cover
  82. __required_attributes__ = frozenset({
  83. 'delay', 'apply_async', 'apply',
  84. })
  85. @abstractmethod
  86. def delay(self, *args, **kwargs):
  87. pass
  88. @abstractmethod
  89. def apply_async(self, *args, **kwargs):
  90. pass
  91. @abstractmethod
  92. def apply(self, *args, **kwargs):
  93. pass
  94. @classmethod
  95. def __subclasshook__(cls, C):
  96. return cls._subclasshook_using(CallableTask, C)
  97. class CallableSignature(CallableTask): # pragma: no cover
  98. __required_attributes__ = frozenset({
  99. 'clone', 'freeze', 'set', 'link', 'link_error', '__or__',
  100. })
  101. @abstractproperty
  102. def name(self):
  103. pass
  104. @abstractproperty
  105. def type(self):
  106. pass
  107. @abstractproperty
  108. def app(self):
  109. pass
  110. @abstractproperty
  111. def id(self):
  112. pass
  113. @abstractproperty
  114. def task(self):
  115. pass
  116. @abstractproperty
  117. def args(self):
  118. pass
  119. @abstractproperty
  120. def kwargs(self):
  121. pass
  122. @abstractproperty
  123. def options(self):
  124. pass
  125. @abstractproperty
  126. def subtask_type(self):
  127. pass
  128. @abstractproperty
  129. def chord_size(self):
  130. pass
  131. @abstractproperty
  132. def immutable(self):
  133. pass
  134. @abstractmethod
  135. def clone(self, args=None, kwargs=None):
  136. pass
  137. @abstractmethod
  138. def freeze(self, id: str=None, group_id: str=None,
  139. chord: str=None, root_id: str=None) -> AbstractResult:
  140. pass
  141. @abstractmethod
  142. def set(self, immutable: bool=None, **options) -> 'CallableSignature':
  143. pass
  144. @abstractmethod
  145. def link(self, callback: 'CallableSignature') -> 'CallableSignature':
  146. pass
  147. @abstractmethod
  148. def link_error(self, errback: 'CallableSignature') -> 'CallableSignature':
  149. pass
  150. @abstractmethod
  151. def __or__(self, other: 'CallableSignature') -> 'CallableSignature':
  152. pass
  153. @abstractmethod
  154. def __invert__(self) -> Any:
  155. pass
  156. @classmethod
  157. def __subclasshook__(cls, C: Any) -> Any:
  158. return cls._subclasshook_using(CallableSignature, C)