default.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.loaders.default
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. The default loader used when no custom app has been initialized.
  6. """
  7. from __future__ import absolute_import
  8. import os
  9. import sys
  10. import warnings
  11. from celery.datastructures import AttributeDict
  12. from celery.exceptions import NotConfigured
  13. from celery.utils import strtobool
  14. from celery.utils.imports import NotAPackage, find_module
  15. from .base import BaseLoader
  16. DEFAULT_CONFIG_MODULE = 'celeryconfig'
  17. #: Warns if configuration file is missing if :envvar:`C_WNOCONF` is set.
  18. C_WNOCONF = strtobool(os.environ.get('C_WNOCONF', False))
  19. CONFIG_INVALID_NAME = """
  20. Error: Module '%(module)s' doesn't exist, or it's not a valid \
  21. Python module name.
  22. """
  23. CONFIG_WITH_SUFFIX = CONFIG_INVALID_NAME + """
  24. Did you mean '%(suggest)s'?
  25. """
  26. class Loader(BaseLoader):
  27. """The loader used by the default app."""
  28. def setup_settings(self, settingsdict):
  29. return AttributeDict(settingsdict)
  30. def find_module(self, module):
  31. return find_module(module)
  32. def read_configuration(self):
  33. """Read configuration from :file:`celeryconfig.py` and configure
  34. celery and Django so it can be used by regular Python."""
  35. configname = os.environ.get('CELERY_CONFIG_MODULE',
  36. DEFAULT_CONFIG_MODULE)
  37. try:
  38. self.find_module(configname)
  39. except NotAPackage:
  40. if configname.endswith('.py'):
  41. raise NotAPackage, NotAPackage(
  42. CONFIG_WITH_SUFFIX % {
  43. 'module': configname,
  44. 'suggest': configname[:-3]}), sys.exc_info()[2]
  45. raise NotAPackage, NotAPackage(
  46. CONFIG_INVALID_NAME % {
  47. 'module': configname}), sys.exc_info()[2]
  48. except ImportError:
  49. # billiard sets this if forked using execv
  50. if C_WNOCONF and not os.environ.get('FORKED_BY_MULTIPROCESSING'):
  51. warnings.warn(NotConfigured(
  52. 'No %r module found! Please make sure it exists and '
  53. 'is available to Python.' % (configname, )))
  54. return self.setup_settings({})
  55. else:
  56. celeryconfig = self.import_from_cwd(configname)
  57. usercfg = dict((key, getattr(celeryconfig, key))
  58. for key in dir(celeryconfig)
  59. if self.wanted_module_item(key))
  60. self.configured = True
  61. return self.setup_settings(usercfg)
  62. def wanted_module_item(self, item):
  63. return item[0].isupper() and not item.startswith('_')