Browse Source

Adds abstract classes CallableTask and CallableSignature

Ask Solem 9 years ago
parent
commit
89f5f33e7e
3 changed files with 132 additions and 0 deletions
  1. 2 0
      celery/app/task.py
  2. 2 0
      celery/canvas.py
  3. 128 0
      celery/utils/abstract.py

+ 2 - 0
celery/app/task.py

@@ -19,6 +19,7 @@ from celery.canvas import signature
 from celery.exceptions import Ignore, MaxRetriesExceededError, Reject, Retry
 from celery.exceptions import Ignore, MaxRetriesExceededError, Reject, Retry
 from celery.five import class_property, items
 from celery.five import class_property, items
 from celery.result import EagerResult
 from celery.result import EagerResult
+from celery.utils import abstract
 from celery.utils import uuid, maybe_reraise
 from celery.utils import uuid, maybe_reraise
 from celery.utils.functional import mattrgetter, maybe_list
 from celery.utils.functional import mattrgetter, maybe_list
 from celery.utils.imports import instantiate
 from celery.utils.imports import instantiate
@@ -923,4 +924,5 @@ class Task(object):
     @property
     @property
     def __name__(self):
     def __name__(self):
         return self.__class__.__name__
         return self.__class__.__name__
+abstract.CallableTask.register(Task)
 BaseTask = Task  # compat alias
 BaseTask = Task  # compat alias

+ 2 - 0
celery/canvas.py

@@ -22,6 +22,7 @@ from kombu.utils import cached_property, fxrange, reprcall, uuid
 
 
 from celery._state import current_app, get_current_worker_task
 from celery._state import current_app, get_current_worker_task
 from celery.result import GroupResult
 from celery.result import GroupResult
+from celery.utils import abstract
 from celery.utils.functional import (
 from celery.utils.functional import (
     maybe_list, is_list, regen,
     maybe_list, is_list, regen,
     chunks as _chunks,
     chunks as _chunks,
@@ -356,6 +357,7 @@ class Signature(dict):
     subtask_type = _getitem_property('subtask_type')
     subtask_type = _getitem_property('subtask_type')
     chord_size = _getitem_property('chord_size')
     chord_size = _getitem_property('chord_size')
     immutable = _getitem_property('immutable')
     immutable = _getitem_property('immutable')
+abstract.CallableSignature.register(Signature)
 
 
 
 
 @Signature.register_type
 @Signature.register_type

+ 128 - 0
celery/utils/abstract.py

@@ -0,0 +1,128 @@
+# -*- coding: utf-8 -*-
+"""
+    celery.utils.abstract
+    ~~~~~~~~~~~~~~~~~~~~~
+
+    Abstract classes.
+
+"""
+from __future__ import absolute_import
+
+__all__ = ['CallableTask', 'CallableSignature']
+
+from abc import ABCMeta, abstractmethod, abstractproperty
+from collections import Callable
+
+from celery.five import with_metaclass
+
+
+def _hasattr(C, attr):
+    return any(attr in B.__dict__ for B in C.__mro__)
+
+
+@with_metaclass(ABCMeta)
+class _AbstractClass(object):
+    __required_attributes__ = frozenset()
+
+    @classmethod
+    def __subclasshook__(cls, C):
+        return (
+            cls is AsynCallable and
+            all(_hasattr(C, attr) for attr in cls.__required_attributes__)
+        ) or NotImplemented
+
+
+class CallableTask(_AbstractClass, Callable):
+    __required_attributes__ = frozenset({
+        'delay', 'apply_async', 'apply',
+    })
+
+    @abstractmethod
+    def delay(self, *args, **kwargs):
+        pass
+
+    @abstractmethod
+    def apply_async(self, *args, **kwargs):
+        pass
+
+    @abstractmethod
+    def apply(self, *args, **kwargs):
+        pass
+
+
+class CallableSignature(AsynCallable):
+    __required_attributes__ = frozenset({
+        'clone', 'freeze', 'set', 'link', 'link_error', '__or__',
+    })
+
+    @abstractproperty
+    def name(self):
+        pass
+
+    @abstractproperty
+    def type(self):
+        pass
+
+    @abstractproperty
+    def app(self):
+        pass
+
+    @abstractproperty
+    def id(self):
+        pass
+
+    @abstractproperty
+    def task(self):
+        pass
+
+    @abstractproperty
+    def args(self):
+        pass
+
+    @abstractproperty
+    def kwargs(self):
+        pass
+
+    @abstractproperty
+    def options(self):
+        pass
+
+    @abstractproperty
+    def subtask_type(self):
+        pass
+
+    @abstractproperty
+    def chord_size(self):
+        pass
+
+    @abstractproperty
+    def immutable(self):
+        pass
+
+    @abstractmethod
+    def clone(self, args=None, kwargs=None):
+        pass
+
+    @abstractmethod
+    def freeze(self, id=None, group_id=None, chord=None, root_id=None):
+        pass
+
+    @abstractmethod
+    def set(self, immutable=None, **options):
+        pass
+
+    @abstractmethod
+    def link(self, callback):
+        pass
+
+    @abstractmethod
+    def link_error(self, errback):
+        pass
+
+    @abstractmethod
+    def __or__(self, other):
+        pass
+
+    @abstractmethod
+    def __invert__(self):
+        pass