Explorar o código

config_from_object and config_from_envvar is now available in the base loader.

Ask Solem %!s(int64=14) %!d(string=hai) anos
pai
achega
f3b78866c7
Modificáronse 3 ficheiros con 41 adicións e 43 borrados
  1. 2 40
      celery/loaders/app.py
  2. 38 2
      celery/loaders/base.py
  3. 1 1
      celery/tests/test_app/test_loaders.py

+ 2 - 40
celery/loaders/app.py

@@ -1,48 +1,10 @@
-import os
-
-from celery.datastructures import DictAttribute
-from celery.exceptions import ImproperlyConfigured
 from celery.loaders.base import BaseLoader
-from celery.utils import get_cls_by_name
-
-
-ERROR_ENVVAR_NOT_SET = (
-"""The environment variable %r is not set,
-and as such the configuration could not be loaded.
-Please set this variable and make it point to
-a configuration module.""")
 
 
 class AppLoader(BaseLoader):
 
-    def __init__(self, *args, **kwargs):
-        self._conf = {}
-        super(AppLoader, self).__init__(*args, **kwargs)
-
-    def config_from_envvar(self, variable_name, silent=False):
-        module_name = os.environ.get(variable_name)
-        if not module_name:
-            if silent:
-                return False
-            raise ImproperlyConfigured(ERROR_ENVVAR_NOT_SET % (module_name, ))
-        return self.config_from_object(module_name, silent=silent)
-
-    def config_from_object(self, obj, silent=False):
-        if isinstance(obj, basestring):
-            try:
-                obj = get_cls_by_name(obj, imp=self.import_from_cwd)
-            except (ImportError, AttributeError):
-                if silent:
-                    return False
-                raise
-        if not hasattr(obj, "__getitem__"):
-            obj = DictAttribute(obj)
-        self._conf = obj
-        return True
-
     def on_worker_init(self):
         self.import_default_modules()
 
-    @property
-    def conf(self):
-        return self._conf
+    def read_configuration(self):
+        return {}

+ 38 - 2
celery/loaders/base.py

@@ -1,14 +1,25 @@
 import importlib
+import os
 import re
 import warnings
 
 from anyjson import deserialize
 from kombu.utils import cached_property
 
+from celery.datastructures import DictAttribute
+from celery.exceptions import ImproperlyConfigured
+from celery.utils import get_cls_by_name
 from celery.utils import import_from_cwd as _import_from_cwd
 
 BUILTIN_MODULES = ["celery.task"]
 
+ERROR_ENVVAR_NOT_SET = (
+"""The environment variable %r is not set,
+and as such the configuration could not be loaded.
+Please set this variable and make it point to
+a configuration module.""")
+
+
 
 class BaseLoader(object):
     """The base class for loaders.
@@ -29,6 +40,7 @@ class BaseLoader(object):
     worker_initialized = False
     override_backends = {}
     configured = False
+    _conf = None
 
     def __init__(self, app=None, **kwargs):
         from celery.app import app_or_default
@@ -68,6 +80,28 @@ class BaseLoader(object):
             self.worker_initialized = True
             self.on_worker_init()
 
+    def config_from_envvar(self, variable_name, silent=False):
+        module_name = os.environ.get(variable_name)
+        if not module_name:
+            if silent:
+                return False
+            raise ImproperlyConfigured(ERROR_ENVVAR_NOT_SET % (module_name, ))
+        return self.config_from_object(module_name, silent=silent)
+
+    def config_from_object(self, obj, silent=False):
+        if isinstance(obj, basestring):
+            try:
+                obj = get_cls_by_name(obj, imp=self.import_from_cwd)
+            except (ImportError, AttributeError):
+                if silent:
+                    return False
+                raise
+        if not hasattr(obj, "__getitem__"):
+            obj = DictAttribute(obj)
+        self._conf = obj
+        return True
+
+
     def cmdline_config_parser(self, args, namespace="celery",
                 re_type=re.compile(r"\((\w+)\)"),
                 extra_types={"json": deserialize},
@@ -131,10 +165,12 @@ class BaseLoader(object):
                 "Mail could not be sent: %r %r" % (
                     exc, {"To": to, "Subject": subject})))
 
-    @cached_property
+    @property
     def conf(self):
         """Loader configuration."""
-        return self.read_configuration()
+        if self._conf is None:
+            self._conf = self.read_configuration()
+        return self._conf
 
     @cached_property
     def mail(self):

+ 1 - 1
celery/tests/test_app/test_loaders.py

@@ -107,7 +107,7 @@ class TestLoaderBase(unittest.TestCase):
 
     def test_conf_property(self):
         self.assertEqual(self.loader.conf["foo"], "bar")
-        self.assertEqual(self.loader.__dict__["conf"]["foo"], "bar")
+        self.assertEqual(self.loader._conf["foo"], "bar")
         self.assertEqual(self.loader.conf["foo"], "bar")
 
     def test_import_default_modules(self):