abstract.py 2.8 KB

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