objects.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.utils.objects
  4. ~~~~~~~~~~~~~~~~~~~~
  5. Object related utilities including introspection, etc.
  6. """
  7. from __future__ import absolute_import
  8. __all__ = ['mro_lookup']
  9. def mro_lookup(cls, attr, stop=(), monkey_patched=[]):
  10. """Return the first node by MRO order that defines an attribute.
  11. :keyword stop: A list of types that if reached will stop the search.
  12. :keyword monkey_patched: Use one of the stop classes if the attr's
  13. module origin is not in this list, this to detect monkey patched
  14. attributes.
  15. :returns None: if the attribute was not found.
  16. """
  17. for node in cls.mro():
  18. if node in stop:
  19. try:
  20. attr = node.__dict__[attr]
  21. module_origin = attr.__module__
  22. except (AttributeError, KeyError):
  23. pass
  24. else:
  25. if module_origin not in monkey_patched:
  26. return node
  27. return
  28. if attr in node.__dict__:
  29. return node