Skip to content
Snippets Groups Projects
Commit 7f1efb9d authored by plup's avatar plup
Browse files

bugfix: extracted YAML loading from settings class

parent 79f1480e
No related branches found
No related tags found
2 merge requests!167Add python code in settings,!158Load djangoldp conguration from YAML file
......@@ -20,8 +20,14 @@ logger = logging.getLogger(__name__)
def configure(filename='settings.yml'):
"""Helper function to configure django from LDPSettings."""
try:
with open(self.path, 'r') as f:
config = yaml.safe_load(f)
except FileNotFoundError:
logger.info('Starting project without configuration file')
# ref: https://docs.djangoproject.com/fr/2.2/topics/settings/#custom-default-settings
ldpsettings = LDPSettings(path=filename)
ldpsettings = LDPSettings(config)
django_settings.configure(ldpsettings)
......@@ -30,34 +36,14 @@ class LDPSettings(object):
"""Class managing the DjangoLDP configuration."""
def __init__(self, path):
def __init__(self, config):
if django_settings.configured:
raise ImproperlyConfigured('Settings have been configured already')
self.path = path
self._config = None
self._config = config
self._settings = self.build_settings()
@property
def config(self):
"""Load configuration from file."""
if not self._config:
try:
with open(self.path, 'r') as f:
self._config = yaml.safe_load(f)
except FileNotFoundError:
logger.info('Starting project without configuration file')
return self._config
@config.setter
def config(self, value):
"""Set a dict has current configuration."""
self._config = value
def build_settings(self, extend=['INSTALLED_APPS', 'MIDDLEWARE']):
"""
Look for the parameters in multiple places.
......@@ -113,7 +99,7 @@ class LDPSettings(object):
# look in YAML config file 'server' section
try:
conf = self.config.get('server', {})
conf = self._config.get('server', {})
update_with(conf)
logger.debug(f'Updating settings with project config')
except KeyError:
......@@ -126,7 +112,7 @@ class LDPSettings(object):
"""Returns the list of LDP packages configured."""
pkg = self.config.get('ldppackages', [])
pkg = self._config.get('ldppackages', [])
return [] if pkg is None else pkg
@property
......
......@@ -6,9 +6,9 @@ from django.conf import settings as django_settings
from djangoldp.conf.ldpsettings import LDPSettings
from djangoldp.tests.settings_default import yaml_config
# override config loading
ldpsettings = LDPSettings("")
ldpsettings.config = yaml.safe_load(yaml_config)
# load test config
config = yaml.safe_load(yaml_config)
ldpsettings = LDPSettings(config)
django_settings.configure(ldpsettings)
django.setup()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment