mirror of
https://github.com/jazzband/django-configurations.git
synced 2026-03-16 22:20:27 +00:00
Merge pull request #386 from adamchainz/pep_451
Move to PEP-451 style loader
This commit is contained in:
commit
57b6827ae3
6 changed files with 99 additions and 67 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
import importlib.util
|
|
||||||
from importlib.machinery import PathFinder
|
from importlib.machinery import PathFinder
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
@ -47,12 +46,12 @@ def install(check_options=False):
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
base.BaseCommand.create_parser = create_parser
|
base.BaseCommand.create_parser = create_parser
|
||||||
importer = ConfigurationImporter(check_options=check_options)
|
importer = ConfigurationFinder(check_options=check_options)
|
||||||
sys.meta_path.insert(0, importer)
|
sys.meta_path.insert(0, importer)
|
||||||
installed = True
|
installed = True
|
||||||
|
|
||||||
|
|
||||||
class ConfigurationImporter:
|
class ConfigurationFinder(PathFinder):
|
||||||
modvar = SETTINGS_ENVIRONMENT_VARIABLE
|
modvar = SETTINGS_ENVIRONMENT_VARIABLE
|
||||||
namevar = CONFIGURATION_ENVIRONMENT_VARIABLE
|
namevar = CONFIGURATION_ENVIRONMENT_VARIABLE
|
||||||
error_msg = ("Configuration cannot be imported, "
|
error_msg = ("Configuration cannot be imported, "
|
||||||
|
|
@ -71,7 +70,7 @@ class ConfigurationImporter:
|
||||||
self.announce()
|
self.announce()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<ConfigurationImporter for '{}.{}'>".format(self.module,
|
return "<ConfigurationFinder for '{}.{}'>".format(self.module,
|
||||||
self.name)
|
self.name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -129,56 +128,53 @@ class ConfigurationImporter:
|
||||||
|
|
||||||
def find_spec(self, fullname, path=None, target=None):
|
def find_spec(self, fullname, path=None, target=None):
|
||||||
if fullname is not None and fullname == self.module:
|
if fullname is not None and fullname == self.module:
|
||||||
spec = PathFinder.find_spec(fullname, path)
|
spec = super().find_spec(fullname, path, target)
|
||||||
if spec is not None:
|
if spec is not None:
|
||||||
return importlib.machinery.ModuleSpec(spec.name,
|
wrap_loader(spec.loader, self.name)
|
||||||
ConfigurationLoader(self.name, spec),
|
return spec
|
||||||
origin=spec.origin)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigurationLoader:
|
|
||||||
|
|
||||||
def __init__(self, name, spec):
|
|
||||||
self.name = name
|
|
||||||
self.spec = spec
|
|
||||||
|
|
||||||
def load_module(self, fullname):
|
|
||||||
if fullname in sys.modules:
|
|
||||||
mod = sys.modules[fullname] # pragma: no cover
|
|
||||||
else:
|
else:
|
||||||
mod = importlib.util.module_from_spec(self.spec)
|
return None
|
||||||
sys.modules[fullname] = mod
|
|
||||||
self.spec.loader.exec_module(mod)
|
|
||||||
|
|
||||||
cls_path = f'{mod.__name__}.{self.name}'
|
|
||||||
|
|
||||||
try:
|
def wrap_loader(loader, class_name):
|
||||||
cls = getattr(mod, self.name)
|
class ConfigurationLoader(loader.__class__):
|
||||||
except AttributeError as err: # pragma: no cover
|
def exec_module(self, module):
|
||||||
reraise(err, "Couldn't find configuration '{}' "
|
super().exec_module(module)
|
||||||
"in module '{}'".format(self.name,
|
|
||||||
mod.__package__))
|
|
||||||
try:
|
|
||||||
cls.pre_setup()
|
|
||||||
cls.setup()
|
|
||||||
obj = cls()
|
|
||||||
attributes = uppercase_attributes(obj).items()
|
|
||||||
for name, value in attributes:
|
|
||||||
if callable(value) and not getattr(value, 'pristine', False):
|
|
||||||
value = 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', '{}.{}'.format(fullname,
|
mod = module
|
||||||
self.name))
|
|
||||||
cls.post_setup()
|
|
||||||
|
|
||||||
except Exception as err:
|
cls_path = f'{mod.__name__}.{class_name}'
|
||||||
reraise(err, f"Couldn't setup configuration '{cls_path}'")
|
|
||||||
|
|
||||||
return mod
|
try:
|
||||||
|
cls = getattr(mod, class_name)
|
||||||
|
except AttributeError as err: # pragma: no cover
|
||||||
|
reraise(
|
||||||
|
err,
|
||||||
|
(
|
||||||
|
f"Couldn't find configuration '{class_name}' in "
|
||||||
|
f"module '{mod.__package__}'"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
cls.pre_setup()
|
||||||
|
cls.setup()
|
||||||
|
obj = cls()
|
||||||
|
attributes = uppercase_attributes(obj).items()
|
||||||
|
for name, value in attributes:
|
||||||
|
if callable(value) and not getattr(value, 'pristine', False):
|
||||||
|
value = 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(module.__name__,
|
||||||
|
class_name))
|
||||||
|
cls.post_setup()
|
||||||
|
|
||||||
|
except Exception as err:
|
||||||
|
reraise(err, f"Couldn't setup configuration '{cls_path}'")
|
||||||
|
|
||||||
|
loader.__class__ = ConfigurationLoader
|
||||||
|
|
|
||||||
|
|
@ -6,3 +6,6 @@ class DotEnvConfiguration(Configuration):
|
||||||
DOTENV = 'test_project/.env'
|
DOTENV = 'test_project/.env'
|
||||||
|
|
||||||
DOTENV_VALUE = values.Value()
|
DOTENV_VALUE = values.Value()
|
||||||
|
|
||||||
|
def DOTENV_VALUE_METHOD(self):
|
||||||
|
return values.Value(environ_name="DOTENV_VALUE")
|
||||||
|
|
|
||||||
8
tests/settings/error.py
Normal file
8
tests/settings/error.py
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
from configurations import Configuration
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorConfiguration(Configuration):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def pre_setup(cls):
|
||||||
|
raise ValueError("Error in pre_setup")
|
||||||
|
|
@ -11,4 +11,5 @@ class DotEnvLoadingTests(TestCase):
|
||||||
def test_env_loaded(self):
|
def test_env_loaded(self):
|
||||||
from tests.settings import dot_env
|
from tests.settings import dot_env
|
||||||
self.assertEqual(dot_env.DOTENV_VALUE, 'is set')
|
self.assertEqual(dot_env.DOTENV_VALUE, 'is set')
|
||||||
|
self.assertEqual(dot_env.DOTENV_VALUE_METHOD, 'is set')
|
||||||
self.assertEqual(dot_env.DOTENV_LOADED, dot_env.DOTENV)
|
self.assertEqual(dot_env.DOTENV_LOADED, dot_env.DOTENV)
|
||||||
|
|
|
||||||
22
tests/test_error.py
Normal file
22
tests/test_error.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import os
|
||||||
|
from django.test import TestCase
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorTests(TestCase):
|
||||||
|
|
||||||
|
@patch.dict(os.environ, clear=True,
|
||||||
|
DJANGO_CONFIGURATION='ErrorConfiguration',
|
||||||
|
DJANGO_SETTINGS_MODULE='tests.settings.error')
|
||||||
|
def test_env_loaded(self):
|
||||||
|
with self.assertRaises(ValueError) as cm:
|
||||||
|
from tests.settings import error # noqa: F401
|
||||||
|
|
||||||
|
self.assertIsInstance(cm.exception, ValueError)
|
||||||
|
self.assertEqual(
|
||||||
|
cm.exception.args,
|
||||||
|
(
|
||||||
|
"Couldn't setup configuration "
|
||||||
|
"'tests.settings.error.ErrorConfiguration': Error in pre_setup ",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
@ -7,7 +7,7 @@ from django.core.exceptions import ImproperlyConfigured
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from configurations.importer import ConfigurationImporter
|
from configurations.importer import ConfigurationFinder
|
||||||
|
|
||||||
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
|
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
|
||||||
TEST_PROJECT_DIR = os.path.join(ROOT_DIR, 'test_project')
|
TEST_PROJECT_DIR = os.path.join(ROOT_DIR, 'test_project')
|
||||||
|
|
@ -42,12 +42,14 @@ class MainTests(TestCase):
|
||||||
|
|
||||||
@patch.dict(os.environ, clear=True, DJANGO_CONFIGURATION='Test')
|
@patch.dict(os.environ, clear=True, DJANGO_CONFIGURATION='Test')
|
||||||
def test_empty_module_var(self):
|
def test_empty_module_var(self):
|
||||||
self.assertRaises(ImproperlyConfigured, ConfigurationImporter)
|
with self.assertRaises(ImproperlyConfigured):
|
||||||
|
ConfigurationFinder()
|
||||||
|
|
||||||
@patch.dict(os.environ, clear=True,
|
@patch.dict(os.environ, clear=True,
|
||||||
DJANGO_SETTINGS_MODULE='tests.settings.main')
|
DJANGO_SETTINGS_MODULE='tests.settings.main')
|
||||||
def test_empty_class_var(self):
|
def test_empty_class_var(self):
|
||||||
self.assertRaises(ImproperlyConfigured, ConfigurationImporter)
|
with self.assertRaises(ImproperlyConfigured):
|
||||||
|
ConfigurationFinder()
|
||||||
|
|
||||||
def test_global_settings(self):
|
def test_global_settings(self):
|
||||||
from configurations.base import Configuration
|
from configurations.base import Configuration
|
||||||
|
|
@ -70,21 +72,21 @@ class MainTests(TestCase):
|
||||||
DJANGO_SETTINGS_MODULE='tests.settings.main',
|
DJANGO_SETTINGS_MODULE='tests.settings.main',
|
||||||
DJANGO_CONFIGURATION='Test')
|
DJANGO_CONFIGURATION='Test')
|
||||||
def test_initialization(self):
|
def test_initialization(self):
|
||||||
importer = ConfigurationImporter()
|
finder = ConfigurationFinder()
|
||||||
self.assertEqual(importer.module, 'tests.settings.main')
|
self.assertEqual(finder.module, 'tests.settings.main')
|
||||||
self.assertEqual(importer.name, 'Test')
|
self.assertEqual(finder.name, 'Test')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
repr(importer),
|
repr(finder),
|
||||||
"<ConfigurationImporter for 'tests.settings.main.Test'>")
|
"<ConfigurationFinder for 'tests.settings.main.Test'>")
|
||||||
|
|
||||||
@patch.dict(os.environ, clear=True,
|
@patch.dict(os.environ, clear=True,
|
||||||
DJANGO_SETTINGS_MODULE='tests.settings.inheritance',
|
DJANGO_SETTINGS_MODULE='tests.settings.inheritance',
|
||||||
DJANGO_CONFIGURATION='Inheritance')
|
DJANGO_CONFIGURATION='Inheritance')
|
||||||
def test_initialization_inheritance(self):
|
def test_initialization_inheritance(self):
|
||||||
importer = ConfigurationImporter()
|
finder = ConfigurationFinder()
|
||||||
self.assertEqual(importer.module,
|
self.assertEqual(finder.module,
|
||||||
'tests.settings.inheritance')
|
'tests.settings.inheritance')
|
||||||
self.assertEqual(importer.name, 'Inheritance')
|
self.assertEqual(finder.name, 'Inheritance')
|
||||||
|
|
||||||
@patch.dict(os.environ, clear=True,
|
@patch.dict(os.environ, clear=True,
|
||||||
DJANGO_SETTINGS_MODULE='tests.settings.main',
|
DJANGO_SETTINGS_MODULE='tests.settings.main',
|
||||||
|
|
@ -93,12 +95,12 @@ class MainTests(TestCase):
|
||||||
'--settings=tests.settings.main',
|
'--settings=tests.settings.main',
|
||||||
'--configuration=Test'])
|
'--configuration=Test'])
|
||||||
def test_configuration_option(self):
|
def test_configuration_option(self):
|
||||||
importer = ConfigurationImporter(check_options=False)
|
finder = ConfigurationFinder(check_options=False)
|
||||||
self.assertEqual(importer.module, 'tests.settings.main')
|
self.assertEqual(finder.module, 'tests.settings.main')
|
||||||
self.assertEqual(importer.name, 'NonExisting')
|
self.assertEqual(finder.name, 'NonExisting')
|
||||||
importer = ConfigurationImporter(check_options=True)
|
finder = ConfigurationFinder(check_options=True)
|
||||||
self.assertEqual(importer.module, 'tests.settings.main')
|
self.assertEqual(finder.module, 'tests.settings.main')
|
||||||
self.assertEqual(importer.name, 'Test')
|
self.assertEqual(finder.name, 'Test')
|
||||||
|
|
||||||
def test_configuration_argument_in_cli(self):
|
def test_configuration_argument_in_cli(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue