default.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.loaders.default
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. The default loader used when no custom app has been initialized.
  6. :copyright: (c) 2009 - 2011 by Ask Solem.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from __future__ import absolute_import
  10. import os
  11. import warnings
  12. from ..datastructures import AttributeDict
  13. from ..exceptions import NotConfigured
  14. from ..utils import find_module
  15. from .base import BaseLoader
  16. DEFAULT_CONFIG_MODULE = "celeryconfig"
  17. class Loader(BaseLoader):
  18. """The loader used by the default app."""
  19. def setup_settings(self, settingsdict):
  20. return AttributeDict(settingsdict)
  21. def find_module(self, module):
  22. return find_module(module)
  23. def read_configuration(self):
  24. """Read configuration from :file:`celeryconfig.py` and configure
  25. celery and Django so it can be used by regular Python."""
  26. configname = os.environ.get("CELERY_CONFIG_MODULE",
  27. DEFAULT_CONFIG_MODULE)
  28. try:
  29. self.find_module(configname)
  30. except ImportError:
  31. warnings.warn(NotConfigured(
  32. "No %r module found! Please make sure it exists and "
  33. "is available to Python." % (configname, )))
  34. return self.setup_settings({})
  35. else:
  36. celeryconfig = self.import_from_cwd(configname)
  37. usercfg = dict((key, getattr(celeryconfig, key))
  38. for key in dir(celeryconfig)
  39. if self.wanted_module_item(key))
  40. self.configured = True
  41. return self.setup_settings(usercfg)
  42. def wanted_module_item(self, item):
  43. return item[0].isupper() and not item.startswith("_")
  44. def on_worker_init(self):
  45. """Imports modules at worker init so tasks can be registered
  46. and used by the worked.
  47. The list of modules to import is taken from the
  48. :setting:`CELERY_IMPORTS` setting.
  49. """
  50. self.import_default_modules()