abstract.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # -*- coding: utf-8 -*-
  2. """Abstract classes."""
  3. from __future__ import absolute_import, unicode_literals
  4. from abc import ABCMeta, abstractmethod, abstractproperty
  5. from collections import Callable
  6. from celery.five import with_metaclass
  7. __all__ = ('CallableTask', 'CallableSignature')
  8. def _hasattr(C, attr):
  9. return any(attr in B.__dict__ for B in C.__mro__)
  10. @with_metaclass(ABCMeta)
  11. class _AbstractClass(object):
  12. __required_attributes__ = frozenset()
  13. @classmethod
  14. def _subclasshook_using(cls, parent, C):
  15. return (
  16. cls is parent and
  17. all(_hasattr(C, attr) for attr in cls.__required_attributes__)
  18. ) or NotImplemented
  19. @classmethod
  20. def register(cls, other):
  21. # we override `register` to return other for use as a decorator.
  22. type(cls).register(cls, other)
  23. return other
  24. class CallableTask(_AbstractClass, Callable): # pragma: no cover
  25. """Task interface."""
  26. __required_attributes__ = frozenset({
  27. 'delay', 'apply_async', 'apply',
  28. })
  29. @abstractmethod
  30. def delay(self, *args, **kwargs):
  31. pass
  32. @abstractmethod
  33. def apply_async(self, *args, **kwargs):
  34. pass
  35. @abstractmethod
  36. def apply(self, *args, **kwargs):
  37. pass
  38. @classmethod
  39. def __subclasshook__(cls, C):
  40. return cls._subclasshook_using(CallableTask, C)
  41. class CallableSignature(CallableTask): # pragma: no cover
  42. """Celery Signature interface."""
  43. __required_attributes__ = frozenset({
  44. 'clone', 'freeze', 'set', 'link', 'link_error', '__or__',
  45. })
  46. @abstractproperty
  47. def name(self):
  48. pass
  49. @abstractproperty
  50. def type(self):
  51. pass
  52. @abstractproperty
  53. def app(self):
  54. pass
  55. @abstractproperty
  56. def id(self):
  57. pass
  58. @abstractproperty
  59. def task(self):
  60. pass
  61. @abstractproperty
  62. def args(self):
  63. pass
  64. @abstractproperty
  65. def kwargs(self):
  66. pass
  67. @abstractproperty
  68. def options(self):
  69. pass
  70. @abstractproperty
  71. def subtask_type(self):
  72. pass
  73. @abstractproperty
  74. def chord_size(self):
  75. pass
  76. @abstractproperty
  77. def immutable(self):
  78. pass
  79. @abstractmethod
  80. def clone(self, args=None, kwargs=None):
  81. pass
  82. @abstractmethod
  83. def freeze(self, id=None, group_id=None, chord=None, root_id=None):
  84. pass
  85. @abstractmethod
  86. def set(self, immutable=None, **options):
  87. pass
  88. @abstractmethod
  89. def link(self, callback):
  90. pass
  91. @abstractmethod
  92. def link_error(self, errback):
  93. pass
  94. @abstractmethod
  95. def __or__(self, other):
  96. pass
  97. @abstractmethod
  98. def __invert__(self):
  99. pass
  100. @classmethod
  101. def __subclasshook__(cls, C):
  102. return cls._subclasshook_using(CallableSignature, C)