Browse Source

Programs --app argument module is now imported from the current directory

Ask Solem 14 years ago
parent
commit
781111e11c
3 changed files with 41 additions and 29 deletions
  1. 3 2
      celery/bin/base.py
  2. 8 25
      celery/loaders/base.py
  3. 30 2
      celery/utils/__init__.py

+ 3 - 2
celery/bin/base.py

@@ -4,6 +4,7 @@ import sys
 from optparse import OptionParser, make_option as Option
 from optparse import OptionParser, make_option as Option
 
 
 import celery
 import celery
+from celery.utils import import_from_cwd
 
 
 
 
 class Command(object):
 class Command(object):
@@ -85,8 +86,8 @@ class Command(object):
         return argv
         return argv
 
 
     def get_cls_by_name(self, name):
     def get_cls_by_name(self, name):
-        from celery.utils import get_cls_by_name
-        return get_cls_by_name(name)
+        from celery.utils import get_cls_by_name, import_from_cwd
+        return get_cls_by_name(name, imp=import_from_cwd)
 
 
     def process_cmdline_config(self, argv):
     def process_cmdline_config(self, argv):
         try:
         try:

+ 8 - 25
celery/loaders/base.py

@@ -1,10 +1,9 @@
-import os
+import importlib
 import re
 import re
-import sys
 
 
 import anyjson
 import anyjson
 
 
-from importlib import import_module as _import_module
+from celery.utils import import_from_cwd as _import_from_cwd
 
 
 BUILTIN_MODULES = ["celery.task"]
 BUILTIN_MODULES = ["celery.task"]
 
 
@@ -50,7 +49,12 @@ class BaseLoader(object):
         return self.import_from_cwd(module)
         return self.import_from_cwd(module)
 
 
     def import_module(self, module):
     def import_module(self, module):
-        return _import_module(module)
+        return importlib.import_module(module)
+
+    def import_from_cwd(self, module, imp=None):
+        if imp is None:
+            imp = self.import_module
+        return _import_from_cwd(module, imp)
 
 
     def import_default_modules(self):
     def import_default_modules(self):
         imports = self.conf.get("CELERY_IMPORTS") or []
         imports = self.conf.get("CELERY_IMPORTS") or []
@@ -108,27 +112,6 @@ class BaseLoader(object):
 
 
         return dict(map(getarg, args))
         return dict(map(getarg, args))
 
 
-    def import_from_cwd(self, module, imp=None):
-        """Import module, but make sure it finds modules
-        located in the current directory.
-
-        Modules located in the current directory has
-        precedence over modules located in ``sys.path``.
-        """
-        if imp is None:
-            imp = self.import_module
-        cwd = os.getcwd()
-        if cwd in sys.path:
-            return imp(module)
-        sys.path.insert(0, cwd)
-        try:
-            return imp(module)
-        finally:
-            try:
-                sys.path.remove(cwd)
-            except ValueError:
-                pass
-
     @property
     @property
     def conf(self):
     def conf(self):
         """Loader configuration."""
         """Loader configuration."""

+ 30 - 2
celery/utils/__init__.py

@@ -1,5 +1,7 @@
 from __future__ import generators
 from __future__ import generators
 
 
+import os
+import sys
 import time
 import time
 import operator
 import operator
 try:
 try:
@@ -300,7 +302,7 @@ def fun_takes_kwargs(fun, kwlist=[]):
     return filter(partial(operator.contains, args), kwlist)
     return filter(partial(operator.contains, args), kwlist)
 
 
 
 
-def get_cls_by_name(name, aliases={}):
+def get_cls_by_name(name, aliases={}, imp=None):
     """Get class by name.
     """Get class by name.
 
 
     The name should be the full dot-separated path to the class::
     The name should be the full dot-separated path to the class::
@@ -330,13 +332,15 @@ def get_cls_by_name(name, aliases={}):
         True
         True
 
 
     """
     """
+    if imp is None:
+        imp = importlib.import_module
 
 
     if not isinstance(name, basestring):
     if not isinstance(name, basestring):
         return name                                 # already a class
         return name                                 # already a class
 
 
     name = aliases.get(name) or name
     name = aliases.get(name) or name
     module_name, _, cls_name = rpartition(name, ".")
     module_name, _, cls_name = rpartition(name, ".")
-    module = importlib.import_module(module_name)
+    module = imp(module_name)
     return getattr(module, cls_name)
     return getattr(module, cls_name)
 
 
 get_symbol_by_name = get_cls_by_name
 get_symbol_by_name = get_cls_by_name
@@ -385,3 +389,27 @@ def isatty(fh):
 def textindent(t, indent=0):
 def textindent(t, indent=0):
         """Indent text."""
         """Indent text."""
         return "\n".join(" " * indent + p for p in t.split("\n"))
         return "\n".join(" " * indent + p for p in t.split("\n"))
+
+
+
+def import_from_cwd(module, imp=None):
+    """Import module, but make sure it finds modules
+    located in the current directory.
+
+    Modules located in the current directory has
+    precedence over modules located in ``sys.path``.
+    """
+    if imp is None:
+        imp = importlib.import_module
+    cwd = os.getcwd()
+    if cwd in sys.path:
+        return imp(module)
+    sys.path.insert(0, cwd)
+    try:
+        return imp(module)
+    finally:
+        try:
+            sys.path.remove(cwd)
+        except ValueError:
+            pass
+