abstract.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.utils.abstract
  4. ~~~~~~~~~~~~~~~~~~~~~
  5. Abstract classes.
  6. """
  7. from __future__ import absolute_import
  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. class CallableTask(_AbstractClass, Callable):
  24. __required_attributes__ = frozenset({
  25. 'delay', 'apply_async', 'apply',
  26. })
  27. @abstractmethod
  28. def delay(self, *args, **kwargs):
  29. pass
  30. @abstractmethod
  31. def apply_async(self, *args, **kwargs):
  32. pass
  33. @abstractmethod
  34. def apply(self, *args, **kwargs):
  35. pass
  36. @classmethod
  37. def __subclasshook__(cls, C):
  38. return cls._subclasshook_using(CallableTask, C)
  39. class CallableSignature(CallableTask):
  40. __required_attributes__ = frozenset({
  41. 'clone', 'freeze', 'set', 'link', 'link_error', '__or__',
  42. })
  43. @abstractproperty
  44. def name(self):
  45. pass
  46. @abstractproperty
  47. def type(self):
  48. pass
  49. @abstractproperty
  50. def app(self):
  51. pass
  52. @abstractproperty
  53. def id(self):
  54. pass
  55. @abstractproperty
  56. def task(self):
  57. pass
  58. @abstractproperty
  59. def args(self):
  60. pass
  61. @abstractproperty
  62. def kwargs(self):
  63. pass
  64. @abstractproperty
  65. def options(self):
  66. pass
  67. @abstractproperty
  68. def subtask_type(self):
  69. pass
  70. @abstractproperty
  71. def chord_size(self):
  72. pass
  73. @abstractproperty
  74. def immutable(self):
  75. pass
  76. @abstractmethod
  77. def clone(self, args=None, kwargs=None):
  78. pass
  79. @abstractmethod
  80. def freeze(self, id=None, group_id=None, chord=None, root_id=None):
  81. pass
  82. @abstractmethod
  83. def set(self, immutable=None, **options):
  84. pass
  85. @abstractmethod
  86. def link(self, callback):
  87. pass
  88. @abstractmethod
  89. def link_error(self, errback):
  90. pass
  91. @abstractmethod
  92. def __or__(self, other):
  93. pass
  94. @abstractmethod
  95. def __invert__(self):
  96. pass
  97. @classmethod
  98. def __subclasshook__(cls, C):
  99. return cls._subclasshook_using(CallableSignature, C)