annotations.py 985 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from __future__ import absolute_import
  2. from ..utils import firstmethod, instantiate, mpromise
  3. _first_match = firstmethod("annotate")
  4. _first_match_any = firstmethod("annotate_any")
  5. class MapAnnotation(dict):
  6. def annotate_any(self):
  7. try:
  8. return dict(self["*"])
  9. except KeyError:
  10. pass
  11. def annotate(self, task):
  12. try:
  13. return dict(self[task.name])
  14. except KeyError:
  15. pass
  16. def prepare(annotations):
  17. """Expands the :setting:`CELERY_ANNOTATIONS` setting."""
  18. def expand_annotation(annotation):
  19. if isinstance(annotation, dict):
  20. return MapAnnotation(annotation)
  21. elif isinstance(annotation, basestring):
  22. return mpromise(instantiate, annotation)
  23. return annotation
  24. if annotations is None:
  25. return ()
  26. elif not isinstance(annotations, (list, tuple)):
  27. annotations = (annotations, )
  28. return map(expand_annotation, annotations)