Skip to content

Commit

Permalink
Show helpful error when user gives config name ending with '.py'
Browse files Browse the repository at this point in the history
  • Loading branch information
ask committed Jan 27, 2012
1 parent b0077db commit 977e2f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 14 additions & 1 deletion celery/loaders/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@

from ..datastructures import AttributeDict
from ..exceptions import NotConfigured
from ..utils import find_module
from ..utils import find_module, NotAPackage

from .base import BaseLoader

DEFAULT_CONFIG_MODULE = "celeryconfig"

CONFIG_INVALID_NAME = """
Error: Module '%(module)s' doesn't exist, or it's not a valid \
Python module name.
"""

CONFIG_WITH_SUFFIX = CONFIG_INVALID_NAME + """
Did you mean '%(suggest)s'?
"""

class Loader(BaseLoader):
"""The loader used by the default app."""
Expand All @@ -39,6 +47,11 @@ def read_configuration(self):
DEFAULT_CONFIG_MODULE)
try:
self.find_module(configname)
except NotAPackage:
if configname.endswith('.py'):
raise NotAPackage(CONFIG_WITH_SUFFIX % {
"module": configname, "suggest": configname[:-3]})
raise NotAPackage(CONFIG_INVALID_NAME % {"module": configname})
except ImportError:
warnings.warn(NotConfigured(
"No %r module found! Please make sure it exists and "
Expand Down
10 changes: 9 additions & 1 deletion celery/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ def cwd_in_path():
pass


class NotAPackage(Exception):
pass


def find_module(module, path=None, imp=None):
"""Version of :func:`imp.find_module` supporting dots."""
if imp is None:
Expand All @@ -364,7 +368,11 @@ def find_module(module, path=None, imp=None):
last = None
parts = module.split(".")
for i, part in enumerate(parts[:-1]):
path = imp(".".join(parts[:i + 1])).__path__
mpart = imp(".".join(parts[:i + 1]))
try:
path = mpart.__path__
except AttributeError:
raise NotAPackage(module)
last = _imp.find_module(parts[i + 1], path)
return last
return _imp.find_module(module)
Expand Down

0 comments on commit 977e2f7

Please sign in to comment.