default.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # -*- coding: utf-8 -*-
  2. """The default loader used when no custom app has been initialized."""
  3. import os
  4. import warnings
  5. from celery.exceptions import NotConfigured
  6. from celery.utils.collections import DictAttribute
  7. from celery.utils.serialization import strtobool
  8. from .base import BaseLoader
  9. __all__ = ['Loader', 'DEFAULT_CONFIG_MODULE']
  10. DEFAULT_CONFIG_MODULE = 'celeryconfig'
  11. #: Warns if configuration file is missing if :envvar:`C_WNOCONF` is set.
  12. C_WNOCONF = strtobool(os.environ.get('C_WNOCONF', False))
  13. class Loader(BaseLoader):
  14. """The loader used by the default app."""
  15. def setup_settings(self, settingsdict):
  16. return DictAttribute(settingsdict)
  17. def read_configuration(self, fail_silently=True):
  18. """Read configuration from :file:`celeryconfig.py` and configure
  19. celery and Django so it can be used by regular Python."""
  20. configname = os.environ.get('CELERY_CONFIG_MODULE',
  21. DEFAULT_CONFIG_MODULE)
  22. try:
  23. usercfg = self._import_config_module(configname)
  24. except ImportError:
  25. if not fail_silently:
  26. raise
  27. # billiard sets this if forked using execv
  28. if C_WNOCONF and not os.environ.get('FORKED_BY_MULTIPROCESSING'):
  29. warnings.warn(NotConfigured(
  30. 'No {module} module found! Please make sure it exists and '
  31. 'is available to Python.'.format(module=configname)))
  32. return self.setup_settings({})
  33. else:
  34. self.configured = True
  35. return self.setup_settings(usercfg)