Refactor the initial environment settings and configuration file loading fixing some issues loading Django settings. Consolidate all database settings into a new single setting called "DATABASES". This mirrors Django database setting structure. This changes makes it possible to use configure multiple databases and database routers from the environment variables or configuration file. Remove usage of django-environ. Only a small set of the features provided by django-environ were being used. Variable typecasting is now only YAML. YAML parsing is implemented in code. Previously the initial setting code added all settings it found into the global symbol table. Now the settings found are matched to a explicit list of allowed settings. Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
from __future__ import unicode_literals
|
|
|
|
import errno
|
|
import os
|
|
|
|
import yaml
|
|
|
|
from .literals import DJANGO_SETTINGS_LIST
|
|
|
|
|
|
def get_environment_variables():
|
|
result = {}
|
|
|
|
for setting in DJANGO_SETTINGS_LIST:
|
|
environment_value = os.environ.get('MAYAN_{}'.format(setting))
|
|
if environment_value:
|
|
environment_value = yaml_loads(environment_value)
|
|
result[setting] = environment_value
|
|
|
|
return result
|
|
|
|
|
|
def read_configuration_file(path):
|
|
try:
|
|
with open(path) as file_object:
|
|
file_object.seek(0, os.SEEK_END)
|
|
if file_object.tell():
|
|
file_object.seek(0)
|
|
try:
|
|
return yaml.safe_load(file_object)
|
|
except yaml.YAMLError as exception:
|
|
exit(
|
|
'Error loading configuration file: {}; {}'.format(
|
|
path, exception
|
|
)
|
|
)
|
|
except IOError as exception:
|
|
if exception.errno == errno.ENOENT:
|
|
pass
|
|
else:
|
|
raise
|
|
|
|
|
|
def yaml_loads(data, error_message=None):
|
|
if not error_message:
|
|
error_message = 'Error loading: {}; {}'
|
|
|
|
try:
|
|
return yaml.safe_load(data)
|
|
except yaml.YAMLError as exception:
|
|
exit(
|
|
error_message.format(data, exception)
|
|
)
|