mirror of
https://github.com/jazzband/django-configurations.git
synced 2026-03-16 22:20:27 +00:00
Drastically simplified exception messages in the importer.
Less eyebleed® included!
This commit is contained in:
parent
dacca7e073
commit
3848e6b818
3 changed files with 22 additions and 43 deletions
|
|
@ -129,7 +129,8 @@ class ConfigurationImporter(object):
|
|||
def find_module(self, fullname, path=None):
|
||||
if fullname is not None and fullname == self.module:
|
||||
module = fullname.rsplit('.', 1)[-1]
|
||||
return ConfigurationLoader(self.name, imp.find_module(module, path))
|
||||
return ConfigurationLoader(self.name,
|
||||
imp.find_module(module, path))
|
||||
return None
|
||||
|
||||
|
||||
|
|
@ -149,50 +150,29 @@ class ConfigurationLoader(object):
|
|||
try:
|
||||
cls = getattr(mod, self.name)
|
||||
except AttributeError as err: # pragma: no cover
|
||||
reraise(err, "While trying to find the '{0}' "
|
||||
"settings in module '{1}'".format(self.name,
|
||||
mod.__package__))
|
||||
reraise(err, "Couldn't find configuration '{0}' "
|
||||
"in module '{1}'".format(self.name,
|
||||
mod.__package__))
|
||||
try:
|
||||
cls.pre_setup()
|
||||
except Exception as err:
|
||||
reraise(err, "While calling '{0}.pre_setup()'".format(cls_path))
|
||||
|
||||
try:
|
||||
cls.setup()
|
||||
except Exception as err:
|
||||
reraise(err, "While calling the '{0}.setup()'".format(cls_path))
|
||||
|
||||
try:
|
||||
obj = cls()
|
||||
except Exception as err:
|
||||
reraise(err, "While initializing the '{0}' "
|
||||
"configuration".format(cls_path))
|
||||
|
||||
try:
|
||||
attributes = uppercase_attributes(obj).items()
|
||||
except Exception as err:
|
||||
reraise(err, "While getting the items "
|
||||
"of the '{0}' configuration".format(cls_path))
|
||||
|
||||
for name, value in attributes:
|
||||
if callable(value) and not getattr(value, 'pristine', False):
|
||||
try:
|
||||
for name, value in attributes:
|
||||
if callable(value) and not getattr(value, 'pristine', False):
|
||||
value = value()
|
||||
except Exception as err:
|
||||
reraise(err, "While calling '{0}.{1}'".format(cls_path,
|
||||
value))
|
||||
# in case a method returns a Value instance we have
|
||||
# to do the same as the Configuration.setup method
|
||||
if isinstance(value, Value):
|
||||
setup_value(mod, name, value)
|
||||
continue
|
||||
setattr(mod, name, value)
|
||||
# in case a method returns a Value instance we have
|
||||
# to do the same as the Configuration.setup method
|
||||
if isinstance(value, Value):
|
||||
setup_value(mod, name, value)
|
||||
continue
|
||||
setattr(mod, name, value)
|
||||
|
||||
setattr(mod, 'CONFIGURATION', '{0}.{1}'.format(fullname, self.name))
|
||||
|
||||
try:
|
||||
setattr(mod, 'CONFIGURATION', '{0}.{1}'.format(fullname,
|
||||
self.name))
|
||||
cls.post_setup()
|
||||
|
||||
except Exception as err:
|
||||
reraise(err, "While calling '{0}.post_setup()'".format(cls_path))
|
||||
reraise(err, "Couldn't setup configuration '{0}'".format(cls_path))
|
||||
|
||||
return mod
|
||||
|
|
|
|||
|
|
@ -16,8 +16,9 @@ def uppercase_attributes(obj):
|
|||
|
||||
def import_by_path(dotted_path, error_prefix=''):
|
||||
"""
|
||||
Import a dotted module path and return the attribute/class designated by the
|
||||
last name in the path. Raise ImproperlyConfigured if something goes wrong.
|
||||
Import a dotted module path and return the attribute/class designated by
|
||||
the last name in the path. Raise ImproperlyConfigured if something goes
|
||||
wrong.
|
||||
|
||||
Backported from Django 1.6.
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -13,13 +13,11 @@ from .utils import import_by_path
|
|||
|
||||
def setup_value(target, name, value):
|
||||
actual_value = value.setup(name)
|
||||
# overwriting the original Value class with the result
|
||||
setattr(target, name, actual_value)
|
||||
if value.multiple:
|
||||
# overwriting the original Value class with the result
|
||||
setattr(target, name, actual_value)
|
||||
for multiple_name, multiple_value in actual_value.items():
|
||||
setattr(target, multiple_name, multiple_value)
|
||||
else:
|
||||
setattr(target, name, actual_value)
|
||||
|
||||
|
||||
class Value(object):
|
||||
|
|
|
|||
Loading…
Reference in a new issue